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

Migrate to notmuch2 module #320

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ jobs:
build-ubuntu:
strategy:
matrix:
python: [3.6, 3.7, 3.8]
python: [3.6, 3.7, 3.8, 3.9, "3.10"]
name: Build (Python ${{ matrix.python }})
runs-on: ubuntu-18.04
runs-on: ubuntu-21.04
steps:
- uses: actions/checkout@v2
with:
Expand All @@ -22,11 +22,11 @@ jobs:
shell: bash
run: |
sudo apt-get update
sudo apt-get install -y notmuch python3-notmuch python3-venv flake8
sudo apt-get install -y notmuch python3-notmuch2 python3-venv flake8
python3 -m venv env
source ./env/bin/activate
pip install setuptools pytest dkimpy
ln -s /usr/lib/python3/dist-packages/notmuch ./env/lib/python*/site-packages
ln -s /usr/lib/python3/dist-packages/notmuch2 ./env/lib/python*/site-packages
- name: flake8 lint
run: |
source ./env/bin/activate
Expand Down
31 changes: 14 additions & 17 deletions afew/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import time
import logging

import notmuch
import notmuch2

from afew.NotmuchSettings import notmuch_settings, get_notmuch_new_tags

Expand Down Expand Up @@ -46,19 +46,18 @@ def __exit__(self, exc_type, exc_value, traceback):
"""
self.close()

def open(self, rw=False, retry_for=180, retry_delay=1, create=False):
def open(self, rw=False, retry_for=180, retry_delay=1):
if rw:
if self.handle and self.handle.mode == notmuch.Database.MODE.READ_WRITE:
if self.handle and self.handle.mode == notmuch2.Database.MODE.READ_WRITE:
return self.handle

start_time = time.time()
while True:
try:
self.handle = notmuch.Database(self.db_path,
mode=notmuch.Database.MODE.READ_WRITE,
create=create)
self.handle = notmuch2.Database(self.db_path,
mode=notmuch2.Database.MODE.READ_WRITE)
break
except notmuch.NotmuchError:
except notmuch2.NotmuchError:
time_left = int(retry_for - (time.time() - start_time))

if time_left <= 0:
Expand All @@ -71,7 +70,8 @@ def open(self, rw=False, retry_for=180, retry_delay=1, create=False):
time.sleep(retry_delay)
else:
if not self.handle:
self.handle = notmuch.Database(self.db_path, create=create)
self.handle = notmuch2.Database(self.db_path,
mode=notmuch2.Database.MODE.READ_ONLY)

return self.handle

Expand All @@ -93,7 +93,7 @@ def do_query(self, query):
:rtype: :class:`notmuch.Query`
"""
logging.debug('Executing query %r' % query)
return notmuch.Query(self.open(), query)
return notmuch2.Database.messages(self.open(), query)

def get_messages(self, query, full_thread=False):
"""
Expand All @@ -106,10 +106,10 @@ def get_messages(self, query, full_thread=False):
:returns: an iterator over :class:`notmuch.Message` objects
"""
if not full_thread:
for message in self.do_query(query).search_messages():
for message in self.do_query(query):
yield message
else:
for thread in self.do_query(query).search_threads():
for thread in self.do_query(query):
for message in self.walk_thread(thread):
yield message

Expand Down Expand Up @@ -163,16 +163,13 @@ def add_message(self, path, sync_maildir_flags=False, new_mail_handler=None):
"""
# TODO: it would be nice to update notmuchs directory index here
handle = self.open(rw=True)
if hasattr(notmuch.Database, 'index_file'):
message, status = handle.index_file(path, sync_maildir_flags=sync_maildir_flags)
else:
message, status = handle.add_message(path, sync_maildir_flags=sync_maildir_flags)
message, duplicate = handle.add(path, sync_flags=sync_maildir_flags)

if status != notmuch.STATUS.DUPLICATE_MESSAGE_ID:
if not duplicate:
logging.info('Found new mail in {}'.format(path))

for tag in get_notmuch_new_tags():
message.add_tag(tag)
message.tags.add(tag)

if new_mail_handler:
new_mail_handler(message)
Expand Down
8 changes: 3 additions & 5 deletions afew/MailMover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
from datetime import date, datetime, timedelta
from subprocess import check_call, CalledProcessError, DEVNULL

import notmuch

from afew.Database import Database
from afew.utils import get_message_summary

Expand All @@ -21,7 +19,7 @@ class MailMover(Database):

def __init__(self, max_age=0, rename=False, dry_run=False, notmuch_args='', quiet=False):
super().__init__()
self.db = notmuch.Database(self.db_path)
self.db = Database()
self.query = 'folder:"{folder}" AND {subquery}'
if max_age:
days = timedelta(int(max_age))
Expand Down Expand Up @@ -61,11 +59,11 @@ def move(self, maildir, rules):
main_query = self.query.format(
folder=maildir.replace("\"", "\\\""), subquery=query)
logging.debug("query: {}".format(main_query))
messages = notmuch.Query(self.db, main_query).search_messages()
messages = self.db.get_messages(main_query)
for message in messages:
# a single message (identified by Message-ID) can be in several
# places; only touch the one(s) that exists in this maildir
all_message_fnames = message.get_filenames()
all_message_fnames = (str(name) for name in message.filenames())
to_move_fnames = [name for name in all_message_fnames
if maildir in name]
if not to_move_fnames:
Expand Down
10 changes: 5 additions & 5 deletions afew/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import platform
import queue
import threading
import notmuch
import notmuch2
import pyinotify
import ctypes
import contextlib
Expand Down Expand Up @@ -43,19 +43,19 @@ def process_IN_MOVED_TO(self, event):
def new_mail(message):
for filter_ in self.options.enable_filters:
try:
filter_.run('id:"{}"'.format(message.get_message_id()))
filter_.run('id:"{}"'.format(message.messageid))
filter_.commit(self.options.dry_run)
except Exception as e:
logging.warn('Error processing mail with filter {!r}: {}'.format(filter_.message, e))

try:
self.database.add_message(event.pathname,
sync_maildir_flags=True,
sync_flags=True,
new_mail_handler=new_mail)
except notmuch.FileError as e:
except notmuch2.FileError as e:
logging.warn('Error opening mail file: {}'.format(e))
return
except notmuch.FileNotEmailError as e:
except notmuch2.FileNotEmailError as e:
logging.warn('File does not look like an email: {}'.format(e))
return
else:
Expand Down
15 changes: 9 additions & 6 deletions afew/tests/test_mailmover.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import shutil
import tempfile
import unittest
import notmuch2

from afew.Database import Database
from afew.NotmuchSettings import notmuch_settings, write_notmuch_settings
Expand Down Expand Up @@ -35,7 +36,7 @@ def create_mail(msg, maildir, notmuch_db, tags, old=False):
fname = os.path.join(maildir._path, maildir._lookup(message_key))
notmuch_msg = notmuch_db.add_message(fname)
for tag in tags:
notmuch_msg.add_tag(tag, False)
notmuch_msg.tags.add(tag)

# Remove the angle brackets automatically added around the message ID by make_msgid.
stripped_msgid = email_message['Message-ID'].strip('<>')
Expand All @@ -55,7 +56,7 @@ def setUp(self):
write_notmuch_settings()

# Create notmuch database
Database().open(create=True).close()
notmuch2.Database.create().close()

self.root = mailbox.Maildir(self.test_dir)
self.inbox = self.root.add_folder('inbox')
Expand Down Expand Up @@ -88,10 +89,12 @@ def tearDown(self):

@staticmethod
def get_folder_content(db, folder):
return {
(os.path.basename(msg.get_message_id()), msg.get_part(1).decode())
for msg in db.do_query('folder:{}'.format(folder)).search_messages()
}
ret = set()
for msg in db.open().messages('folder:{}'.format(folder)):
with open(msg.path) as f:
ret.add((os.path.basename(msg.messageid),
email.message_from_file(f).get_payload()))
return ret
Comment on lines +92 to +97

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: would it be interesting to run this method asynchronously to speed things up? I believe this will speed things up when someone has a bunch of emails to process. Perhaps having a private method that handles the asynchronous call and calls add? I've done something similar here: https://github.com/afewmail/afew/pull/316/files#r744197423

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just in the test suite, which has 10 e-mails or so.

That mail mover test suite is not exactly "pretty" anyways, the proposed changes are the minimal ones to let it pass with the notmuch2 bindings. I would refactor the e-mail comparisons quite a bit, for example.


def test_all_rule_cases(self):
from afew import MailMover
Expand Down
12 changes: 9 additions & 3 deletions afew/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@


def get_message_summary(message):
when = datetime.fromtimestamp(float(message.get_date()))
when = datetime.fromtimestamp(float(message.date))
sender = get_sender(message)
subject = message.get_header('Subject')
try:
subject = message.header('Subject')
except LookupError:
subject = ''
return '[{date}] {sender} | {subject}'.format(date=when, sender=sender,
subject=subject)


def get_sender(message):
sender = message.get_header('From')
try:
sender = message.header('From')
except LookupError:
sender = ''
name_match = re.search(r'(.+) <.+@.+\..+>', sender)
if name_match:
sender = name_match.group(1)
Expand Down