task-status/task_status/task_status.py

74 lines
2.2 KiB
Python
Raw Permalink Normal View History

2021-08-17 12:12:01 -04:00
#!/usr/bin/env python
2021-08-20 08:49:14 -04:00
""" Get completed tasks from taskwarrior and output a simple report """
2021-08-17 12:12:01 -04:00
import subprocess
import json
import itertools
import operator
2021-08-17 12:12:01 -04:00
from datetime import date
from dateutil.relativedelta import relativedelta, MO
import click
2021-08-17 12:12:01 -04:00
__version__ = "0.3.1"
2021-08-17 12:12:01 -04:00
def validate_tasks(tasks):
""" Validate tasks have a proejct, inject "None" if they don't """
try:
for task in tasks:
test = task["project"]
except KeyError:
task["project"] = "None"
return tasks
@click.command()
@click.version_option(__version__, prog_name="task-status")
@click.option("--uuid", is_flag=True, help="Display the task UUID")
@click.option("--header", is_flag=True, help="Display date header")
@click.option("--bullet", default="*", help="Character to use for bullets in list")
@click.option(
"--sort", is_flag=True, default=True, help="Alphabetically sort by project"
)
def main(uuid, header, bullet, sort):
2021-08-20 08:49:14 -04:00
"""Main function that does all the work for task_status"""
2021-08-17 12:12:01 -04:00
today = date.today()
last_monday = today + relativedelta(weekday=MO(-2))
task_command = [
"task",
f"end.after:{last_monday}",
"status_report:display",
"-home",
"-DELETED",
"-delegated",
"export",
]
2021-08-17 12:12:01 -04:00
tasks = subprocess.run(
task_command,
capture_output=True,
2021-08-20 08:49:14 -04:00
check=True,
2021-08-17 12:12:01 -04:00
)
entries = validate_tasks(json.loads(tasks.stdout.decode()))
output_list = []
project_list = []
if header:
print(f"Reporting from: {last_monday}")
for status_projects, status_entries in itertools.groupby(
entries, key=operator.itemgetter("project")
):
if status_projects not in project_list:
project_list.append(status_projects)
output_list.append(list(status_entries))
if sort:
project_list.sort()
for project in project_list:
print(f"{bullet} {project}")
for entry in entries:
if entry["project"] == project and uuid:
print(f'\t{bullet} {entry["description"]} ({entry["uuid"]})')
elif entry["project"] == project:
print(f'\t{bullet} {entry["description"]}')
2021-08-17 12:12:01 -04:00
if __name__ == "__main__":
main() # pylint: disable=no-value-for-parameter # pragma: no cover