From 99e5230e4bf26166ee1accce1e57ff24b0629826 Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 15 Apr 2021 15:40:55 +0200 Subject: mpm: PeriphManagerBase: Add _add_public_methods() This allows conditionally adding public API methods. --- mpm/python/usrp_mpm/periph_manager/base.py | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'mpm/python/usrp_mpm') 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) ########################################################################### -- cgit v1.2.3