-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from lsst/tickets/DM-13349
DM-13349: Define storage classes in config yaml
- Loading branch information
Showing
17 changed files
with
841 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# | ||
# 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. | ||
Parameters | ||
---------- | ||
refType : `type` | ||
Python reference `type` to use to ensure that items stored in the | ||
registry create instance objects of the correct class. Subclasses | ||
of this type are allowed. Using `None` disables the check. | ||
""" | ||
|
||
def __init__(self, refType): | ||
self._registry = {} | ||
self.refType = refType | ||
|
||
def getFromRegistry(self, *targetClasses): | ||
"""Get a new instance of the object stored in the registry. | ||
Parameters | ||
---------- | ||
*targetClasses : `str` or objects supporting `.name` attribute | ||
Each item is tested in turn until a match is found in the registry. | ||
Items with `None` value are skipped. | ||
Returns | ||
------- | ||
instance : `object` | ||
Instance of class stored in registry associated with the first | ||
matching target class. | ||
Raises | ||
------ | ||
e : KeyError | ||
None of the supplied target classes match an item in the registry. | ||
""" | ||
attempts = [] | ||
for t in (targetClasses): | ||
if t is None: | ||
attempts.append(t) | ||
else: | ||
key = self._getName(t) | ||
attempts.append(key) | ||
try: | ||
typeName = self._registry[key] | ||
except KeyError: | ||
pass | ||
else: | ||
return self._getInstanceOf(typeName) | ||
raise KeyError("Unable to find item in registry with keys: {}".format(attempts)) | ||
|
||
def placeInRegistry(self, registryKey, typeName): | ||
"""Register a class name with the associated type. | ||
The type name provided is validated against the reference | ||
class, `refType` attribute, 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() | ||
|
||
def _isValidStr(self, typeName): | ||
"""Validate that the class type name provided does create instances of | ||
objects that are of the expected type, as stored in the | ||
`refType` attribute. | ||
""" | ||
if self.refType is None: | ||
return True | ||
try: | ||
c = self._getInstanceOf(typeName) | ||
except (ImportError, TypeError, AttributeError): | ||
return False | ||
else: | ||
return isinstance(c, self.refType) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.