Hide keyboard shortcuts

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/>. 

21 

22""" 

23Configuration classes specific to the Butler 

24""" 

25from __future__ import annotations 

26 

27__all__ = ("ButlerConfig",) 

28 

29import copy 

30from typing import ( 

31 Optional, 

32 Sequence, 

33 Union, 

34) 

35 

36from .core import ( 

37 ButlerURI, 

38 Config, 

39 DatastoreConfig, 

40 StorageClassConfig, 

41) 

42from .registry import RegistryConfig 

43from .transfers import RepoTransferFormatConfig 

44 

45CONFIG_COMPONENT_CLASSES = (RegistryConfig, StorageClassConfig, 

46 DatastoreConfig, RepoTransferFormatConfig) 

47 

48 

49class ButlerConfig(Config): 

50 """Contains the configuration for a `Butler` 

51 

52 The configuration is read and merged with default configurations for 

53 the particular classes. The defaults are read according to the rules 

54 outlined in `ConfigSubset`. Each component of the configuration associated 

55 with a configuration class reads its own defaults. 

56 

57 Parameters 

58 ---------- 

59 other : `str`, `Config`, `ButlerConfig`, optional 

60 Path to butler configuration YAML file or a directory containing a 

61 "butler.yaml" file. If `None` the butler will 

62 be configured based entirely on defaults read from the environment 

63 or from ``searchPaths``. 

64 No defaults will be read if a `ButlerConfig` is supplied directly. 

65 searchPaths : `list` or `tuple`, optional 

66 Explicit additional paths to search for defaults. They should 

67 be supplied in priority order. These paths have higher priority 

68 than those read from the environment in 

69 `ConfigSubset.defaultSearchPaths()`. They are only read if ``other`` 

70 refers to a configuration file or directory. 

71 """ 

72 

73 def __init__(self, other: Optional[Union[str, ButlerURI, Config]] = None, 

74 searchPaths: Sequence[Union[str, ButlerURI]] = None): 

75 

76 self.configDir: Optional[ButlerURI] = None 

77 

78 # If this is already a ButlerConfig we assume that defaults 

79 # have already been loaded. 

80 if other is not None and isinstance(other, ButlerConfig): 

81 super().__init__(other) 

82 # Ensure that the configuration directory propagates 

83 self.configDir = copy.copy(other.configDir) 

84 return 

85 

86 if isinstance(other, str): 

87 # This will only allow supported schemes 

88 uri = ButlerURI(other) 

89 

90 # We allow the butler configuration file to be left off the 

91 # URI supplied by the user. If a directory-like URI is given 

92 # we add the default configuration name. 

93 

94 # It's easy to miss a trailing / for remote URIs so try to guess 

95 # we have been given a directory-like URI if there is no 

96 # file extension. Local URIs do not need any guess work. 

97 if not uri.isLocal and not uri.getExtension(): 

98 uri = ButlerURI(other, forceDirectory=True) 

99 

100 if uri.isdir(): 

101 # Could also be butler.json (for example in the butler 

102 # server) but checking for existence will slow things 

103 # down given that this might involve two checks and then 

104 # the config read below would still do the read. 

105 other = uri.join("butler.yaml") 

106 

107 # Create an empty config for us to populate 

108 super().__init__() 

109 

110 # Read the supplied config so that we can work out which other 

111 # defaults to use. 

112 butlerConfig = Config(other) 

113 

114 configFile = butlerConfig.configFile 

115 if configFile is not None: 

116 uri = ButlerURI(configFile) 

117 self.configFile = uri 

118 self.configDir = uri.dirname() 

119 

120 # A Butler config contains defaults defined by each of the component 

121 # configuration classes. We ask each of them to apply defaults to 

122 # the values we have been supplied by the user. 

123 for configClass in CONFIG_COMPONENT_CLASSES: 

124 # Only send the parent config if the child 

125 # config component is present (otherwise it assumes that the 

126 # keys from other components are part of the child) 

127 localOverrides = None 

128 if configClass.component in butlerConfig: 

129 localOverrides = butlerConfig 

130 config = configClass(localOverrides, searchPaths=searchPaths) 

131 # Re-attach it using the global namespace 

132 self.update({configClass.component: config}) 

133 # Remove the key from the butlerConfig since we have already 

134 # merged that information. 

135 if configClass.component in butlerConfig: 

136 del butlerConfig[configClass.component] 

137 

138 # Now that we have all the defaults we can merge the externally 

139 # provided config into the defaults. 

140 # Not needed if there is never information in a butler config file 

141 # not present in component configurations 

142 self.update(butlerConfig)