try: import ConfigParser except ImportError: # py 3 import configparser as ConfigParser import re from string import Template from syncdutils import escape, unescape, norm, update_file SECT_ORD = '__section_order__' SECT_META = '__meta__' config_version = 2.0 re_type = type(re.compile('')) class MultiDict(object): def __init__(self, *dd): self.dicts = dd def __getitem__(self, key): val = None for d in self.dicts: if d.get(key): val = d[key] if not val: raise KeyError(key) return val class GConffile(object): def _normconfig(self): for n, s in self.config._sections.items(): if n.find('__') == 0: continue s2 = type(s)() for k, v in s.items(): if k.find('__') != 0: k = norm(k) s2[k] = v self.config._sections[n] = s2 def __init__(self, path, peers, *dd): self.peers = peers self.path = path self.auxdicts = dd self.config = ConfigParser.RawConfigParser() self.config.read(path) self._normconfig() def section(self, rx=False): peers = self.peers if not peers: peers = ['.', '.'] rx = True if rx: st = 'peersrx' else: st = 'peers' return ' '.join([st] + [escape(u) for u in peers]) @staticmethod def parse_section(section): sl = section.split() st = sl.pop(0) sl = [unescape(u) for u in sl] if st == 'peersrx': sl = [re.compile(u) for u in sl] return sl def ord_sections(self): """Return an ordered list of sections. Ordering happens based on the auxiliary SECT_ORD section storing indices for each section added through the config API. To not to go corrupt in case of manually written config files, we take care to append also those sections which are not registered in SECT_ORD. Needed for python 2.{4,5} where ConfigParser cannot yet order sections/options internally. """ so = {} if self.config.has_section(SECT_ORD): so = self.config._sections[SECT_ORD] so2 = {} for k, v in so.items(): if k != '__name__': so2[k] = int(v) tv = 0 if so2: tv = max(so2.values()) + 1 ss = [s for s in self.config.sections() if s.find('__') != 0] for s in ss: if s in so.keys(): continue so2[s] = tv tv += 1 def scmp(x, y): return cmp(*(so2[s] for s in (x, y))) ss.sort(scmp) return ss def update_to(self, dct, allow_unresolved=False): if not self.peers: raise RuntimeError('no peers given, cannot select matching options') def update_from_sect(sect, mud): for k, v in self.config._sections[sect].items(): if k == '__name__': continue if allow_unresolved: dct[k] = Template(v).safe_substitute(mud) else: dct[k] = Template(v).substitute(mud) for sect in self.ord_sections(): sp = self.parse_section(sect) if isinstance(sp[0], re_type) and len(sp) == len(self.peers): match = True mad = {} for i in range(len(sp)): m = sp[i].search(self.peers[i]) if not m: match = False break for j in range(len(m.groups())): mad['match%d_%d' % (i+1, j+1)] = m.groups()[j] if match: update_from_sect(sect, MultiDict(dct, mad, *self.auxdicts)) if self.config.has_section(self.section()): update_from_sect(self.section(), MultiDict(dct, mad, *self.auxdicts)) def get(self, opt=None): d = {} self.update_to(d, allow_unresolved = True) if opt: opt = norm(opt) v = d.get(opt) if v: print v else: for k, v in d.iteritems(): if k == '__name__': continue print("%s: %s" % (k, v)) def write(self, trfn, opt, *a, **kw): def mergeconf(f): self.config = ConfigParser.RawConfigParser() self.config.readfp(f) self._normconfig() if not self.config.has_section(SECT_META): self.config.add_section(SECT_META) self.config.set(SECT_META, 'version', config_version) return trfn(norm(opt), *a, **kw) def updateconf(f): self.config.write(f) update_file(self.path, updateconf, mergeconf) def _set(self, opt, val, rx=False): sect = self.section(rx) if not self.config.has_section(sect): self.config.add_section(sect) # regarding SECT_ORD, cf. ord_sections if not self.config.has_section(SECT_ORD): self.config.add_section(SECT_ORD) self.config.set(SECT_ORD, sect, len(self.config._sections[SECT_ORD])) self.config.set(sect, opt, val) return True def set(self, opt, *a, **kw): self.write(self._set, opt, *a, **kw) def _delete(self, opt, rx=False): sect = self.section(rx) if self.config.has_section(sect): return self.config.remove_option(sect, opt) def delete(self, opt, *a, **kw): self.write(self._delete, opt, *a, **kw)