From 0e992aaab9af2ea6c6b0427b430594e3990678fc Mon Sep 17 00:00:00 2001 From: Aerex Date: Sat, 9 Mar 2019 19:50:42 -0600 Subject: [PATCH] feat: added chore --- grocy/cli.py | 9 ++++++ grocy/commands/__init__.py | 1 + grocy/commands/chore.py | 64 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 grocy/commands/chore.py diff --git a/grocy/cli.py b/grocy/cli.py index 003c19b..ed19f3a 100644 --- a/grocy/cli.py +++ b/grocy/cli.py @@ -97,3 +97,12 @@ def recipe(ctx): receipe = Recipe(**cfg) recipes = receipe.get_list() click.echo(recipes) + +@main.command() +@click.pass_context +def chore(ctx): + cfg = ctx.obj['cfg'] + + chore = Chore(**cfg) + chores = chore.get_list() + click.echo(chores) diff --git a/grocy/commands/__init__.py b/grocy/commands/__init__.py index 87e99da..c426efa 100644 --- a/grocy/commands/__init__.py +++ b/grocy/commands/__init__.py @@ -1,3 +1,4 @@ from grocy.commands.stock import Stock from grocy.commands.recipe import Recipe +from grocy.commands.chore import Chore from grocy.commands.shopping import Shopping diff --git a/grocy/commands/chore.py b/grocy/commands/chore.py new file mode 100644 index 0000000..ad93e19 --- /dev/null +++ b/grocy/commands/chore.py @@ -0,0 +1,64 @@ +import re +from datetime import datetime +from grocy import RestService +from tabulate import tabulate +from os import path + +class Chore(object): + GET_CURRENT_CHORES = '/chores/get-current' + GET_CHORE_BY_ID = '/get-object/chores/{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: + get_current_chores = self.rest_service.get(Chore.GET_CURRENT_CHORES) + + table_headers = ['Name', 'Due'] + table_entries = [] + for chore in get_current_chores: + path = Chore.GET_CHORE_BY_ID.format(chore['chore_id']) + chore_info = self.rest_service.get(path) + if chore.get('next_estimated_execution_time') is None: + due_date = 'None' + elif re.match('2999',chore.get('next_estimated_execution_time')): + due_date = 'None' + else: + due_date = datetime.strptime(chore.get('next_estimated_execution_time'), '%Y-%m-%d') + + table_entry = [chore_info.get('name'), due_date] + table_entries.append(table_entry) + + except Exception as e: + raise e + # Generate stock overview table + return tabulate(table_entries, headers=table_headers)