-
Notifications
You must be signed in to change notification settings - Fork 0
/
environconfig.py
294 lines (224 loc) · 7.42 KB
/
environconfig.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from functools import partial
import abc
import collections.abc
import csv
import logging
import os
#
# Abstract classes
#
class VarABC(metaclass=abc.ABCMeta):
_name = None
def __get_name__(self, owner):
if self._name is None:
self.__set_name__(owner)
return self._name
def __set_name__(self, owner, name=None):
if name is None:
# Explicit call (non-native).
for name, value in owner.__dict__.items():
if value is self:
self._name = name
break
else:
raise ValueError("Name not found.")
else:
# Called by python >= 3.6 at the time the owning class owner
# is created.
self._name = name
@abc.abstractmethod
def __get__(self, instance, owner):
pass
class EnvironConfigABCMeta(abc.ABCMeta):
def __new__(mcl, name, bases, nmspc):
# Register fields
fields = []
for key, value in nmspc.items():
if isinstance(value, VarABC):
fields.append(key)
nmspc["fields"] = tuple(fields)
return super(EnvironConfigABCMeta,
mcl).__new__(mcl, name, bases, nmspc)
class EnvironConfigABC(metaclass=EnvironConfigABCMeta):
@abc.abstractmethod
def getvar(self, name):
"""Return the raw value for this variable."""
pass
#
# Other
#
class NoVarDefault:
pass
class ClassAndInstanceMethod:
def __init__(self, fn):
self.fn = fn
self.obj = None
def __call__(self, *args, **kwargs):
return self.fn(self.obj, *args, **kwargs)
def __get__(self, instance, owner):
self.obj = owner if instance is None else instance
return self
classandinstancemethod = ClassAndInstanceMethod # A prettier alias
#
# Base classes
#
class EnvironConfig(EnvironConfigABC):
__varprefix__ = ""
environ = None
def __init__(self, environ=None):
if environ is not None \
and not isinstance(environ, collections.abc.Mapping):
raise TypeError("environ must be a mapping")
self.environ = environ
@classandinstancemethod
def getvar(obj, name):
varname = obj.__varprefix__ + name
try:
if obj.environ is None:
return os.environ[varname]
else:
return obj.environ[varname]
except KeyError as exc:
raise VarUnsetError("Unset var '{}'".format(varname))
@classandinstancemethod
def verify(obj, name=None):
if name is None:
for name in obj.fields:
try:
getattr(obj, name)
except (VarUnsetError, VarTypeCastError):
return False
return True
elif name not in obj.fields:
raise UnknownVarError("field unknown %r" % name)
else:
try:
getattr(obj, name)
except (VarUnsetError, VarTypeCastError):
return False
else:
return True
class EnvironVar(VarABC):
def __init__(self, default=NoVarDefault):
self.default = default
def __get__(self, instance, owner):
env = owner if instance is None else instance
if issubclass(owner, EnvironConfig):
name = self.__get_name__(owner)
try:
raw = env.getvar(name)
except VarUnsetError:
if self.default is NoVarDefault:
raise
else:
return self.default
else:
try:
return self._to_python(raw)
except Exception as exc:
raise VarTypeCastError(
"Cannot convert the value to python") from exc
else:
raise TypeError("Invalid environment {}".format(env))
@abc.abstractmethod
def _to_python(self, value):
"""Translate the raw value (string) to a python value."""
pass
class VirtualVar(VarABC):
def __get__(self, instance, owner):
env = owner if instance is None else instance
if issubclass(owner, EnvironConfig):
name = self.__get_name__(owner)
return self._action(env, name)
else:
raise TypeError("Invalid environment {}".format(env))
@abc.abstractmethod
def _action(self, env, name):
"""Action to do in the VirtualVar."""
pass
#
# Var types definition
#
class StringVar(EnvironVar):
def _to_python(self, value):
return value
class BytesVar(EnvironVar):
def _to_python(self, value):
return os.environ.encodevalue(value)
class PathVar(EnvironVar):
def _to_python(self, value):
return os.path.abspath(os.path.expanduser(value))
class IntVar(EnvironVar):
def _to_python(self, value):
return int(value)
class FloatVar(EnvironVar):
def _to_python(self, value):
return float(value)
class BooleanVar(EnvironVar):
def _to_python(self, value):
if value in ('1', 'yes', 'true', 'on'):
return True
elif value in ('0', 'no', 'false', 'off'):
return False
else:
raise ValueError("Unknown value %r" % value)
class ListVar(EnvironVar):
def __init__(self, dialect='excel', **kwargs):
self.reader = partial(csv.reader, dialect=dialect)
super().__init__(**kwargs)
def _to_python(self, value):
return tuple(next(self.reader([value])))
class CustomVar(EnvironVar):
def __init__(self, to_python, **kwargs):
self.to_python = to_python
super().__init__(**kwargs)
def _to_python(self, value):
return self.to_python(value)
@classmethod
def new(cls, to_python):
return partial(cls, to_python)
class MethodVar(VirtualVar):
def __init__(self, callable):
self.callable = callable
def _action(self, env, name):
return self.callable(env)
class LoggingLevelVar(EnvironVar):
def _to_python(self, value):
try:
return {"CRITICAL": logging.CRITICAL,
"ERROR": logging.ERROR,
"WARNING": logging.WARNING,
"INFO": logging.INFO,
"DEBUG": logging.DEBUG,
"NOTSET": logging.NOTSET}[value]
except KeyError:
raise VarTypeCastError("Invalid logging level %r" % value)
#
# Custom exceptions
#
class VarUnsetError(KeyError):
"""The environment variable is not set."""
pass
class VarTypeCastError(ValueError):
"""
The environment variable can't be casted to the appropiate Python
type.
"""
pass
class UnknownVarError(ValueError):
"""
This variable was not declared into the corresponding EnvironConfig.
"""
pass