From 56136ad5d2c56d79f47b0f3c17565fa858ebe49b Mon Sep 17 00:00:00 2001 From: Philip Trauner Date: Sat, 27 Feb 2016 12:40:59 +0100 Subject: [PATCH] Added new config framework --- Shared/Config.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Shared/Config.py diff --git a/Shared/Config.py b/Shared/Config.py new file mode 100644 index 0000000..bcc139c --- /dev/null +++ b/Shared/Config.py @@ -0,0 +1,67 @@ +from imp import load_source +from os.path import isfile + + +class ValidationFailedError(Exception): + def __init__(self, name): + super(InputError, self).__init__("validation failed for '%s'" % name) + + +class OptionNotFoundError(IndexError): + def __init__(self, name): + super(InputError, self).__init__("'%s' does not exist" % name) + + +def make_value(value): + if type(value) is str: + value = '"%s"' % value + elif type(value) in (list, tuple, tuple): + value = str(value) + return value + + +class Option: + def __init__(self, name, default_value, validator=None, comment=""): + self.name = name + self.default_value = default_value + self.validator = validator + self.comment = comment + + +class Config: + def __init__(self, options=[]): + self.options = options + + + def read_from_file(self, file): + if isfile(file): + config = load_source("config", file) + return config + else: + raise FileNotFoundError() + + + def add(self, option): + self.options.append(option) + + + def remove(self, option): + if option in self.options: + del self.options[self.options.index(option)] + else: + raise OptionNotFoundError(option.name) + + + def write_to_file(self, file): + open(file, "w").write(self.get()) + + + def get(self): + out = "" + for option in self.options: + out += "%s = %s %s\n" % (option.name, make_value(option.default_value), ("# %s" % option.comment) if option.comment else "") + return out + + + def __repr__(self): + return self.get() \ No newline at end of file