From 371dcc56e6f77c44f9ebf4b178a540b9d0833490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Piel?= Date: Fri, 31 Jan 2014 15:07:15 +0100 Subject: [PATCH] swig: remove comedi_ prefix from the function names python always use the namespace of the library so it was redundant. so what used to be written comedi.comedi_open() can be written comedi.open(). The main trick is to add a special python code that allows for the old names to still be compatible. That avoids to break every single python program previously written. --- swig/comedi.i | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/swig/comedi.i b/swig/comedi.i index 46c8298..1f7e75d 100644 --- a/swig/comedi.i +++ b/swig/comedi.i @@ -38,6 +38,11 @@ %include "carrays.i" %include "typemaps.i" +#ifdef SWIGPYTHON +%rename("%(strip:[COMEDI_])s", regextarget=1) "COMEDI_.*"; +%rename("%(strip:[comedi_])s", regextarget=1) "comedi_.*"; +#endif + %inline %{ static unsigned int cr_pack(unsigned int chan, unsigned int rng, unsigned int aref){ return CR_PACK(chan,rng,aref); @@ -69,3 +74,24 @@ static unsigned int cr_aref(unsigned int a){ %array_class(sampl_t, sampl_array); %array_class(lsampl_t, lsampl_array); %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__]) +%} +