summaryrefslogtreecommitdiff
path: root/gr-uhd/swig/__init__.py
diff options
context:
space:
mode:
authorNick Foster2011-04-13 17:18:14 -0700
committerNick Foster2011-04-13 17:18:14 -0700
commit9b7d444aaebbe0708e9703bce30c63b63bc81825 (patch)
tree29121aa250decfb1ed9ca7876e1857eec8779d83 /gr-uhd/swig/__init__.py
parent258186d5ca2e811ced7ea637fd16e3ed3bb5573e (diff)
parente8ff9ef4bb77517428e1208ff4b3551a38107bbd (diff)
downloadgnuradio-9b7d444aaebbe0708e9703bce30c63b63bc81825.tar.gz
gnuradio-9b7d444aaebbe0708e9703bce30c63b63bc81825.tar.bz2
gnuradio-9b7d444aaebbe0708e9703bce30c63b63bc81825.zip
Merge branch 'master' of http://gnuradio.org/git/gnuradio into cpuid
Conflicts: volk/Makefile.common volk/lib/qa_utils.cc
Diffstat (limited to 'gr-uhd/swig/__init__.py')
-rw-r--r--gr-uhd/swig/__init__.py116
1 files changed, 59 insertions, 57 deletions
diff --git a/gr-uhd/swig/__init__.py b/gr-uhd/swig/__init__.py
index 1a9f8358d..7ed689ec0 100644
--- a/gr-uhd/swig/__init__.py
+++ b/gr-uhd/swig/__init__.py
@@ -17,70 +17,72 @@
# along with GNU Radio; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street,
# Boston, MA 02110-1301, USA.
-#
-
-# The presence of this file turns this directory into a Python package
+#
########################################################################
-# Create aliases for uhd swig attributes to avoid the "_t"
-# Install the __str__ and __repr__ handlers if applicable
+# Prepare uhd swig module to make it more pythonic
########################################################################
-import uhd_swig
-for attr in dir(uhd_swig):
- myobj = getattr(uhd_swig, attr)
- if hasattr(myobj, 'to_string'): myobj.__repr__ = lambda s: s.to_string().strip()
- if hasattr(myobj, 'to_pp_string'): myobj.__str__ = lambda s: s.to_pp_string().strip()
- if attr.endswith('_t'): setattr(uhd_swig, attr[:-2], myobj)
+def _prepare_uhd_swig():
+ import uhd_swig
-########################################################################
-# Add SWIG generated code to this namespace
-########################################################################
-from uhd_swig import *
+ #some useful typedefs for the user
+ setattr(uhd_swig, 'freq_range_t', uhd_swig.meta_range_t)
+ setattr(uhd_swig, 'gain_range_t', uhd_swig.meta_range_t)
-########################################################################
-# Add other content from pure-Python modules here
-########################################################################
-class freq_range_t(meta_range_t): pass #a typedef for the user
-class gain_range_t(meta_range_t): pass #a typedef for the user
+ #Make the python tune request object inherit from float
+ #so that it can be passed in GRC as a frequency parameter.
+ #The type checking in GRC will accept the tune request.
+ class tune_request_t(uhd_swig.tune_request_t, float):
+ def __new__(self, *args): return float.__new__(self)
+ def __float__(self): return self.target_freq
+ setattr(uhd_swig, 'tune_request_t', tune_request_t)
+
+ #Make the python tune request object inherit from string
+ #so that it can be passed in GRC as a string parameter.
+ #The type checking in GRC will accept the device address.
+ #Define the set/get item special methods for dict access.
+ class device_addr_t(uhd_swig.device_addr_t, str):
+ def __new__(self, *args): return str.__new__(self)
+ def __getitem__(self, key): return self.get(key)
+ def __setitem__(self, key, val): self.set(key, val)
+ setattr(uhd_swig, 'device_addr_t', device_addr_t)
-class tune_request_t(tune_request_t, float):
- """
- Make the python tune request object inherit from float
- so that it can be passed in GRC as a frequency parameter.
- The type checking in GRC will accept the tune request.
- """
- def __new__(self, *args): return float.__new__(self)
- def __float__(self): return self.target_freq
+ #handle general things on all uhd_swig attributes
+ #Install the __str__ and __repr__ handlers if applicable
+ #Create aliases for uhd swig attributes to avoid the "_t"
+ for attr in dir(uhd_swig):
+ myobj = getattr(uhd_swig, attr)
+ if hasattr(myobj, 'to_string'): myobj.__repr__ = lambda o: o.to_string().strip()
+ if hasattr(myobj, 'to_pp_string'): myobj.__str__ = lambda o: o.to_pp_string().strip()
+ if hasattr(myobj, 'to_bool'): myobj.__nonzero__ = lambda o: o.to_bool()
+ if hasattr(myobj, 'to_int'): myobj.__int__ = lambda o: o.to_int()
+ if hasattr(myobj, 'to_real'): myobj.__float__ = lambda o: o.to_real()
+ if attr.endswith('_t'): setattr(uhd_swig, attr[:-2], myobj)
-class device_addr_t(device_addr_t, str):
- """
- Make the python tune request object inherit from string
- so that it can be passed in GRC as a string parameter.
- The type checking in GRC will accept the device address.
- Define the set/get item special methods for dict access.
- """
- def __new__(self, *args): return str.__new__(self)
- def __getitem__(self, key): return self.get(key)
- def __setitem__(self, key, val): self.set(key, val)
+ #Cast constructor args (FIXME swig handle overloads?)
+ for attr in ('usrp_source', 'usrp_sink'):
+ def constructor_factory(old_constructor):
+ def constructor_interceptor(*args, **kwargs):
+ args = list(args)
+ kwargs = dict(kwargs)
+ for index, key, cast in (
+ (0, 'device_addr', device_addr),
+ (1, 'io_type', io_type),
+ ):
+ if len(args) > index: args[index] = cast(args[index])
+ if kwargs.has_key(key): kwargs[key] = cast(kwargs[key])
+ return old_constructor(*args, **kwargs)
+ return constructor_interceptor
+ setattr(uhd_swig, attr, constructor_factory(getattr(uhd_swig, attr)))
+
+ #Aliases for deprecated constructors
+ setattr(uhd_swig, 'single_usrp_source', uhd_swig.usrp_source)
+ setattr(uhd_swig, 'single_usrp_sink', uhd_swig.usrp_sink)
+ setattr(uhd_swig, 'multi_usrp_source', uhd_swig.usrp_source)
+ setattr(uhd_swig, 'multi_usrp_sink', uhd_swig.usrp_sink)
########################################################################
-# Cast constructor args (FIXME swig handle overloads?)
+# Initialize this module with the contents of uhd swig
########################################################################
-for attr in (
- 'single_usrp_source', 'single_usrp_sink',
- 'multi_usrp_source', 'multi_usrp_sink'
-):
- def constructor_factory(old_constructor):
- def constructor_interceptor(*args, **kwargs):
- args = list(args)
- kwargs = dict(kwargs)
- for index, key, cast in (
- (0, 'device_addr', device_addr),
- (1, 'io_type', io_type),
- ):
- if len(args) > index: args[index] = cast(args[index])
- if kwargs.has_key(key): kwargs[key] = cast(kwargs[key])
- return old_constructor(*args, **kwargs)
- return constructor_interceptor
- import uhd_swig
- globals()[attr] = constructor_factory(getattr(uhd_swig, attr))
+_prepare_uhd_swig()
+from uhd_swig import *