diff --git a/apps/grocy.py b/apps/grocy.py index 705c6a2..dd19ac6 100644 --- a/apps/grocy.py +++ b/apps/grocy.py @@ -113,4 +113,5 @@ class Grocy(TaskService): raise e return responses - + # TODO: do this part next + def sync(): diff --git a/cli.py b/cli.py index 5f13188..a621732 100644 --- a/cli.py +++ b/cli.py @@ -1,9 +1,37 @@ import click -from os import path +from pydoc import importfile, ErrorDuringImport +import yaml +from site import exit +from shutil import copy +from os import path, chmod, makedirs +import stat APP_NAME = 'twservices' +SAMPLE_CONFIG_FILE = 'sample.config.yml' +CONFIG_FILE = 'config.yml' +CONFIG_DIR = click.get_app_dir(APP_NAME) +PROJ_DIR = os.path.dirname(os.path.realpath(__file__)) def __create_config_file(): + user_cfg_file = path.join(CONFIG_DIR, CONFIG_FILE) + sample_cfg_file = path.join(PROJ_DIR, SAMPLE_CONFIG) + if not os.path.exists(CONFIG_DIR): + print('Config {} director does not exist, create...'.format(CONFIG_DIR)) + makedirs(CONFIG_DIR) + + copy(sample_cfg_file, user_cfg_file) + print('Copying {} to {}'.format(sample_cfg_file, user_cfg_file)) + chmod(user_cfg_file, 0o664) + + return user_cfg_file + + + + + + + + @click.command() @click.group() @@ -17,18 +45,27 @@ def get_config_file() if not path.exists(cfg_file): create_config_app_dir = click.confirm(no_config_msg) if create_config_app_dir: + cfg_file = __create_config_file() + else: + exit(0) + return cfg_file - - + @main.command() def sync(): - - - - - - + cfg_file = get_config_file() + fd = open(cfdg_file); + parse_cfg_file = yaml.safe_load(fd) + services = parse_cfg_file['services'] + for service in services: + try: + ServiceClass = importfile('{}/apps/{}.py'.format(PROJ_DIR, service['name']) + ServiceInstance = ServiceClass(**service) + ServiceInstance.sync() + except ErrorDuringImport as e: + print(e) + exit(1) diff --git a/config.py b/config.py new file mode 100644 index 0000000..45922bf --- /dev/null +++ b/config.py @@ -0,0 +1,4 @@ +SERVICES = ['grocy'] + +def getListOfServicesNames(): + return SERVICES diff --git a/sample.config.yml b/sample.config.yml index 020affd..dd99b7f 100644 --- a/sample.config.yml +++ b/sample.config.yml @@ -1,6 +1,7 @@ logger: level: debug -grocy: - api: 'https://aerex.me/grocy/api' - token: 'McaeCf5FrT9Sqr96tPcZg9l4uUCexR1fGVGIfDR6qNQxsWECpv' +services: + - name: 'grocy' + api: 'https://aerex.me/grocy/api' + token: 'McaeCf5FrT9Sqr96tPcZg9l4uUCexR1fGVGIfDR6qNQxsWECpv'