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