Skip to content

Instantly share code, notes, and snippets.

@wbsch

wbsch/filter.py

Last active Dec 17, 2015
Embed
What would you like to do?
Filter Taskwarrior tasks completed/deleted before a certain time. Reads from STDIN, outputs to STDOUT. Backups still recommended. More detailed documentation and options.
#!/usr/bin/env python
import re
import sys
import time
if len(sys.argv) < 3 or sys.argv[2] not in ["all", "completed", "deleted"]:
sys.stderr.write("Reads data in Taskwarrior format from STDIN and writes to STDOUT only those tasks completed/deleted after the supplied date.\n\n")
sys.stderr.write("### Usage: " + sys.argv[0] + " <Y-m-d> <all|completed|deleted>\n")
sys.stderr.write("Where \"all\" filters all tasks and \"completed\"/\"deleted\" only those with the status set to the keyword specified.\n\n")
sys.stderr.write("### Example usage:\n")
sys.stderr.write(sys.argv[0] + " 2013-05-05 all < ~/.task/completed.data > ~/.task/completed.data.tmp && mv ~/.task/completed.data.tmp ~/.task/completed.data\n")
sys.exit(1)
# Date before which entries are to be filtered out.
filter_date = time.mktime(time.strptime(sys.argv[1], "%Y-%m-%d"))
# Filter (all|completed|deleted) tasks.
filter_status = sys.argv[2]
for line in sys.stdin:
# Get the current task's status
m = re.match('.*(?:\[| )status:"(completed|deleted)"(?:\]| )', line)
if m:
task_status = m.group(1)
else:
print "---"
sys.stderr.write("---\nERROR: Task has unknown status!\n")
sys.stderr.write(line)
if filter_status not in ["all", "completed"] and task_status == "completed"\
or filter_status not in ["all", "deleted"] and task_status == "deleted":
# Output current task unchanged as requested by user.
sys.stdout.write(line)
continue
m = re.match('.*(?:\[| )end:"([^"]+)"(?:\]| )', line)
if m:
if int(m.group(1)) > filter_date:
sys.stdout.write(line)
else:
sys.stderr.write("---\nERROR: Task has no \"end\" date!\n")
sys.stderr.write(line)
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment