From 32cb61fe90647f1ea70159b0d8ab12eda4cd9d20 Mon Sep 17 00:00:00 2001 From: "Spencer E. Olson" Date: Sat, 19 Mar 2016 22:53:39 -0600 Subject: [PATCH] Reimplement 371dcc56e6f7(strip comedi_/COMEDI prefix) This patch reimplements 371dcc56e6f7 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. --- swig/comedi.i | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/swig/comedi.i b/swig/comedi.i index 92269a9..6a2cecb 100644 --- a/swig/comedi.i +++ b/swig/comedi.i @@ -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 %} -