]> git.notmuchmail.org Git - notmuch/blob - cnotmuch/globals.py
553670a49c5c1460a1c77c24ff3b144e5cf2a05a
[notmuch] / cnotmuch / globals.py
1 from ctypes import CDLL, c_char_p, c_int
2 #package-global instance of the notmuch library
3 #TODO: lazy load this on first access?
4 nmlib = CDLL('/usr/local/lib/libnotmuch.so')
5
6 class STATUS(object):
7   SUCCESS = 0
8   OUT_OF_MEMORY = 1
9   READ_ONLY_DATABASE = 2
10   XAPIAN_EXCEPTION = 3
11   FILE_ERROR = 4
12   FILE_NOT_EMAIL = 5
13   DUPLICATE_MESSAGE_ID = 6
14   NULL_POINTER = 7
15   TAG_TOO_LONG = 8
16   UNBALANCED_FREEZE_THAW = 9
17   NOT_INITIALIZED = 10
18
19   """Get a string representation of a notmuch_status_t value."""
20   status2str = nmlib.notmuch_status_to_string
21   status2str.restype = c_char_p
22   status2str.argtypes = [c_int]
23
24   def __init__(self, status):
25       self._status = status
26
27   def __str__(self):
28       """Get a string representation of a notmuch_status_t value."""   
29       # define strings for custom error messages
30       if self._status == STATUS.NOT_INITIALIZED:
31         return "Operation on uninitialized DB/MSG/THREAD impossible."
32       return str(STATUS.status2str(self._status))
33
34 class NotmuchError(Exception):
35     def __init__(self, status=None, message=None):
36         """Is initiated with a (notmuch.STATUS[,message=None])"""
37         super(NotmuchError, self).__init__(message, status)
38
39     def __str__(self):
40         if self.args[0] is not None: return self.args[0]
41         else: return str(STATUS(self.args[1]))