Added basic config, still not implemented inside the application
This commit is contained in:
parent
b0e02c831a
commit
2e6443a4aa
@ -6,4 +6,5 @@ python-vlc
|
||||
google-api-python-client
|
||||
youtube-dl
|
||||
pyinstaller
|
||||
isodate
|
||||
isodate
|
||||
configobj
|
2
src/app-configuration.defaults
Normal file
2
src/app-configuration.defaults
Normal file
@ -0,0 +1,2 @@
|
||||
[main]
|
||||
volume = integer(default=50)
|
18
src/config.py
Normal file
18
src/config.py
Normal file
@ -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))
|
||||
|
73
src/config_utils.py
Normal file
73
src/config_utils.py
Normal file
@ -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
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user