aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2021-04-15 15:40:55 +0200
committerAaron Rossetto <aaron.rossetto@ni.com>2021-04-29 14:34:55 -0500
commit99e5230e4bf26166ee1accce1e57ff24b0629826 (patch)
treedc3657ee20558867575180fae3b50ad7736bc992
parentbb22f3bebb84b18e506e8dcfa67382ba25c21a5f (diff)
downloaduhd-99e5230e4bf26166ee1accce1e57ff24b0629826.tar.gz
uhd-99e5230e4bf26166ee1accce1e57ff24b0629826.tar.bz2
uhd-99e5230e4bf26166ee1accce1e57ff24b0629826.zip
mpm: PeriphManagerBase: Add _add_public_methods()
This allows conditionally adding public API methods.
-rw-r--r--mpm/python/usrp_mpm/periph_manager/base.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/mpm/python/usrp_mpm/periph_manager/base.py b/mpm/python/usrp_mpm/periph_manager/base.py
index 47a356012..22630dbf6 100644
--- a/mpm/python/usrp_mpm/periph_manager/base.py
+++ b/mpm/python/usrp_mpm/periph_manager/base.py
@@ -500,6 +500,47 @@ class PeriphManagerBase(object):
self.dboards.append(db_class(dboard_idx, **dboard_info))
self.log.info("Initialized %d daughterboard(s).", len(self.dboards))
+ def _add_public_methods(self, src, prefix="", filter_cb=None):
+ """
+ Add public methods (=API) of src to self. To avoid naming conflicts and
+ make relations clear, all added method names are prefixed with 'prefix'.
+
+ Example:
+ >>> class Foo:
+ ... def print_x(self, x):
+ ... print(x)
+ ...
+ >>> foo = Foo()
+ >>> self._add_public_methods(foo, prefix="ext")
+ >>> self.ext_print_x(5) # Prints 5
+
+ :param source: The object to import the API from
+ :param prefix: method names in dest will be prefixed with prefix
+ :param filter_cb: A callback that returns true if the method should be
+ added. Defaults to always returning True
+ """
+ filter_cb = filter_cb or (lambda *args: True)
+ assert callable(filter_cb)
+ self.log.trace("Adding API functions from %s to %s" % (
+ src.__class__.__name__, self.__class__.__name__))
+ # append _ to prefix if it is not an empty string
+ if prefix:
+ prefix = prefix + "_"
+ for name in [name for name in dir(src)
+ if not name.startswith("_")
+ and callable(getattr(src, name))
+ and filter_cb(name, getattr(src, name))
+ ]:
+ destname = prefix + name
+ if hasattr(self, destname):
+ self.log.warn("Cannot add method {} because it would "
+ "overwrite existing method.".format(destname))
+ else:
+ method = getattr(src, name)
+ self.log.trace("Add function %s as %s", name, destname)
+ setattr(self, destname, method)
+
+
###########################################################################
# Session (de-)initialization (at UHD startup)
###########################################################################