2019-03-03 19:29:38 -06:00
|
|
|
import click
|
|
|
|
from grocy import RestService
|
2019-03-09 19:02:42 -06:00
|
|
|
from grocy.commands import *
|
2019-03-03 19:29:38 -06:00
|
|
|
from pkg_resources import iter_entry_points
|
|
|
|
import yaml
|
|
|
|
from sys import exit
|
|
|
|
from shutil import copy
|
|
|
|
from os import path, chmod, makedirs
|
|
|
|
from taskw import TaskWarriorShellout
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
APP_NAME = 'grocy-cli'
|
|
|
|
SAMPLE_CONFIG_FILE = 'sample.config.yml'
|
|
|
|
CONFIG_FILE = 'config.yml'
|
|
|
|
CONFIG_DIR = click.get_app_dir(APP_NAME)
|
|
|
|
PROJ_DIR = path.join(path.dirname(path.realpath(__file__)))
|
|
|
|
|
2019-03-09 19:02:42 -06:00
|
|
|
def __validate_token(cfg):
|
|
|
|
# Validate token
|
|
|
|
if hasattr(cfg, 'token') or cfg['token'] is None:
|
|
|
|
click.echo('No token was found. Please add your token to the config file', err=True)
|
|
|
|
logger.error('No token was found. Please add your token')
|
|
|
|
exit(1)
|
2019-03-03 19:29:38 -06:00
|
|
|
|
|
|
|
def __create_config_file():
|
|
|
|
user_cfg_file = path.join(CONFIG_DIR, CONFIG_FILE)
|
|
|
|
sample_cfg_file = path.join(PROJ_DIR, '..', SAMPLE_CONFIG_FILE)
|
|
|
|
if not path.exists(CONFIG_DIR):
|
|
|
|
click.echo('Config {} director does not exist, create...'.format(CONFIG_DIR))
|
|
|
|
makedirs(CONFIG_DIR)
|
|
|
|
|
|
|
|
copy(sample_cfg_file, user_cfg_file)
|
|
|
|
click.echo('Copying sample config to {}'.format(sample_cfg_file, user_cfg_file))
|
|
|
|
chmod(user_cfg_file, 0o664)
|
|
|
|
return user_cfg_file
|
|
|
|
|
|
|
|
|
|
|
|
@click.group()
|
2019-03-09 19:02:42 -06:00
|
|
|
@click.pass_context
|
|
|
|
def main(ctx):
|
|
|
|
cfg = get_config_file()
|
|
|
|
# Get logger
|
|
|
|
if 'logger' in cfg:
|
|
|
|
log_cfg = cfg['logger']
|
|
|
|
else:
|
|
|
|
log_cfg = path.join(click.get_app_dir(APP_NAME), 'grocy.log')
|
|
|
|
log_level = 'DEBUG' if 'level' not in log_cfg else log_cfg['level']
|
|
|
|
log_filename = 'log' if 'file_location' not in log_cfg else log_cfg['file_location']
|
|
|
|
logging.basicConfig(level=log_level, filename=log_filename)
|
|
|
|
|
|
|
|
__validate_token(cfg)
|
|
|
|
ctx.ensure_object(dict)
|
|
|
|
ctx.obj['cfg'] = cfg
|
2019-03-03 19:29:38 -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))
|
|
|
|
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:
|
|
|
|
cfg_file = __create_config_file()
|
|
|
|
exit(0)
|
|
|
|
fd = open(cfg_file)
|
|
|
|
parse_cfg_file = yaml.safe_load(fd)
|
2019-03-09 19:02:42 -06:00
|
|
|
|
2019-03-03 19:29:38 -06:00
|
|
|
return parse_cfg_file
|
|
|
|
|
|
|
|
|
|
|
|
@main.command()
|
2019-03-09 19:02:42 -06:00
|
|
|
@click.pass_context
|
|
|
|
def stock(ctx):
|
|
|
|
cfg = ctx.obj['cfg']
|
2019-03-03 19:29:38 -06:00
|
|
|
|
|
|
|
stock = Stock(**cfg)
|
|
|
|
stock_entries = stock.get_entries()
|
|
|
|
click.echo(stock_entries)
|
2019-03-09 19:02:42 -06:00
|
|
|
|
|
|
|
@main.command()
|
|
|
|
@click.pass_context
|
|
|
|
def shopping(ctx):
|
|
|
|
cfg = ctx.obj['cfg']
|
|
|
|
|
|
|
|
shopping = Shopping(**cfg)
|
|
|
|
shopping_list = shopping.get_list()
|
|
|
|
click.echo(shopping_list)
|
|
|
|
|
|
|
|
@main.command()
|
|
|
|
@click.pass_context
|
|
|
|
def recipe(ctx):
|
|
|
|
cfg = ctx.obj['cfg']
|
|
|
|
|
|
|
|
receipe = Recipe(**cfg)
|
|
|
|
recipes = receipe.get_list()
|
|
|
|
click.echo(recipes)
|
2019-03-09 19:50:42 -06:00
|
|
|
|
|
|
|
@main.command()
|
|
|
|
@click.pass_context
|
|
|
|
def chore(ctx):
|
|
|
|
cfg = ctx.obj['cfg']
|
|
|
|
|
|
|
|
chore = Chore(**cfg)
|
|
|
|
chores = chore.get_list()
|
|
|
|
click.echo(chores)
|