Implementation

Database Manager

The design of the database manager was inspired by alots database manager alot.db.DBManager.

class afew.Database.Database[source]

Convenience wrapper around notmuch.

add_message(path, sync_maildir_flags=False, new_mail_handler=None)[source]

Adds the given message to the notmuch index.

Parameters:
  • path (str) – path to the message
  • sync_maildir_flags (bool) – if True notmuch converts the standard maildir flags to tags
  • new_mail_handler (a function that is called with a notmuch.Message object as its only argument) – callback for new messages
Raises:

notmuch.NotmuchError if adding the message fails

Returns:

a notmuch.Message object

close()[source]

Closes the notmuch database if it has been opened.

do_query(query)[source]

Executes a notmuch query.

Parameters:query (str) – the query to execute
Returns:the query result
Return type:notmuch.Query
get_messages(query, full_thread=False)[source]

Get all messages mathing the given query.

Parameters:
Returns:

an iterator over notmuch.Message objects

mail_bodies_matching(*args, **kwargs)[source]

Filters each message yielded from Database.get_messages() through afew.utils.extract_mail_body().

This functions accepts the same arguments as Database.get_messages().

Returns:an iterator over list of str
remove_message(path)[source]

Remove the given message from the notmuch index.

Parameters:path (str) – path to the message
walk_replies(message)[source]

Returns all replies to the given message.

Parameters:message (notmuch.Message) – the message to start from
Returns:an iterator over notmuch.Message objects
walk_thread(thread)[source]

Returns all messages in the given thread.

Parameters:message (notmuch.Thread) – the tread you are interested in
Returns:an iterator over notmuch.Message objects

Filter

Mail classification

class afew.DBACL.Classifier(categories, database_directory=u'/home/docs/.local/share/afew/categories')[source]
class afew.DBACL.DBACL(database_directory=u'/home/docs/.local/share/afew/categories')[source]

Configuration management

Miscellanious utility functions

afew.utils.extract_mail_body(message)[source]

Extract the plain text body of the message with signatures stripped off.

Parameters:message (notmuch.Message) – the message to extract the body from
Returns:the extracted text body
Return type:list of str
afew.utils.filter_compat(*args)[source]

Compatibility wrapper for filter builtin.

The semantic of the filter builtin has been changed in python3.x. This is a temporary workaround to support both python versions in one code base.

afew.utils.strip_signatures(lines, max_signature_size=10)[source]

Strip signatures from a mail. Used to filter mails before classifying mails.

Parameters:
  • lines (list of str) – a mail split at newlines
  • max_signature_size (int) – consider message parts up to this size as signatures
Returns:

the mail with signatures stripped off

Return type:

list of str

>>> strip_signatures([
...     'Huhu',
...     '--',
...     'Ikke',
... ])
['Huhu']
>>> strip_signatures([
...     'Huhu',
...     '--',
...     'Ikke',
...     '**',
...     "Sponsored by PowerDoh\'",
...     "Sponsored by PowerDoh\'",
...     "Sponsored by PowerDoh\'",
...     "Sponsored by PowerDoh\'",
...     "Sponsored by PowerDoh\'",
... ], 5)
['Huhu']