From 505da62f2ad7f6839c07f22c5cc413d6ecd49600 Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 8 Nov 2019 14:55:49 +0100 Subject: slimmed down methods (#4) * slimmed down config class + add start stop arguments to config class init * update requirements.txt * correctly apply custom timestamp to report functions too * init report methods earlier --- config.py | 42 +++++------------------------------------- 1 file changed, 5 insertions(+), 37 deletions(-) (limited to 'config.py') diff --git a/config.py b/config.py index 10e938f..117c016 100644 --- a/config.py +++ b/config.py @@ -1,20 +1,17 @@ # -*- coding: utf-8 -*- import json import os +import sys class Config(object): def __init__(self): self.config = dict() - self.valid_config = bool # 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() - def load(self): try: # try to read config.json @@ -24,41 +21,12 @@ class Config(object): except FileNotFoundError: # if file is absent create file open(self.filepath, "w").close() + print("-- config.json is missing.", file=sys.stderr) + print("-- {file} has been created.".format(file=self.filepath), file=sys.stderr) except json.decoder.JSONDecodeError: # config file is present but empty + print("-- JSON parsing error, please check your config.json file.", file=sys.stderr) 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) + return self.config -- cgit v1.2.3-18-g5258