1 from ctypes import CDLL, c_char_p, c_int
2 from ctypes.util import find_library
4 #-----------------------------------------------------------------------------
5 #package-global instance of the notmuch library
7 nmlib = CDLL("libnotmuch.so.1")
9 raise ImportError("Could not find shared 'notmuch' library.")
11 #-----------------------------------------------------------------------------
13 """Provides ENUMS as "code=Enum(['a','b','c'])" where code.a=0 etc..."""
14 def __init__(self, names):
15 for number, name in enumerate(names):
16 setattr(self, name, number)
18 #-----------------------------------------------------------------------------
20 """Enum with a string representation of a notmuch_status_t value."""
22 _status2str = nmlib.notmuch_status_to_string
23 _status2str.restype = c_char_p
24 _status2str.argtypes = [c_int]
26 def __init__(self, statuslist):
27 """It is initialized with a list of strings that are available as
28 Status().string1 - Status().stringn attributes.
30 super(Status, self).__init__(statuslist)
33 def status2str(self, status):
34 """Get a string representation of a notmuch_status_t value."""
35 # define strings for custom error messages
36 if status == STATUS.NOT_INITIALIZED:
37 return "Operation on uninitialized object impossible."
38 return str(Status._status2str(status))
40 STATUS = Status(['SUCCESS',
46 'DUPLICATE_MESSAGE_ID',
49 'UNBALANCED_FREEZE_THAW',
53 class NotmuchError(Exception):
54 def __init__(self, status=None, message=None):
55 """Is initiated with a (notmuch.STATUS[,message=None])"""
56 super(NotmuchError, self).__init__(message, status)
59 if self.args[0] is not None: return self.args[0]
60 else: return STATUS.status2str(self.args[1])