diff options
author | nico <nico@magicbroccoli.de> | 2019-07-17 17:56:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-17 17:56:15 +0200 |
commit | 47e577ebaf07e4785a7a7cabfa402c36087fa1eb (patch) | |
tree | 6eea0eefd3b31d56c5530355f925251528131341 /config.py | |
parent | 4e3d7d431d22cdb627943ea872a3c06650b052a3 (diff) |
Code cleanup (#2)
* config class revamp
+ add get_at method
+ add set_at method
+ add unset_at method
new config class to interactively get/set/unset config parameters from within other methods
* move report function to new file
+ move all report functions to new report class
the new report class works exactly like the internal one, but it is easier to maintain
* + implement the new config functions
+ implement the changed report functions
+ update docstrings
+ update code comments
* code cleanup
* further code cleanup
+ add variable type to all methods
* many indentation fixes
Diffstat (limited to 'config.py')
-rw-r--r-- | config.py | 71 |
1 files changed, 56 insertions, 15 deletions
@@ -2,22 +2,63 @@ import json import os -# filepath of the config.json in the project directory -path = os.path.dirname(__file__) -filepath = ("/".join([path, "config.json"])) -# try to read config.json if nonexistent create config.json an populate it -try: - with open(filepath, "r", encoding="utf-8") as f: - config = json.load(f) +class Config(object): + def __init__(self): + self.config = dict() + self.valid_config = bool -except FileNotFoundError: - with open(filepath, "w", encoding="utf-8") as f: - config = { - "name": "", - } - f.write(json.dumps(config)) + # filepath of the config.json in the project directory + self.path = os.path.dirname(__file__) + self.filepath = ('/'.join([self.path, 'config.json'])) + # load config + self.load() -class Config(object): - name = config["name"] + def load(self): + try: + # try to read config.json + with open(self.filepath, "r", encoding="utf-8") as f: + self.config = json.load(f) + + except FileNotFoundError: + # if file is absent create file + open(self.filepath, "w").close() + + except json.decoder.JSONDecodeError: + # config file is present but empty + pass + + def get_at(self, attrib: str): + """ + retrieve attribute from config file + :param attrib: keyword corresponding to keyword in config dictionary + :return: value of specified keyword or False if keyword is not present in dictionary + """ + if attrib in self.config: + # return corresponding attrib from config + return self.config[attrib] + else: + # if attrib is not present in config return False + self.config[attrib] = False + + def set_at(self, attrib: str, param): + """ + set attribute to parameter inside config file + :param attrib: keyword which should be updated/created in config dictionary + :param param: parameter the keyword should be updated to + """ + self.config[attrib] = param + + # save new attrib to file + with open(self.filepath, "w", encoding="utf-8") as f: + f.write(json.dumps(self.config, indent=4)) + + def unset_at(self, attrib: str): + """ + unset attribute inside config file + :param attrib: attribute which should be unset inside config file + """ + if attrib in self.config: + # only if attrib is actually present unset it + self.config.pop(attrib) |