Coverage for python/lsst/pex/config/registry.py : 80%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# # LSST Data Management System # Copyright 2008, 2009, 2010 LSST Corporation. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <http://www.lsstcorp.org/LegalNotices/>. #
"""A wrapper for configurables
Used for configurables that don't contain a ConfigClass attribute, or contain one that is being overridden. """
"""A base class for global registries, mapping names to configurables.
There are no hard requirements on configurable, but they typically create an algorithm or are themselves the algorithm, and typical usage is as follows: - configurable is a callable whose call signature is (config, ...extra arguments...) - All configurables added to a particular registry will have the same call signature - All configurables in a registry will typically share something important in common. For example all configurables in psfMatchingRegistry return a psf matching class that has a psfMatch method with a particular call signature.
A registry acts like a read-only dictionary with an additional register method to add items. The dict contains configurables and each configurable has an instance ConfigClass.
Example: registry = Registry() class FooConfig(Config): val = Field(dtype=int, default=3, doc="parameter for Foo") class Foo: ConfigClass = FooConfig def __init__(self, config): self.config = config def addVal(self, num): return self.config.val + num registry.register("foo", Foo) names = registry.keys() # returns ("foo",) fooConfigurable = registry["foo"] fooConfig = fooItem.ConfigClass() foo = fooConfigurable(fooConfig) foo.addVal(5) # returns config.val + 5 """
"""Construct a registry of name: configurables
@param configBaseType: base class for config classes in registry """ raise TypeError("configBaseType=%s must be a subclass of Config" % _typeStr(configBaseType,))
"""Add a new item to the registry.
@param target A callable 'object that takes a Config instance as its first argument. This may be a Python type, but is not required to be. @param ConfigClass A subclass of pex_config Config used to configure the configurable; if None then configurable.ConfigClass is used.
@note: If ConfigClass is provided then then 'target' is wrapped in a new object that forwards function calls to it. Otherwise the original 'target' is stored.
@raise AttributeError if ConfigClass is None and target does not have attribute ConfigClass """ else: raise TypeError("ConfigClass=%s is not a subclass of %r" % (_typeStr(wrapper.ConfigClass), _typeStr(self._configBaseType)))
return len(self._dict)
"""Private class that makes a Registry behave like the thing a ConfigChoiceField expects."""
return len(self.registry)
raise FieldValidationError(self._field, self._config, "Multi-selection field has no attribute 'target'")
raise FieldValidationError(self._field, self._config, "Single-selection field has no attribute 'targets'")
"""Call the active target(s) with the active config as a keyword arg
If this is a multi-selection field, return a list obtained by calling each active target with its corresponding active config.
Additional arguments will be passed on to the configurable target(s) """ msg = "No selection has been made. Options: %s" % \ (" ".join(list(self._field.typemap.registry.keys()))) raise FieldValidationError(self._field, self._config, msg) retvals = [] for c in self._selection: retvals.append(self._field.typemap.registry[c](*args, config=self[c], **kw)) return retvals else:
else:
"""Customize deep-copying, want a reference to the original registry. WARNING: this must be overridden by subclasses if they change the constructor signature! """ default=copy.deepcopy(self.default), optional=self.optional, multi=self.multi)
"""A convenience function to create a new registry.
The returned value is an instance of a trivial subclass of Registry whose only purpose is to customize its doc string and set attrList. """
"""A decorator that adds a class as a configurable in a Registry.
If the 'ConfigClass' argument is None, the class's ConfigClass attribute will be used. """
"""A decorator that adds a class as a ConfigClass in a Registry, and associates it with the given configurable. """ def decorate(cls): registry.register(name, target=target, ConfigClass=cls) return cls return decorate |