summaryrefslogtreecommitdiff
path: root/alot/db/manager.py
blob: c902eb54f0ef72aa268db760d9ee3031e559febe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (C) 2011-2012  Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
from collections import deque
import logging
import os

from notmuch2 import Database, NotmuchError, XapianError

from .errors import DatabaseError
from .errors import DatabaseLockedError
from .errors import DatabaseROError
from .errors import NonexistantObjectError
from .thread import Thread
from ..settings.const import settings

def _is_subdir_of(subpath, superpath):
    # make both absolute
    superpath = os.path.realpath(superpath)
    subpath = os.path.realpath(subpath)

    # return true, if the common prefix of both is equal to directory
    # e.g. /a/b/c/d.rst and directory is /a/b, the common prefix is /a/b
    return os.path.commonprefix([subpath, superpath]) == superpath

class DBManager:
    """
    Keeps track of your index parameters, maintains a write-queue and
    lets you look up threads and messages directly to the persistent wrapper
    classes.
    """
    _sort_orders = {
        'oldest_first': Database.SORT.OLDEST_FIRST,
        'newest_first': Database.SORT.NEWEST_FIRST,
        'unsorted':     Database.SORT.UNSORTED,
        'message_id':   Database.SORT.MESSAGE_ID,
    }
    """constants representing sort orders"""

    _exclude_tags = None
    _property_tags = None

    def __init__(self, path=None, ro=False):
        """
        :param path: absolute path to the notmuch index
        :type path: str
        :param ro: open the index in read-only mode
        :type ro: bool
        """
        self.ro = ro
        self.path = path
        self.writequeue = deque([])
        self.processes = []

        self._exclude_tags = frozenset(settings.get('exclude_tags'))
        self._property_tags = frozenset(settings.get('property_tags'))

    def _db_ro(self):
        return Database(path = self.path, mode = Database.MODE.READ_ONLY)

    def flush(self):
        """
        write out all queued write-commands in order, each one in a separate
        :meth:`atomic <notmuch.Database.begin_atomic>` transaction.

        If this fails the current action is rolled back, stays in the write
        queue and an exception is raised.
        You are responsible to retry flushing at a later time if you want to
        ensure that the cached changes are applied to the database.

        :exception: :exc:`~errors.DatabaseROError` if db is opened read-only
        :exception: :exc:`~errors.DatabaseLockedError` if db is locked
        """
        if self.ro:
            raise DatabaseROError()
        if not self.writequeue:
            return

        # read notmuch's config regarding imap flag synchronization
        sync = settings.get_notmuch_setting('maildir', 'synchronize_flags')

        # go through writequeue entries
        while self.writequeue:
            current_item = self.writequeue.popleft()
            logging.debug('write-out item: %s', str(current_item))

            # watch out for notmuch errors to re-insert current_item
            # to the queue on errors
            try:
                # the first two coordinants are cnmdname and post-callback
                cmd, afterwards = current_item[:2]
                logging.debug('cmd created')

                # acquire a writeable db handler
                try:
                    mode = Database.MODE.READ_WRITE
                    db = Database(path=self.path, mode=mode)
                except NotmuchError:
                    raise DatabaseLockedError()
                logging.debug('got write lock')

                with db.atomic():
                    if cmd == 'add':
                        path, op_tags = current_item[2:]

                        msg, _   = db.add(path, sync_flags = sync)
                        msg_tags = msg.tags

                        msg_tags |= op_tags
                    else:  # tag/set/untag
                        querystring, op_tags = current_item[2:]
                        for msg in db.messages(querystring):
                            with msg.frozen():
                                msg_tags = msg.tags
                                if cmd == 'tag':
                                    msg_tags |= op_tags
                                if cmd == 'set':
                                    property_tags = msg_tags & self._property_tags
                                    msg_tags.clear()
                                    msg_tags |= op_tags | property_tags
                                elif cmd == 'untag':
                                    msg_tags -= op_tags

                # close db
                db.close()
                logging.debug('closed db')

                # call post-callback
                if callable(afterwards):
                    logging.debug(str(afterwards))
                    afterwards()
                    logging.debug('called callback')

            # re-insert item to the queue upon Xapian/NotmuchErrors
            except (XapianError, NotmuchError) as e:
                logging.exception(e)
                self.writequeue.appendleft(current_item)
                raise DatabaseError(str(e))
            except DatabaseLockedError as e:
                logging.debug('index temporarily locked')
                self.writequeue.appendleft(current_item)
                raise e
            logging.debug('flush finished')

    def tag(self, querystring, tags, afterwards=None, remove_rest=False):
        """
        add tags to messages matching `querystring`.
        This appends a tag operation to the write queue and raises
        :exc:`~errors.DatabaseROError` if in read only mode.

        :param querystring: notmuch search string
        :type querystring: str
        :param tags: a set of tags to be added
        :type tags: set of str
        :param afterwards: callback that gets called after successful
                           application of this tagging operation
        :type afterwards: callable
        :param remove_rest: remove tags from matching messages before tagging
        :type remove_rest: bool
        :exception: :exc:`~errors.DatabaseROError`

        .. note::
            This only adds the requested operation to the write queue.
            You need to call :meth:`DBManager.flush` to actually write out.
        """
        if self.ro:
            raise DatabaseROError()
        if remove_rest:
            self.writequeue.append(('set', afterwards, querystring, tags))
        else:
            self.writequeue.append(('tag', afterwards, querystring, tags))

    def untag(self, querystring, tags, afterwards=None):
        """
        removes tags from messages that match `querystring`.
        This appends an untag operation to the write queue and raises
        :exc:`~errors.DatabaseROError` if in read only mode.

        :param querystring: notmuch search string
        :type querystring: str
        :param tags: a list of tags to be added
        :type tags: list of str
        :param afterwards: callback that gets called after successful
                           application of this tagging operation
        :type afterwards: callable
        :exception: :exc:`~errors.DatabaseROError`

        .. note::
            This only adds the requested operation to the write queue.
            You need to call :meth:`DBManager.flush` to actually write out.
        """
        if self.ro:
            raise DatabaseROError()
        self.writequeue.append(('untag', afterwards, querystring, tags))

    def count_messages(self, querystring):
        """returns number of messages that match `querystring`"""
        return self._db_ro().count_messages(querystring, exclude_tags = self._exclude_tags)

    def count_threads(self, querystring):
        """returns number of threads that match `querystring`"""
        return self._db_ro().count_threads(querystring, exclude_tags = self._exclude_tags)

    def _get_notmuch_thread(self, tid):
        """returns :class:`notmuch.database.Thread` with given id"""
        querystr = 'thread:' + tid
        try:
            return next(self._db_ro().threads(querystr, exclude_tags = self._exclude_tags))
        except StopIteration:
            errmsg = 'no thread with id %s exists!' % tid
            raise NonexistantObjectError(errmsg)

    def get_thread(self, tid):
        """returns :class:`Thread` with given thread id (str)"""
        return Thread(self, self._get_notmuch_thread(tid))

    def get_all_tags(self):
        """
        returns all tagsstrings used in the database
        :rtype: set of str
        """
        return self._db_ro().tags

    def get_named_queries(self):
        """
        returns the named queries stored in the database.
        :rtype: dict (str -> str) mapping alias to full query string
        """
        q_prefix = 'query.'

        db = self._db_ro()
        queries = filter(lambda k: k.startswith(q_prefix), db.config)
        return { q[len(q_prefix):] : db.config[q] for q in queries }

    def get_threads(self, querystring, sort='newest_first', exclude_tags = frozenset()):
        """
        asynchronously look up thread ids matching `querystring`.

        :param querystring: The query string to use for the lookup
        :type querystring: str.
        :param sort: Sort order. one of ['oldest_first', 'newest_first',
                     'message_id', 'unsorted']
        :type query: str
        :param exclude_tags: Tags to exclude by default unless included in the
                             search
        :type exclude_tags: set of str
        :returns: iterator over thread ids
        """
        # TODO: use a symbolic constant for this
        assert sort in self._sort_orders

        db           = self._db_ro()
        sort         = self._sort_orders[sort]
        exclude_tags = self._exclude_tags | exclude_tags

        return (t.threadid for t in db.threads(querystring, sort = sort, exclude_tags = exclude_tags))

    def add_message(self, path, tags):
        """
        Adds a file to the notmuch index.

        :param path: path to the file
        :type path: str
        :param tags: tagstrings to add
        :type tags: list of str
        """
        tags = tags or []

        if self.ro:
            raise DatabaseROError()
        if not _is_subdir_of(path, self.path):
            msg = 'message path %s ' % path
            msg += ' is not below notmuchs '
            msg += 'root path (%s)' % self.path
            raise DatabaseError(msg)
        else:
            self.writequeue.append(('add', None, path, tags))