64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import re
|
|
from datetime import datetime
|
|
from grocy import RestService
|
|
from tabulate import tabulate
|
|
from os import path
|
|
|
|
class Chore(object):
|
|
GET_BATTERIES = '/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)
|