2 This file is part of notmuch.
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.
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
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/>.
17 Copyright 2010 Sebastian Spaeth <Sebastian@SSpaeth.de>'
20 from ctypes import CDLL, c_char_p, c_int, Structure, POINTER
21 from ctypes.util import find_library
23 #-----------------------------------------------------------------------------
24 #package-global instance of the notmuch library
26 nmlib = CDLL("libnotmuch.so.2")
28 raise ImportError("Could not find shared 'notmuch' library.")
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)
39 """Enum with a string representation of a notmuch_status_t value."""
40 _status2str = nmlib.notmuch_status_to_string
41 _status2str.restype = c_char_p
42 _status2str.argtypes = [c_int]
44 def __init__(self, statuslist):
45 """It is initialized with a list of strings that are available as
46 Status().string1 - Status().stringn attributes.
48 super(Status, self).__init__(statuslist)
51 def status2str(self, status):
52 """Get a string representation of a notmuch_status_t value."""
53 # define strings for custom error messages
54 if status == STATUS.NOT_INITIALIZED:
55 return "Operation on uninitialized object impossible."
56 return str(Status._status2str(status))
58 STATUS = Status(['SUCCESS',
64 'DUPLICATE_MESSAGE_ID',
67 'UNBALANCED_FREEZE_THAW',
70 """STATUS is a class, whose attributes provide constants that serve as return
71 indicators for notmuch functions. Currently the following ones are defined. For
72 possible return values and specific meaning for each method, see the method
81 * DUPLICATE_MESSAGE_ID
84 * UNBALANCED_FREEZE_THAW
88 Invoke the class method `notmuch.STATUS.status2str` with a status value as
89 argument to receive a human readable string"""
90 STATUS.__name__ = 'STATUS'
92 class NotmuchError(Exception):
93 """Is initiated with a (notmuch.STATUS[, message=None]). It will not
94 return an instance of the class NotmuchError, but a derived instance
95 of a more specific Error Message, e.g. OutOfMemoryError. Each status
96 but SUCCESS has a corresponding subclassed Exception."""
99 def get_exc_subclass(cls, status):
100 """Returns a fine grained Exception() type,detailing the error status"""
102 STATUS.OUT_OF_MEMORY: OutOfMemoryError,
103 STATUS.READ_ONLY_DATABASE: ReadOnlyDatabaseError,
104 STATUS.XAPIAN_EXCEPTION: XapianError,
105 STATUS.FILE_ERROR: FileError,
106 STATUS.FILE_NOT_EMAIL: FileNotEmailError,
107 STATUS.DUPLICATE_MESSAGE_ID: DuplicateMessageIdError,
108 STATUS.NULL_POINTER: NullPointerError,
109 STATUS.TAG_TOO_LONG: TagTooLongError,
110 STATUS.UNBALANCED_FREEZE_THAW: UnbalancedFreezeThawError,
111 STATUS.UNBALANCED_ATOMIC: UnbalancedAtomicError,
112 STATUS.NOT_INITIALIZED: NotInitializedError
114 assert 0 < status <= len(subclasses)
115 return subclasses[status]
117 def __new__(cls, *args, **kwargs):
118 """Return a correct subclass of NotmuchError if needed
120 We return a NotmuchError instance if status is None (or 0) and a
121 subclass that inherits from NotmuchError depending on the
122 'status' parameter otherwise."""
123 # get 'status'. Passed in as arg or kwarg?
124 status = args[0] if len(args) else kwargs.get('status', None)
125 # no 'status' or cls is subclass already, return 'cls' instance
126 if not status or cls != NotmuchError:
127 return super(NotmuchError, cls).__new__(cls)
128 subclass = cls.get_exc_subclass(status) # which class to use?
129 return subclass.__new__(subclass, *args, **kwargs)
131 def __init__(self, status=None, message=None):
133 self.message = message
136 if self.message is not None:
138 elif self.status is not None:
139 return STATUS.status2str(self.status)
141 return 'Unknown error'
143 # List of Subclassed exceptions that correspond to STATUS values and are
144 # subclasses of NotmuchError.
145 class OutOfMemoryError(NotmuchError):
146 status = STATUS.OUT_OF_MEMORY
147 class ReadOnlyDatabaseError(NotmuchError):
148 status = STATUS.READ_ONLY_DATABASE
149 class XapianError(NotmuchError):
150 status = STATUS.XAPIAN_EXCEPTION
151 class FileError(NotmuchError):
152 status = STATUS.FILE_ERROR
153 class FileNotEmailError(NotmuchError):
154 status = STATUS.FILE_NOT_EMAIL
155 class DuplicateMessageIdError(NotmuchError):
156 status = STATUS.DUPLICATE_MESSAGE_ID
157 class NullPointerError(NotmuchError):
158 status = STATUS.NULL_POINTER
159 class TagTooLongError(NotmuchError):
160 status = STATUS.TAG_TOO_LONG
161 class UnbalancedFreezeThawError(NotmuchError):
162 status = STATUS.UNBALANCED_FREEZE_THAW
163 class UnbalancedAtomicError(NotmuchError):
164 status = STATUS.UNBALANCED_ATOMIC
165 class NotInitializedError(NotmuchError):
166 """Derived from NotmuchError, this occurs if the underlying data
167 structure (e.g. database is not initialized (yet) or an iterator has
168 been exhausted. You can test for NotmuchError with .status =
169 STATUS.NOT_INITIALIZED"""
170 status = STATUS.NOT_INITIALIZED
175 """Ensure a nicely utf-8 encoded string to pass to libnotmuch
177 C++ code expects strings to be well formatted and
178 unicode strings to have no null bytes."""
179 if not isinstance(value, basestring):
180 raise TypeError("Expected str or unicode, got %s" % str(type(value)))
181 if isinstance(value, unicode):
182 return value.encode('UTF-8')
186 class NotmuchDatabaseS(Structure):
188 NotmuchDatabaseP = POINTER(NotmuchDatabaseS)
190 class NotmuchQueryS(Structure):
192 NotmuchQueryP = POINTER(NotmuchQueryS)
194 class NotmuchThreadsS(Structure):
196 NotmuchThreadsP = POINTER(NotmuchThreadsS)
198 class NotmuchThreadS(Structure):
200 NotmuchThreadP = POINTER(NotmuchThreadS)
202 class NotmuchMessagesS(Structure):
204 NotmuchMessagesP = POINTER(NotmuchMessagesS)
206 class NotmuchMessageS(Structure):
208 NotmuchMessageP = POINTER(NotmuchMessageS)
210 class NotmuchTagsS(Structure):
212 NotmuchTagsP = POINTER(NotmuchTagsS)
214 class NotmuchDirectoryS(Structure):
216 NotmuchDirectoryP = POINTER(NotmuchDirectoryS)
218 class NotmuchFilenamesS(Structure):
220 NotmuchFilenamesP = POINTER(NotmuchFilenamesS)