def populate_namespace(self, namespace=None, names=None, kinds=None): """ Populate a nominated namespace with references to a subset of ducts. While a registry object is a great way to store and configure `Duct` instances, it is sometimes desirable to surface frequently used instances in other more convenient namespaces (such as the globals of your module). Args: namespace (dict, None): The namespace to populate. If using from a module you can pass `globals()`. If `None`, a new dictionary is created, populated and then returned. names (list<str>, None): The names to include in the population. If not specified then all names will be exported. kinds (list<str>, None): The kinds of ducts to include in the population. If not specified, all kinds will be exported. Returns: dict: The populated namespace. """ if namespace is None: namespace = {} if kinds is not None: kinds = [ Duct.Type(kind) if not isinstance(kind, Duct.Type) else kind for kind in kinds ] for name, duct in self._registry.items(): if (kinds is None or duct.DUCT_TYPE in kinds) and (names is None or name in names): namespace[name.split('/')[-1]] = duct return namespace
def lookup(self, name, kind=None): if kind and not isinstance(kind, Duct.Type): kind = Duct.Type(kind) r = self._registry[name] if kind and r.DUCT_TYPE != kind: raise KeyError("No duct called '{}' of kind '{}'.".format(name, kind.value)) return r
def lookup(self, name, kind=None): """ Look up an existing registered `Duct` by name and (optionally) kind. Args: name (str): The name of the `Duct` instance. kind (str, Duct.Type): The kind of `Duct` to which the lookup should be restricted. Returns: `Duct`: The looked up `Duct` instance. Raises: DuctNotFound: If no `Duct` can be found for requested name and/or type. """ if kind and not isinstance(kind, Duct.Type): kind = Duct.Type(kind) if name not in self._registry: raise DuctNotFound(name) duct = self._registry[name] if kind and duct.DUCT_TYPE != kind: raise DuctNotFound( "Duct named '{}' exists, but is not of kind '{}'.".format( name, kind.value)) return duct
def populate_namespace(self, namespace=None, include=None, kinds=None): if namespace is None: namespace = {} if kinds is not None: kinds = [Duct.Type(kind) if not isinstance(kind, Duct.Type) else kind for kind in kinds] for name, duct in self._registry.items(): if (kinds is None or duct.DUCT_TYPE in kinds) and (include is None or name in include): namespace[name.split('/')[-1]] = duct return namespace