Added logic to show correct change counter in terminal

pull/30/head
Hoffelhas 2023-01-13 16:58:16 +01:00
parent 707482cf17
commit 4dcce138c0
1 changed files with 37 additions and 23 deletions

View File

@ -377,7 +377,7 @@ def initialise_api(args):
api = TodoistAPI(**api_arguments) api = TodoistAPI(**api_arguments)
logging.info("Autodoist has connected and is running fine!\n") logging.info("Autodoist has successfully connected to Todoist!\n")
# Check if labels exist # Check if labels exist
@ -608,7 +608,7 @@ def update_labels(api, overview_task_ids, overview_task_labels):
# Check if header logic needs to be applied # Check if header logic needs to be applied
def check_header(api, model): def check_header(api, model, overview_updated_ids):
header_all_in_level = False header_all_in_level = False
unheader_all_in_level = False unheader_all_in_level = False
regex_a = '(^[*]{2}\s*)(.*)' regex_a = '(^[*]{2}\s*)(.*)'
@ -623,10 +623,12 @@ def check_header(api, model):
header_all_in_level = True header_all_in_level = True
model.content = ra[2] # Local record model.content = ra[2] # Local record
api.update_task(task_id=model.id, content=ra[2]) api.update_task(task_id=model.id, content=ra[2])
# overview_updated_ids.append(model.id) # Ignore this one, since else it's count double
if rb: if rb:
unheader_all_in_level = True unheader_all_in_level = True
model.content = rb[2] # Local record model.content = rb[2] # Local record
api.update_task(task_id=model.id, content=rb[2]) api.update_task(task_id=model.id, content=rb[2])
overview_updated_ids.append(model.id)
else: else:
ra = re.search(regex_a, model.name) ra = re.search(regex_a, model.name)
rb = re.search(regex_b, model.name) rb = re.search(regex_b, model.name)
@ -635,17 +637,21 @@ def check_header(api, model):
if ra: if ra:
header_all_in_level = True header_all_in_level = True
api.update_section(section_id=model.id, name=ra[2]) api.update_section(section_id=model.id, name=ra[2])
overview_updated_ids.append(model.id)
if rb: if rb:
unheader_all_in_level = True unheader_all_in_level = True
api.update_section(section_id=model.id, name=rb[2]) api.update_section(section_id=model.id, name=rb[2])
overview_updated_ids.append(model.id)
elif isinstance(model, Project): elif isinstance(model, Project):
if ra: if ra:
header_all_in_level = True header_all_in_level = True
api.update_project(project_id=model.id, name=ra[2]) api.update_project(project_id=model.id, name=ra[2])
overview_updated_ids.append(model.id)
if rb: if rb:
unheader_all_in_level = True unheader_all_in_level = True
api.update_project(project_id=model.id, name=rb[2]) api.update_project(project_id=model.id, name=rb[2])
overview_updated_ids.append(model.id)
except: except:
logging.debug('check_header: no right model found') logging.debug('check_header: no right model found')
@ -654,25 +660,29 @@ def check_header(api, model):
# Logic for applying and removing headers # Logic for applying and removing headers
def modify_task_headers(api, task, section_tasks, header_all_in_p, unheader_all_in_p, header_all_in_s, unheader_all_in_s, header_all_in_t, unheader_all_in_t): def modify_task_headers(api, task, section_tasks, overview_updated_ids, header_all_in_p, unheader_all_in_p, header_all_in_s, unheader_all_in_s, header_all_in_t, unheader_all_in_t):
if any([header_all_in_p, header_all_in_s]): if any([header_all_in_p, header_all_in_s]):
if task.content[:2] != '* ': if task.content[:2] != '* ':
api.update_task(task_id=task.id, content='* ' + task.content) api.update_task(task_id=task.id, content='* ' + task.content)
overview_updated_ids.append(task.id)
if any([unheader_all_in_p, unheader_all_in_s]): if any([unheader_all_in_p, unheader_all_in_s]):
if task.content[:2] == '* ': if task.content[:2] == '* ':
api.update_task(task_id=task.id, content=task.content[2:]) api.update_task(task_id=task.id, content=task.content[2:])
overview_updated_ids.append(task.id)
if header_all_in_t: if header_all_in_t:
if task.content[:2] != '* ': if task.content[:2] != '* ':
api.update_task(task_id=task.id, content='* ' + task.content) api.update_task(task_id=task.id, content='* ' + task.content)
find_and_headerify_all_children(api, task, section_tasks, 1) overview_updated_ids.append(task.id)
find_and_headerify_all_children(api, task, section_tasks, overview_updated_ids, 1)
if unheader_all_in_t: if unheader_all_in_t:
if task.content[:2] == '* ': if task.content[:2] == '* ':
api.update_task(task_id=task.id, content=task.content[2:]) api.update_task(task_id=task.id, content=task.content[2:])
find_and_headerify_all_children(api, task, section_tasks, 2) overview_updated_ids.append(task.id)
find_and_headerify_all_children(api, task, section_tasks, overview_updated_ids, 2)
@ -714,7 +724,7 @@ def check_regen_mode(api, item, regen_labels_id):
# Recurring lists logic # Recurring lists logic
def run_recurring_lists_logic(args, api,connection, task, task_items, task_items_all, regen_labels_id): def run_recurring_lists_logic(args, api, connection, overview_updated_ids, task, task_items, task_items_all, regen_labels_id):
if task.parent_id == 0: if task.parent_id == 0:
try: try:
@ -793,7 +803,8 @@ def run_recurring_lists_logic(args, api,connection, task, task_items, task_items
# Update due-date to today # Update due-date to today
api.update_task(task_id=task.id, due_date=today_str) #TODO: Apparently this breaks the reccuring string... api.update_task(task_id=task.id, due_date=today_str) #TODO: Apparently this breaks the reccuring string...
logging.info("Update date on task: '%s'" % (task.content)) logging.debug("Update date on task: '%s'" % (task.content))
overview_updated_ids.append(task.id)
# Save the new date for reference us # Save the new date for reference us
db_update_value(connection, task, 'due_date', task.due.date) db_update_value(connection, task, 'due_date', task.due.date)
@ -837,7 +848,7 @@ def find_and_clean_all_children(task_ids, task, section_tasks):
return task_ids return task_ids
def find_and_headerify_all_children(api, task, section_tasks, mode): def find_and_headerify_all_children(api, task, section_tasks, overview_updated_ids, mode):
child_tasks = list(filter(lambda x: x.parent_id == task.id, section_tasks)) child_tasks = list(filter(lambda x: x.parent_id == task.id, section_tasks))
@ -847,12 +858,14 @@ def find_and_headerify_all_children(api, task, section_tasks, mode):
if mode == 1: if mode == 1:
if child_task.content[:2] != '* ': if child_task.content[:2] != '* ':
api.update_task(task_id=child_task.id, content='* ' + child_task.content) api.update_task(task_id=child_task.id, content='* ' + child_task.content)
overview_updated_ids.append(child_task.id)
elif mode == 2: elif mode == 2:
if child_task.content[:2] == '* ': if child_task.content[:2] == '* ':
api.update_task(task_id=child_task.id, content=child_task.content[2:]) api.update_task(task_id=child_task.id, content=child_task.content[2:])
overview_updated_ids.append(child_task.id)
find_and_headerify_all_children(api, child_task, section_tasks, mode) find_and_headerify_all_children(api, child_task, section_tasks, overview_updated_ids, mode)
@ -866,10 +879,12 @@ def autodoist_magic(args, api, connection):
# Preallocate dictionaries and other values # Preallocate dictionaries and other values
overview_task_ids = {} overview_task_ids = {}
overview_task_labels = {} overview_task_labels = {}
overview_updated_ids = []
next_action_label = args.label next_action_label = args.label
regen_labels_id = args.regen_label_names regen_labels_id = args.regen_label_names
first_found = [False, False, False] first_found = [False, False, False]
# Get all projects info
try: try:
projects = api.get_projects() projects = api.get_projects()
except Exception as error: except Exception as error:
@ -885,7 +900,7 @@ def autodoist_magic(args, api, connection):
db_check_existance(connection, project) db_check_existance(connection, project)
# Check if we need to (un)header entire project # Check if we need to (un)header entire project
header_all_in_p, unheader_all_in_p = check_header(api, project) header_all_in_p, unheader_all_in_p = check_header(api, project, overview_updated_ids)
# Get project type # Get project type
if next_action_label is not None: if next_action_label is not None:
@ -941,7 +956,7 @@ def autodoist_magic(args, api, connection):
db_check_existance(connection, section) db_check_existance(connection, section)
# Check if we need to (un)header entire secion # Check if we need to (un)header entire secion
header_all_in_s, unheader_all_in_s = check_header(api, section) header_all_in_s, unheader_all_in_s = check_header(api, section, overview_updated_ids)
# Get section type # Get section type
if next_action_label: if next_action_label:
@ -996,10 +1011,10 @@ def autodoist_magic(args, api, connection):
filter(lambda x: x.parent_id == task.id, non_completed_tasks)) filter(lambda x: x.parent_id == task.id, non_completed_tasks))
# Check if we need to (un)header entire task tree # Check if we need to (un)header entire task tree
header_all_in_t, unheader_all_in_t = check_header(api, task) header_all_in_t, unheader_all_in_t = check_header(api, task, overview_updated_ids)
# Modify headers where needed # Modify headers where needed
modify_task_headers(api, task, section_tasks, header_all_in_p, unheader_all_in_p, header_all_in_s, unheader_all_in_s, header_all_in_t, unheader_all_in_t) modify_task_headers(api, task, section_tasks, overview_updated_ids, header_all_in_p, unheader_all_in_p, header_all_in_s, unheader_all_in_s, header_all_in_t, unheader_all_in_t)
# TODO: Check is regeneration is still needed, now that it's part of core Todoist. Disabled for now. # TODO: Check is regeneration is still needed, now that it's part of core Todoist. Disabled for now.
# Logic for recurring lists # Logic for recurring lists
@ -1015,7 +1030,7 @@ def autodoist_magic(args, api, connection):
# If options turned on, start recurring lists logic #TODO: regeneration currently doesn't work, becaue TASK_ENDPOINT doesn't show completed tasks. Use workaround. # If options turned on, start recurring lists logic #TODO: regeneration currently doesn't work, becaue TASK_ENDPOINT doesn't show completed tasks. Use workaround.
if args.regeneration is not None or args.end: if args.regeneration is not None or args.end:
run_recurring_lists_logic( run_recurring_lists_logic(
args, api, connection, task, child_tasks, child_tasks_all, regen_labels_id) args, api, connection, overview_updated_ids, task, child_tasks, child_tasks_all, regen_labels_id)
# If options turned on, start labelling logic # If options turned on, start labelling logic
if next_action_label is not None: if next_action_label is not None:
@ -1280,7 +1295,7 @@ def autodoist_magic(args, api, connection):
first_found[0] = True first_found[0] = True
# Return all ids and corresponding labels that need to be modified # Return all ids and corresponding labels that need to be modified
return overview_task_ids, overview_task_labels return overview_task_ids, overview_task_labels, overview_updated_ids
# Main # Main
@ -1358,23 +1373,22 @@ def main():
# sync(api) # sync(api)
# Evaluate projects, sections, and tasks # Evaluate projects, sections, and tasks
overview_task_ids, overview_task_labels = autodoist_magic( overview_task_ids, overview_task_labels, overview_updated_ids = autodoist_magic(
args, api, connection) args, api, connection)
# Commit next action label changes # Commit next action label changes
if args.label is not None: if args.label is not None:
updated_ids = update_labels(api, overview_task_ids, updated_ids = update_labels(api, overview_task_ids,
overview_task_labels) overview_task_labels)
num_changes = len(updated_ids)+len(overview_updated_ids)
if len(updated_ids): if num_changes:
len_api_q = len(updated_ids) if num_changes == 1:
if len_api_q == 1:
logging.info( logging.info(
'%d change committed to Todoist.', len_api_q) '%d change committed to Todoist.', num_changes)
else: else:
logging.info( logging.info(
'%d changes committed to Todoist.', len_api_q) '%d changes committed to Todoist.', num_changes)
else: else:
logging.info('No changes in queue, skipping sync.') logging.info('No changes in queue, skipping sync.')