-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.py
222 lines (188 loc) · 7.29 KB
/
Configuration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# coding=utf-8
"""
Python configuration encapsulation
==================================
Configuration objects that may container options or sub-configuration layers.
License (MIT)
=============
Copyright (c) 2014 Paul Tunison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from __future__ import print_function
from . import errors
from .ConfigurationOption import ConfigurationOption
__all__ = [
"Configuration",
]
class Configuration (object):
"""
Container of options and sub-configurations
Options may be accessed and set by either methods, bracket notation or
dot notation. To use dot notation for getting/setting, the naming of the
option must not conflict with named components of python class/instance
(error raised).
Option descriptions / comments may only be set via the set_option method or
by accessing a set option.
"""
def __init__(self, name=None):
""" Initialize a new configuration block
:param name: A name or None
:type name: None or str
"""
# because of use of __getattr__
self.__dict__['_Configuration__name'] = name
self.__dict__['_Configuration__options'] = {}
@property
def name(self):
return self.__name
def __repr__(self):
return "Configuration{%s}" % self.name
@staticmethod
def __key_name(item):
# given may be a string, ConfigurationOption or a Configuration. Extract
# name and compare against our option keys (names).
if isinstance(item, (Configuration, ConfigurationOption)):
item_name = item.name
elif isinstance(item, str):
item_name = item
else:
raise errors.ConfigurationInvalidItemKeyTypeError()
return item_name
def get_option(self, key):
""" option getter helper function
"""
item_name = self.__key_name(key)
if item_name in self.__options:
return self.__options[item_name]
else:
raise errors.ConfigurationKeyNotPresentError(item_name)
def set_option(self, key, value, descr=None):
if isinstance(value, (Configuration, ConfigurationOption)):
self.__options[key] = value
if descr:
self.__options[key].description = descr
else:
self.__options[key] = ConfigurationOption(key, value, descr)
def __getitem__(self, key):
""" option bracket access """
return self.get_option(key)
def __setitem__(self, key, value):
""" Option bracket setter """
return self.set_option(key, value)
def __getattr__(self, key):
""" Dot notation """
return self.get_option(key)
def __setattr__(self, key, value):
# Check that we're not setting class or instance names
if key in self.__dict__ or key in self.__class__.__dict__:
raise errors.ConfigurationDotNotationSetError(key)
self.set_option(key, value)
def __contains__(self, item):
return self.__key_name(item) in self.__options
def __iter__(self):
for k in self.__options:
yield k
def __len__(self):
return len(self.__options)
# TODO: Merge functionality
# def __add__(self, other):
# if not isinstance(other, Configuration):
# raise ValueError("Cannot combine with something that is not "
# "another Configuration instance.")
# c = Configuration(name='+'.join((self.name, other.name)))
# for s in self:
# for o in s:
# setattr(getattr(c, s()), o.name, o())
# for s in other:
# for o in s:
# setattr(getattr(c, s()), o.name, o())
# return c
#
# def __iadd__(self, other):
# if not isinstance(other, Configuration):
# raise ValueError("Cannot combine with something that is not "
# "another Configuration instance.")
# for s in other:
# for o in s:
# setattr(getattr(self, s()), o.name, o())
# return self
# TODO: Copy/Deepcopy methods
# def __copy__(self):
# pass
# def __deepcopy__(self, memo=None):
# pass
# TODO: Move the following to the io submodule + assert that to-file
# configurations are strictly two levels (configuration of configurations
# of only options) in order to fit INI format.
# def load_ini_configuration(*in_files, **kwds):
# """ Load a configuration from one or more INI files.
#
# The order in which files are specified matters. If multiple files in the
# list have the same sections/options, the last file that defines them will
# take precedence.
#
# :param in_file: Files to initialize a Configuration object from.
# :type in_file: tuple of str
# :param partial_load_error: Flag telling us to error when input files are
# only partially loaded. This is False by default (logging module warning
# issued instead).
# :type partial_load_error: bool
#
# :return: Configuration object based on the input files
# :rtype:
#
# """
# log = logging.getLogger('pyConfig.load_ini_configuration')
#
# partial_load_error = kwds.get("partial_load_error", False)
#
# scp_config = SafeConfigParser()
# files_read = scp_config.read(in_files)
# if set(in_files) != set(files_read):
# if partial_load_error:
# def log_func(msg):
# raise RuntimeError(msg)
# else:
# log_func = log.warn
# log_func("Failed to load all given input files")
#
# config = Configuration()
# for sect in scp_config.sections():
# for opt in scp_config.options(sect):
# setattr(getattr(config, sect), opt, scp_config.get(sect, opt))
# return config
#
#
# def save_ini_configuration(config, out_file):
# """ Save a configuration to file using SafeConfigParser (INI file format)
#
# :param config: Configuration object to save
# :type config: Configuration
# :param out_file: Output file path
# :type out_file: str
#
# :return: True if save was successful, false otherwise.
# :rtype: bool
#
# """
# p = SafeConfigParser()
# for s in config:
# if s:
# p.add_section(s())
# for o in s:
# p.set(s(), o.name, o())
# p.write(open(out_file, 'w'))