]> git.notmuchmail.org Git - notmuch/blob - cnotmuch/globals.py
Extent pypi documentation. Set version to 0.2.2
[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 try:
7     nmlib = CDLL("libnotmuch.so.1")
8 except:
9     raise ImportError("Could not find shared 'notmuch' library.")
10
11 #-----------------------------------------------------------------------------
12 class Enum(object):
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)
17
18 #-----------------------------------------------------------------------------
19 class Status(Enum):
20     """Enum with a string representation of a notmuch_status_t value."""
21     __name__="foo"
22     _status2str = nmlib.notmuch_status_to_string
23     _status2str.restype = c_char_p
24     _status2str.argtypes = [c_int]
25
26     def __init__(self, statuslist):
27         """It is initialized with a list of strings that are available as
28         Status().string1 - Status().stringn attributes.
29         """
30         super(Status, self).__init__(statuslist)
31
32     @classmethod
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))
39
40 STATUS = Status(['SUCCESS',
41   'OUT_OF_MEMORY',
42   'READ_ONLY_DATABASE',
43   'XAPIAN_EXCEPTION',
44   'FILE_ERROR',
45   'FILE_NOT_EMAIL',
46   'DUPLICATE_MESSAGE_ID',
47   'NULL_POINTER',
48   'TAG_TOO_LONG',
49   'UNBALANCED_FREEZE_THAW',
50   'NOT_INITIALIZED'])
51
52
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)
57
58     def __str__(self):
59         if self.args[0] is not None: return self.args[0]
60         else: return STATUS.status2str(self.args[1])
61