Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alkim0 committed Aug 30, 2020
0 parents commit c0a9b96
Show file tree
Hide file tree
Showing 16 changed files with 656 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Albert Kim

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# `task-attach`
`task-attach` is a set of utility scripts to manage attachments in TaskWarrior. Currently, TaskWarrior has no built-in support for attaching external files or resources to tasks (via annotations), and `task-attach` hopes to address this problem. `task-attach` is inspired the by the [`taskopen`](https://github.com/ValiValpas/taskopen) script, but has the following additional features:
- Support for attachments other than files (i.e., urls and emails)
- A `mutt2task` script which helps you create tasks from emails
- A `open-via-mutt` script which opens mutt and jumps to the email corresponding to the given message-id.

## Installation
To install `task-attach`, simply run:
```
pip install task-attach
```
This installs the scripts:
- [`task-attach-add`](#task-attach-add)
- [`task-attach-new`](#task-attach-new)
- [`task-attach-open`](#task-attach-open)
- [`open-via-mutt`](#open-via-mutt)
- [`mutt2task`](#mutt2task)


## Configuration
Upon the first run of any of the `task-attach-*` scripts, a config file will be created at `$XDG_CONFIG_HOME/task-attach/config.yaml`. For specific configuration options, please see the generated file.


## `task-attach-add`
This script attaches an already existing resource to a task. The basic syntax of the command is as follows:
```
task-attach-add [-t {file,mail,url}] <id-or-filter> <spec> [<comment> ...]
```
For example if I want to attach a grocery list to a "Go grocery shopping" task:
```
$ task add Go grocery shopping
Created task 1.
$ task-attach-add 1 ~/grocery-list The grocery list.
$ task info 1
Name Value
ID 1
Description Go grocery shopping
2020-08-30 12:57:45 The grocery list. file:/home/$user/grocery-list
```
Or if I want to associate a URL with a task:
```
$ task add Remember to star!
Created task 2.
$ task-attach-add 2 github.com/alkim0/task-attach github link
$ task info 2
Name Value
ID 2
Description Remember to star!
2020-08-30 13:04:56 github link url:https://github.com/alkim0/task-attach
```
Similarly, the resource specification can be a message-id (with the enclosing "<>") for an email.

Note that `task-attach-add` is meant to work only for existing resources, so if the given resource is a file, the path must exist.

The script tries to guess the best type for the given resource specification, but the `-t` flag can be specified to make the resource type explicit.


## `task-attach-new`
Sometimes, I just want to create a new notes file to associate with a task without having to think about it. That is what `task-attach-new` is for. The syntax is:
```
task-attach-new [-e EDITOR] [-t ext] <id-or-filter> [<comment> ...]
```
This creates a new file in the `~/tasknotes` directory ([configurable](#configuration)) with a randomly generated UUID and opens it up with the specified editor. After editing, saving, and quitting the file, `task-attach-new` will add the newly generated file as an attachment to the specified task. By default, `$EDITOR` is used for the editor. The default extension for the generated file is `txt`, but any other extension may be specified.


## `task-attach-open`
Opens a resource for the specified task. Syntax is:
```
task-attach-open [-x cmd] <id-or-filter>
```
If a task has more than one attachment, you will be shown an interactive menu to pick from.

By default, `$EDITOR` is used to open any file resource, and `xdg-open` is used to open any url. For email resources, the provided `open-via-mutt` is the default ([configurable](#configuration)).


## `open-via-mutt`
To supplment `task-attach-open` for emails, the `open-via-mutt` script is provided. `open-via-mutt` attempts to open `mutt` (or `neomutt`) and jump to/open the email specified by the given message-id by using mutt's `limit` function. By default, it attempts to find the email in the default inbox, so it may fail if the message is in another folder. However, if emails have been indexed by `notmuch`, then `open-via-mutt` will use the message's path and the `mailboxes ...` clause of the muttrc file to try to the appropriate folder.


## `mutt2task`
This script is meant to be called from within mutt to quickly create a task based on an email. `mutt2task` expects to read the message from standard input, and interactively asks for the user for a task description. When the task is created and an email resource pointing to the message is automatically attached to newly created task.


## Putting it all together
For my setup, I have the following aliases:
```
task config alias.attach "exec $(which task-attach-add)"
task config alias.note "exec $(which task-attach-new)"
task config alias.open "exec $(which task-attach-open)"
```

In my muttrc, I have:
```
macro index,pager i "<pipe-message>mutt2task<enter>"
```
49 changes: 49 additions & 0 deletions bin/mutt2task
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python
"""
Creates a new task with the given message as annotation attachment.
TODO: Have an option to add to existing task
"""

import argparse
import email
import sys

from tasklib import TaskWarrior, Task

from task_attach.add import add as add_annotation
from task_attach.config import Config
from task_attach.utils import MAIL


def get_args():
parser = argparse.ArgumentParser(
description='Creates a task with given message and annotates the task with message id'
)
return parser.parse_args()


def get_task_description(message):
default = 'Reply to "{}" from {}'.format(
message['subject'], message['from'])
description = input('Enter task description or press enter for default: ')
return description if description else default


def main():
try:
args = get_args()
config = Config()
message = email.message_from_file(sys.stdin)
sys.stdin = open('/dev/tty')
description = get_task_description(message)
tw = TaskWarrior(config.task_home)
task = Task(tw, description=description)
task.save()
add_annotation(str(task['id']), MAIL, message['message-id'], [])
except Exception as e:
print(e)
sys.exit(2)

if __name__ == "__main__":
main()
105 changes: 105 additions & 0 deletions bin/open-via-mutt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/env python
"""
Open an email message in mutt/neomutt given the message-id. Uses notmuch if available. If notmuch is not available, tries to open in default folder.
"""

import argparse
import os
import subprocess
import sys


def get_args():
parser = argparse.ArgumentParser(
description="Opens mutt and points to given email"
)
parser.add_argument('-m', '--mutt', default='mutt', choices=('mutt', 'neomutt'))
parser.add_argument('message_id', help='Should include the enclosing <>')
return parser.parse_args()



def get_mailboxes():
POSS_MUTTRC_PATHS = (
os.path.expandvars('$XDG_CONFIG_HOME/neomutt/neomuttrc'),
os.path.expandvars('$XDG_CONFIG_HOME/neomutt/muttrc'),
os.path.expandvars('$XDG_CONFIG_HOME/mutt/neomuttrc'),
os.path.expandvars('$XDG_CONFIG_HOME/mutt/muttrc'),
os.path.expanduser('~/.neomutt/neomuttrc'),
os.path.expanduser('~/.neomutt/muttrc'),
os.path.expanduser('~/.mutt/neomuttrc'),
os.path.expanduser('~/.mutt/muttrc'),
os.path.expanduser('~/.neomuttrc'),
os.path.expanduser('~/.muttrc'),
)

for path in POSS_MUTTRC_PATHS:
if os.path.exists(path):
with open(path) as f:
lines = [line.strip() for line in f.readlines()]
lines = [l for l in lines if l.startswith('mailboxes ')]
if len(lines) != 1:
raise Exception("Could not parse muttrc")
line = lines[0]
mailboxes = line[len('mailboxes '):].strip().split()
return mailboxes


raise Exception("Could not open muttrc")

def levenshtein(s1, s2):
if len(s1) < len(s2):
return levenshtein(s2, s1)

# len(s1) >= len(s2)
if len(s2) == 0:
return len(s1)

previous_row = range(len(s2) + 1)
for i, c1 in enumerate(s1):
current_row = [i + 1]
for j, c2 in enumerate(s2):
insertions = previous_row[j + 1] + 1 # j+1 instead of j since previous_row and current_row are one character longer
deletions = current_row[j] + 1 # than s2
substitutions = previous_row[j] + (c1 != c2)
current_row.append(min(insertions, deletions, substitutions))
previous_row = current_row

return previous_row[-1]


def try_get_folder(args):
"""
Returns None if not possible (because notmuch is not installed or muttrc could not be parsed).
"""
try:
import notmuch
db = notmuch.Database()
message = db.find_message(args.message_id[1:-1])

mailboxes = get_mailboxes()
msg_path = message.get_filename()
folder = sorted(mailboxes, key=lambda mbox: levenshtein(mbox, msg_path))[0]
return folder

except:
return None


def main():
args = get_args()
folder = try_get_folder(args)
subprocess.run(
'{} {}{}'.format(
args.mutt,
'-f {} '.format(folder) if folder else '',
"-e 'push \"<limit>~i {}<enter><enter>\"'".format(args.message_id)),
shell=True, check=True
)

if __name__ == "__main__":
try:
main()
except Exception as e:
print(e)
sys.exit(2)
6 changes: 6 additions & 0 deletions bin/task-attach-add
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

from task_attach.add import main

if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions bin/task-attach-new
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

from task_attach.new import main

if __name__ == "__main__":
main()
6 changes: 6 additions & 0 deletions bin/task-attach-open
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env python

from task_attach.open import main

if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python

from setuptools import setup

setup(
name="task-attach",
version="0.1.0",
packages=["task_attach"],
description="Scripts to manage attachments in taskwarrior",
author="Albert Kim",
author_email="[email protected]",
install_requires=["pyyaml", "tasklib"],
scripts=[
"bin/task-attach-new",
"bin/task-attach-open",
"bin/task-attach-add",
"bin/mutt2task",
"bin/open-via-mutt",
],
)
1 change: 1 addition & 0 deletions task_attach/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#
69 changes: 69 additions & 0 deletions task_attach/add.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env python
"""
Script to add annotations to task.
"""

import argparse
import re
import os
import sys

from tasklib import TaskWarrior

from .config import Config
from .utils import select_task, FILE, MAIL, URL


def get_args():
parser = argparse.ArgumentParser(description="Adding a new attachment to a task")
parser.add_argument("-t", "--type", choices=(FILE, MAIL, URL))
parser.add_argument("id_or_filter")
parser.add_argument("spec")
parser.add_argument("comments", nargs="*")
return parser.parse_args()


def determine_type(type_, spec):
if type_:
return type_
else:
file_path = os.path.expandvars(os.path.expanduser(spec))
if os.path.exists(file_path):
return FILE

if spec.startswith("<") and spec.endswith(">"):
return MAIL

if re.match(r"https?://", spec):
return URL

if "@" in spec:
return MAIL

if "." in spec:
return URL

raise Exception("Could not determine type of spec")


def add(id_or_filter, type_, spec, comments):
config = Config()
tw = TaskWarrior(config.task_home)
task = select_task(tw, id_or_filter)

type_ = determine_type(type_, spec)
if type_ == URL and not re.match(r"https?://", spec):
spec = "http://" + spec
annotation = "{}:{}".format(type_, spec)
if comments:
annotation = " ".join(comments) + " " + annotation
task.add_annotation(annotation)


def main():
try:
args = get_args()
add(args.id_or_filter, args.type, args.spec, args.comments)
except Exception as e:
print(e)
sys.exit(2)
Loading

0 comments on commit c0a9b96

Please sign in to comment.