2019-06-25 00:52:17 -05:00
|
|
|
from os import path, chmod, makedirs, pardir
|
|
|
|
from pathlib import Path
|
2019-06-22 02:21:23 -05:00
|
|
|
from shutil import copy
|
2019-06-16 23:54:10 -05:00
|
|
|
from yaml import safe_load, dump
|
2019-06-22 02:21:23 -05:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
2019-06-16 23:54:10 -05:00
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
|
|
|
|
class Configuration(object):
|
|
|
|
# TODO: Figure out how to handle windows config
|
|
|
|
CONFIG_DIR = path.expanduser('~/.config/grocy')
|
2019-06-22 02:21:23 -05:00
|
|
|
USER_TEMPLATE_DIR = path.expanduser('~/.config/grocy/templates')
|
2019-06-16 23:54:10 -05:00
|
|
|
CONFIG_FILE = CONFIG_DIR + '/config.yml'
|
2019-06-25 00:52:17 -05:00
|
|
|
TEMPLATE_EXT = 'yml'
|
|
|
|
PROJ_DIR = Path(__file__).resolve().parent.parent
|
2019-06-22 02:21:23 -05:00
|
|
|
PROJ_TEMPLATE_DIR = '{}/templates'.format(PROJ_DIR)
|
2019-06-16 23:54:10 -05:00
|
|
|
API_KEY_HEADER = 'GROCY-API-KEY'
|
|
|
|
DEFAULT_CFG = {
|
|
|
|
'logger': {
|
|
|
|
'level': 'DEBUG',
|
|
|
|
'file_location': None
|
|
|
|
},
|
2019-07-07 13:58:01 -05:00
|
|
|
'domain': 'https://demo-en.grocy.info',
|
2019-06-16 23:54:10 -05:00
|
|
|
'token': None,
|
|
|
|
'formats': {
|
|
|
|
'col': 'center',
|
|
|
|
'table': 'simple'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def update(self, props):
|
|
|
|
# Set nested fields (e.g. logger, formats)
|
|
|
|
if props['logger']:
|
|
|
|
for prop in props['logger']:
|
|
|
|
prop_attr = 'logger_{prop}'.format(prop=prop)
|
|
|
|
prop_value = props['logger'][prop]
|
|
|
|
setattr(self, prop_attr, prop_value)
|
|
|
|
if props['formats']:
|
|
|
|
for prop in props['formats']:
|
|
|
|
prop_attr = '{prop}_format'.format(prop=prop)
|
|
|
|
prop_value = props['formats'][prop]
|
|
|
|
setattr(self, prop_attr, prop_value)
|
|
|
|
|
|
|
|
for prop, value in props.items():
|
|
|
|
if not prop == 'logger' and prop in props:
|
|
|
|
setattr(self, prop, value)
|
|
|
|
|
|
|
|
def create(self, user_cfg_options={}):
|
|
|
|
cfg_json = deepcopy(self.DEFAULT_CFG)
|
|
|
|
|
|
|
|
if user_cfg_options['logger_level']:
|
|
|
|
self.logger_level = user_cfg_options['logger_level']
|
|
|
|
cfg_json['logger']['level'] = self.logger_level
|
|
|
|
|
|
|
|
if user_cfg_options['logger_file_location']:
|
|
|
|
self.logger_file_location = user_cfg_options['logger_file_location']
|
|
|
|
cfg_json['logger']['file_location'] = self.logger_file_location
|
|
|
|
|
2019-07-07 13:58:01 -05:00
|
|
|
if user_cfg_options['domain']:
|
|
|
|
self.domain = user_cfg_options['domain']
|
|
|
|
cfg_json['domain'] = self.domain
|
2019-06-16 23:54:10 -05:00
|
|
|
|
|
|
|
if user_cfg_options['token']:
|
|
|
|
self.token = user_cfg_options['token']
|
|
|
|
cfg_json['token'] = self.token
|
|
|
|
|
|
|
|
if user_cfg_options['col_format']:
|
|
|
|
self.col_format = user_cfg_options['col_format']
|
|
|
|
cfg_json['formats']['col'] = self.col_format
|
|
|
|
|
|
|
|
if user_cfg_options['table_format']:
|
|
|
|
self.table_format = user_cfg_options['table_format']
|
|
|
|
cfg_json['formats']['table'] = self.table_format
|
|
|
|
|
|
|
|
# Create new configuration
|
|
|
|
makedirs(self.CONFIG_DIR)
|
|
|
|
with open(self.CONFIG_FILE, 'x') as fd:
|
|
|
|
dump_cfg = dump(cfg_json, default_flow_style=False, allow_unicode=True, encoding=None)
|
|
|
|
fd.write(dump_cfg)
|
|
|
|
chmod(self.CONFIG_DIR, 0o755)
|
|
|
|
|
2019-06-22 02:21:23 -05:00
|
|
|
# Create template directory
|
|
|
|
makedirs(self.USER_TEMPLATE_DIR)
|
|
|
|
copy(self.PROJ_TEMPLATE_DIR, self.USER_TEMPLATE_DIR)
|
|
|
|
|
|
|
|
|
2019-06-16 23:54:10 -05:00
|
|
|
@property
|
|
|
|
def exists(self):
|
|
|
|
return path.exists(self.CONFIG_FILE)
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
if self.exists:
|
|
|
|
with open(self.CONFIG_FILE) as fd:
|
|
|
|
data = safe_load(fd)
|
|
|
|
self.update(data)
|
|
|
|
else:
|
|
|
|
self.create()
|
2019-06-22 02:21:23 -05:00
|
|
|
|
|
|
|
def templates(self, name):
|
|
|
|
try:
|
|
|
|
TEMPLATE_LOADER = Environment(loader=FileSystemLoader([self.USER_TEMPLATE_DIR, self.PROJ_TEMPLATE_DIR]),
|
2019-08-04 23:59:28 -05:00
|
|
|
trim_blocks=False, lstrip_blocks=True, keep_trailing_newline=False)
|
2019-06-25 00:52:17 -05:00
|
|
|
|
|
|
|
return TEMPLATE_LOADER.get_template('{}.{}'.format(name, self.TEMPLATE_EXT))
|
2019-06-22 02:21:23 -05:00
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|