-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig.py
More file actions
633 lines (525 loc) · 21.4 KB
/
config.py
File metadata and controls
633 lines (525 loc) · 21.4 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import optparse
from rpython.tool.pairtype import extendabletype
SUPPRESS_USAGE = optparse.SUPPRESS_USAGE
class AmbigousOptionError(Exception):
pass
class NoMatchingOptionFound(AttributeError):
pass
class ConfigError(Exception):
pass
class ConflictConfigError(ConfigError):
pass
class Config(object):
_cfgimpl_frozen = False
def __init__(self, descr, parent=None, **overrides):
self._cfgimpl_descr = descr
self._cfgimpl_value_owners = {}
self._cfgimpl_parent = parent
self._cfgimpl_values = {}
self._cfgimpl_warnings = []
self._cfgimpl_build(overrides)
def _cfgimpl_build(self, overrides):
for child in self._cfgimpl_descr._children:
if isinstance(child, Option):
self._cfgimpl_values[child._name] = child.getdefault()
self._cfgimpl_value_owners[child._name] = 'default'
elif isinstance(child, OptionDescription):
self._cfgimpl_values[child._name] = Config(child, parent=self)
self.override(overrides)
def override(self, overrides):
for name, value in overrides.iteritems():
homeconfig, name = self._cfgimpl_get_home_by_path(name)
homeconfig.setoption(name, value, 'default')
def copy(self, as_default=False, parent=None):
result = Config.__new__(self.__class__)
result._cfgimpl_descr = self._cfgimpl_descr
result._cfgimpl_value_owners = owners = {}
result._cfgimpl_parent = parent
result._cfgimpl_values = v = {}
for child in self._cfgimpl_descr._children:
if isinstance(child, Option):
v[child._name] = self._cfgimpl_values[child._name]
if as_default:
owners[child._name] = 'default'
else:
owners[child._name] = (
self._cfgimpl_value_owners[child._name])
elif isinstance(child, OptionDescription):
v[child._name] = self._cfgimpl_values[child._name].copy(
as_default, parent=result)
return result
def __setattr__(self, name, value):
if self._cfgimpl_frozen and getattr(self, name) != value:
raise TypeError("trying to change a frozen option object")
if name.startswith('_cfgimpl_'):
self.__dict__[name] = value
return
self.setoption(name, value, 'user')
def __getattr__(self, name):
if '.' in name:
homeconfig, name = self._cfgimpl_get_home_by_path(name)
return getattr(homeconfig, name)
if name.startswith('_cfgimpl_'):
# if it were in __dict__ it would have been found already
raise AttributeError("%s object has no attribute %s" %
(self.__class__, name))
if name not in self._cfgimpl_values:
raise AttributeError("%s object has no attribute %s" %
(self.__class__, name))
return self._cfgimpl_values[name]
def __dir__(self):
from_type = dir(type(self))
from_dict = list(self.__dict__)
extras = list(self._cfgimpl_values)
return sorted(set(extras + from_type + from_dict))
def __delattr__(self, name):
# XXX if you use delattr you are responsible for all bad things
# happening
if name.startswith('_cfgimpl_'):
del self.__dict__[name]
return
self._cfgimpl_value_owners[name] = 'default'
opt = getattr(self._cfgimpl_descr, name)
if isinstance(opt, OptionDescription):
raise AttributeError("can't option subgroup")
self._cfgimpl_values[name] = getattr(opt, 'default', None)
def setoption(self, name, value, who):
if name not in self._cfgimpl_values:
raise AttributeError('unknown option %s' % (name,))
child = getattr(self._cfgimpl_descr, name)
oldowner = self._cfgimpl_value_owners[child._name]
if oldowner not in ("default", "suggested"):
oldvalue = getattr(self, name)
if oldvalue == value or who in ("default", "suggested"):
return
raise ConflictConfigError('cannot override value to %s for '
'option %s' % (value, name))
child.setoption(self, value, who)
self._cfgimpl_value_owners[name] = who
def suggest(self, **kwargs):
for name, value in kwargs.items():
self.suggestoption(name, value)
def suggestoption(self, name, value):
try:
self.setoption(name, value, "suggested")
except ConflictConfigError:
# setting didn't work, but that is fine, since it is
# suggested only
pass
def set(self, **kwargs):
all_paths = [p.split(".") for p in self.getpaths()]
for key, value in kwargs.iteritems():
key_p = key.split('.')
candidates = [p for p in all_paths if p[-len(key_p):] == key_p]
if len(candidates) == 1:
name = '.'.join(candidates[0])
homeconfig, name = self._cfgimpl_get_home_by_path(name)
homeconfig.setoption(name, value, "user")
elif len(candidates) > 1:
raise AmbigousOptionError(
'more than one option that ends with %s' % (key, ))
else:
raise NoMatchingOptionFound(
'there is no option that matches %s' % (key, ))
def _cfgimpl_get_home_by_path(self, path):
"""returns tuple (config, name)"""
path = path.split('.')
for step in path[:-1]:
self = getattr(self, step)
return self, path[-1]
def _cfgimpl_get_toplevel(self):
while self._cfgimpl_parent is not None:
self = self._cfgimpl_parent
return self
def add_warning(self, warning):
self._cfgimpl_get_toplevel()._cfgimpl_warnings.append(warning)
def get_warnings(self):
return self._cfgimpl_get_toplevel()._cfgimpl_warnings
def _freeze_(self):
self.__dict__['_cfgimpl_frozen'] = True
return True
def getkey(self):
return self._cfgimpl_descr.getkey(self)
def __hash__(self):
return hash(self.getkey())
def __eq__(self, other):
return self.getkey() == other.getkey()
def __ne__(self, other):
return not self == other
def __iter__(self):
for child in self._cfgimpl_descr._children:
if isinstance(child, Option):
yield child._name, getattr(self, child._name)
def __str__(self, indent=""):
lines = []
children = [(child._name, child)
for child in self._cfgimpl_descr._children]
children.sort()
for name, child in children:
if self._cfgimpl_value_owners.get(name, None) == 'default':
continue
value = getattr(self, name)
if isinstance(value, Config):
substr = value.__str__(indent + " ")
else:
substr = "%s %s = %s" % (indent, name, value)
if substr:
lines.append(substr)
if indent and not lines:
return '' # hide subgroups with all default values
lines.insert(0, "%s[%s]" % (indent, self._cfgimpl_descr._name,))
return '\n'.join(lines)
def getpaths(self, include_groups=False):
"""returns a list of all paths in self, recursively
"""
return self._cfgimpl_descr.getpaths(include_groups=include_groups)
DEFAULT_OPTION_NAME = object()
class Option(object):
__metaclass__ = extendabletype
def __init__(self, name, doc, cmdline=DEFAULT_OPTION_NAME):
self._name = name
self.doc = doc
self.cmdline = cmdline
def validate(self, value):
raise NotImplementedError('abstract base class')
def getdefault(self):
return self.default
def setoption(self, config, value, who):
name = self._name
if who == "default" and value is None:
pass
elif not self.validate(value):
raise ConfigError('invalid value %s for option %s' % (value, name))
config._cfgimpl_values[name] = value
def getkey(self, value):
return value
def convert_from_cmdline(self, value):
return value
def add_optparse_option(self, argnames, parser, config):
callback = ConfigUpdate(config, self)
parser.add_option(help=self.doc+" %default",
action='callback', type=self.opt_type,
callback=callback, metavar=self._name.upper(),
*argnames)
class ChoiceOption(Option):
opt_type = 'string'
def __init__(self, name, doc, values, default=None, requires=None,
suggests=None, cmdline=DEFAULT_OPTION_NAME):
super(ChoiceOption, self).__init__(name, doc, cmdline)
self.values = values
self.default = default
if requires is None:
requires = {}
self._requires = requires
if suggests is None:
suggests = {}
self._suggests = suggests
def setoption(self, config, value, who):
name = self._name
for path, reqvalue in self._requires.get(value, []):
toplevel = config._cfgimpl_get_toplevel()
homeconfig, name = toplevel._cfgimpl_get_home_by_path(path)
if who == 'default':
who2 = 'default'
else:
who2 = 'required'
homeconfig.setoption(name, reqvalue, who2)
for path, reqvalue in self._suggests.get(value, []):
toplevel = config._cfgimpl_get_toplevel()
homeconfig, name = toplevel._cfgimpl_get_home_by_path(path)
homeconfig.suggestoption(name, reqvalue)
super(ChoiceOption, self).setoption(config, value, who)
def validate(self, value):
return value is None or value in self.values
def convert_from_cmdline(self, value):
return value.strip()
def _getnegation(optname):
if optname.startswith("without"):
return "with" + optname[len("without"):]
if optname.startswith("with"):
return "without" + optname[len("with"):]
return "no-" + optname
class BoolOption(Option):
def __init__(self, name, doc, default=None, requires=None,
suggests=None, validator=None,
cmdline=DEFAULT_OPTION_NAME, negation=True):
super(BoolOption, self).__init__(name, doc, cmdline=cmdline)
self._requires = requires
self._suggests = suggests
self.default = default
self.negation = negation
self._validator = validator
def validate(self, value):
return isinstance(value, bool)
def setoption(self, config, value, who):
name = self._name
if value and self._validator is not None:
toplevel = config._cfgimpl_get_toplevel()
self._validator(toplevel)
if value and self._requires is not None:
for path, reqvalue in self._requires:
toplevel = config._cfgimpl_get_toplevel()
homeconfig, name = toplevel._cfgimpl_get_home_by_path(path)
if who == 'default':
who2 = 'default'
else:
who2 = 'required'
homeconfig.setoption(name, reqvalue, who2)
if value and self._suggests is not None:
for path, reqvalue in self._suggests:
toplevel = config._cfgimpl_get_toplevel()
homeconfig, name = toplevel._cfgimpl_get_home_by_path(path)
homeconfig.suggestoption(name, reqvalue)
super(BoolOption, self).setoption(config, value, who)
def add_optparse_option(self, argnames, parser, config):
callback = BoolConfigUpdate(config, self, True)
parser.add_option(help=self.doc+" %default",
action='callback',
callback=callback, *argnames)
if not self.negation:
return
no_argnames = ["--" + _getnegation(argname.lstrip("-"))
for argname in argnames
if argname.startswith("--")]
if len(no_argnames) == 0:
no_argnames = ["--" + _getnegation(argname.lstrip("-"))
for argname in argnames]
callback = BoolConfigUpdate(config, self, False)
parser.add_option(help="unset option set by %s %%default" % (argname, ),
action='callback',
callback=callback, *no_argnames)
class IntOption(Option):
opt_type = 'int'
def __init__(self, name, doc, default=None, cmdline=DEFAULT_OPTION_NAME):
super(IntOption, self).__init__(name, doc, cmdline)
self.default = default
def validate(self, value):
try:
int(value)
except TypeError:
return False
return True
def setoption(self, config, value, who):
try:
super(IntOption, self).setoption(config, int(value), who)
except TypeError as e:
raise ConfigError(*e.args)
class FloatOption(Option):
opt_type = 'float'
def __init__(self, name, doc, default=None, cmdline=DEFAULT_OPTION_NAME):
super(FloatOption, self).__init__(name, doc, cmdline)
self.default = default
def validate(self, value):
try:
float(value)
except TypeError:
return False
return True
def setoption(self, config, value, who):
try:
super(FloatOption, self).setoption(config, float(value), who)
except TypeError as e:
raise ConfigError(*e.args)
class StrOption(Option):
opt_type = 'string'
def __init__(self, name, doc, default=None, cmdline=DEFAULT_OPTION_NAME):
super(StrOption, self).__init__(name, doc, cmdline)
self.default = default
def validate(self, value):
return isinstance(value, str)
def setoption(self, config, value, who):
try:
super(StrOption, self).setoption(config, value, who)
except TypeError as e:
raise ConfigError(*e.args)
class ArbitraryOption(Option):
def __init__(self, name, doc, default=None, defaultfactory=None):
super(ArbitraryOption, self).__init__(name, doc, cmdline=None)
self.default = default
self.defaultfactory = defaultfactory
if defaultfactory is not None:
assert default is None
def validate(self, value):
return True
def add_optparse_option(self, *args, **kwargs):
return
def getdefault(self):
if self.defaultfactory is not None:
return self.defaultfactory()
return self.default
class OptionDescription(object):
__metaclass__ = extendabletype
cmdline = None
def __init__(self, name, doc, children):
self._name = name
self.doc = doc
self._children = children
self._build()
def _build(self):
for child in self._children:
setattr(self, child._name, child)
def getkey(self, config):
return tuple([child.getkey(getattr(config, child._name))
for child in self._children])
def add_optparse_option(self, argnames, parser, config):
return
def getpaths(self, include_groups=False, currpath=None):
"""returns a list of all paths in self, recursively
currpath should not be provided (helps with recursion)
"""
if currpath is None:
currpath = []
paths = []
for option in self._children:
attr = option._name
if attr.startswith('_cfgimpl'):
continue
value = getattr(self, attr)
if isinstance(value, OptionDescription):
if include_groups:
paths.append('.'.join(currpath + [attr]))
currpath.append(attr)
paths += value.getpaths(include_groups=include_groups,
currpath=currpath)
currpath.pop()
else:
paths.append('.'.join(currpath + [attr]))
return paths
class OptHelpFormatter(optparse.TitledHelpFormatter):
extra_useage = None
def expand_default(self, option):
assert self.parser
dfls = self.parser.defaults
defl = ""
choices = None
if option.action == 'callback' and isinstance(option.callback, ConfigUpdate):
callback = option.callback
defl = callback.help_default()
if isinstance(callback.option, ChoiceOption):
choices = callback.option.values
else:
val = dfls.get(option.dest)
if val is None:
pass
elif isinstance(val, bool):
if val is True and option.action=="store_true":
defl = "default"
else:
defl = "default: %s" % val
if option.type == 'choice':
choices = option.choices
if choices is not None:
choices = "%s=%s" % (option.metavar, '|'.join(choices))
else:
choices = ""
if '%default' in option.help:
if choices and defl:
sep = ", "
else:
sep = ""
defl = '[%s%s%s]' % (choices, sep, defl)
if defl == '[]':
defl = ""
return option.help.replace("%default", defl)
elif choices:
return option.help + ' [%s]' % choices
return option.help
def format_usage(self, usage):
# XXX bit of a hack
result = optparse.TitledHelpFormatter.format_usage(self, usage)
if self.extra_useage is not None:
return result + "\n" + self.extra_useage + "\n\n"
return result
class ConfigUpdate(object):
def __init__(self, config, option):
self.config = config
self.option = option
def convert_from_cmdline(self, value):
return self.option.convert_from_cmdline(value)
def __call__(self, option, opt_str, value, parser, *args, **kwargs):
try:
value = self.convert_from_cmdline(value)
self.config.setoption(self.option._name, value, who='cmdline')
except ConfigError as e:
# This OptionValueError is going to exit the translate.py process.
# Now is the last chance to print the warnings, which might give
# more information... hack.
import sys
for warning in self.config.get_warnings():
print >> sys.stderr, warning
raise optparse.OptionValueError(e.args[0])
def help_default(self):
default = getattr(self.config, self.option._name)
owner = self.config._cfgimpl_value_owners[self.option._name]
if default is None:
if owner == 'default':
return ''
else:
default = '???'
return "%s: %s" % (owner, default)
class BoolConfigUpdate(ConfigUpdate):
def __init__(self, config, option, which_value):
super(BoolConfigUpdate, self).__init__(config, option)
self.which_value = which_value
def convert_from_cmdline(self, value):
return self.which_value
def help_default(self):
default = getattr(self.config, self.option._name)
owner = self.config._cfgimpl_value_owners[self.option._name]
if default == self.which_value:
return owner
else:
return ""
def to_optparse(config, useoptions=None, parser=None,
parserargs=None, parserkwargs=None, extra_useage=None):
grps = {}
def get_group(name, doc):
steps = name.split('.')
if len(steps) < 2:
return parser
grpname = steps[-2]
grp = grps.get(grpname, None)
if grp is None:
grp = grps[grpname] = parser.add_option_group(doc)
return grp
if parser is None:
if parserargs is None:
parserargs = []
if parserkwargs is None:
parserkwargs = {}
formatter = OptHelpFormatter()
formatter.extra_useage = extra_useage
parser = optparse.OptionParser(
formatter=formatter,
*parserargs, **parserkwargs)
if useoptions is None:
useoptions = config.getpaths(include_groups=True)
seen = {}
for path in useoptions:
if path.endswith(".*"):
path = path[:-2]
homeconf, name = config._cfgimpl_get_home_by_path(path)
subconf = getattr(homeconf, name)
children = [
path + "." + child
for child in subconf.getpaths()]
useoptions.extend(children)
else:
if path in seen:
continue
seen[path] = True
homeconf, name = config._cfgimpl_get_home_by_path(path)
option = getattr(homeconf._cfgimpl_descr, name)
if option.cmdline is DEFAULT_OPTION_NAME:
chunks = ('--%s' % (path.replace('.', '-'),),)
elif option.cmdline is None:
continue
else:
chunks = option.cmdline.split(' ')
grp = get_group(path, homeconf._cfgimpl_descr.doc)
option.add_optparse_option(chunks, grp, homeconf)
return parser
def make_dict(config):
paths = config.getpaths()
options = dict([(path, getattr(config, path)) for path in paths])
return options