]> git.notmuchmail.org Git - notmuch/blob - bindings/python/notmuch/globals.py
python: move the exception classes into error.py
[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 import sys
20 from ctypes import CDLL, Structure, POINTER
21
22 #-----------------------------------------------------------------------------
23 #package-global instance of the notmuch library
24 try:
25     nmlib = CDLL("libnotmuch.so.2")
26 except:
27     raise ImportError("Could not find shared 'notmuch' library.")
28
29
30 if sys.version_info[0] == 2:
31     class Python3StringMixIn(object):
32         def __str__(self):
33             return unicode(self).encode('utf-8')
34
35
36     def _str(value):
37         """Ensure a nicely utf-8 encoded string to pass to libnotmuch
38
39         C++ code expects strings to be well formatted and
40         unicode strings to have no null bytes."""
41         if not isinstance(value, basestring):
42             raise TypeError("Expected str or unicode, got %s" % type(value))
43         if isinstance(value, unicode):
44             return value.encode('UTF-8')
45         return value
46 else:
47     class Python3StringMixIn(object):
48         def __str__(self):
49             return self.__unicode__()
50
51
52     def _str(value):
53         """Ensure a nicely utf-8 encoded string to pass to libnotmuch
54
55         C++ code expects strings to be well formatted and
56         unicode strings to have no null bytes."""
57         if not isinstance(value, str):
58             raise TypeError("Expected str, got %s" % type(value))
59         return value.encode('UTF-8')
60
61
62 class Enum(object):
63     """Provides ENUMS as "code=Enum(['a','b','c'])" where code.a=0 etc..."""
64     def __init__(self, names):
65         for number, name in enumerate(names):
66             setattr(self, name, number)
67
68
69 class NotmuchDatabaseS(Structure):
70     pass
71 NotmuchDatabaseP = POINTER(NotmuchDatabaseS)
72
73
74 class NotmuchQueryS(Structure):
75     pass
76 NotmuchQueryP = POINTER(NotmuchQueryS)
77
78
79 class NotmuchThreadsS(Structure):
80     pass
81 NotmuchThreadsP = POINTER(NotmuchThreadsS)
82
83
84 class NotmuchThreadS(Structure):
85     pass
86 NotmuchThreadP = POINTER(NotmuchThreadS)
87
88
89 class NotmuchMessagesS(Structure):
90     pass
91 NotmuchMessagesP = POINTER(NotmuchMessagesS)
92
93
94 class NotmuchMessageS(Structure):
95     pass
96 NotmuchMessageP = POINTER(NotmuchMessageS)
97
98
99 class NotmuchTagsS(Structure):
100     pass
101 NotmuchTagsP = POINTER(NotmuchTagsS)
102
103
104 class NotmuchDirectoryS(Structure):
105     pass
106 NotmuchDirectoryP = POINTER(NotmuchDirectoryS)
107
108
109 class NotmuchFilenamesS(Structure):
110     pass
111 NotmuchFilenamesP = POINTER(NotmuchFilenamesS)