2018-12-02 22:09:55 -06:00
|
|
|
import click
|
2018-12-16 02:17:29 -06:00
|
|
|
from pkg_resources import iter_entry_points
|
2018-12-07 01:03:17 -06:00
|
|
|
import yaml
|
2018-12-08 21:19:17 -06:00
|
|
|
from sys import exit
|
2018-12-16 02:17:29 -06:00
|
|
|
from shutil import copy
|
2018-12-07 01:03:17 -06:00
|
|
|
from os import path, chmod, makedirs
|
2018-12-16 02:17:29 -06:00
|
|
|
from taskw import TaskWarriorShellout
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
APP_NAME = 'twservices'
|
2018-12-07 01:03:17 -06:00
|
|
|
SAMPLE_CONFIG_FILE = 'sample.config.yml'
|
|
|
|
CONFIG_FILE = 'config.yml'
|
|
|
|
CONFIG_DIR = click.get_app_dir(APP_NAME)
|
2018-12-22 01:07:12 -06:00
|
|
|
PROJ_DIR = path.join(path.dirname(path.realpath(__file__)))
|
2018-12-16 02:17:29 -06:00
|
|
|
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
def __create_config_file():
|
2018-12-07 01:03:17 -06:00
|
|
|
user_cfg_file = path.join(CONFIG_DIR, CONFIG_FILE)
|
2018-12-16 02:17:29 -06:00
|
|
|
sample_cfg_file = path.join(PROJ_DIR, SAMPLE_CONFIG_FILE)
|
|
|
|
if not path.exists(CONFIG_DIR):
|
2018-12-07 01:03:17 -06:00
|
|
|
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))
|
2018-12-16 02:17:29 -06:00
|
|
|
chmod(user_cfg_file, 0o664)
|
2018-12-07 01:03:17 -06:00
|
|
|
return user_cfg_file
|
|
|
|
|
|
|
|
|
2018-12-02 22:09:55 -06:00
|
|
|
@click.group()
|
|
|
|
def main():
|
|
|
|
pass
|
|
|
|
|
2018-12-16 02:17:29 -06:00
|
|
|
|
|
|
|
def get_config_file():
|
|
|
|
no_config_msg = 'A config file was not found'
|
|
|
|
no_config_msg+= ' and will be created under {}. Is that Ok?'
|
|
|
|
no_config_msg = no_config_msg.format(click.get_app_dir(APP_NAME))
|
2018-12-02 22:09:55 -06:00
|
|
|
cfg_file = path.join(click.get_app_dir(APP_NAME), 'config.yml')
|
|
|
|
if not path.exists(cfg_file):
|
|
|
|
create_config_app_dir = click.confirm(no_config_msg)
|
|
|
|
if create_config_app_dir:
|
2018-12-07 01:03:17 -06:00
|
|
|
cfg_file = __create_config_file()
|
|
|
|
else:
|
|
|
|
exit(0)
|
|
|
|
return cfg_file
|
2018-12-02 22:09:55 -06:00
|
|
|
|
|
|
|
|
|
|
|
@main.command()
|
|
|
|
def sync():
|
2018-12-07 01:03:17 -06:00
|
|
|
cfg_file = get_config_file()
|
2018-12-16 02:17:29 -06:00
|
|
|
fd = open(cfg_file)
|
2018-12-07 01:03:17 -06:00
|
|
|
parse_cfg_file = yaml.safe_load(fd)
|
2018-12-08 21:19:17 -06:00
|
|
|
taskwarrior_cfg = parse_cfg_file['taskwarrior']
|
2018-12-07 01:03:17 -06:00
|
|
|
services = parse_cfg_file['services']
|
2018-12-08 21:19:17 -06:00
|
|
|
|
|
|
|
log_cfg = parse_cfg_file['logger']
|
2018-12-16 02:17:29 -06:00
|
|
|
log_level = 'info' if 'level' not in log_cfg else log_cfg['level']
|
|
|
|
log_filename = 'log' if 'file'not in log_cfg else log_cfg['file']
|
2018-12-08 21:19:17 -06:00
|
|
|
|
2018-12-16 02:17:29 -06:00
|
|
|
logging.basicConfig(level=log_level, filename=log_filename)
|
2018-12-08 21:19:17 -06:00
|
|
|
|
2018-12-07 01:03:17 -06:00
|
|
|
for service in services:
|
|
|
|
try:
|
2018-12-16 02:17:29 -06:00
|
|
|
entry_service_point = iter_entry_points(group='twservices.apps', name=service['name'])
|
|
|
|
entry_service_point = next(entry_service_point)
|
|
|
|
except StopIteration as e:
|
|
|
|
log.error(e)
|
|
|
|
log.error('Could not import %s service', services['name'])
|
|
|
|
|
|
|
|
try:
|
|
|
|
ServiceClass = entry_service_point.load()
|
2018-12-07 01:03:17 -06:00
|
|
|
ServiceInstance = ServiceClass(**service)
|
2018-12-16 02:17:29 -06:00
|
|
|
udas = ServiceInstance.get_udas()
|
|
|
|
tw = TaskWarriorShellout(
|
|
|
|
config_filename=taskwarrior_cfg['rc'],
|
|
|
|
config_overrides=udas,
|
|
|
|
marshal=True,
|
|
|
|
)
|
|
|
|
ServiceInstance.set_taskwarrior(tw)
|
2018-12-07 01:03:17 -06:00
|
|
|
ServiceInstance.sync()
|
2018-12-16 02:17:29 -06:00
|
|
|
except BaseException as e:
|
|
|
|
# TODO: Need to handle base exceptions better
|
|
|
|
# See if you can print stacktrace
|
|
|
|
log.exception('Could not sync %s', e)
|
|
|
|
print(e.stderr)
|
2018-12-07 01:03:17 -06:00
|
|
|
exit(1)
|