X-Git-Url: https://git.notmuchmail.org/git?a=blobdiff_plain;f=notmuch;h=2bb496f3132c08d29f5c8dac1b7a909b24f0325e;hb=61a547bd3e2760f1f835762f92c83cff2c1c71e5;hp=b08334d79adf0a20d56acc2c2fb082f4ac636944;hpb=9058e3d1b55ed35cda2df6426578435934af19de;p=notmuch diff --git a/notmuch b/notmuch index b08334d7..2bb496f3 100755 --- a/notmuch +++ b/notmuch @@ -1,10 +1,14 @@ #!/usr/bin/env python -"""This is a notmuch implementation in python. It's goal is to allow running the test suite on the cnotmuch python bindings. +"""This is a notmuch implementation in python. +It's goal is to allow running the test suite on the cnotmuch python bindings. This "binary" honors the NOTMUCH_CONFIG environmen variable for reading a user's -notmuch configuration (e.g. the database path) +notmuch configuration (e.g. the database path). -This code is licensed under the GNU GPL v3+.""" + (c) 2010 by Sebastian Spaeth + Jesse Rosenthal + This code is licensed under the GNU GPL v3+. +""" from __future__ import with_statement # This isn't required in Python 2.6 import sys, os, re, logging from subprocess import call @@ -286,29 +290,83 @@ if __name__ == '__main__': #------------------------------------- elif sys.argv[1] == 'search': db = Database() - if len(sys.argv) == 2: - #no further search term - querystr='' - else: - #mangle arguments wrapping terms with spaces in quotes - querystr = quote_query_line(sys.argv[2:]) + query_string = '' + sort_order="newest-first" + first_search_term = None + for (i, arg) in enumerate(sys.argv[1:]): + if arg.startswith('--sort='): + sort_order=arg.split("=")[1] + if not sort_order in ("oldest-first", "newest-first"): + raise Exception("unknown sort order") + elif not arg.startswith('--'): + #save the position of the first sys.argv that is a search term + first_search_term = i+1 + + if first_search_term: + #mangle arguments wrapping terms with spaces in quotes + querystr = quote_query_line(sys.argv[first_search_term:]) + + logging.debug("search "+querystr) - t = Query(db,querystr).search_threads() + qry = Query(db,querystr) + if sort_order == "oldest-first": + qry.set_sort(Query.SORT.OLDEST_FIRST) + else: + qry.set_sort(Query.SORT.NEWEST_FIRST) + t = qry.search_threads() + for thread in t: - print(str(thread)) + print(str(thread)) + #------------------------------------- elif sys.argv[1] == 'show': + entire_thread = False db = Database() - if len(sys.argv) == 2: - #no further search term - querystr='' - else: - #mangle arguments wrapping terms with spaces in quotes - querystr = quote_query_line(sys.argv[2:]) + out_format="text" + querystr='' + first_search_term = None + + #ugly homegrown option parsing + #TODO: use OptionParser + for (i, arg) in enumerate(sys.argv[1:]): + if arg == '--entire-thread': + entire_thread = True + elif arg.startswith("--format="): + out_format = arg.split("=")[1] + if out_format == 'json': + #for compatibility use --entire-thread for json + entire_thread = True + if not out_format in ("json", "text"): + raise Exception("unknown format") + elif not arg.startswith('--'): + #save the position of the first sys.argv that is a search term + first_search_term = i+1 + + if first_search_term: + #mangle arguments wrapping terms with spaces in quotes + querystr = quote_query_line(sys.argv[first_search_term:]) + logging.debug("show "+querystr) - m = Query(db,querystr).search_messages() - for msg in m: - print(msg.format_as_text()) + t = Query(db,querystr).search_threads() + + first_toplevel=True + if out_format.lower()=="json": + sys.stdout.write("[") + + for thrd in t: + msgs = thrd.get_toplevel_messages() + + if not first_toplevel: + if out_format.lower()=="json": + sys.stdout.write(", ") + + first_toplevel = False + + msgs.print_messages(out_format, 0, entire_thread) + + if out_format.lower() == "json": + sys.stdout.write("]") + sys.stdout.write("\n") #------------------------------------- elif sys.argv[1] == 'reply':