Add configs for records.config
Project: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/repo Commit: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/commit/f16c79a8 Tree: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/tree/f16c79a8 Diff: http://git-wip-us.apache.org/repos/asf/trafficserver-qa/diff/f16c79a8 Branch: refs/heads/master Commit: f16c79a8d3f08b8b59581ace841fe3e1fa0e1eaf Parents: 1cec7ac Author: Thomas Jackson <[email protected]> Authored: Tue Dec 23 10:50:13 2014 -0800 Committer: Thomas Jackson <[email protected]> Committed: Tue Dec 23 10:50:13 2014 -0800 ---------------------------------------------------------------------- tsqa/configs.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/trafficserver-qa/blob/f16c79a8/tsqa/configs.py ---------------------------------------------------------------------- diff --git a/tsqa/configs.py b/tsqa/configs.py new file mode 100644 index 0000000..51f563f --- /dev/null +++ b/tsqa/configs.py @@ -0,0 +1,62 @@ +class Config(object): + ''' + Class to represent a config file + ''' + + def __init__(self, filename): + self.filename = filename + self.load() + + def load(self): + with open(self.filename, 'r') as fh: + self.contents = fh.read() + + def write(self): + ''' + Write contents to disk + ''' + with open(self.filename, 'w') as fh: + fh.write(self.contents) + + + +class RecordsConfig(Config, dict): + ''' + Create a "dict" representation of records.config + ''' + kind_map = {'STRING': str, + 'INT': int, + 'FLOAT': float, + } + + reverse_kind_map = {str: 'STRING', + int: 'INT', + float: 'FLOAT', + } + + line_template = 'CONFIG {name} {kind} {val}\n' + + def __init__(self, filename): + dict.__init__(self) + self.filename = filename + + self.load() + + def load(self): + self._config = {} + with open(self.filename, 'r') as fh: + for line in fh: + line = line.strip() + # skip comments + if line.startswith('#'): + continue + _, name, kind, val = line.split(' ', 3) + self[name] = self.kind_map[kind](val) + + def write(self, dest): + with open(dest, 'w') as fh: + for name, val in self.iteritems(): + fh.write(self.line_template.format(name=name, + kind=self.reverse_kind_map[type(val)], + val=val)) +
