Taskwarrior Python Script: Decrease Priority Tag for Pending Tasks in Projects with 3 or More Completed Tasks Today

25 views Asked by At

I'm trying to achieve a logic in Taskwarrior that adds a specific tag to tasks that is nested under a project where there is 3 or more tasks for this projects finished today , so that the priority of the left pending task in this project is being decreased once i finish 3 tasks for this project. am doing the script using python using taskw lib , and here is what i have tried :

#!/usr/bin/env python3

from taskw import TaskWarrior
from datetime import datetime,timezone,timedelta
from collections import Counter
import time

def getTasksCount():
    w = TaskWarrior()
    tasks = w.load_tasks()['completed']
    tasks_done_today = []
    project_count = {}
    for single_task in tasks:
        date_object = datetime.strptime(single_task['end'], "%Y%m%dT%H%M%SZ")
        day = date_object.day
        month = date_object.month
        year = date_object.year
        end_date = f"{day}-{month}-{year}"
        end_date = date_object.strftime("%d-%m-%Y")
        # today_date = datetime.now().date()
        today_date = datetime.now().date() - timedelta(days=1)
        formatted_date = today_date.strftime("%d-%m-%Y")
        today = formatted_date
        print(f"XXX {today} : YYY {end_date}")
        if today == end_date:
            if 'project' in single_task:
                print(f"{single_task['project']} : {single_task['description']}")
                if single_task['project'] in project_count:
                    project_count[single_task['project']] += 1
                else:
                    project_count[single_task['project']] = 1
    print(f"completed tasks for every projet today: {project_count}")
    for key,value in project_count.items():
        if value >= 3:
            print(f"you have done enough for {key} project today , decreasing it's ugency")
            tasks = w.load_tasks()['pending']
            for project_task in tasks:
                print(project_task)
                if 'project' in project_task and project_task['project'] == key:
                    id, task = w.get_task(id=project_task['id'])
                    if "enoughToday" not in task['tags']:
                        task['tags'].append("enoughToday")
                        w.task_update(task)
        else:
            tasks = w.load_tasks()['pending']
            for project_task in tasks:
                print(project_task)
                if 'project' in project_task and project_task['project'] == key:
                    id, task = w.get_task(id=project_task['id'])
                    if "enoughToday" in task['tags']:
                        task['tags'].remove("enoughToday")
                        w.task_update(task)
if __name__ == "__main__":
    getTasksCount()

but the problem am getting this error :

Traceback (most recent call last):
  File "/home/ahmed/.task/enough_today.py", line 54, in <module>
    getTasksCount()
  File "/home/ahmed/.task/enough_today.py", line 43, in getTasksCount
    w.task_update(task)
  File "/home/ahmed/.local/lib/python3.10/site-packages/taskw/warrior.py", line 812, in task_update
    self._execute(task_uuid, 'modify', *modification)
  File "/home/ahmed/.local/lib/python3.10/site-packages/taskw/warrior.py", line 489, in _execute
    raise TaskwarriorError(command, stderr, stdout, proc.returncode)
taskw.exceptions.TaskwarriorError: [b'task', b'rc.verbose=new-uuid', b'rc.json.array=TRUE', b'rc.confirmation=no', b'rc.dependency.confirmation=no', b'rc.recurrence.confirmation=no', b'06353759-e3cd-4c55-a0cf-8a7d438f3bb6', b'modify', b'depends:"[\'d8928788-eef6-4551-bf64-18e4fba8d3f2\']"', b'description:"Add rating app"', b'entry:"20240114T125011Z"', b'modified:"20240118T081319Z"', b'project:"#jobsquare"', b'status:"pending"', b'tags:"jobsquare,paid,enoughToday"'] #2; stderr:"b'Could not create a dependency on task 0 - not found.'"; stdout:"b''"
0

There are 0 answers