dotfiles

Personal dotfiles
git clone https://git.bracken.jp/dotfiles.git
Log | Files | Refs | LICENSE

mutt-notmuch-py (2475B)


      1 #!/usr/bin/env python3
      2 """
      3 mutt-notmuch-py
      4 
      5 This is a Gmail-only version of the original mutt-notmuch script.
      6 
      7 It will interactively ask you for a search query and then symlink the matching
      8 messages to $HOME/.cache/mutt_results.
      9 
     10 Add this to your muttrc.
     11 
     12 macro index / "<enter-command>unset wait_key<enter><shell-escape>mutt-notmuch-py<enter><change-folder-readonly>~/.cache/mutt_results<enter>" \
     13           "search mail (using notmuch)"
     14 
     15 This script overrides the $HOME/.cache/mutt_results each time you run a query.
     16 
     17 Install this by adding this file somewhere on your PATH.
     18 
     19 Only tested on OSX Lion.
     20 
     21 (c) 2012 - Honza Pokorny
     22 Licensed under BSD
     23 """
     24 import hashlib, subprocess, sys
     25 from mailbox import Maildir
     26 from optparse import OptionParser
     27 
     28 
     29 def digest(filename):
     30     with open(filename, 'rb') as f:
     31         return hashlib.sha1(f.read()).hexdigest()
     32 
     33 
     34 def pick_all_mail(messages):
     35     for m in messages:
     36         if 'All Mail' in m:
     37             return m
     38 
     39 
     40 def empty_dir(directory):
     41     box = Maildir(directory)
     42     box.clear()
     43 
     44 
     45 def command(cmd):
     46     return subprocess.check_output(cmd, shell=True)
     47 
     48 
     49 def main(dest_box):
     50     query = input('Query: ')
     51 
     52     command('mkdir -p %s' % dest_box)
     53     command('mkdir -p %s/cur' % dest_box)
     54     command('mkdir -p %s/new' % dest_box)
     55 
     56     empty_dir(dest_box)
     57 
     58     output = command('notmuch search --output=files %s' % query)
     59     files = output.decode('utf-8').split('\n')
     60     files = filter(None, files)
     61 
     62     data = {}
     63     messages = []
     64 
     65     for f in files:
     66         sha = digest(f)
     67         if sha not in data.keys():
     68             data[sha] = [f]
     69         else:
     70             data[sha].append(f)
     71 
     72     for sha in data.keys():
     73         if is_gmail and len(data[sha]) > 1:
     74             messages.append(pick_all_mail(data[sha]))
     75         else:
     76             messages.append(data[sha][0])
     77 
     78     for m in messages:
     79         command('ln -s "%s" %s/cur/' % (m, dest_box))
     80 
     81 
     82 if __name__ == '__main__':
     83     global is_gmail
     84 
     85     p = OptionParser("usage: %prog [OPTIONS] [RESULTDIR]")
     86     p.add_option('-g', '--gmail', dest='gmail',
     87                  action='store_true', default=True,
     88                  help='gmail-specific behavior')
     89     p.add_option('-G', '--not-gmail', dest='gmail',
     90                  action='store_false',
     91                  help='gmail-specific behavior')
     92     (options, args) = p.parse_args()
     93 
     94     is_gmail = options.gmail
     95 
     96     if args:
     97         dest = args[0]
     98     else:
     99         dest = '~/.cache/mutt_results'
    100 
    101     main(dest.rstrip('/'))