Show More
@@ -8,6 +8,7 b'' | |||
|
8 | 8 | from __future__ import absolute_import |
|
9 | 9 | |
|
10 | 10 | import functools |
|
11 | import re | |
|
11 | 12 | |
|
12 | 13 | from . import ( |
|
13 | 14 | encoding, |
@@ -33,20 +34,56 b' class configitem(object):' | |||
|
33 | 34 | :section: the official config section where to find this item, |
|
34 | 35 | :name: the official name within the section, |
|
35 | 36 | :default: default value for this item, |
|
36 |
:alias: optional list of tuples as alternatives |
|
|
37 | :alias: optional list of tuples as alternatives, | |
|
38 | :generic: this is a generic definition, match name using regular expression. | |
|
37 | 39 | """ |
|
38 | 40 | |
|
39 |
def __init__(self, section, name, default=None, alias=() |
|
|
41 | def __init__(self, section, name, default=None, alias=(), | |
|
42 | generic=False, priority=0): | |
|
40 | 43 | self.section = section |
|
41 | 44 | self.name = name |
|
42 | 45 | self.default = default |
|
43 | 46 | self.alias = list(alias) |
|
47 | self.generic = generic | |
|
48 | self.priority = priority | |
|
49 | self._re = None | |
|
50 | if generic: | |
|
51 | self._re = re.compile(self.name) | |
|
52 | ||
|
53 | class itemregister(dict): | |
|
54 | """A specialized dictionary that can handle wild-card selection""" | |
|
55 | ||
|
56 | def __init__(self): | |
|
57 | super(itemregister, self).__init__() | |
|
58 | self._generics = set() | |
|
59 | ||
|
60 | def update(self, other): | |
|
61 | super(itemregister, self).update(other) | |
|
62 | self._generics.update(other._generics) | |
|
63 | ||
|
64 | def __setitem__(self, key, item): | |
|
65 | super(itemregister, self).__setitem__(key, item) | |
|
66 | if item.generic: | |
|
67 | self._generics.add(item) | |
|
68 | ||
|
69 | def get(self, key): | |
|
70 | if key in self: | |
|
71 | return self[key] | |
|
72 | ||
|
73 | # search for a matching generic item | |
|
74 | generics = sorted(self._generics, key=(lambda x: (x.priority, x.name))) | |
|
75 | for item in generics: | |
|
76 | if item._re.match(key): | |
|
77 | return item | |
|
78 | ||
|
79 | # fallback to dict get | |
|
80 | return super(itemregister, self).get(key) | |
|
44 | 81 | |
|
45 | 82 | coreitems = {} |
|
46 | 83 | |
|
47 | 84 | def _register(configtable, *args, **kwargs): |
|
48 | 85 | item = configitem(*args, **kwargs) |
|
49 |
section = configtable.setdefault(item.section, |
|
|
86 | section = configtable.setdefault(item.section, itemregister()) | |
|
50 | 87 | if item.name in section: |
|
51 | 88 | msg = "duplicated config item registration for '%s.%s'" |
|
52 | 89 | raise error.ProgrammingError(msg % (item.section, item.name)) |
General Comments 0
You need to be logged in to leave comments.
Login now