]> git.notmuchmail.org Git - notmuch/blob - bindings/python/notmuch/globals.py
bindings/python: Bump bindings version to 0.6
[notmuch] / bindings / python / notmuch / globals.py
1 """
2 This file is part of notmuch.
3
4 Notmuch is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation, either version 3 of the License, or (at your
7 option) any later version.
8
9 Notmuch is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with notmuch.  If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
18 """
19
20 from ctypes import CDLL, c_char_p, c_int
21 from ctypes.util import find_library
22
23 #-----------------------------------------------------------------------------
24 #package-global instance of the notmuch library
25 try:
26     nmlib = CDLL("libnotmuch.so.1")
27 except:
28     raise ImportError("Could not find shared 'notmuch' library.")
29
30 #-----------------------------------------------------------------------------
31 class Enum(object):
32     """Provides ENUMS as "code=Enum(['a','b','c'])" where code.a=0 etc..."""
33     def __init__(self, names):
34         for number, name in enumerate(names):
35             setattr(self, name, number)
36
37 #-----------------------------------------------------------------------------
38 class Status(Enum):
39     """Enum with a string representation of a notmuch_status_t value."""
40     __name__="foo"
41     _status2str = nmlib.notmuch_status_to_string
42     _status2str.restype = c_char_p
43     _status2str.argtypes = [c_int]
44
45     def __init__(self, statuslist):
46         """It is initialized with a list of strings that are available as
47         Status().string1 - Status().stringn attributes.
48         """
49         super(Status, self).__init__(statuslist)
50
51     @classmethod
52     def status2str(self, status):
53         """Get a string representation of a notmuch_status_t value."""   
54         # define strings for custom error messages
55         if status == STATUS.NOT_INITIALIZED:
56           return "Operation on uninitialized object impossible."
57         return str(Status._status2str(status))
58
59 STATUS = Status(['SUCCESS',
60   'OUT_OF_MEMORY',
61   'READ_ONLY_DATABASE',
62   'XAPIAN_EXCEPTION',
63   'FILE_ERROR',
64   'FILE_NOT_EMAIL',
65   'DUPLICATE_MESSAGE_ID',
66   'NULL_POINTER',
67   'TAG_TOO_LONG',
68   'UNBALANCED_FREEZE_THAW',
69   'NOT_INITIALIZED'])
70
71
72 class NotmuchError(Exception):
73     def __init__(self, status=None, message=None):
74         """Is initiated with a (notmuch.STATUS[,message=None])"""
75         super(NotmuchError, self).__init__(message, status)
76
77     def __str__(self):
78         if self.args[0] is not None: return self.args[0]
79         else: return STATUS.status2str(self.args[1])
80