from os import path, chmod, makedirs from yaml import safe_load, dump from copy import deepcopy class Configuration(object): # TODO: Figure out how to handle windows config CONFIG_DIR = path.expanduser('~/.config/grocy') CONFIG_FILE = CONFIG_DIR + '/config.yml' API_KEY_HEADER = 'GROCY-API-KEY' DEFAULT_CFG = { 'logger': { 'level': 'DEBUG', 'file_location': None }, 'api': 'https://demo-en.grocy.info/api', '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 if user_cfg_options['api']: self.api = user_cfg_options['api'] cfg_json['api'] = self.api 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) @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()