2 from ctypes import c_int, c_char_p, c_void_p
3 from cnotmuch.globals import nmlib, STATUS, NotmuchError
6 class Database(object):
7 """ Wrapper around a notmuch_database_t
9 Do note that as soon as we tear down this object, all derived queries,
10 threads, and messages will be freed as well.
17 """Class attribute of users default database"""
19 """notmuch_database_get_path (notmuch_database_t *database)"""
20 _get_path = nmlib.notmuch_database_get_path
21 _get_path.restype = c_char_p
23 """notmuch_database_open (const char *path, notmuch_database_mode_t mode)"""
24 _open = nmlib.notmuch_database_open
25 _open.restype = c_void_p
27 """notmuch_database_get_all_tags (notmuch_database_t *database)"""
28 _get_all_tags = nmlib.notmuch_database_get_all_tags
29 _get_all_tags.restype = c_void_p
31 class notmuch_database_t(ctypes.Structure):
32 """the opaque database that is returned by functions."""
35 def __init__(self, path=None, create=False, status= MODE_READ_ONLY):
36 """ Open or create a notmuch database"""
39 self.open(path, status)
42 raise NotmuchError(message="Not implemented yet")
44 #TODO: make a proper function
45 create=nmlib.notmuch_database_create
46 """ notmuch_database_create(const char *path):"""
48 def open(self, path=None, status= MODE_READ_ONLY):
49 """calls notmuch_database_open
51 If path is None, we will try to read a users notmuch configuration and
52 use his default database.
53 :returns: Raises :exc:`notmuch.NotmuchError` in case
54 of any failure (after printing an error message on stderr).
57 if Database._std_db_path is None:
58 from ConfigParser import SafeConfigParser
60 config = SafeConfigParser()
61 config.read(os.path.expanduser('~/.notmuch-config'))
62 if not config.has_option('database','path'):
63 raise NotmuchError(message=
64 "No DB path specified and no user default found")
65 Database._std_db_path=config.get('database','path')
66 path = Database._std_db_path
68 res = Database._open(path, status)
72 message="Could not open the specified database")
76 """notmuch_database_get_path (notmuch_database_t *database); """
77 return Database._get_path(self._db)
80 #If no message is found with the given message_id or if an
81 #out-of-memory situation occurs, this function returns NULL.
83 #notmuch_database_find_message (notmuch_database_t *database,
84 # const char *message_id);
86 def get_all_tags(self):
87 """Return a Tags() object (list of all tags found in the database)
89 :returns: Tags() object or raises :exc:`NotmuchError` with
90 STATUS.NULL_POINTER on error
92 tags_p = Database._get_all_tags (self._db)
94 raise NotmuchError(STATUS.NULL_POINTER)
95 return Tags(tags_p, self)
98 return "'Notmuch DB " + self.get_path() + "'"
101 """Close and free the notmuch database if needed"""
102 if self._db is not None:
103 print("Freeing the database now")
104 nmlib.notmuch_database_close(self._db)
108 """Returns a pointer to the current notmuch_database_t or None"""
112 #------------------------------------------------------------------------------
114 """Wrapper around notmuch_tags_t"""
115 class notmuch_tags_t(ctypes.Structure):
116 """the opaque tags struct that is returned by functions."""
120 _get = nmlib.notmuch_tags_get
121 _get.restype = c_char_p
123 def __init__(self, tags_p, db=None):
124 """ Is passed the db these tags are derived from, and saves a
125 reference to it, so we can automatically delete the db object
126 once all derived objects are dead."""
129 print "inited tags with %d %s" %(tags_p, str(db))
132 """ Make Tags an iterator """
136 if self._tags is None:
138 nmlib.notmuch_tags_move_to_next(self._tags)
139 if not nmlib.notmuch_tags_valid(self._tags):
141 return Tags._get (self._tags)
144 """Close and free the notmuch tags"""
145 if self._tags is not None:
146 print("Freeing the Tags now")
147 nmlib.notmuch_tags_destroy (self._tags)