Debian Codegraph @

src/lazr/config/interfaces.py

raw symbols SCIP
1 # Copyright 2007-2015 Canonical Ltd. All rights reserved.
2 #
3 # This file is part of lazr.config
4 #
5 # lazr.config is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, version 3 of the License.
8 #
9 # lazr.config is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12 # License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with lazr.config. If not, see <http://www.gnu.org/licenses/>.
16
17 # pylint: disable-msg=E0211,E0213,W0231
18 """Interfaces for process configuration.."""
19
20 __all__ = [
21 "ConfigErrors",
22 "ConfigSchemaError",
23 "IConfigData",
24 "NoConfigError",
25 "ICategory",
26 "IConfigLoader",
27 "IConfigSchema",
28 "InvalidSectionNameError",
29 "ISection",
30 "ISectionSchema",
31 "IStackableConfig",
32 "NoCategoryError",
33 "RedefinedKeyError",
34 "RedefinedSectionError",
35 "UnknownKeyError",
36 "UnknownSectionError",
37 ]
38
39 from zope.interface import Attribute, Interface
40
41
42 class ConfigSchemaError(Exception):
43 """A base class of all `IConfigSchema` errors."""
44
45
46 class RedefinedKeyError(ConfigSchemaError):
47 """A key in a section cannot be redefined."""
48
49
50 class RedefinedSectionError(ConfigSchemaError):
51 """A section in a config file cannot be redefined."""
52
53
54 class InvalidSectionNameError(ConfigSchemaError):
55 """The section name contains more than one category."""
56
57
58 class NoCategoryError(LookupError):
59 """No `ISectionSchema`s belong to the category name."""
60
61
62 class UnknownSectionError(ConfigSchemaError):
63 """The config has a section that is not in the schema."""
64
65
66 class UnknownKeyError(ConfigSchemaError):
67 """The section has a key that is not in the schema."""
68
69
70 class NoConfigError(ConfigSchemaError):
71 """No config has the name."""
72
73
74 class ConfigErrors(ConfigSchemaError):
75 """The errors in a Config.
76
77 The list of errors can be accessed via the errors attribute.
78 """
79
80 def __init__(self, message, errors=None):
81 """Initialize the error with a message and errors.
82
83 :param message: a message string
84 :param errors: a list of errors in the config, or None
85 """
86 self.message = message
87 self.errors = errors
88
89 def __str__(self):
90 return "{}: {}".format(self.__class__.__name__, self.message)
91
92
93 class ISectionSchema(Interface):
94 """Defines the valid keys and default values for a configuration group."""
95
96 name = Attribute("The section name.")
97 optional = Attribute("Is the section optional in the config?")
98 category_and_section_names = Attribute(
99 "A 2-Tuple of the category and specific name parts."
100 )
101
102 def __iter__():
103 """Iterate over the keys."""
104
105 def __contains__(name):
106 """Return True or False if name is a key."""
107
108 def __getitem__(key):
109 """Return the default value of the key.
110
111 :raise `KeyError`: if the key does not exist.
112 """
113
114
115 class ISection(ISectionSchema):
116 """Defines the values for a configuration group."""
117
118 schema = Attribute("The ISectionSchema that defines this ISection.")
119
120 def __getattr__(name):
121 """Return the named key.
122
123 :name: a key name.
124 :return: the value of the matching key.
125 :raise: AttributeError if there is no key with the name.
126 """
127
128
129 class IConfigLoader(Interface):
130 """A configuration file loader."""
131
132 def load(filename):
133 """Load a configuration from the file at filename."""
134
135 def loadFile(source_file, filename=None):
136 """Load a configuration from the open source_file.
137
138 :param source_file: A file-like object that supports read() and
139 readline()
140 :param filename: The name of the configuration. If filename is None,
141 The name will be taken from source_file.name.
142 """
143
144
145 class IConfigSchema(Interface):
146 """A process configuration schema.
147
148 The config file contains sections enclosed in square brackets ([]).
149 The section name may be divided into major and minor categories using a
150 dot (.). Beneath each section is a list of key-value pairs, separated
151 by a colon (:).
152
153 Multiple sections with the same major category may have their keys defined
154 in another section that appends the '.template' or '.master' suffixes to
155 the category name. A section with '.optional' suffix is not
156 required. Lines that start with a hash (#) are comments.
157 """
158
159 name = Attribute("The basename of the config filename.")
160 filename = Attribute("The path to config file")
161 category_names = Attribute("The list of section category names.")
162
163 def __iter__():
164 """Iterate over the `ISectionSchema`s."""
165
166 def __contains__(name):
167 """Return True or False if the name matches a `ISectionSchema`."""
168
169 def __getitem__(name):
170 """Return the `ISectionSchema` with the matching name.
171
172 :raise `NoSectionError`: if the no ISectionSchema has the name.
173 """
174
175 def getByCategory(name):
176 """Return a list of ISectionSchemas that belong to the category name.
177
178 `ISectionSchema` names may be made from a category name and a group
179 name, separated by a dot (.). The category is synonymous with a
180 arbitrary resource such as a database or a vhost. Thus database.bugs
181 and database.answers are two sections that both use the database
182 resource.
183
184 :raise `CategoryNotFound`: if no sections have a name that starts
185 with the category name.
186 """
187
188
189 class IConfigData(IConfigSchema):
190 """A process configuration.
191
192 See `IConfigSchema` for more information about the config file format.
193 """
194
195
196 class IStackableConfig(IConfigSchema):
197 """A configuration that is built from configs that extend each other.
198
199 A config may extend another config so that a configuration for a
200 process need only define the localized sections and keys. The
201 configuration is constructed from a stack of data that defines,
202 and redefines, the sections and keys in the configuration. Each config
203 overlays its data to define the final configuration.
204
205 A config file declares that is extends another using the 'extends' key
206 in the 'meta' section of the config data file:
207 [meta]
208 extends: common.conf
209
210 The push() and pop() methods can be used to test processes where the
211 test environment must be configured differently.
212 """
213
214 schema = Attribute("The schema that defines the config.")
215 data = Attribute("The current ConfigData. use by the config.")
216 extends = Attribute("The ConfigData that this config extends.")
217 overlays = Attribute("The stack of ConfigData that define this config.")
218
219 def __getattr__(name):
220 """Return the named section.
221
222 :name: a section or category name.
223 :return: the matching `ISection` or `ICategory`.
224 :raise: AttributeError if there is no section or category with the
225 name.
226 """
227
228 def validate():
229 """Return True if the config is valid for the schema.
230
231 :raise `ConfigErrors`: if the are errors. A list of all schema
232 problems can be retrieved via the errors property.
233 """
234
235 def push(conf_name, conf_data):
236 """Overlay the config with unparsed config data.
237
238 :param conf_name: the name of the config.
239 :param conf_data: a string of unparsed config data.
240
241 This method appends the parsed `IConfigData` to the overlays property.
242 """
243
244 def pop(conf_name):
245 """Remove conf_name from the overlays stack.
246
247 :param conf_name: the name of the `IConfigData` to remove.
248 :return: the tuple of `IConfigData` that was removed from overlays.
249 :raise NoConfigError: if no `IConfigData` has the conf_name.
250
251 This method removes the named ConfigData from the stack; ConfigData
252 above the named ConfigData are removed too.
253 """
254
255
256 class ICategory(Interface):
257 """A group of related sections.
258
259 The sections within a category are access as attributes of the
260 `ICategory`.
261 """
262
263 def __getattr__(name):
264 """Return the named section.
265
266 :name: a section name.
267 :return: the matching `ISection`.
268 :raise: AttributeError if there is no section with the name.
269 """