| 1 |
import collections |
| 2 |
import os |
| 3 |
import os.path |
| 4 |
import pickle |
| 5 |
import re |
| 6 |
import sys |
| 7 |
|
| 8 |
from configparser import RawConfigParser |
| 9 |
|
| 10 |
__all__ = [ |
| 11 |
'ConfigCoreDump', |
| 12 |
'ConfigCoreHierarchy', |
| 13 |
'ConfigParser', |
| 14 |
] |
| 15 |
|
| 16 |
|
| 17 |
class SchemaItemBoolean(object): |
| 18 |
def __call__(self, i): |
| 19 |
i = i.strip().lower() |
| 20 |
if i in ("true", "1"): |
| 21 |
return True |
| 22 |
if i in ("false", "0"): |
| 23 |
return False |
| 24 |
raise ValueError |
| 25 |
|
| 26 |
|
| 27 |
class SchemaItemInteger(object): |
| 28 |
def __call__(self, i): |
| 29 |
return int(i.strip(), 0) |
| 30 |
|
| 31 |
|
| 32 |
class SchemaItemList(object): |
| 33 |
def __init__(self, type=r"\s+"): |
| 34 |
self.type = type |
| 35 |
|
| 36 |
def __call__(self, i): |
| 37 |
i = i.strip() |
| 38 |
if not i: |
| 39 |
return [] |
| 40 |
return [j.strip() for j in re.split(self.type, i)] |
| 41 |
|
| 42 |
|
| 43 |
# Using OrderedDict instead of dict makes the pickled config reproducible |
| 44 |
class ConfigCore(collections.OrderedDict): |
| 45 |
def get_merge(self, section, arch, featureset, flavour, key, default=None): |
| 46 |
temp = [] |
| 47 |
|
| 48 |
if arch and featureset and flavour: |
| 49 |
temp.append(self.get((section, arch, featureset, flavour), {}) |
| 50 |
.get(key)) |
| 51 |
temp.append(self.get((section, arch, None, flavour), {}).get(key)) |
| 52 |
if arch and featureset: |
| 53 |
temp.append(self.get((section, arch, featureset), {}).get(key)) |
| 54 |
if arch: |
| 55 |
temp.append(self.get((section, arch), {}).get(key)) |
| 56 |
if featureset: |
| 57 |
temp.append(self.get((section, None, featureset), {}).get(key)) |
| 58 |
temp.append(self.get((section,), {}).get(key)) |
| 59 |
|
| 60 |
ret = [] |
| 61 |
|
| 62 |
for i in temp: |
| 63 |
if i is None: |
| 64 |
continue |
| 65 |
elif isinstance(i, (list, tuple)): |
| 66 |
ret.extend(i) |
| 67 |
elif ret: |
| 68 |
# TODO |
| 69 |
return ret |
| 70 |
else: |
| 71 |
return i |
| 72 |
|
| 73 |
return ret or default |
| 74 |
|
| 75 |
def merge(self, section, arch=None, featureset=None, flavour=None): |
| 76 |
ret = {} |
| 77 |
ret.update(self.get((section,), {})) |
| 78 |
if featureset: |
| 79 |
ret.update(self.get((section, None, featureset), {})) |
| 80 |
if arch: |
| 81 |
ret.update(self.get((section, arch), {})) |
| 82 |
if arch and featureset: |
| 83 |
ret.update(self.get((section, arch, featureset), {})) |
| 84 |
if arch and featureset and flavour: |
| 85 |
ret.update(self.get((section, arch, None, flavour), {})) |
| 86 |
ret.update(self.get((section, arch, featureset, flavour), {})) |
| 87 |
return ret |
| 88 |
|
| 89 |
def dump(self, fp): |
| 90 |
pickle.dump(self, fp, 0) |
| 91 |
|
| 92 |
|
| 93 |
class ConfigCoreDump(object): |
| 94 |
def __new__(self, fp): |
| 95 |
return pickle.load(fp) |
| 96 |
|
| 97 |
|
| 98 |
class ConfigCoreHierarchy(object): |
| 99 |
schema_base = { |
| 100 |
'base': { |
| 101 |
'arches': SchemaItemList(), |
| 102 |
'enabled': SchemaItemBoolean(), |
| 103 |
'featuresets': SchemaItemList(), |
| 104 |
'flavours': SchemaItemList(), |
| 105 |
}, |
| 106 |
} |
| 107 |
|
| 108 |
def __new__(cls, schema, dirs=[]): |
| 109 |
schema_complete = cls.schema_base.copy() |
| 110 |
for key, value in schema.items(): |
| 111 |
schema_complete.setdefault(key, {}).update(value) |
| 112 |
return cls.Reader(dirs, schema_complete)() |
| 113 |
|
| 114 |
class Reader(object): |
| 115 |
config_name = "defines" |
| 116 |
|
| 117 |
def __init__(self, dirs, schema): |
| 118 |
self.dirs, self.schema = dirs, schema |
| 119 |
|
| 120 |
def __call__(self): |
| 121 |
ret = ConfigCore() |
| 122 |
self.read(ret) |
| 123 |
return ret |
| 124 |
|
| 125 |
def get_files(self, *dirs): |
| 126 |
dirs = list(dirs) |
| 127 |
dirs.append(self.config_name) |
| 128 |
return (os.path.join(i, *dirs) for i in self.dirs if i) |
| 129 |
|
| 130 |
def read_arch(self, ret, arch): |
| 131 |
config = ConfigParser(self.schema) |
| 132 |
config.read(self.get_files(arch)) |
| 133 |
|
| 134 |
featuresets = config['base', ].get('featuresets', []) |
| 135 |
flavours = config['base', ].get('flavours', []) |
| 136 |
|
| 137 |
for section in iter(config): |
| 138 |
if section[0] in featuresets: |
| 139 |
real = (section[-1], arch, section[0]) |
| 140 |
elif len(section) > 1: |
| 141 |
real = (section[-1], arch, None) + section[:-1] |
| 142 |
else: |
| 143 |
real = (section[-1], arch) + section[:-1] |
| 144 |
s = ret.get(real, {}) |
| 145 |
s.update(config[section]) |
| 146 |
ret[tuple(real)] = s |
| 147 |
|
| 148 |
for featureset in featuresets: |
| 149 |
self.read_arch_featureset(ret, arch, featureset) |
| 150 |
|
| 151 |
if flavours: |
| 152 |
base = ret['base', arch] |
| 153 |
featuresets.insert(0, 'none') |
| 154 |
base['featuresets'] = featuresets |
| 155 |
del base['flavours'] |
| 156 |
ret['base', arch] = base |
| 157 |
ret['base', arch, 'none'] = {'flavours': flavours, |
| 158 |
'implicit-flavour': True} |
| 159 |
|
| 160 |
def read_arch_featureset(self, ret, arch, featureset): |
| 161 |
config = ConfigParser(self.schema) |
| 162 |
config.read(self.get_files(arch, featureset)) |
| 163 |
|
| 164 |
for section in iter(config): |
| 165 |
real = (section[-1], arch, featureset) + section[:-1] |
| 166 |
s = ret.get(real, {}) |
| 167 |
s.update(config[section]) |
| 168 |
ret[tuple(real)] = s |
| 169 |
|
| 170 |
def read(self, ret): |
| 171 |
config = ConfigParser(self.schema) |
| 172 |
config.read(self.get_files()) |
| 173 |
|
| 174 |
arches = config['base', ]['arches'] |
| 175 |
featuresets = config['base', ].get('featuresets', []) |
| 176 |
|
| 177 |
for section in iter(config): |
| 178 |
if section[0].startswith('featureset-'): |
| 179 |
real = (section[-1], None, section[0][11:]) |
| 180 |
else: |
| 181 |
real = (section[-1],) + section[1:] |
| 182 |
ret[real] = config[section] |
| 183 |
|
| 184 |
for arch in arches: |
| 185 |
self.read_arch(ret, arch) |
| 186 |
for featureset in featuresets: |
| 187 |
self.read_featureset(ret, featureset) |
| 188 |
|
| 189 |
def read_featureset(self, ret, featureset): |
| 190 |
config = ConfigParser(self.schema) |
| 191 |
config.read(self.get_files('featureset-%s' % featureset)) |
| 192 |
|
| 193 |
for section in iter(config): |
| 194 |
real = (section[-1], None, featureset) |
| 195 |
s = ret.get(real, {}) |
| 196 |
s.update(config[section]) |
| 197 |
ret[real] = s |
| 198 |
|
| 199 |
|
| 200 |
class ConfigParser(object): |
| 201 |
__slots__ = '_config', 'schemas' |
| 202 |
|
| 203 |
def __init__(self, schemas): |
| 204 |
self.schemas = schemas |
| 205 |
|
| 206 |
self._config = RawConfigParser() |
| 207 |
|
| 208 |
def __getitem__(self, key): |
| 209 |
return self._convert()[key] |
| 210 |
|
| 211 |
def __iter__(self): |
| 212 |
return iter(self._convert()) |
| 213 |
|
| 214 |
def __str__(self): |
| 215 |
return '<%s(%s)>' % (self.__class__.__name__, self._convert()) |
| 216 |
|
| 217 |
def _convert(self): |
| 218 |
ret = {} |
| 219 |
for section in self._config.sections(): |
| 220 |
data = {} |
| 221 |
for key, value in self._config.items(section): |
| 222 |
data[key] = value |
| 223 |
section_list = section.split('_') |
| 224 |
section_base = section_list[-1] |
| 225 |
if section_base in self.schemas: |
| 226 |
section_ret = tuple(section_list) |
| 227 |
data = self._convert_one(self.schemas[section_base], data) |
| 228 |
else: |
| 229 |
section_ret = (section, ) |
| 230 |
ret[section_ret] = data |
| 231 |
return ret |
| 232 |
|
| 233 |
def _convert_one(self, schema, data): |
| 234 |
ret = {} |
| 235 |
for key, value in data.items(): |
| 236 |
value = value.replace('\n', ' ') |
| 237 |
if key in schema: |
| 238 |
value = schema[key](value) |
| 239 |
ret[key] = value |
| 240 |
return ret |
| 241 |
|
| 242 |
def keys(self): |
| 243 |
return self._convert().keys() |
| 244 |
|
| 245 |
def read(self, data): |
| 246 |
return self._config.read(data) |
| 247 |
|
| 248 |
|
| 249 |
if __name__ == '__main__': |
| 250 |
sys.path.append('debian/lib/python') |
| 251 |
config = ConfigCoreDump(open('debian/config.defines.dump', 'rb')) |
| 252 |
for section, items in sorted(config.items(), |
| 253 |
key=(lambda a: tuple(i or '' for i in a[0]))): |
| 254 |
print(u"[%s]" % (section,)) |
| 255 |
for item, value in sorted(items.items()): |
| 256 |
print(u"%s: %s" % (item, value)) |
| 257 |
print() |