From 2e6443a4aa117f95eb758617386d8098456150fa Mon Sep 17 00:00:00 2001 From: Manuel Cortez Date: Tue, 9 Oct 2018 15:38:33 -0500 Subject: [PATCH] Added basic config, still not implemented inside the application --- requirements.txt | 3 +- src/app-configuration.defaults | 2 + src/config.py | 18 +++++++++ src/config_utils.py | 73 ++++++++++++++++++++++++++++++++++ src/main.py | 3 ++ 5 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/app-configuration.defaults create mode 100644 src/config.py create mode 100644 src/config_utils.py diff --git a/requirements.txt b/requirements.txt index 306954a..4cea7c0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,4 +6,5 @@ python-vlc google-api-python-client youtube-dl pyinstaller -isodate \ No newline at end of file +isodate +configobj \ No newline at end of file diff --git a/src/app-configuration.defaults b/src/app-configuration.defaults new file mode 100644 index 0000000..2dcbca4 --- /dev/null +++ b/src/app-configuration.defaults @@ -0,0 +1,2 @@ +[main] +volume = integer(default=50) diff --git a/src/config.py b/src/config.py new file mode 100644 index 0000000..9bee8f2 --- /dev/null +++ b/src/config.py @@ -0,0 +1,18 @@ +# -*- coding: cp1252 -*- +import os +import config_utils +import paths +import storage +import logging + +log = logging.getLogger("config") + +MAINFILE = "settings.conf" +MAINSPEC = "app-configuration.defaults" + +app = None +def setup (): + global app + log.debug("Loading global app settings...") + app = config_utils.load_config(os.path.join(storage.data_directory, MAINFILE), os.path.join(paths.app_path(), MAINSPEC)) + \ No newline at end of file diff --git a/src/config_utils.py b/src/config_utils.py new file mode 100644 index 0000000..45a574a --- /dev/null +++ b/src/config_utils.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +from configobj import ConfigObj, ParseError +from validate import Validator, ValidateError +import os +import string +class ConfigLoadError(Exception): pass + +def load_config(config_path, configspec_path=None, *args, **kwargs): + if os.path.exists(config_path): + clean_config(config_path) + spec = ConfigObj(configspec_path, encoding='UTF8', list_values=False, _inspec=True) + try: + config = ConfigObj(infile=config_path, configspec=spec, create_empty=True, encoding='UTF8', *args, **kwargs) + except ParseError: + raise ConfigLoadError("Unable to load %r" % config_path) + validator = Validator() + validated = config.validate(validator, copy=True) + if validated == True: + config.write() + return config + +def is_blank(arg): + "Check if a line is blank." + for c in arg: + if c not in string.whitespace: + return False + return True +def get_keys(path): + "Gets the keys of a configobj config file." + res=[] + fin=open(path) + for line in fin: + if not is_blank(line): + res.append(line[0:line.find('=')].strip()) + fin.close() + return res + +def hist(keys): + "Generates a histogram of an iterable." + res={} + for k in keys: + res[k]=res.setdefault(k,0)+1 + return res + +def find_problems(hist): + "Takes a histogram and returns a list of items occurring more than once." + res=[] + for k,v in hist.items(): + if v>1: + res.append(k) + return res + +def clean_config(path): + "Cleans a config file. If duplicate values are found, delete all of them and just use the default." + orig=[] + cleaned=[] + fin=open(path) + for line in fin: + orig.append(line) + fin.close() + for p in find_problems(hist(get_keys(path))): + for o in orig: + o.strip() + if p not in o: + cleaned.append(o) + if len(cleaned) != 0: + cam=open(path,'w') + for c in cleaned: + cam.write(c) + cam.close() + return True + else: + return False \ No newline at end of file diff --git a/src/main.py b/src/main.py index b6299ee..04b77f3 100644 --- a/src/main.py +++ b/src/main.py @@ -10,6 +10,8 @@ import storage import traceback import sys storage.setup() +# Let's import config module here as it is dependent on storage being setup. +import config logging.basicConfig(filename=os.path.join(storage.data_directory, "info.log"), level=logging.DEBUG, filemode="w") # Let's mute the google discovery_cache logger as we won't use it and we'll avoid some tracebacks. glog = logging.getLogger("googleapiclient.discovery_cache") @@ -26,6 +28,7 @@ if sys.version[0] == "2": fixes.setup() import i18n i18n.setup() +config.setup() import application import widgetUtils import paths