]> git.notmuchmail.org Git - notmuch/blob - notmuch
notmuch: fix to use new Query.SORT enum
[notmuch] / notmuch
1 #!/usr/bin/env python
2 """This is a notmuch implementation in python. It's goal is to allow running the test suite on the cnotmuch python bindings."""
3 import sys, os, re, logging
4 from cnotmuch.notmuch import Database, Query
5 PREFIX=re.compile('(\w+):(.*$)')
6 #TODO Handle variable: NOTMUCH-CONFIG
7
8 #-------------------------------------------------------------------------
9 HELPTEXT="""The notmuch mail system.
10
11 Usage: notmuch <command> [args...]
12
13 Where <command> and [args...] are as follows:
14
15         setup   Interactively setup notmuch for first use.
16
17         new     [--verbose]
18
19                 Find and import new messages to the notmuch database.
20
21         search  [options...] <search-terms> [...]
22
23                 Search for messages matching the given search terms.
24
25         show    <search-terms> [...]
26
27                 Show all messages matching the search terms.
28
29         count   <search-terms> [...]
30
31                 Count messages matching the search terms.
32
33         reply   [options...] <search-terms> [...]
34
35                 Construct a reply template for a set of messages.
36
37         tag     +<tag>|-<tag> [...] [--] <search-terms> [...]
38
39                 Add/remove tags for all messages matching the search terms.
40
41         dump    [<filename>]
42
43                 Create a plain-text dump of the tags for each message.
44
45         restore <filename>
46
47                 Restore the tags from the given dump file (see 'dump').
48
49         search-tags     [<search-terms> [...] ]
50
51                 List all tags found in the database or matching messages.
52
53         help    [<command>]
54
55                 This message, or more detailed help for the named command.
56
57 Use "notmuch help <command>" for more details on each command.
58 And "notmuch help search-terms" for the common search-terms syntax.
59 """
60 #-------------------------------------------------------------------------
61 #TODO: replace the dynamic pieces
62 USAGE="""Notmuch is configured and appears to have a database. Excellent!
63
64 At this point you can start exploring the functionality of notmuch by
65 using commands such as:
66
67         notmuch search tag:inbox
68
69         notmuch search to:"Sebastian Spaeth"
70
71         notmuch search from:"Sebastian@SSpaeth.de"
72
73         notmuch search subject:"my favorite things"
74
75 See "notmuch help search" for more details.
76
77 You can also use "notmuch show" with any of the thread IDs resulting
78 from a search. Finally, you may want to explore using a more sophisticated
79 interface to notmuch such as the emacs interface implemented in notmuch.el
80 or any other interface described at http://notmuchmail.org
81
82 And don't forget to run "notmuch new" whenever new mail arrives.
83
84 Have fun, and may your inbox never have much mail.
85 """
86 #-------------------------------------------------------------------------
87 def quote_query_line(argv):
88    #mangle arguments wrapping terms with spaces in quotes
89    for i in xrange(0,len(argv)):
90       if argv[i].find(' ') >= 0:
91          #if we use prefix:termWithSpaces, put quotes around term
92          m = PREFIX.match(argv[i])
93          if m:
94             argv[i] = '%s:"%s"' % (m.group(1), m.group(2))
95          else:
96             argv[i] = '"'+argv[i]+'"'
97    return ' '.join(argv)
98
99 if __name__ == '__main__':
100
101    # Handle command line options
102    # No option 
103    if len(sys.argv) == 1:
104       print USAGE
105
106    elif sys.argv[1] == 'setup':
107        """ Interactively setup notmuch for first use. """
108        print "Not implemented."
109
110    elif sys.argv[1] == 'help':
111        if len(sys.argv) == 2: print HELPTEXT
112        else: print "Not implemented"
113
114    elif sys.argv[1] == 'show':
115       db = Database()
116       if len(sys.argv) == 2:
117          #no further search term
118          querystr=''
119       else:
120          #mangle arguments wrapping terms with spaces in quotes
121          querystr = quote_query_line(sys.argv[2:])
122       logging.debug("show "+querystr)
123       m = Query(db,querystr).search_messages()
124       for msg in m:
125          print(msg.format_as_text())
126
127    elif sys.argv[1] == 'new':
128        #TODO: handle --verbose
129        print "Not implemented."
130
131    elif sys.argv[1] == 'count':
132       db = Database()
133       if len(sys.argv) == 2:
134          #no further search term
135          querystr=''
136       else:
137          #mangle arguments wrapping terms with spaces in quotes
138          querystr = quote_query_line(sys.argv[2:])
139       logging.debug("count "+querystr)
140       print(len(Query(db,querystr).search_messages()))
141
142    elif sys.argv[1] == 'search-tags':
143       if len(sys.argv) == 2:
144          #no further search term
145          print("\n".join(Database().get_all_tags()))
146       else:
147          #mangle arguments wrapping terms with spaces in quotes
148          querystr = quote_query_line(sys.argv[2:])
149          logging.debug("search-term "+querystr)
150          db = Database()
151          m  = Query(db,querystr).search_messages()
152          print("\n".join([t for t in m.collect_tags()]))
153
154    elif sys.argv[1] == 'dump':
155       #TODO: implement "dump <filename>"
156       db = Database()
157       q = Query(db,'')
158       q.set_sort(Query.SORT.MESSAGE_ID)
159       m = q.search_messages()
160       for msg in m:
161          print("%s (%s)" % (msg.get_message_id(), msg.get_tags()))
162
163    else:
164        # unknown command
165        print "Error: Unknown command '%s' (see \"notmuch help\")" % sys.argv[1]
166
167
168    #TODO: implement
169    """
170 search  [options...] <search-terms> [...]
171 show    <search-terms> [...]
172 reply   [options...] <search-terms> [...]
173 tag     +<tag>|-<tag> [...] [--] <search-terms> [...]
174 restore <filename>
175    """