X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch;h=9f63e2107f84affcb1c9fc47f6a7cd9f3106cb15;hp=6111a7ecfd215058f19d66733df67d676ffce256;hb=3d6590be0207d7f0babfda3d26cf9b7c76451fab;hpb=46d06838ae171b744c443b1ec812cebe82996cfa diff --git a/notmuch b/notmuch index 6111a7ec..9f63e210 100755 --- a/notmuch +++ b/notmuch @@ -288,38 +288,100 @@ if __name__ == '__main__': if len(sys.argv) == 2: print HELPTEXT else: print "Not implemented" #------------------------------------- - elif sys.argv[1] == 'search': + elif sys.argv[1] == 'part': db = Database() - if len(sys.argv) == 2: - #no further search term - querystr='' + query_string = '' + part_num=0 + first_search_term = None + for (i, arg) in enumerate(sys.argv[1:]): + if arg.startswith('--part='): + part_num_str=arg.split("=")[1] + try: + part_num = int(part_num_str) + except ValueError: + # just emulating behavior + sys.exit() + 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("part "+querystr) + qry = Query(db,querystr) + msgs = qry.search_messages() + msg_list = [] + for m in msgs: + msg_list.append(m) + + if len(msg_list) == 0: + sys.exit() + elif len(msg_list) > 1: + raise Exception("search term did not match precisely one message") else: - #mangle arguments wrapping terms with spaces in quotes - querystr = quote_query_line(sys.argv[2:]) + msg = msg_list[0] + print(msg.get_part(part_num)) + #------------------------------------- + elif sys.argv[1] == 'search': + db = Database() + 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() out_format="text" - if len(sys.argv) == 2: - #no further search term - querystr='' - elif sys.argv[2].startswith("--format="): - out_format = sys.argv[2].split("=")[1].strip() - - if not out_format in ("json", "text"): - raise Exception("unknown format") - - if len(sys.argv) == 3: - querystr = '' - else: - querystr = quote_query_line(sys.argv[3:]) - else: - #mangle arguments wrapping terms with spaces in quotes - querystr = quote_query_line(sys.argv[2:]) + 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) t = Query(db,querystr).search_threads() @@ -332,12 +394,12 @@ if __name__ == '__main__': msgs = thrd.get_toplevel_messages() if not first_toplevel: - if format.lower()=="json": + if out_format.lower()=="json": sys.stdout.write(", ") first_toplevel = False - msgs.show_messages(out_format, 0, True) + msgs.print_messages(out_format, 0, entire_thread) if out_format.lower() == "json": sys.stdout.write("]")