-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DM-13349: Define storage classes in config yaml #11
Changes from 8 commits
b1d7445
2dc5817
6806a84
b54ce0c
8e6eef1
b26eee8
8e4335b
950c067
dbf2764
1596bef
6560499
19a7d94
7c87172
33ecb3b
faa83ba
1270029
68f27cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# | ||
# LSST Data Management System | ||
# | ||
# Copyright 2008-2018 AURA/LSST. | ||
# | ||
# 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 <https://www.lsstcorp.org/LegalNotices/>. | ||
# | ||
from lsst.daf.butler.core.utils import doImport | ||
|
||
|
||
class MappingFactory: | ||
""" | ||
Register the mapping of some key to a python type and retrieve instances. | ||
|
||
Enables instances of these classes to be retrieved from the factory later. | ||
The class can be specified as an object, class or string. | ||
If the key is an object it is converted to a string by accessing | ||
a `name` attribute. | ||
""" | ||
|
||
refType = None | ||
"""Type of instances expected to be returned from factory.""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be documented in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I spent ten minutes reading numpydoc documentation and this seemed to be the way they wanted me to document class attributes. I admit it was somewhat unclear to me (and I'm about to delete that line anyhow). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that I read the numpydoc docs again I see the |
||
|
||
def __init__(self): | ||
self._registry = {} | ||
|
||
def getFromRegistry(self, targetClass, override=None): | ||
"""Get a new instance of the object stored in the registry. | ||
|
||
Parameters | ||
---------- | ||
targetClass : `str` or object supporting `.name` attribute | ||
Get item from registry associated with this target class, unless | ||
override : `str` or object supporting `.name` attribute, optional | ||
If given, look if an override has been specified for this and, | ||
if so return that instead. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps make it slightly more generic? By taking |
||
|
||
Returns | ||
------- | ||
instance : `object` | ||
Instance of class stored in registry associated with the target. | ||
""" | ||
for t in (override, targetClass): | ||
if t is not None: | ||
try: | ||
typeName = self._registry[self._getName(t)] | ||
except KeyError: | ||
pass | ||
else: | ||
return self._getInstanceOf(typeName) | ||
raise KeyError("Unable to find item in registry with keys {} or {}".format(targetClass, override)) | ||
|
||
def placeInRegistry(self, registryKey, typeName): | ||
"""Register a class name with the associated type. | ||
The type name provided is validated against the reference | ||
class, `refType`, if defined. | ||
|
||
Parameters | ||
---------- | ||
registryKey : `str` or object supporting `.name` attribute. | ||
Item to associate with the provided type. | ||
typeName : `str` or Python type | ||
Identifies a class to associate with the provided key. | ||
|
||
Raises | ||
------ | ||
e : `ValueError` | ||
If instance of class is not of the expected type. | ||
""" | ||
if not self._isValidStr(typeName): | ||
raise ValueError("Not a valid class string: {}".format(typeName)) | ||
keyString = self._getName(registryKey) | ||
if keyString in self._registry: | ||
raise ValueError("Item with key {} already registered".format(keyString)) | ||
|
||
self._registry[keyString] = typeName | ||
|
||
@staticmethod | ||
def _getName(typeOrName): | ||
"""Extract name of supplied object as string. | ||
|
||
Parameters | ||
---------- | ||
typeOrName : `str` or object supporting `.name` attribute. | ||
Item from which to extract a name. | ||
|
||
Returns | ||
------- | ||
name : `str` | ||
Extracted name as a string. | ||
""" | ||
if isinstance(typeOrName, str): | ||
return typeOrName | ||
elif hasattr(typeOrName, 'name'): | ||
return typeOrName.name | ||
else: | ||
raise ValueError("Cannot extract name from type") | ||
|
||
@staticmethod | ||
def _getInstanceOf(typeOrName): | ||
"""Given the type name or a type, instantiate an object of that type. | ||
|
||
If a type name is given, an attempt will be made to import the type. | ||
|
||
Parameters | ||
---------- | ||
typeOrName : `str` or Python class | ||
A string describing the Python class to load or a Python type. | ||
""" | ||
if isinstance(typeOrName, str): | ||
cls = doImport(typeOrName) | ||
else: | ||
cls = typeOrName | ||
return cls() | ||
|
||
@classmethod | ||
def _isValidStr(cls, typeName): | ||
"""Validate that the class name provided does create instances of | ||
objects that are of the expected type, as stored in the class | ||
`refType` attribute. | ||
""" | ||
if cls.refType is None: | ||
return True | ||
try: | ||
c = cls._getInstanceOf(typeName) | ||
except (ImportError, TypeError, AttributeError): | ||
return False | ||
else: | ||
return isinstance(c, cls.refType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can switch this to using composition if you prefer since the get and put methods do not have the same names as the base class implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that way we can also get rid of my other gripe (having
refType
specified) and move that to an argument (e.g.self.mappingFactory = MappingFactory(Formatter)
).