feat: added dynamic import of service classes
This commit is contained in:
parent
d7120ac60b
commit
bf7de34bef
@ -113,4 +113,5 @@ class Grocy(TaskService):
|
||||
raise e
|
||||
return responses
|
||||
|
||||
|
||||
# TODO: do this part next
|
||||
def sync():
|
||||
|
55
cli.py
55
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)
|
||||
|
||||
|
||||
|
||||
|
4
config.py
Normal file
4
config.py
Normal file
@ -0,0 +1,4 @@
|
||||
SERVICES = ['grocy']
|
||||
|
||||
def getListOfServicesNames():
|
||||
return SERVICES
|
@ -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'
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user