-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move registry factory methods to a separate class.
Trying to further separate concerns into independent classes. Re-introduced `Registry.createFromConfig` to avoid updating tests in many other packages.
- Loading branch information
Showing
12 changed files
with
256 additions
and
57 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,185 @@ | ||
# This file is part of daf_butler. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# 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 GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from __future__ import annotations | ||
|
||
__all__ = ("RegistryFactory",) | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from lsst.resources import ResourcePathExpression | ||
from lsst.utils import doImportType | ||
|
||
from ..core import Config, DimensionConfig | ||
from ._butler_registry import ButlerRegistry | ||
from ._config import RegistryConfig | ||
from ._defaults import RegistryDefaults | ||
|
||
if TYPE_CHECKING: | ||
from .._butlerConfig import ButlerConfig | ||
|
||
|
||
class RegistryFactory: | ||
"""Interface for creating and initializing Registry instances. | ||
Each registry implementation can have its own constructor parameters. | ||
The assumption is that an instance of a specific subclass will be | ||
constructed from configuration using ``RegistryClass.fromConfig()`` or | ||
``RegistryClass.createFromConfig()``. | ||
This class will look for a ``cls`` entry in registry configuration object | ||
(defaulting to ``SqlRegistry``), import that class, and call one of the | ||
above methods on the imported class. | ||
""" | ||
|
||
@classmethod | ||
def force_registry_config( | ||
cls, config: ButlerConfig | RegistryConfig | Config | str | None | ||
) -> RegistryConfig: | ||
"""Force the supplied config to a `RegistryConfig`. | ||
Parameters | ||
---------- | ||
config : `RegistryConfig`, `Config` or `str` or `None` | ||
Registry configuration, if missing then default configuration will | ||
be loaded from registry.yaml. | ||
Returns | ||
------- | ||
registry_config : `RegistryConfig` | ||
A registry config. | ||
""" | ||
if not isinstance(config, RegistryConfig): | ||
if isinstance(config, (str, Config)) or config is None: | ||
config = RegistryConfig(config) | ||
else: | ||
raise ValueError(f"Incompatible Registry configuration: {config}") | ||
return config | ||
|
||
@classmethod | ||
def determine_trampoline( | ||
cls, config: ButlerConfig | RegistryConfig | Config | str | None | ||
) -> tuple[type[ButlerRegistry], RegistryConfig]: | ||
"""Return class to use to instantiate real registry. | ||
Parameters | ||
---------- | ||
config : `RegistryConfig` or `str`, optional | ||
Registry configuration, if missing then default configuration will | ||
be loaded from registry.yaml. | ||
Returns | ||
------- | ||
requested_cls : `type` of `ButlerRegistry` | ||
The real registry class to use. | ||
registry_config : `RegistryConfig` | ||
The `RegistryConfig` to use. | ||
""" | ||
config = cls.force_registry_config(config) | ||
|
||
# Default to the standard registry | ||
registry_cls_name = config.get("cls", "lsst.daf.butler.registries.sql.SqlRegistry") | ||
registry_cls = doImportType(registry_cls_name) | ||
if registry_cls is cls: | ||
raise ValueError("Can not instantiate the abstract base Registry from config") | ||
if not issubclass(registry_cls, ButlerRegistry): | ||
raise TypeError( | ||
f"Registry class obtained from config {registry_cls_name} is not a ButlerRegistry class." | ||
) | ||
return registry_cls, config | ||
|
||
@classmethod | ||
def create_from_config( | ||
cls, | ||
config: RegistryConfig | str | None = None, | ||
dimensionConfig: DimensionConfig | str | None = None, | ||
butlerRoot: ResourcePathExpression | None = None, | ||
) -> ButlerRegistry: | ||
"""Create registry database and return `ButlerRegistry` instance. | ||
This method initializes database contents, database must be empty | ||
prior to calling this method. | ||
Parameters | ||
---------- | ||
config : `RegistryConfig` or `str`, optional | ||
Registry configuration, if missing then default configuration will | ||
be loaded from registry.yaml. | ||
dimensionConfig : `DimensionConfig` or `str`, optional | ||
Dimensions configuration, if missing then default configuration | ||
will be loaded from dimensions.yaml. | ||
butlerRoot : convertible to `lsst.resources.ResourcePath`, optional | ||
Path to the repository root this `Registry` will manage. | ||
Returns | ||
------- | ||
registry : `ButlerRegistry` | ||
A new `ButlerRegistry` instance. | ||
Notes | ||
----- | ||
This class will determine the concrete `ButlerRegistry` subclass to | ||
use from configuration. Each subclass should implement this method | ||
even if it can not create a registry. | ||
""" | ||
registry_cls, registry_config = cls.determine_trampoline(config) | ||
return registry_cls.createFromConfig(registry_config, dimensionConfig, butlerRoot) | ||
|
||
@classmethod | ||
def from_config( | ||
cls, | ||
config: ButlerConfig | RegistryConfig | Config | str, | ||
butlerRoot: ResourcePathExpression | None = None, | ||
writeable: bool = True, | ||
defaults: RegistryDefaults | None = None, | ||
) -> ButlerRegistry: | ||
"""Create `ButlerRegistry` subclass instance from ``config``. | ||
Registry database must be initialized prior to calling this method. | ||
Parameters | ||
---------- | ||
config : `ButlerConfig`, `RegistryConfig`, `Config` or `str` | ||
Registry configuration | ||
butlerRoot : convertible to `lsst.resources.ResourcePath`, optional | ||
Path to the repository root this `Registry` will manage. | ||
writeable : `bool`, optional | ||
If `True` (default) create a read-write connection to the database. | ||
defaults : `~lsst.daf.butler.registry.RegistryDefaults`, optional | ||
Default collection search path and/or output `~CollectionType.RUN` | ||
collection. | ||
Returns | ||
------- | ||
registry : `ButlerRegistry` (subclass) | ||
A new `ButlerRegistry` subclass instance. | ||
Notes | ||
----- | ||
This class will determine the concrete `ButlerRegistry` subclass to | ||
use from configuration. Each subclass should implement this method. | ||
""" | ||
# The base class implementation should trampoline to the correct | ||
# subclass. No implementation should ever use this implementation | ||
# directly. If no class is specified, default to the standard | ||
# registry. | ||
registry_cls, registry_config = cls.determine_trampoline(config) | ||
return registry_cls.fromConfig(config, butlerRoot, writeable, defaults) |
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
Oops, something went wrong.