Bug has magically gone away on Todoist's end. For now moving on.

Parser lay-out, help content, and arguments have been improved.
master
Hoffelhas 2020-05-30 13:14:07 +02:00
parent c4792b9a17
commit a552954d8b
1 changed files with 78 additions and 49 deletions

View File

@ -11,34 +11,51 @@ from datetime import datetime
global overview_item_ids global overview_item_ids
global overview_item_labels global overview_item_labels
def make_wide(formatter, w=120, h=36):
"""Return a wider HelpFormatter, if possible."""
try:
# https://stackoverflow.com/a/5464440
# beware: "Only the name of this class is considered a public API."
kwargs = {'width': w, 'max_help_position': h}
formatter(None, **kwargs)
return lambda prog: formatter(prog, **kwargs)
except TypeError:
warnings.warn("argparse help formatter failed, falling back.")
return formatter
def main(): def main():
# Version # Version
current_version = 'v1.2' current_version = 'v1.2'
"""Main process function.""" """Main process function."""
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser(
parser.add_argument('-a', '--api_key', help='Todoist API Key') formatter_class=make_wide(argparse.HelpFormatter, w=110, h=50))
parser.add_argument('-a', '--api_key', help='Takes your Todoist API Key.', type=str)
parser.add_argument( parser.add_argument(
'-l', '--label', help='The next action label to use', type=str) '-l', '--label', help='Enable next action labelling. Define which label to use.', type=str)
parser.add_argument( parser.add_argument(
'-d', '--delay', help='Specify the delay in seconds between syncs', default=5, type=int) '-r', '--recurring', help='Enable regeneration of sub-tasks in recurring lists.', action='store_true')
parser.add_argument( parser.add_argument(
'-r', '--recurring', help='Enable re-use of recurring lists', action='store_true') '-e', '--end', help='Enable alternative end-of-day time instead of default midnight. Enter a number from 1 to 24 to define which hour is used.', type=int)
parser.add_argument('--debug', help='Enable debugging', parser.add_argument(
action='store_true') '-d', '--delay', help='Specify the delay in seconds between syncs (default 5).', default=5, type=int)
parser.add_argument('--inbox', help='The method the Inbox project should be processed', parser.add_argument(
default=None, choices=['parallel', 'sequential']) '-ps', '--p_suffix', help='Change suffix for parallel labeling (default "//").', default='//')
parser.add_argument('--parallel_suffix', default='//') parser.add_argument(
parser.add_argument('--sequential_suffix', default='--') '-ss', '--s_suffix', help='Change suffix for sequential labeling (default "--").', default='--')
parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days', parser.add_argument('-hf', '--hide_future', help='Hide future dated next actions until the specified number of days (default 7).',
default=7, type=int) default=7, type=int)
parser.add_argument( parser.add_argument(
'--onetime', help='Update Todoist once and exit', action='store_true') '--onetime', help='Update Todoist once and exit.', action='store_true')
parser.add_argument( parser.add_argument(
'--nocache', help='Disables caching data to disk for quicker syncing', action='store_true') '--nocache', help='Disables caching data to disk for quicker syncing.', action='store_true')
parser.add_argument( parser.add_argument('--debug', help='Enable detailed debugging in log.',
'-e', '--end', help='Enter a number from 1-24 to define which hour is used as end-of-day time.', type=int) action='store_true')
parser.add_argument('--inbox', help='The method the Inbox should be processed.',
default=None, choices=['parallel', 'sequential'])
args = parser.parse_args() args = parser.parse_args()
# Set debug # Set debug
@ -56,16 +73,18 @@ def main():
) )
def initialise(args): def initialise(args):
# Check we have a API key # Check we have a API key
if not args.api_key: if not args.api_key:
logging.error("\n\nNo API key set. Run Autodoist with '-a <YOUR_API_KEY>'\n") logging.error(
"\n\nNo API key set. Run Autodoist with '-a <YOUR_API_KEY>'\n")
sys.exit(1) sys.exit(1)
# Check if AEOD is used # Check if AEOD is used
if args.end is not None: if args.end is not None:
if args.end < 1 or args.end > 24: if args.end < 1 or args.end > 24:
logging.error("\n\nPlease choose a number from 1 to 24 to indicate which hour is used as alternative end-of-day time.\n") logging.error(
"\n\nPlease choose a number from 1 to 24 to indicate which hour is used as alternative end-of-day time.\n")
sys.exit(1) sys.exit(1)
else: else:
pass pass
@ -95,10 +114,10 @@ def main():
if len(labels) > 0: if len(labels) > 0:
label_id = labels[0]['id'] label_id = labels[0]['id']
logging.debug('Label \'%s\' found as label id %d', logging.debug('Label \'%s\' found as label id %d',
args.label, label_id) args.label, label_id)
else: else:
# Create a new label in Todoist # Create a new label in Todoist
#TODO: #TODO:
logging.error( logging.error(
"\n\nLabel \'%s\' doesn't exist in your Todoist. Please create it or use your custom label by running Autodoist with the argument '-l <YOUR_EXACT_LABEL>'.\n", args.label) "\n\nLabel \'%s\' doesn't exist in your Todoist. Please create it or use your custom label by running Autodoist with the argument '-l <YOUR_EXACT_LABEL>'.\n", args.label)
sys.exit(1) sys.exit(1)
@ -137,7 +156,7 @@ def main():
return 1 return 1
def get_type(object, key): def get_type(object, key):
len_suffix = [len(args.parallel_suffix), len(args.sequential_suffix)] len_suffix = [len(args.p_suffix), len(args.s_suffix)]
try: try:
old_type = object[key] old_type = object[key]
@ -152,9 +171,9 @@ def main():
if name == 'Inbox': if name == 'Inbox':
current_type = args.inbox current_type = args.inbox
elif name[-len_suffix[0]:] == args.parallel_suffix: elif name[-len_suffix[0]:] == args.p_suffix:
current_type = 'parallel' current_type = 'parallel'
elif name[-len_suffix[1]:] == args.sequential_suffix: elif name[-len_suffix[1]:] == args.s_suffix:
current_type = 'sequential' current_type = 'sequential'
else: else:
current_type = None current_type = None
@ -233,12 +252,12 @@ def main():
overview_item_labels = {} overview_item_labels = {}
for project in api.projects.all(): for project in api.projects.all():
if label_id is not None: if label_id is not None:
# Get project type # Get project type
project_type, project_type_changed = get_project_type(project) project_type, project_type_changed = get_project_type(project)
logging.debug('Project \'%s\' being processed as %s', logging.debug('Project \'%s\' being processed as %s',
project['name'], project_type) project['name'], project_type)
# Get all items for the project # Get all items for the project
items = api.items.all( items = api.items.all(
@ -295,45 +314,54 @@ def main():
try: try:
if item['due']['is_recurring']: if item['due']['is_recurring']:
try: try:
if item['content'] == 'TASK':
print('b;ah')
# Check if the T0 task date has changed # Check if the T0 task date has changed
if item['due']['date'] != item['date_old']: if item['due']['date'] != item['date_old']:
if args.end is not None: if args.end is not None:
# Determine current hour # Determine current hour
t = datetime.today() t = datetime.today()
current_hour = t.hour current_hour = t.hour
# Check if current time is before our end-of-day # Check if current time is before our end-of-day
if (args.end - current_hour) > 0: if (args.end - current_hour) > 0:
# Determine the difference in days set by todoist # Determine the difference in days set by todoist
nd = [int(x) for x in item['due']['date'].split('-')] nd = [
od = [int(x) for x in item['old_date'].split('-')] int(x) for x in item['due']['date'].split('-')]
od = [
new_date = datetime(nd[0], nd[1], nd[2]) int(x) for x in item['date_old'].split('-')]
old_date = datetime(od[0], od[1], od[2])
today = datetime(t.year, t.month, t.day) new_date = datetime(
days_difference = (new_date-today).days nd[0], nd[1], nd[2])
days_overdue = (today - old_date).days old_date = datetime(
od[0], od[1], od[2])
today = datetime(
t.year, t.month, t.day)
days_difference = (
new_date-today).days
days_overdue = (
today - old_date).days
# Only apply if overdue and if it's a daily recurring tasks # Only apply if overdue and if it's a daily recurring tasks
if days_overdue >= 1 and days_difference == 1: if days_overdue >= 1 and days_difference == 1:
# Find curreny date in string format # Find curreny date in string format
today_str = [str(x) for x in [today.year, today.month, today.day]] today_str = [str(x) for x in [
today.year, today.month, today.day]]
if len(today_str[1]) == 1: if len(today_str[1]) == 1:
today_str[1] = ''.join(['0',today_str[1]]) today_str[1] = ''.join(
['0', today_str[1]])
# Update due-date to today # Update due-date to today
item_due = item['due'] item_due = item['due']
item_due['date'] = '-'.join(today_str) item_due['date'] = '-'.join(
today_str)
item.update(due=item_due) item.update(due=item_due)
# item.update(due={'date': '2020-05-29', 'is_recurring': True, 'string': 'every day'}) # item.update(due={'date': '2020-05-29', 'is_recurring': True, 'string': 'every day'})
# Save the new date for reference us # Save the new date for reference us
item.update(date_old=item['due']['date']) item.update(
date_old=item['due']['date'])
# Mark children for action # Mark children for action
if args.recurring is True: if args.recurring is True:
@ -363,20 +391,21 @@ def main():
for child_item in child_items_all: for child_item in child_items_all:
child_item['r_tag'] = 1 child_item['r_tag'] = 1
except Exception as e: except Exception as e:
logging.debug('Child not recurring: %s' % item['content']) logging.debug('Child not recurring: %s' %
item['content'])
pass pass
# If options turned on, start labelling logic # If options turned on, start labelling logic
if label_id is not None: if label_id is not None:
# Skip processing an item if it has already been checked # Skip processing an item if it has already been checked
if item['checked'] == 1: if item['checked'] == 1:
continue continue
# Check item type # Check item type
item_type, item_type_changed = get_item_type( item_type, item_type_changed = get_item_type(
item, project_type) item, project_type)
logging.debug('Identified \'%s\' as %s type', logging.debug('Identified \'%s\' as %s type',
item['content'], item_type) item['content'], item_type)
# Check the item_type of the project or parent # Check the item_type of the project or parent
if item_type is None: if item_type is None:
@ -458,7 +487,7 @@ def main():
if len(api.queue): if len(api.queue):
len_api_q = len(api.queue) len_api_q = len(api.queue)
api.commit() api.commit()
if len_api_q == 1: if len_api_q == 1:
logging.info( logging.info(
'%d change committed to Todoist.', len_api_q) '%d change committed to Todoist.', len_api_q)
else: else: