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 

30import os.path 

31from typing import ( 

32 Optional, 

33 Sequence, 

34 Union, 

35) 

36 

37from .core import ( 

38 ButlerURI, 

39 Config, 

40 DatastoreConfig, 

41 StorageClassConfig, 

42) 

43from .registry import RegistryConfig 

44from .transfers import RepoTransferFormatConfig 

45 

46CONFIG_COMPONENT_CLASSES = (RegistryConfig, StorageClassConfig, 

47 DatastoreConfig, RepoTransferFormatConfig) 

48 

49 

50class ButlerConfig(Config): 

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

52 

53 The configuration is read and merged with default configurations for 

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

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

56 with a configuration class reads its own defaults. 

57 

58 Parameters 

59 ---------- 

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

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

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

63 be configured based entirely on defaults read from the environment 

64 or from ``searchPaths``. 

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

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

67 Explicit additional paths to search for defaults. They should 

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

69 than those read from the environment in 

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

71 refers to a configuration file or directory. 

72 """ 

73 

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

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

76 

77 self.configDir: Optional[ButlerURI] = None 

78 

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

80 # have already been loaded. 

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

82 super().__init__(other) 

83 # Ensure that the configuration directory propagates 

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

85 return 

86 

87 if isinstance(other, str): 

88 # This will only allow supported schemes 

89 uri = ButlerURI(other) 

90 if uri.isLocal: 

91 # Check explicitly that we have a directory 

92 if os.path.isdir(uri.ospath): 

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

94 else: 

95 # For generic URI assume that we have a directory 

96 # if the basename does not have a file extension 

97 # This heuristic is needed since we can not rely on 

98 # external users to include the trailing / and we 

99 # can't always check that the remote resource is a directory. 

100 if not uri.dirLike and not uri.getExtension(): 

101 # Force to a directory and add the default config name 

102 uri = ButlerURI(other, forceDirectory=True).join("butler.yaml") 

103 other = uri 

104 

105 # Create an empty config for us to populate 

106 super().__init__() 

107 

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

109 # defaults to use. 

110 butlerConfig = Config(other) 

111 

112 configFile = butlerConfig.configFile 

113 if configFile is not None: 

114 uri = ButlerURI(configFile) 

115 self.configDir = uri.dirname() 

116 

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

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

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

120 for configClass in CONFIG_COMPONENT_CLASSES: 

121 # Only send the parent config if the child 

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

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

124 localOverrides = None 

125 if configClass.component in butlerConfig: 

126 localOverrides = butlerConfig 

127 config = configClass(localOverrides, searchPaths=searchPaths) 

128 # Re-attach it using the global namespace 

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

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

131 # merged that information. 

132 if configClass.component in butlerConfig: 

133 del butlerConfig[configClass.component] 

134 

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

136 # provided config into the defaults. 

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

138 # not present in component configurations 

139 self.update(butlerConfig)