def _Load(self, expression): self._Flush() parser = config_file.KeyValueParser(kv_sep=":", sep=",", term=(r"\s+", r"\n")) parsed = {} for entry in parser.ParseEntries(expression): parsed.update(entry) self.cfg = rdf_protodict.AttributedDict(parsed) return parsed
def _ProcessEntries(self, fd): """Extract entries from the xinetd config files.""" p = config_file.KeyValueParser(kv_sep="{", term="}", sep=None) data = fd.read() entries = p.ParseEntries(data) for entry in entries: for section, cfg in entry.items(): # The parser returns a list of configs. There will only be one. if cfg: cfg = cfg[0].strip() else: cfg = "" self._ParseSection(section, cfg)
def _ParseSection(self, section, cfg): p = config_file.KeyValueParser() # Skip includedir, we get this from the artifact. if section.startswith("includedir"): return elif section.startswith("default"): for val in p.ParseEntries(cfg): self.default.update(val) elif section.startswith("service"): svc = section.replace("service", "").strip() if not svc: return self.entries[svc] = {} for val in p.ParseEntries(cfg): self.entries[svc].update(val)
def testParser(self): test_data = r""" key1 = a list of \ fields # but not \n this, or \ this. # Nothing here. key 2:another entry = # Bad line 'a key'with" no" value field ;; and not this comment. """ expected = [{ "key1": ["a", "list", "of", "fields"] }, { "key 2": ["another", "entry"] }, { "a keywith no value field": [] }] cfg = config_file.KeyValueParser(kv_sep=["=", ":"], comments=["#", ";;"]) results = cfg.ParseEntries(test_data) for i, expect in enumerate(expected): self.assertDictEqual(expect, results[i])
def __init__(self, *args, **kwargs): super(SysctlCmdParser, self).__init__(*args, **kwargs) self.lexer = config_file.KeyValueParser()