Coverage for python/lsst/daf/butler/_butlerConfig.py : 15%

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
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22"""
23Configuration classes specific to the Butler
24"""
26__all__ = ("ButlerConfig",)
28import os.path
29import posixpath
31from .core import (
32 ButlerURI,
33 CompositesConfig,
34 Config,
35 DatastoreConfig,
36 DimensionConfig,
37 RepoTransferFormatConfig,
38 StorageClassConfig,
39)
40from .registry import RegistryConfig
42CONFIG_COMPONENT_CLASSES = (RegistryConfig, StorageClassConfig,
43 DatastoreConfig, CompositesConfig, DimensionConfig,
44 RepoTransferFormatConfig)
47class ButlerConfig(Config):
48 """Contains the configuration for a `Butler`
50 The configuration is read and merged with default configurations for
51 the particular classes. The defaults are read according to the rules
52 outlined in `ConfigSubset`. Each component of the configuration associated
53 with a configuration class reads its own defaults.
55 Parameters
56 ----------
57 other : `str`, `Config`, optional
58 Path to butler configuration YAML file or a directory containing a
59 "butler.yaml" file. If `None` the butler will
60 be configured based entirely on defaults read from the environment
61 or from ``searchPaths``.
62 No defaults will be read if a `ButlerConfig` is supplied directly.
63 searchPaths : `list` or `tuple`, optional
64 Explicit additional paths to search for defaults. They should
65 be supplied in priority order. These paths have higher priority
66 than those read from the environment in
67 `ConfigSubset.defaultSearchPaths()`. They are only read if ``other``
68 refers to a configuration file or directory.
69 """
71 def __init__(self, other=None, searchPaths=None):
73 self.configDir = None
75 # If this is already a ButlerConfig we assume that defaults
76 # have already been loaded.
77 if other is not None and isinstance(other, ButlerConfig):
78 super().__init__(other)
79 # Ensure that the configuration directory propagates
80 self.configDir = other.configDir
81 return
83 if isinstance(other, str):
84 uri = ButlerURI(other)
85 if uri.scheme == "file" or not uri.scheme:
86 if os.path.isdir(uri.ospath):
87 other = os.path.join(uri.ospath, "butler.yaml")
88 elif uri.scheme == "s3":
89 head, filename = posixpath.split(uri.path)
90 if "." not in filename:
91 uri.updateFile("butler.yaml")
92 other = uri.geturl()
93 else:
94 raise ValueError(f"Unrecognized URI scheme: {uri.scheme}")
96 # Create an empty config for us to populate
97 super().__init__()
99 # Read the supplied config so that we can work out which other
100 # defaults to use.
101 butlerConfig = Config(other)
103 configFile = butlerConfig.configFile
104 if configFile is not None:
105 self.configDir = os.path.dirname(os.path.abspath(configFile))
107 # A Butler config contains defaults defined by each of the component
108 # configuration classes. We ask each of them to apply defaults to
109 # the values we have been supplied by the user.
110 for configClass in CONFIG_COMPONENT_CLASSES:
111 # Only send the parent config if the child
112 # config component is present (otherwise it assumes that the
113 # keys from other components are part of the child)
114 localOverrides = None
115 if configClass.component in butlerConfig:
116 localOverrides = butlerConfig
117 config = configClass(localOverrides, searchPaths=searchPaths)
118 # Re-attach it using the global namespace
119 self.update({configClass.component: config})
120 # Remove the key from the butlerConfig since we have already
121 # merged that information.
122 if configClass.component in butlerConfig:
123 del butlerConfig[configClass.component]
125 # Now that we have all the defaults we can merge the externally
126 # provided config into the defaults.
127 # Not needed if there is never information in a butler config file
128 # not present in component configurations
129 self.update(butlerConfig)