Reimplement 371dcc56e6f7(strip comedi_/COMEDI prefix)
This patch reimplements 371dcc56e6
where the comedi_/COMEDI_ prefix was
removed. Just like that patch, this patch also allows for all old names
to resolve properly. Differently than that patch, this patch simply adds
new items in the modules dictionary with the comedi_/COMEDI_ prefix
removed. Benefits of this approach:
1) Do not have to create a fake module.
2) Tab completion works well in ipython/python since the module is left
intact.
3) Adding new items without the comedi_/COMEDI_ prefix happens only once at
module load time, instead of every time an item is requested from the
module.
This commit is contained in:
parent
b9821b9d8c
commit
32cb61fe90
1 changed files with 14 additions and 20 deletions
|
@ -40,8 +40,11 @@
|
|||
%include "typemaps.i"
|
||||
|
||||
#ifdef SWIGPYTHON
|
||||
%rename("%(strip:[COMEDI_])s", regextarget=1) "COMEDI_.*";
|
||||
%rename("%(strip:[comedi_])s", regextarget=1) "comedi_.*";
|
||||
// Uncomment these two lines and remove the "%insert('python')" entry below to
|
||||
// finallize the removal of the comedi_/COMEDI_ prefix and break backwards
|
||||
// compatibility.
|
||||
// %rename("%(strip:[COMEDI_])s", regextarget=1) "COMEDI_.*";
|
||||
// %rename("%(strip:[comedi_])s", regextarget=1) "comedi_.*";
|
||||
|
||||
// These need to be explicitly written as unsigned ints
|
||||
%rename(CR_FLAGS_MASK) _CR_FLAGS_MASK;
|
||||
|
@ -112,22 +115,13 @@ unsigned int NI_AO_SCAN_BEGIN_SRC_RTSI(unsigned int rtsi_channel);
|
|||
%array_class(comedi_insn, insn_array);
|
||||
|
||||
%insert("python") %{
|
||||
# Trick to allow accessing functions and constants with their original C name
|
||||
import sys
|
||||
|
||||
class __Wrapper(object):
|
||||
def __init__(self, wrapped):
|
||||
self.wrapped = wrapped
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
return getattr(self.wrapped, name)
|
||||
except AttributeError:
|
||||
if name.startswith("comedi_") or name.startswith("COMEDI_"):
|
||||
return getattr(self.wrapped, name[7:])
|
||||
else:
|
||||
raise
|
||||
|
||||
sys.modules[__name__] = __Wrapper(sys.modules[__name__])
|
||||
# Add entries in module dictionary to strip comedi_/COMEDI_ prefix
|
||||
import re
|
||||
for k,v in globals().items():
|
||||
if re.match('^comedi_', k, flags=re.IGNORECASE):
|
||||
globals()[k[7:]] = v
|
||||
# uncommenting following line removes compatibility with old code using
|
||||
# comedi_ prefix:
|
||||
# globals().pop(k)
|
||||
del re, k, v
|
||||
%}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue