-
-
Save wbsch/a2f7264c6302918dfb30 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
# | |
# Adds the ability to add / modify tasks using a "blocks:" attribute, | |
# the opposite of "depends:". | |
# | |
# This script acts as an on-modify, on-add and on-launch hook at the same time. | |
# | |
### SETUP | |
# Save this file as | |
# ~/.task/hooks/on-modify.blocks_attr.py | |
# change to that directory: | |
# $ cd ~/.task/hooks | |
# make the script executable: | |
# $ chmod +x on-modify.blocks_attr.py | |
# then create symlinks to that file with the other hook names: | |
# $ ln -s on-modify.blocks_attr.py on-add.blocks_attr.py | |
# $ ln -s on-modify.blocks_attr.py on-launch.blocks_attr.py | |
# | |
# Now tell Taskwarrior about the "blocks" attribute: | |
# $ task config uda.blocks.type string | |
# $ task config uda.blocks.label Blocks | |
# | |
# And we're ready to go! | |
# | |
# bf@id:~$ t add a | |
# Created task 1. | |
# | |
# bf@id:~$ t add b | |
# Created task 2. | |
# | |
# bf@id:~$ t add c blocks:1 | |
# Created task 3. | |
# | |
# bf@id:~$ t 2 mod blocks:1 | |
# Modifying task 2 'b'. | |
# Modified 1 task. | |
# | |
# bf@id:~$ t | |
# [t next] | |
# | |
# ID Age Deps Description Urg | |
# 2 18s b 8 | |
# 3 8s c 8 | |
# 1 29s 2 3 a -5 | |
# | |
import json | |
import os | |
import subprocess | |
import sys | |
# Adjust this if "task" is not regular Taskwarrior. | |
TASK = 'task' | |
# Adjust path if needed. It is a temporary file managed by the hook. | |
SHIMFILE = "%s/.task/hooks/blocks_shim.txt" % os.getenv('HOME') | |
def on_launch(): | |
try: | |
with open(SHIMFILE, 'r') as f: | |
shim = f.readlines() | |
except IOError: | |
sys.exit(0) | |
while shim: | |
line = shim.pop(0).rstrip() | |
if not line: | |
continue | |
if subprocess.call(line.split()) != 0: | |
# Error handling by escalating to the user :) | |
print("%s ERROR: First line in %s is failing to run!" % (sys.argv[0], SHIMFILE)) | |
print("Feel free to delete the file, or edit its contents to resolve the problem.") | |
shim = [line + '\n'] + shim | |
break | |
if shim: | |
with open(SHIMFILE, 'w') as f: | |
f.write(''.join(shim)) | |
sys.exit(1) | |
else: | |
os.remove(SHIMFILE) | |
def handle_blocks_attribute(new): | |
if 'blocks' in new: | |
with open(SHIMFILE, "a") as f: | |
# This needs error handling. If a later hook aborts, the UUID is invalid. | |
f.write("%s rc.hooks=off rc.confirmation=no rc.bulk=10000 rc.verbose=nothing %s mod dep:%s\n" % (TASK, new['blocks'], new['uuid'])) | |
del new['blocks'] | |
print(json.dumps(new)) | |
old = sys.stdin.readline() | |
new = sys.stdin.readline() | |
if not old: | |
on_launch() | |
elif not new: | |
handle_blocks_attribute(json.loads(old)) | |
else: | |
handle_blocks_attribute(json.loads(new)) |
note: The gist works with both python (aka python3) & legacy python (aka python2). That's nice!
Thanks @wbsch for this great script.
I'm using Taskwarrior on several computers, including on Ubuntu on top of the Windows Linux Subsystem (WSL).
I'd like to point out for future users that you may need to specify the #!/usr/bin/env python
shebang as #!/usr/bin/env python3
, depending on your Linux distribution and version of python available.
I've made the following addition to allow the syntax to support unblock by prefixing the parent id with a '-' ;
# This needs error handling. If a later hook aborts, the UUID is invalid.
if not '-' in new['blocks'] :
f.write("%s rc.hooks=off rc.confirmation=no rc.bulk=10000 rc.verbose=nothing %s mod dep:%s\n" % (TASK, new['blocks'], new['uuid']))
else :
f.write("%s rc.hooks=off rc.confirmation=no rc.bulk=10000 rc.verbose=nothing %s mod dep:%s\n" % (TASK, new['blocks'][1:], '-'+new['uuid']))
There are 2 issues (with 2.5.1):
- There is no way to remove a block:
task 2 modify blocks:
does not remove the dependencies - Re-running
task 2 modify blocks:1
saysModified 1 task
when nothing was modified
task undo
doesn't work for me. I don't have my task data.location at default but I changed the line where it is asked to do so. Basic functionality works (meaning I can add tasks with blocks: attribute) successfully.
The error it gives:
Hook Error: Expected 1 JSON task(s), found 0, in hook script: on-modify.blocks_attr.py
Weird to me is that it does not fail at removing the dependency but the blocking task. Here's the sequence:
create task a
create task b
create task c with blocks:1 and it works
task undo and it deletes the dependency of a on c successfully
task undo again but it fails to remove task c
Heres all the output:
The last modification was made 2022-03-11
Prior Values Current Values
description a a
entry 2022-03-11 2022-03-11
modified 2022-03-11 2022-03-11
status pending pending
uuid fc90d4a0-04c5-4c25-a22c-9dc68f77be99 fc90d4a0-04c5-4c25-a22c-9dc68f77be99
dep_66b26bbf-5406-4656-8159-0aca20db9da8 x
depends 66b26bbf-5406-4656-8159-0aca20db9da8
The undo command is not reversible. Are you sure you want to revert to the previous state? (yes/no) yes
Modified task reverted.
[username@hostname ~/.local/share/task/hooks]$ tu
The last modification was made 2022-03-11
Prior Values Current Values
description c
entry 2022-03-11
modified 2022-03-11
status pending
uuid 66b26bbf-5406-4656-8159-0aca20db9da8
The undo command is not reversible. Are you sure you want to revert to the previous state? (yes/no) yes
Hook Error: Expected 1 JSON task(s), found 0, in hook script: on-modify.blocks_attr.py
Great script!
I've made one change in my fork: I changed the declaration of SHIMFILE
to use the hook file's own location to determine the directory that the shim file should reside in. Please feel free to look at my fork (https://gist.github.com/rjray/f95d0919447afa956a1753d7ccec87b6)... I've tested the hook in my own taskwarrior repo and it works fine.
note: hooks with helpful metadata (like this one) might mention that taskwarrior v2.3.x+ is required for hooks functionality.