Skip to content
Browse files

init

  • Loading branch information...
0 parents commit a63fad6af1364984d6e29fa87ecff366e43fa734 @honza committed Sep 10, 2012
Showing with 126 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +25 −0 README.rst
  3. +79 −0 mutt-notmuch-py.py
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Honza Pokorny
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 README.rst
@@ -0,0 +1,25 @@
+mutt-notmuch-py
+===============
+
+This is a Gmail-only version of the original mutt-notmuch script.
+
+It will interactively ask you for a search query and then symlink the matching
+messages to ``$HOME/.cache/mutt_results``.
+
+Add this to your muttrc.
+
+::
+ macro index / "<enter-command>unset wait_key<enter><shell-escape>mutt-notmuch-py<enter><change-folder-readonly>~/.cache/mutt_results<enter>" \
+ "search mail (using notmuch)"
+
+This script overrides the ``$HOME/.cache/mutt_results`` each time you run a
+query.
+
+Install this by adding this file somewhere on your PATH.
+
+Only tested on OSX Lion.
+
+License
+-------
+
+BSD, short and sweet
79 mutt-notmuch-py.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+"""
+mutt-notmuch-py
+
+This is a Gmail-only version of the original mutt-notmuch script.
+
+It will interactively ask you for a search query and then symlink the matching
+messages to $HOME/.cache/mutt_results.
+
+Add this to your muttrc.
+
+macro index / "<enter-command>unset wait_key<enter><shell-escape>mutt-notmuch-py<enter><change-folder-readonly>~/.cache/mutt_results<enter>" \
+ "search mail (using notmuch)"
+
+This script overrides the $HOME/.cache/mutt_results each time you run a query.
+
+Install this by adding this file somewhere on your PATH.
+
+Only tested on OSX Lion.
+
+(c) 2012 - Honza Pokorny
+Licensed under BSD
+"""
+import hashlib
+from commands import getoutput
+from mailbox import Maildir
+
+
+def digest(filename):
+ with open(filename) as f:
+ return hashlib.sha1(f.read()).hexdigest()
+
+
+def pick_all_mail(messages):
+ for m in messages:
+ if 'All Mail' in m:
+ return m
+
+
+def empty_dir(directory):
+ box = Maildir(directory)
+ box.clear()
+
+
+def command(cmd):
+ return getoutput(cmd)
+
+
+def main(dest_box):
+
+ query = raw_input('Query: ')
+
+ empty_dir(dest_box)
+
+ files = command('notmuch search --output=files %s' % query).split('\n')
+ files = [f for f in files if f != '']
+
+ data = {}
+ messages = []
+
+ for f in files:
+ sha = digest(f)
+ if sha not in data.keys():
+ data[sha] = [f]
+ else:
+ data[sha].append(f)
+
+ for sha in data.keys():
+ if len(data[sha]) > 1:
+ messages.append(pick_all_mail(data[sha]))
+ else:
+ messages.append(data[sha][0])
+
+ for m in messages:
+ command('ln -s "%s" %s/cur/' % (m, dest_box))
+
+
+if __name__ == '__main__':
+ main('~/.cache/mutt_results')

0 comments on commit a63fad6

Please sign in to comment.
Something went wrong with that request. Please try again.