/
config.py
975 lines (789 loc) · 35.3 KB
/
config.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
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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005-2009 Edgewall Software
# Copyright (C) 2005-2007 Christopher Lenz <cmlenz@gmx.de>
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
import copy
import os.path
import re
from ConfigParser import ConfigParser, ParsingError
from genshi.builder import tag
from trac.admin import AdminCommandError, IAdminCommandProvider
from trac.core import Component, ExtensionPoint, TracError, implements
from trac.util import AtomicFile, as_bool
from trac.util.compat import OrderedDict, wait_for_file_mtime_change
from trac.util.text import cleandoc, printout, to_unicode, to_utf8
from trac.util.translation import _, N_, tag_
__all__ = ['Configuration', 'ConfigSection', 'Option', 'BoolOption',
'IntOption', 'FloatOption', 'ListOption', 'ChoiceOption',
'PathOption', 'ExtensionOption', 'OrderedExtensionsOption',
'ConfigurationError']
_use_default = object()
def _getint(value):
return int(value or 0)
def _getfloat(value):
return float(value or 0.0)
def _getlist(value, sep, keep_empty):
if not value:
return []
if isinstance(value, basestring):
if isinstance(sep, (list, tuple)):
splitted = re.split('|'.join(map(re.escape, sep)), value)
else:
splitted = value.split(sep)
items = [item.strip() for item in splitted]
else:
items = list(value)
if not keep_empty:
items = [item for item in items if item not in (None, '')]
return items
class ConfigurationError(TracError):
"""Exception raised when a value in the configuration file is not valid."""
title = N_("Configuration Error")
def __init__(self, message=None, title=None, show_traceback=False):
if message is None:
message = _("Look in the Trac log for more information.")
super(ConfigurationError, self).__init__(message, title,
show_traceback)
class UnicodeConfigParser(ConfigParser):
"""A Unicode-aware version of ConfigParser. Arguments are encoded to
UTF-8 and return values are decoded from UTF-8.
"""
# All of the methods of ConfigParser are overridden except
# `getboolean`, `getint`, `getfloat`, `defaults`, `read`, `readfp`,
# `optionxform` and `write`. `getboolean`, `getint` and `getfloat`
# call `get`, so it isn't necessary to reimplement them.
# The base class `RawConfigParser` doesn't inherit from `object`
# so we can't use `super`.
def __init__(self, **kwargs):
dict_type = kwargs.pop('dict_type', None) or OrderedDict
ConfigParser.__init__(self, dict_type=dict_type, **kwargs)
def sections(self):
return map(to_unicode, ConfigParser.sections(self))
def add_section(self, section):
section_str = to_utf8(section)
ConfigParser.add_section(self, section_str)
def has_section(self, section):
section_str = to_utf8(section)
return ConfigParser.has_section(self, section_str)
def options(self, section):
section_str = to_utf8(section)
return map(to_unicode, ConfigParser.options(self, section_str))
def get(self, section, option, raw=False, vars=None):
section_str = to_utf8(section)
option_str = to_utf8(option)
return to_unicode(ConfigParser.get(self, section_str,
option_str, raw, vars))
def items(self, section, raw=False, vars=None):
section_str = to_utf8(section)
return [(to_unicode(k), to_unicode(v))
for k, v in ConfigParser.items(self, section_str, raw, vars)]
def has_option(self, section, option):
section_str = to_utf8(section)
option_str = to_utf8(option)
return ConfigParser.has_option(self, section_str, option_str)
def set(self, section, option, value=None):
section_str = to_utf8(section)
option_str = to_utf8(option)
value_str = to_utf8(value) if value is not None else ''
ConfigParser.set(self, section_str, option_str, value_str)
def remove_option(self, section, option):
section_str = to_utf8(section)
option_str = to_utf8(option)
ConfigParser.remove_option(self, section_str, option_str)
def remove_section(self, section):
section_str = to_utf8(section)
ConfigParser.remove_section(self, section_str)
def __copy__(self):
parser = self.__class__()
parser._sections = copy.copy(self._sections)
return parser
def __deepcopy__(self, memo):
parser = self.__class__()
parser._sections = copy.deepcopy(self._sections)
return parser
class Configuration(object):
"""Thin layer over `ConfigParser` from the Python standard library.
In addition to providing some convenience methods, the class remembers
the last modification time of the configuration file, and reparses it
when the file has changed.
"""
def __init__(self, filename, params={}):
self.filename = filename
self.parser = UnicodeConfigParser()
self._pristine_parser = None
self.parents = []
self._lastmtime = 0
self._sections = {}
self.parse_if_needed(force=True)
def __repr__(self):
return '<%s %r>' % (self.__class__.__name__, self.filename)
def __contains__(self, name):
"""Return whether the configuration contains a section of the given
name.
"""
return name in self.sections()
def __getitem__(self, name):
"""Return the configuration section with the specified name."""
if name not in self._sections:
self._sections[name] = Section(self, name)
return self._sections[name]
def get(self, section, key, default=''):
"""Return the value of the specified option.
Valid default input is a string. Returns a string.
"""
return self[section].get(key, default)
def getbool(self, section, key, default=''):
"""Return the specified option as boolean value.
If the value of the option is one of "yes", "true", "enabled", "on",
or "1", this method wll return `True`, otherwise `False`.
Valid default input is a string or a bool. Returns a bool.
"""
return self[section].getbool(key, default)
def getint(self, section, key, default=''):
"""Return the value of the specified option as integer.
If the specified option can not be converted to an integer, a
`ConfigurationError` exception is raised.
Valid default input is a string or an int. Returns an int.
"""
return self[section].getint(key, default)
def getfloat(self, section, key, default=''):
"""Return the value of the specified option as float.
If the specified option can not be converted to a float, a
`ConfigurationError` exception is raised.
Valid default input is a string, float or int. Returns a float.
"""
return self[section].getfloat(key, default)
def getlist(self, section, key, default='', sep=',', keep_empty=False):
"""Return a list of values that have been specified as a single
comma-separated option.
A different separator can be specified using the `sep` parameter. The
`sep` parameter can specify multiple values using a list or a tuple.
If the `keep_empty` parameter is set to `True`, empty elements are
included in the list.
Valid default input is a string or a list. Returns a string.
"""
return self[section].getlist(key, default, sep, keep_empty)
def getpath(self, section, key, default=''):
"""Return a configuration value as an absolute path.
Relative paths are resolved relative to the location of this
configuration file.
Valid default input is a string. Returns a normalized path.
"""
return self[section].getpath(key, default)
def set(self, section, key, value):
"""Change a configuration value.
These changes are not persistent unless saved with `save()`.
"""
self[section].set(key, value)
def defaults(self, compmgr=None):
"""Returns a dictionary of the default configuration values.
If `compmgr` is specified, return only options declared in components
that are enabled in the given `ComponentManager`.
"""
defaults = {}
for (section, key), option in \
Option.get_registry(compmgr).iteritems():
defaults.setdefault(section, {})[key] = \
option.dumps(option.default)
return defaults
def options(self, section, compmgr=None):
"""Return a list of `(name, value)` tuples for every option in the
specified section.
This includes options that have default values that haven't been
overridden. If `compmgr` is specified, only return default option
values for components that are enabled in the given
`ComponentManager`.
"""
return self[section].options(compmgr)
def remove(self, section, key):
"""Remove the specified option."""
self[section].remove(key)
def sections(self, compmgr=None, defaults=True):
"""Return a list of section names.
If `compmgr` is specified, only the section names corresponding to
options declared in components that are enabled in the given
`ComponentManager` are returned.
"""
sections = set(self.parser.sections())
for parent in self.parents:
sections.update(parent.sections(compmgr, defaults=False))
if defaults:
sections.update(self.defaults(compmgr))
return sorted(sections)
def has_option(self, section, option, defaults=True):
"""Returns True if option exists in section in either the project
trac.ini or one of the parents, or is available through the Option
registry.
"""
return self[section].contains(option, defaults)
def save(self):
"""Write the configuration options to the primary file."""
all_options = {}
for (section, name), option in Option.get_registry().iteritems():
all_options.setdefault(section, {})[name] = option
def normalize(section, name, value):
option = all_options.get(section, {}).get(name)
return option.normalize(value) if option else value
sections = []
for section in self.sections():
options = []
for option in self[section]:
default = None
for parent in self.parents:
if parent.has_option(section, option, defaults=False):
default = normalize(section, option,
parent.get(section, option))
break
if self.parser.has_option(section, option):
current = normalize(section, option,
self.parser.get(section, option))
if current != default:
options.append((option, current))
if options:
sections.append((section, sorted(options)))
# Prepare new file contents to write to disk.
parser = UnicodeConfigParser()
for section, options in sections:
parser.add_section(section)
for key, val in options:
parser.set(section, key, val)
try:
self._write(parser)
except Exception:
# Revert all changes to avoid inconsistencies
self.parser = copy.deepcopy(self._pristine_parser)
raise
else:
self._pristine_parser = copy.deepcopy(self.parser)
def parse_if_needed(self, force=False):
if not self.filename or not os.path.isfile(self.filename):
return False
changed = False
modtime = os.path.getmtime(self.filename)
if force or modtime != self._lastmtime:
self.parser = UnicodeConfigParser()
try:
if not self.parser.read(self.filename):
raise TracError(_("Error reading '%(file)s', make sure "
"it is readable.", file=self.filename))
except ParsingError as e:
raise TracError(e)
self._lastmtime = modtime
self._pristine_parser = copy.deepcopy(self.parser)
changed = True
if changed:
self.parents = self._get_parents()
else:
for parent in self.parents:
changed |= parent.parse_if_needed(force=force)
if changed:
self._sections = {}
return changed
def touch(self):
if self.filename and os.path.isfile(self.filename) \
and os.access(self.filename, os.W_OK):
wait_for_file_mtime_change(self.filename)
def set_defaults(self, compmgr=None, component=None):
"""Retrieve all default values and store them explicitly in the
configuration, so that they can be saved to file.
Values already set in the configuration are not overwritten.
"""
def set_option_default(option):
section = option.section
name = option.name
if not self.has_option(section, name, defaults=False):
value = option.dumps(option.default)
self.set(section, name, value)
if component:
if component.endswith('.*'):
component = component[:-2]
component = component.lower().split('.')
from trac.core import ComponentMeta
for cls in ComponentMeta._components:
clsname = (cls.__module__ + '.' + cls.__name__).lower() \
.split('.')
if clsname[:len(component)] == component:
for option in cls.__dict__.itervalues():
if isinstance(option, Option):
set_option_default(option)
else:
for option in Option.get_registry(compmgr).itervalues():
set_option_default(option)
def _get_parents(self):
_parents = []
if self.parser.has_option('inherit', 'file'):
for filename in self.parser.get('inherit', 'file').split(','):
filename = filename.strip()
if not os.path.isabs(filename):
filename = os.path.join(os.path.dirname(self.filename),
filename)
_parents.append(Configuration(filename))
return _parents
def _write(self, parser):
if not self.filename:
return
wait_for_file_mtime_change(self.filename)
with AtomicFile(self.filename, 'w') as fd:
fd.writelines(['# -*- coding: utf-8 -*-\n', '\n'])
parser.write(fd)
class Section(object):
"""Proxy for a specific configuration section.
Objects of this class should not be instantiated directly.
"""
__slots__ = ['config', 'name', '_cache']
def __init__(self, config, name):
self.config = config
self.name = name
self._cache = {}
def __repr__(self):
return '<%s [%s]>' % (self.__class__.__name__, self.name)
def contains(self, key, defaults=True):
if self.config.parser.has_option(self.name, key):
return True
for parent in self.config.parents:
if parent[self.name].contains(key, defaults=False):
return True
return defaults and (self.name, key) in Option.registry
__contains__ = contains
def iterate(self, compmgr=None, defaults=True):
"""Iterate over the options in this section.
If `compmgr` is specified, only return default option values for
components that are enabled in the given `ComponentManager`.
"""
options = set()
if self.config.parser.has_section(self.name):
for option in self.config.parser.options(self.name):
options.add(option.lower())
yield option
for parent in self.config.parents:
for option in parent[self.name].iterate(defaults=False):
loption = option.lower()
if loption not in options:
options.add(loption)
yield option
if defaults:
for section, option in Option.get_registry(compmgr).iterkeys():
if section == self.name and option.lower() not in options:
yield option
__iter__ = iterate
def get(self, key, default=''):
"""Return the value of the specified option.
Valid default input is a string. Returns a string.
"""
cached = self._cache.get(key, _use_default)
if cached is not _use_default:
return cached
if self.config.parser.has_option(self.name, key):
value = self.config.parser.get(self.name, key)
else:
for parent in self.config.parents:
value = parent[self.name].get(key, _use_default)
if value is not _use_default:
break
else:
if default is not _use_default:
option = Option.registry.get((self.name, key))
value = option.dumps(option.default) if option \
else _use_default
else:
value = _use_default
if value is _use_default:
return default
self._cache[key] = value
return value
def getbool(self, key, default=''):
"""Return the value of the specified option as boolean.
This method returns `True` if the option value is one of "yes",
"true", "enabled", "on", or non-zero numbers, ignoring case.
Otherwise `False` is returned.
Valid default input is a string or a bool. Returns a bool.
"""
return as_bool(self.get(key, default))
def getint(self, key, default=''):
"""Return the value of the specified option as integer.
If the specified option can not be converted to an integer, a
`ConfigurationError` exception is raised.
Valid default input is a string or an int. Returns an int.
"""
value = self.get(key, default)
try:
return _getint(value)
except ValueError:
raise ConfigurationError(
_('[%(section)s] %(entry)s: expected integer,'
' got %(value)s', section=self.name, entry=key,
value=repr(value)))
def getfloat(self, key, default=''):
"""Return the value of the specified option as float.
If the specified option can not be converted to a float, a
`ConfigurationError` exception is raised.
Valid default input is a string, float or int. Returns a float.
"""
value = self.get(key, default)
try:
return _getfloat(value)
except ValueError:
raise ConfigurationError(
_('[%(section)s] %(entry)s: expected float,'
' got %(value)s', section=self.name, entry=key,
value=repr(value)))
def getlist(self, key, default='', sep=',', keep_empty=True):
"""Return a list of values that have been specified as a single
comma-separated option.
A different separator can be specified using the `sep` parameter. The
`sep` parameter can specify multiple values using a list or a tuple.
If the `keep_empty` parameter is set to `True`, empty elements are
included in the list.
Valid default input is a string or a list. Returns a list.
"""
return _getlist(self.get(key, default), sep, keep_empty)
def getpath(self, key, default=''):
"""Return the value of the specified option as a path, relative to
the location of this configuration file.
Valid default input is a string. Returns a normalized path.
"""
path = self.get(key, default)
if not path:
return default
if not os.path.isabs(path):
path = os.path.join(os.path.dirname(self.config.filename), path)
return os.path.normcase(os.path.realpath(path))
def options(self, compmgr=None):
"""Return `(key, value)` tuples for every option in the section.
This includes options that have default values that haven't been
overridden. If `compmgr` is specified, only return default option
values for components that are enabled in the given `ComponentManager`.
"""
for key in self.iterate(compmgr):
yield key, self.get(key)
def set(self, key, value):
"""Change a configuration value.
These changes are not persistent unless saved with `save()`.
"""
self._cache.pop(key, None)
if not self.config.parser.has_section(self.name):
self.config.parser.add_section(self.name)
return self.config.parser.set(self.name, key, value)
def remove(self, key):
"""Delete a key from this section.
Like for `set()`, the changes won't persist until `save()` gets
called.
"""
if self.config.parser.has_section(self.name):
self._cache.pop(key, None)
self.config.parser.remove_option(self.name, key)
def _get_registry(cls, compmgr=None):
"""Return the descriptor registry.
If `compmgr` is specified, only return descriptors for components that
are enabled in the given `ComponentManager`.
"""
if compmgr is None:
return cls.registry
from trac.core import ComponentMeta
components = {}
for comp in ComponentMeta._components:
for attr in comp.__dict__.itervalues():
if isinstance(attr, cls):
components[attr] = comp
return dict(each for each in cls.registry.iteritems()
if each[1] not in components
or compmgr.is_enabled(components[each[1]]))
class ConfigSection(object):
"""Descriptor for configuration sections."""
registry = {}
@staticmethod
def get_registry(compmgr=None):
"""Return the section registry, as a `dict` mapping section names to
`ConfigSection` objects.
If `compmgr` is specified, only return sections for components that
are enabled in the given `ComponentManager`.
"""
return _get_registry(ConfigSection, compmgr)
def __init__(self, name, doc, doc_domain='tracini'):
"""Create the configuration section."""
self.name = name
self.registry[self.name] = self
self.__doc__ = cleandoc(doc)
self.doc_domain = doc_domain
def __get__(self, instance, owner):
if instance is None:
return self
config = getattr(instance, 'config', None)
if config and isinstance(config, Configuration):
return config[self.name]
def __repr__(self):
return '<%s [%s]>' % (self.__class__.__name__, self.name)
class Option(object):
"""Descriptor for configuration options."""
registry = {}
def accessor(self, section, name, default):
return section.get(name, default)
@staticmethod
def get_registry(compmgr=None):
"""Return the option registry, as a `dict` mapping `(section, key)`
tuples to `Option` objects.
If `compmgr` is specified, only return options for components that are
enabled in the given `ComponentManager`.
"""
return _get_registry(Option, compmgr)
def __init__(self, section, name, default=None, doc='',
doc_domain='tracini'):
"""Create the configuration option.
@param section: the name of the configuration section this option
belongs to
@param name: the name of the option
@param default: the default value for the option
@param doc: documentation of the option
"""
self.section = section
self.name = name
self.default = self.normalize(default)
self.registry[(self.section, self.name)] = self
self.__doc__ = cleandoc(doc)
self.doc_domain = doc_domain
def __get__(self, instance, owner):
if instance is None:
return self
config = getattr(instance, 'config', None)
if config and isinstance(config, Configuration):
section = config[self.section]
value = self.accessor(section, self.name, self.default)
return value
def __set__(self, instance, value):
raise AttributeError(_("Setting attribute is not allowed."))
def __repr__(self):
return '<%s [%s] %r>' % (self.__class__.__name__, self.section,
self.name)
def dumps(self, value):
"""Return the value as a string to write to a trac.ini file"""
if value is None:
return ''
if value is True:
return 'enabled'
if value is False:
return 'disabled'
if isinstance(value, unicode):
return value
return to_unicode(value)
def normalize(self, value):
"""Normalize the given value to write to a trac.ini file"""
return self.dumps(value)
class BoolOption(Option):
"""Descriptor for boolean configuration options."""
def accessor(self, section, name, default):
return section.getbool(name, default)
def normalize(self, value):
if value not in (True, False):
value = as_bool(value)
return self.dumps(value)
class IntOption(Option):
"""Descriptor for integer configuration options."""
def accessor(self, section, name, default):
return section.getint(name, default)
def normalize(self, value):
try:
value = _getint(value)
except ValueError:
pass
return self.dumps(value)
class FloatOption(Option):
"""Descriptor for float configuration options."""
def accessor(self, section, name, default):
return section.getfloat(name, default)
def normalize(self, value):
try:
value = _getfloat(value)
except ValueError:
pass
return self.dumps(value)
class ListOption(Option):
"""Descriptor for configuration options that contain multiple values
separated by a specific character.
"""
def __init__(self, section, name, default=None, sep=',', keep_empty=False,
doc='', doc_domain='tracini'):
self.sep = sep
self.keep_empty = keep_empty
Option.__init__(self, section, name, default, doc, doc_domain)
def accessor(self, section, name, default):
return section.getlist(name, default, self.sep, self.keep_empty)
def dumps(self, value):
if isinstance(value, (list, tuple)):
sep = self.sep
if isinstance(sep, (list, tuple)):
sep = sep[0]
return sep.join(Option.dumps(self, v) or '' for v in value)
return Option.dumps(self, value)
def normalize(self, value):
return self.dumps(_getlist(value, self.sep, self.keep_empty))
class ChoiceOption(Option):
"""Descriptor for configuration options providing a choice among a list
of items.
The default value is the first choice in the list.
"""
def __init__(self, section, name, choices, doc='', doc_domain='tracini'):
Option.__init__(self, section, name, to_unicode(choices[0]), doc,
doc_domain)
self.choices = set(to_unicode(c).strip() for c in choices)
def accessor(self, section, name, default):
value = section.get(name, default)
if value not in self.choices:
raise ConfigurationError(
_('[%(section)s] %(entry)s: expected one of '
'(%(choices)s), got %(value)s',
section=section.name, entry=name, value=repr(value),
choices=', '.join('"%s"' % c
for c in sorted(self.choices))))
return value
class PathOption(Option):
"""Descriptor for file system path configuration options.
Relative paths are resolved to absolute paths using the directory
containing the configuration file as the reference.
"""
def accessor(self, section, name, default):
return section.getpath(name, default)
class ExtensionOption(Option):
"""Name of a component implementing `interface`. Raises a
`ConfigurationError` if the component cannot be found in the list of
active components implementing the interface."""
def __init__(self, section, name, interface, default=None, doc='',
doc_domain='tracini'):
Option.__init__(self, section, name, default, doc, doc_domain)
self.xtnpt = ExtensionPoint(interface)
def __get__(self, instance, owner):
if instance is None:
return self
value = Option.__get__(self, instance, owner)
for impl in self.xtnpt.extensions(instance):
if impl.__class__.__name__ == value:
return impl
raise ConfigurationError(
tag_("Cannot find an implementation of the %(interface)s "
"interface named %(implementation)s. Please check "
"that the Component is enabled or update the option "
"%(option)s in trac.ini.",
interface=tag.code(self.xtnpt.interface.__name__),
implementation=tag.code(value),
option=tag.code("[%s] %s" % (self.section, self.name))))
class OrderedExtensionsOption(ListOption):
"""A comma separated, ordered, list of components implementing
`interface`. Can be empty.
If `include_missing` is true (the default) all components implementing the
interface are returned, with those specified by the option ordered first.
"""
def __init__(self, section, name, interface, default=None,
include_missing=True, doc='', doc_domain='tracini'):
ListOption.__init__(self, section, name, default, doc=doc,
doc_domain=doc_domain)
self.xtnpt = ExtensionPoint(interface)
self.include_missing = include_missing
def __get__(self, instance, owner):
if instance is None:
return self
order = ListOption.__get__(self, instance, owner)
components = []
implementing_classes = []
for impl in self.xtnpt.extensions(instance):
implementing_classes.append(impl.__class__.__name__)
if self.include_missing or impl.__class__.__name__ in order:
components.append(impl)
not_found = sorted(set(order) - set(implementing_classes))
if not_found:
raise ConfigurationError(
tag_("Cannot find implementation(s) of the %(interface)s "
"interface named %(implementation)s. Please check "
"that the Component is enabled or update the option "
"%(option)s in trac.ini.",
interface=tag.code(self.xtnpt.interface.__name__),
implementation=tag(
(', ' if idx != 0 else None, tag.code(impl))
for idx, impl in enumerate(not_found)),
option=tag.code("[%s] %s" % (self.section, self.name))))
def compare(x, y):
x, y = x.__class__.__name__, y.__class__.__name__
if x not in order:
return int(y in order)
if y not in order:
return -int(x in order)
return cmp(order.index(x), order.index(y))
components.sort(compare)
return components
class ConfigurationAdmin(Component):
"""trac-admin command provider for trac.ini administration."""
implements(IAdminCommandProvider)
# IAdminCommandProvider methods
def get_admin_commands(self):
yield ('config get', '<section> <option>',
'Get the value of the given option in "trac.ini"',
self._complete_config, self._do_get)
yield ('config remove', '<section> <option>',
'Remove the specified option from "trac.ini"',
self._complete_config, self._do_remove)
yield ('config set', '<section> <option> <value>',
'Set the value for the given option in "trac.ini"',
self._complete_config, self._do_set)
def _complete_config(self, args):
if len(args) == 1:
return self.config.sections()
elif len(args) == 2:
return [name for (name, value) in self.config[args[0]].options()]
def _do_get(self, section, option):
if not self.config.has_option(section, option):
raise AdminCommandError(
_("Option '%(option)s' doesn't exist in section"
" '%(section)s'", option=option, section=section))
printout(self.config.get(section, option))
def _do_set(self, section, option, value):
self.config.set(section, option, value)
if section == 'components' and as_bool(value):
self.config.set_defaults(component=option)
self.config.save()
if section == 'inherit' and option == 'file':
self.config.parse_if_needed(force=True) # Full reload
def _do_remove(self, section, option):
if not self.config.has_option(section, option):
raise AdminCommandError(
_("Option '%(option)s' doesn't exist in section"
" '%(section)s'", option=option, section=section))
self.config.remove(section, option)
self.config.save()
if section == 'inherit' and option == 'file':
self.config.parse_if_needed(force=True) # Full reload
def get_configinfo(env):
"""Returns a list of dictionaries containing the `name` and `options`
of each configuration section. The value of `options` is a list of
dictionaries containing the `name`, `value` and `modified` state of
each configuration option. The `modified` value is True if the value
differs from its default.
:since: version 1.1.2
"""
all_options = {}
for (section, name), option in \
Option.get_registry(env.compmgr).iteritems():
all_options.setdefault(section, {})[name] = option
sections = []
for section in env.config.sections(env.compmgr):
options = []
for name, value in env.config.options(section, env.compmgr):
registered = all_options.get(section, {}).get(name)
if registered:
default = registered.default
normalized = registered.normalize(value)
else:
default = u''
normalized = unicode(value)
options.append({'name': name, 'value': value,
'modified': normalized != default})
options.sort(key=lambda o: o['name'])
sections.append({'name': section, 'options': options})
sections.sort(key=lambda s: s['name'])
return sections