2 from ctypes import c_int, c_char_p, c_void_p, c_uint64
3 from cnotmuch.globals import nmlib, STATUS, NotmuchError
5 from datetime import date
7 class Database(object):
8 """ Wrapper around a notmuch_database_t
10 Do note that as soon as we tear down this object, all derived queries,
11 threads, and messages will be freed as well.
18 """Class attribute of users default database"""
20 """notmuch_database_get_path (notmuch_database_t *database)"""
21 _get_path = nmlib.notmuch_database_get_path
22 _get_path.restype = c_char_p
24 """notmuch_database_open (const char *path, notmuch_database_mode_t mode)"""
25 _open = nmlib.notmuch_database_open
26 _open.restype = c_void_p
28 """ notmuch_database_find_message """
29 _find_message = nmlib.notmuch_database_find_message
30 _find_message.restype = c_void_p
32 """notmuch_database_get_all_tags (notmuch_database_t *database)"""
33 _get_all_tags = nmlib.notmuch_database_get_all_tags
34 _get_all_tags.restype = c_void_p
36 """ notmuch_database_create(const char *path):"""
37 _create = nmlib.notmuch_database_create
38 _create.restype = c_void_p
40 def __init__(self, path=None, create=False, status= MODE_READ_ONLY):
41 """ Open or create a notmuch database
43 If path is None, we will try to read a users notmuch configuration and
44 use his default database.
45 Throws a NotmuchError in case of failure.
49 # no path specified. use a user's default database
50 if Database._std_db_path is None:
51 #the following line throws a NotmuchError if it fails
52 Database._std_db_path = self._get_user_default_db()
53 path = Database._std_db_path
56 self.open(path, status)
58 self.create(path, status)
60 def create(self, path, status=MODE_READ_ONLY):
61 """ notmuch_database_create(const char *path)
63 :returns: Raises :exc:`notmuch.NotmuchError` in case
64 of any failure (after printing an error message on stderr).
66 res = Database._create(path, status)
70 message="Could not create the specified database")
73 def open(self, path, status= MODE_READ_ONLY):
74 """calls notmuch_database_open
76 :returns: Raises :exc:`notmuch.NotmuchError` in case
77 of any failure (after printing an error message on stderr).
79 res = Database._open(path, status)
83 message="Could not open the specified database")
87 """notmuch_database_get_path (notmuch_database_t *database); """
88 return Database._get_path(self._db)
90 def find_message(self, msgid):
91 """notmuch_database_find_message
92 :param msgid: The message id
95 :returns: Message() or None if no message is found or if an
96 out-of-memory situation occurs.
99 raise NotmuchError(STATUS.NOT_INITIALIZED)
100 msg_p = Database._find_message(self._db, msgid)
103 return Message(msg_p, self)
105 def get_all_tags(self):
106 """Return a Tags() object (list of all tags found in the database)
108 :returns: Tags() object or raises :exc:`NotmuchError` with
109 STATUS.NULL_POINTER on error
112 raise NotmuchError(STATUS.NOT_INITIALIZED)
114 tags_p = Database._get_all_tags (self._db)
116 raise NotmuchError(STATUS.NULL_POINTER)
117 return Tags(tags_p, self)
120 return "'Notmuch DB " + self.get_path() + "'"
123 """Close and free the notmuch database if needed"""
124 if self._db is not None:
125 logging.debug("Freeing the database now")
126 nmlib.notmuch_database_close(self._db)
128 def _get_user_default_db(self):
129 """ Reads a user's notmuch config and returns his db location
131 Throws a NotmuchError if it cannot find it"""
132 from ConfigParser import SafeConfigParser
134 config = SafeConfigParser()
135 config.read(os.path.expanduser('~/.notmuch-config'))
136 if not config.has_option('database','path'):
137 raise NotmuchError(message=
138 "No DB path specified and no user default found")
139 return config.get('database','path')
143 """Returns a pointer to the current notmuch_database_t or None"""
146 #------------------------------------------------------------------------------
148 """ Wrapper around a notmuch_query_t
150 Do note that as soon as we tear down this object, all derived
151 threads, and messages will be freed as well.
154 SORT_OLDEST_FIRST = 0
155 SORT_NEWEST_FIRST = 1
158 """notmuch_query_create"""
159 _create = nmlib.notmuch_query_create
160 _create.restype = c_void_p
162 """notmuch_query_search_messages"""
163 _search_messages = nmlib.notmuch_query_search_messages
164 _search_messages.restype = c_void_p
166 def __init__(self, db, querystr):
170 self.create(db, querystr)
172 def create(self, db, querystr):
173 """db is a Database() and querystr a string
175 raises NotmuchError STATUS.NOT_INITIALIZED if db is not inited and
176 STATUS.NULL_POINTER if the query creation failed (too little memory)
179 raise NotmuchError(STATUS.NOT_INITIALIZED)
180 # create reference to parent db to keep it alive
183 # create query, return None if too little mem available
184 query_p = Query._create(db.db_p, querystr)
186 NotmuchError(STATUS.NULL_POINTER)
187 self._query = query_p
189 def set_sort(self, sort):
190 """notmuch_query_set_sort
192 :param sort: one of Query.SORT_OLDEST_FIRST|SORT_NEWEST_FIRST|SORT_MESSAGE_ID
193 :returns: Nothing, but raises NotmuchError if query is not inited
195 if self._query is None:
196 raise NotmuchError(STATUS.NOT_INITIALIZED)
198 nmlib.notmuch_query_set_sort(self._query, sort)
200 def search_messages(self):
201 """notmuch_query_search_messages
202 Returns Messages() or a raises a NotmuchError()
204 if self._query is None:
205 raise NotmuchError(STATUS.NOT_INITIALIZED)
207 msgs_p = Query._search_messages(self._query)
210 NotmuchError(STATUS.NULL_POINTER)
212 return Messages(msgs_p,self)
216 """Close and free the Query"""
217 if self._query is not None:
218 logging.debug("Freeing the Query now")
219 nmlib.notmuch_query_destroy (self._query)
221 #------------------------------------------------------------------------------
223 """Wrapper around notmuch_tags_t"""
226 _get = nmlib.notmuch_tags_get
227 _get.restype = c_char_p
229 def __init__(self, tags_p, parent=None):
231 msg_p is a pointer to an notmuch_message_t Structure. If it is None,
232 we will raise an NotmuchError(STATUS.NULL_POINTER).
234 Is passed the parent these tags are derived from, and saves a
235 reference to it, so we can automatically delete the db object
236 once all derived objects are dead.
238 Tags() provides an iterator over all contained tags. However, you will
239 only be able to iterate over the Tags once, because the underlying C
240 function only allows iterating once.
241 #TODO: make the iterator work more than once and cache the tags in
245 NotmuchError(STATUS.NULL_POINTER)
248 #save reference to parent object so we keep it alive
249 self._parent = parent
250 logging.debug("Inited Tags derived from %s" %(repr(parent)))
253 """ Make Tags an iterator """
257 if self._tags is None:
258 raise NotmuchError(STATUS.NOT_INITIALIZED)
260 if not nmlib.notmuch_tags_valid(self._tags):
264 tag = Tags._get (self._tags)
265 nmlib.notmuch_tags_move_to_next(self._tags)
269 """str() of Tags() is a space separated list of tags
271 This iterates over the list of Tags and will therefore 'exhaust' Tags()
273 return " ".join(self)
276 """Close and free the notmuch tags"""
277 if self._tags is not None:
278 logging.debug("Freeing the Tags now")
279 nmlib.notmuch_tags_destroy (self._tags)
282 #------------------------------------------------------------------------------
283 class Messages(object):
284 """Wrapper around notmuch_messages_t"""
287 _get = nmlib.notmuch_messages_get
288 _get.restype = c_void_p
290 _collect_tags = nmlib.notmuch_messages_collect_tags
291 _collect_tags.restype = c_void_p
293 def __init__(self, msgs_p, parent=None):
295 msg_p is a pointer to an notmuch_messages_t Structure. If it is None,
296 we will raise an NotmuchError(STATUS.NULL_POINTER).
298 If passed the parent query this Messages() is derived from, it saves a
299 reference to it, so we can automatically delete the parent query object
300 once all derived objects are dead.
302 Messages() provides an iterator over all contained Message()s.
303 However, you will only be able to iterate over it once,
304 because the underlying C function only allows iterating once.
305 #TODO: make the iterator work more than once and cache the tags in
309 NotmuchError(STATUS.NULL_POINTER)
312 #store parent, so we keep them alive as long as self is alive
313 self._parent = parent
314 logging.debug("Inited Messages derived from %s" %(str(parent)))
316 def collect_tags(self):
317 """ return the Tags() belonging to the messages
319 Do note that collect_tags will iterate over the messages and
320 therefore will not allow further iterationsl
321 Raises NotmuchError(STATUS.NOT_INITIALIZED) if not inited
323 if self._msgs is None:
324 raise NotmuchError(STATUS.NOT_INITIALIZED)
326 # collect all tags (returns NULL on error)
327 tags_p = Messages._collect_tags (self._msgs)
328 #reset _msgs as we iterated over it and can do so only once
332 raise NotmuchError(STATUS.NULL_POINTER)
333 return Tags(tags_p, self)
336 """ Make Messages an iterator """
340 if self._msgs is None:
341 raise NotmuchError(STATUS.NOT_INITIALIZED)
343 if not nmlib.notmuch_messages_valid(self._msgs):
347 msg = Message(Messages._get (self._msgs), self)
348 nmlib.notmuch_messages_move_to_next(self._msgs)
352 """ Returns the number of contained messages
354 :note: As this iterates over the messages, we will not be able to
355 iterate over them again (as in retrieve them)!
357 if self._msgs is None:
358 raise NotmuchError(STATUS.NOT_INITIALIZED)
361 while nmlib.notmuch_messages_valid(self._msgs):
362 nmlib.notmuch_messages_move_to_next(self._msgs)
370 """Close and free the notmuch Messages"""
371 if self._msgs is not None:
372 logging.debug("Freeing the Messages now")
373 nmlib.notmuch_messages_destroy (self._msgs)
376 #------------------------------------------------------------------------------
377 class Message(object):
378 """Wrapper around notmuch_message_t"""
380 """notmuch_message_get_filename (notmuch_message_t *message)"""
381 _get_filename = nmlib.notmuch_message_get_filename
382 _get_filename.restype = c_char_p
383 """notmuch_message_get_message_id (notmuch_message_t *message)"""
384 _get_message_id = nmlib.notmuch_message_get_message_id
385 _get_message_id.restype = c_char_p
387 """notmuch_message_get_tags (notmuch_message_t *message)"""
388 _get_tags = nmlib.notmuch_message_get_tags
389 _get_tags.restype = c_void_p
391 _get_date = nmlib.notmuch_message_get_date
392 _get_date.restype = c_uint64
394 _get_header = nmlib.notmuch_message_get_header
395 _get_header.restype = c_char_p
397 def __init__(self, msg_p, parent=None):
399 msg_p is a pointer to an notmuch_message_t Structure. If it is None,
400 we will raise an NotmuchError(STATUS.NULL_POINTER).
402 Is a 'parent' object is passed which this message is derived from,
403 we save a reference to it, so we can automatically delete the parent
404 object once all derived objects are dead.
407 NotmuchError(STATUS.NULL_POINTER)
409 #keep reference to parent, so we keep it alive
410 self._parent = parent
411 logging.debug("Inited Message derived from %s" %(str(parent)))
414 def get_message_id(self):
415 """ return the msg id
417 Raises NotmuchError(STATUS.NOT_INITIALIZED) if not inited
419 if self._msg is None:
420 raise NotmuchError(STATUS.NOT_INITIALIZED)
421 return Message._get_message_id(self._msg)
424 """returns time_t of the message date
426 For the original textual representation of the Date header from the
427 message call notmuch_message_get_header() with a header value of
429 Raises NotmuchError(STATUS.NOT_INITIALIZED) if not inited
431 if self._msg is None:
432 raise NotmuchError(STATUS.NOT_INITIALIZED)
433 return Message._get_date(self._msg)
435 def get_header(self, header):
436 """ TODO document me"""
437 if self._msg is None:
438 raise NotmuchError(STATUS.NOT_INITIALIZED)
440 #Returns NULL if any error occurs.
441 header = Message._get_header (self._msg, header)
443 raise NotmuchError(STATUS.NULL_POINTER)
446 def get_filename(self):
447 """ return the msg filename
449 Raises NotmuchError(STATUS.NOT_INITIALIZED) if not inited
451 if self._msg is None:
452 raise NotmuchError(STATUS.NOT_INITIALIZED)
453 return Message._get_filename(self._msg)
456 """ return the msg tags
458 Raises NotmuchError(STATUS.NOT_INITIALIZED) if not inited
459 Raises NotmuchError(STATUS.NULL_POINTER) on error.
461 if self._msg is None:
462 raise NotmuchError(STATUS.NOT_INITIALIZED)
464 tags_p = Message._get_tags(self._msg)
466 raise NotmuchError(STATUS.NULL_POINTER)
467 return Tags(tags_p, self)
470 """A message() is represented by a 1-line summary"""
472 msg['from'] = self.get_header('from')
473 msg['tags'] = str(self.get_tags())
474 msg['date'] = date.fromtimestamp(self.get_date())
475 return "%(from)s (%(date)s) (%(tags)s)" % (msg)
477 def format_as_text(self):
478 """ Output like notmuch show """
482 """Close and free the notmuch Message"""
483 if self._msg is not None:
484 logging.debug("Freeing the Message now")
485 nmlib.notmuch_message_destroy (self._msg)