feat: added recipe and shopping list

This commit is contained in:
Aerex 2019-03-09 19:02:42 -06:00
parent a5f290d0e7
commit ccd353ba5f
11 changed files with 176 additions and 20 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
venv venv
grocy_cli.egg-info/ grocy_cli.egg-info/
grocy/**/__pycache__/
*.sqlite
*.pyc

View File

@ -1,5 +1,8 @@
import requests import requests
import requests_cache
import json import json
requests_cache.install_cache('grocy', allowable_methods=('GET',), expire_after=180)
class RestService(object): class RestService(object):
API_KEY_HEADER = 'GROCY-API-KEY' API_KEY_HEADER = 'GROCY-API-KEY'
def __init__(self, api_url, json=False): def __init__(self, api_url, json=False):

Binary file not shown.

View File

@ -1,6 +1,6 @@
import click import click
from grocy import RestService from grocy import RestService
from grocy.commands import Stock from grocy.commands import *
from pkg_resources import iter_entry_points from pkg_resources import iter_entry_points
import yaml import yaml
from sys import exit from sys import exit
@ -17,6 +17,12 @@ CONFIG_FILE = 'config.yml'
CONFIG_DIR = click.get_app_dir(APP_NAME) CONFIG_DIR = click.get_app_dir(APP_NAME)
PROJ_DIR = path.join(path.dirname(path.realpath(__file__))) PROJ_DIR = path.join(path.dirname(path.realpath(__file__)))
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)
def __create_config_file(): def __create_config_file():
user_cfg_file = path.join(CONFIG_DIR, CONFIG_FILE) user_cfg_file = path.join(CONFIG_DIR, CONFIG_FILE)
@ -32,8 +38,21 @@ def __create_config_file():
@click.group() @click.group()
def main(): @click.pass_context
pass 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
def get_config_file(): def get_config_file():
@ -48,28 +67,33 @@ def get_config_file():
exit(0) exit(0)
fd = open(cfg_file) fd = open(cfg_file)
parse_cfg_file = yaml.safe_load(fd) parse_cfg_file = yaml.safe_load(fd)
return parse_cfg_file return parse_cfg_file
@main.command() @main.command()
def stock(): @click.pass_context
cfg = get_config_file() def stock(ctx):
cfg = ctx.obj['cfg']
# 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
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)
stock = Stock(**cfg) stock = Stock(**cfg)
stock_entries = stock.get_entries() stock_entries = stock.get_entries()
click.echo(stock_entries) click.echo(stock_entries)
@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)

View File

@ -1 +1,3 @@
from grocy.commands.stock import Stock from grocy.commands.stock import Stock
from grocy.commands.recipe import Recipe
from grocy.commands.shopping import Shopping

52
grocy/commands/recipe.py Normal file
View File

@ -0,0 +1,52 @@
from grocy import RestService
from tabulate import tabulate
from os import path
class Recipe(object):
GET_RECIPES = '/get-objects/recipes'
GET_PRODUCT_BY_ID = '/get-object/products/{0}'
def __init__(self, **entries):
self.__dict__.update(entries)
self._init_rest_service()
#self._set_default_table_formats()
#if not hasattr('tablefmt', self):
# self.tablefmt = None
#if not hasattr('colalign', self):
# self.colalign = None
def _set_default_table_formats(self):
if not hasattr('formats', self):
self.tablefmt = None
self.colalign = None
elif not hasattr('table', self.formats):
self.tableformat = None
elif not hasattr('col', self.formats):
self.colalign = None
def _init_rest_service(self):
if self.api.startswith == '/':
self.api = self.api[1:]
if self.api.endswith == '/':
self.api = self.api[1:-1]
self.rest_service = RestService(self.api, json=True)
self.rest_service.addHeader('Content-Type', 'application/json')
self.rest_service.addToken(self.token)
def get_list(self):
try:
recipes = self.rest_service.get(Recipe.GET_RECIPES)
table_headers = ['#', 'Name']
table_entries = []
for recipe in recipes:
table_entry = [recipe.get('id'), recipe.get('name')]
table_entries.append(table_entry)
except Exception as e:
raise e
# Generate stock overview table
return tabulate(table_entries, headers=table_headers)

View File

@ -0,0 +1,72 @@
from grocy import RestService
import json
from tabulate import tabulate
from os import path
class Shopping(object):
GET_SHOPPING_LIST = '/get-objects/shopping_list'
GET_PRODUCT_BY_ID = '/get-object/products/{0}'
GET_QUANTITY_UNIIT_BY_ID = '/get-object/quantity_units/{0}'
GET_PRODUCT_GROUP_ID_BY_ID ='/get-object/product_groups/{0}'
def __init__(self, **entries):
self.__dict__.update(entries)
self._init_rest_service()
# self.tablefmt = None
#if not hasattr('colalign', self):
# self.colalign = None
def _set_default_table_formats(self):
if not hasattr('formats', self):
self.tablefmt = None
self.colalign = None
elif not hasattr('table', self.formats):
self.tableformat = None
elif not hasattr('col', self.formats):
self.colalign = None
def _init_rest_service(self):
if self.api.startswith == '/':
self.api = self.api[1:]
if self.api.endswith == '/':
self.api = self.api[1:-1]
self.rest_service = RestService(self.api, json=True)
self.rest_service.addHeader('Content-Type', 'application/json')
self.rest_service.addToken(self.token)
def get_list(self):
try:
get_shopping_list = self.rest_service.get(Shopping.GET_SHOPPING_LIST)
table_headers = ['Group', 'Product', 'Amount']
# Get product names and location from ids and replace
product_ids = [entry['product_id'] for entry in get_shopping_list]
products = []
location_ids = []
table_entries = []
for index in range(len(product_ids)):
product_id = product_ids[index]
path = Shopping.GET_PRODUCT_BY_ID.format(product_id)
product = self.rest_service.get(path)
path = Shopping.GET_QUANTITY_UNIIT_BY_ID.format(product['qu_id_purchase'])
quantity_unit = self.rest_service.get(path)
min_amount = '{} {}'.format(product['min_stock_amount'], quantity_unit['name'])
if product['product_group_id'] == '':
product_group_name = 'Uncategorized'
else:
path = Shopping.GET_PRODUCT_GROUP_ID_BY_ID.format(product['product_group_id'])
product_group = self.rest_service.get(path)
product_group_name = product_group['name']
current_shopping_item = [product_group_name, product['name'], min_amount]
table_entries.append(current_shopping_item)
except Exception as e:
raise e
# Generate stock overview table
return tabulate(table_entries, headers=table_headers)