Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notmuch2 copatibility #1673

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion alot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def main():
# get ourselves a database manager
indexpath = settings.get_notmuch_setting('database', 'path')
indexpath = options.mailindex_path or indexpath
dbman = DBManager(path=indexpath, ro=options.read_only)
dbman = DBManager(path=indexpath, ro=options.read_only,
config=options.notmuch_config)

# determine what to do
if command is None:
Expand Down
35 changes: 23 additions & 12 deletions alot/db/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ class DBManager:
}
"""constants representing sort orders"""

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

Expand Down Expand Up @@ -80,14 +83,15 @@ def flush(self):
# 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
# the first two coordinates 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)
db = Database(path=self.path, mode=mode,
config=self.config)
except NotmuchError:
raise DatabaseLockedError()
logging.debug('got write lock')
Expand Down Expand Up @@ -249,13 +253,15 @@ def toggle_tags(self, querystring, tags, afterwards=None):

def count_messages(self, querystring):
"""returns number of messages that match `querystring`"""
db = Database(path=self.path, mode=Database.MODE.READ_ONLY)
db = Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config)
return db.count_messages(querystring,
exclude_tags=self.exclude_tags)

def collect_tags(self, querystring):
"""returns tags of messages that match `querystring`"""
db = Database(path=self.path, mode=Database.MODE.READ_ONLY)
db = Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config)
tagset = notmuch2._tags.ImmutableTagSet(
db.messages(querystring,
exclude_tags=self.exclude_tags),
Expand All @@ -265,14 +271,16 @@ def collect_tags(self, querystring):

def count_threads(self, querystring):
"""returns number of threads that match `querystring`"""
db = Database(path=self.path, mode=Database.MODE.READ_ONLY)
db = Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config)
return db.count_threads(querystring,
exclude_tags=self.exclude_tags)

@contextlib.contextmanager
def _with_notmuch_thread(self, tid):
"""returns :class:`notmuch2.Thread` with given id"""
with Database(path=self.path, mode=Database.MODE.READ_ONLY) as db:
with Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config) as db:
try:
yield next(db.threads('thread:' + tid))
except NotmuchError:
Expand All @@ -287,8 +295,8 @@ def get_thread(self, tid):
@contextlib.contextmanager
def _with_notmuch_message(self, mid):
"""returns :class:`notmuch2.Message` with given id"""
mode = Database.MODE.READ_ONLY
with Database(path=self.path, mode=mode) as db:
with Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config) as db:
try:
yield db.find_message(mid)
except:
Expand All @@ -305,15 +313,17 @@ def get_all_tags(self):
returns all tagsstrings used in the database
:rtype: list of str
"""
db = Database(path=self.path, mode=Database.MODE.READ_ONLY)
db = Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config)
return [t for t in db.tags]

def get_named_queries(self):
"""
returns the named queries stored in the database.
:rtype: dict (str -> str) mapping alias to full query string
"""
db = Database(path=self.path, mode=Database.MODE.READ_ONLY)
db = Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config)
return {k[6:]: db.config[k] for k in db.config if
k.startswith('query.')}

Expand All @@ -332,7 +342,8 @@ def get_threads(self, querystring, sort='newest_first'):
:class:`multiprocessing.Process`)
"""
assert sort in self._sort_orders
db = Database(path=self.path, mode=Database.MODE.READ_ONLY)
db = Database(path=self.path, mode=Database.MODE.READ_ONLY,
config=self.config)
thread_ids = [t.threadid for t in db.threads(querystring,
sort=self._sort_orders[sort],
exclude_tags=self.exclude_tags)]
Expand Down