| 1 |
--- a/.github/workflows/build.yml |
| 2 |
+++ b/.github/workflows/build.yml |
| 3 |
@@ -7,24 +7,32 @@ |
| 4 |
build-ubuntu: |
| 5 |
strategy: |
| 6 |
matrix: |
| 7 |
- python: [3.6, 3.7, 3.8] |
| 8 |
- name: Build (Python ${{ matrix.python }}) |
| 9 |
- runs-on: ubuntu-18.04 |
| 10 |
+ python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] |
| 11 |
+ name: Build (Python ${{ matrix.python-version }}) |
| 12 |
+ runs-on: ubuntu-20.04 |
| 13 |
steps: |
| 14 |
- uses: actions/checkout@v2 |
| 15 |
- - name: Set up Python |
| 16 |
+ with: |
| 17 |
+ fetch-depth: 0 |
| 18 |
+ - name: Set up Python ${{ matrix.python-version }} |
| 19 |
uses: actions/setup-python@v1 |
| 20 |
with: |
| 21 |
- python-version: '${{ matrix.python }}' |
| 22 |
+ python-version: '${{ matrix.python-version }}' |
| 23 |
+ - name: Display Python version |
| 24 |
+ run: python -c "import sys; print(sys.version)" |
| 25 |
- name: Install dependencies |
| 26 |
shell: bash |
| 27 |
run: | |
| 28 |
sudo apt-get update |
| 29 |
- sudo apt-get install -y notmuch python3-notmuch python3-venv |
| 30 |
+ sudo apt-get install -y notmuch python3-notmuch python3-venv flake8 |
| 31 |
python3 -m venv env |
| 32 |
source ./env/bin/activate |
| 33 |
- pip install setuptools pytest dkimpy |
| 34 |
+ pip install setuptools setuptools_scm pytest dkimpy |
| 35 |
ln -s /usr/lib/python3/dist-packages/notmuch ./env/lib/python*/site-packages |
| 36 |
+ - name: flake8 lint |
| 37 |
+ run: | |
| 38 |
+ source ./env/bin/activate |
| 39 |
+ flake8 --ignore=E501,W504 afew/ |
| 40 |
- name: Tests |
| 41 |
run: | |
| 42 |
source ./env/bin/activate |
| 43 |
@@ -38,20 +46,9 @@ |
| 44 |
run: | |
| 45 |
source ./env/bin/activate |
| 46 |
python setup.py install |
| 47 |
- - name: Generate coverage report |
| 48 |
- run: | |
| 49 |
- source ./env/bin/activate |
| 50 |
- pip install pytest-cov |
| 51 |
- pytest --cov=./ --cov-report=xml |
| 52 |
- - name: Upload coverage to Codecov |
| 53 |
- uses: codecov/codecov-action@v1 |
| 54 |
- with: |
| 55 |
- file: ./coverage.xml |
| 56 |
- flags: unittests |
| 57 |
- name: codecov-umbrella |
| 58 |
- fail_ci_if_error: true |
| 59 |
- name: Docs |
| 60 |
run: | |
| 61 |
source ./env/bin/activate |
| 62 |
pip install sphinx |
| 63 |
- python setup.py build_sphinx -b html,man |
| 64 |
+ sphinx-build -b html docs $(mktemp -d) |
| 65 |
+ sphinx-build -b man docs $(mktemp -d) |
| 66 |
--- /dev/null |
| 67 |
+++ b/.github/workflows/release.yml |
| 68 |
@@ -0,0 +1,31 @@ |
| 69 |
+name: Release |
| 70 |
+ |
| 71 |
+on: |
| 72 |
+ release: |
| 73 |
+ types: |
| 74 |
+ - published |
| 75 |
+ |
| 76 |
+jobs: |
| 77 |
+ release: |
| 78 |
+ needs: [build-ubuntu] |
| 79 |
+ runs-on: ubuntu-latest |
| 80 |
+ steps: |
| 81 |
+ - uses: actions/checkout@v2 |
| 82 |
+ with: |
| 83 |
+ fetch-depth: 0 |
| 84 |
+ - name: Set up Python |
| 85 |
+ uses: actions/setup-python@v2 |
| 86 |
+ with: |
| 87 |
+ python-version: '3.x' |
| 88 |
+ - name: Install dependencies |
| 89 |
+ run: | |
| 90 |
+ python -m pip install --upgrade pip |
| 91 |
+ pip install setuptools setuptools_scm wheel twine |
| 92 |
+ - name: Build and publish (testpypi) |
| 93 |
+ env: |
| 94 |
+ TWINE_USERNAME: __token__ |
| 95 |
+ TWINE_PASSWORD: ${{ secrets.testpypi_token }} |
| 96 |
+ TWINE_REPOSITORY: testpypi |
| 97 |
+ run: | |
| 98 |
+ python setup.py sdist |
| 99 |
+ twine upload dist/* |
| 100 |
--- /dev/null |
| 101 |
+++ b/.readthedocs.yaml |
| 102 |
@@ -0,0 +1,22 @@ |
| 103 |
+# .readthedocs.yaml |
| 104 |
+# Read the Docs configuration file |
| 105 |
+# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details |
| 106 |
+ |
| 107 |
+# Required |
| 108 |
+version: 2 |
| 109 |
+ |
| 110 |
+# Set the version of Python and other tools you might need |
| 111 |
+build: |
| 112 |
+ os: ubuntu-22.04 |
| 113 |
+ tools: |
| 114 |
+ python: "3.11" |
| 115 |
+ |
| 116 |
+# Build documentation in the docs/ directory with Sphinx |
| 117 |
+sphinx: |
| 118 |
+ configuration: docs/conf.py |
| 119 |
+ |
| 120 |
+# We recommend specifying your dependencies to enable reproducible builds: |
| 121 |
+# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html |
| 122 |
+# python: |
| 123 |
+# install: |
| 124 |
+# - requirements: docs/requirements.txt |
| 125 |
--- a/NEWS.md |
| 126 |
+++ b/NEWS.md |
| 127 |
@@ -1,3 +1,26 @@ |
| 128 |
+afew 3.1.0 (unreleased) |
| 129 |
+======================= |
| 130 |
+ |
| 131 |
+Python 3.6 support dropped |
| 132 |
+ |
| 133 |
+ afew stopped supporting the older python version 3.6. |
| 134 |
+ |
| 135 |
+Handle DMARC report with empty spf or dkim XML nodes |
| 136 |
+ |
| 137 |
+DMARC Filter: allow to define subject regexp |
| 138 |
+ |
| 139 |
+ Some DMARC report mail have a prefix before "Dmarc Report" in the subject |
| 140 |
+ and where not checked by the plugin. |
| 141 |
+ Afew now allows the user to define the suject regexp in the config file. |
| 142 |
+ |
| 143 |
+Get notmuch database path using Database wrapper |
| 144 |
+ |
| 145 |
+ This allows FolderNameFilter to work with a relative path in database.path of notmuch config file. |
| 146 |
+ |
| 147 |
+HeaderMatchingFilter: do not convert user supplied tags |
| 148 |
+ |
| 149 |
+ This prevents afew to lowercase the tags defined by the user, allowing to have non lowercase tags. |
| 150 |
+ |
| 151 |
afew 3.0.0 (2020-03-10) |
| 152 |
======================= |
| 153 |
|
| 154 |
--- a/README.rst |
| 155 |
+++ b/README.rst |
| 156 |
@@ -2,7 +2,7 @@ |
| 157 |
afew |
| 158 |
==== |
| 159 |
|
| 160 |
-|GithubTag| |CodeCov| |CI Status| |
| 161 |
+|GithubTag| |CI Status| |
| 162 |
|
| 163 |
About |
| 164 |
----- |
| 165 |
@@ -23,7 +23,7 @@ |
| 166 |
configurable rules that can contain arbitrary notmuch queries to match against |
| 167 |
any searchable attributes. |
| 168 |
|
| 169 |
-fyi: afew plays nicely with alot, a GUI for notmuch mail ;) |
| 170 |
+fyi: afew plays nicely with alot, a TUI for notmuch mail ;) |
| 171 |
|
| 172 |
* https://github.com/pazz/alot |
| 173 |
|
| 174 |
@@ -32,9 +32,9 @@ |
| 175 |
IRC |
| 176 |
--- |
| 177 |
|
| 178 |
-Feel free to ask your questions and discuss usage in the `#afewmail IRC Channel`_ on freenode. |
| 179 |
+Feel free to ask your questions and discuss usage in the `#afewmail IRC Channel`_ on Libera.Chat. |
| 180 |
|
| 181 |
-.. _#afewmail IRC Channel: http://webchat.freenode.net/?channels=#afewmail |
| 182 |
+.. _#afewmail IRC Channel: http://web.libera.chat/?channels=#afewmail |
| 183 |
|
| 184 |
|
| 185 |
Features |
| 186 |
@@ -51,7 +51,6 @@ |
| 187 |
* can move mails based on arbitrary notmuch queries, so your sorting |
| 188 |
may show on your traditional mail client (well, almost ;)) |
| 189 |
* has a ``--dry-run`` mode for safe testing |
| 190 |
-* works with python 3.6+ |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@@ -69,7 +68,5 @@ |
| 195 |
|
| 196 |
.. |GithubTag| image:: https://img.shields.io/github/tag/afewmail/afew.svg |
| 197 |
:target: https://github.com/afewmail/afew/releases |
| 198 |
-.. |CodeCov| image:: https://codecov.io/gh/afewmail/afew/branch/master/graph/badge.svg |
| 199 |
- :target: https://codecov.io/gh/afewmail/afew |
| 200 |
.. |CI Status| image:: https://github.com/afewmail/afew/workflows/CI/badge.svg |
| 201 |
:target: https://github.com/afewmail/afew/actions |
| 202 |
--- a/afew/Database.py |
| 203 |
+++ b/afew/Database.py |
| 204 |
@@ -97,7 +97,7 @@ |
| 205 |
|
| 206 |
def get_messages(self, query, full_thread=False): |
| 207 |
""" |
| 208 |
- Get all messages mathing the given query. |
| 209 |
+ Get all messages matching the given query. |
| 210 |
|
| 211 |
:param query: the query to execute using :func:`Database.do_query` |
| 212 |
:type query: str |
| 213 |
@@ -126,7 +126,7 @@ |
| 214 |
# TODO: bindings are *very* unpythonic here... iterator *or* None |
| 215 |
# is a nono |
| 216 |
replies = message.get_replies() |
| 217 |
- if replies != None: |
| 218 |
+ if replies is not None: |
| 219 |
for message in replies: |
| 220 |
# TODO: yield from |
| 221 |
for message in self.walk_replies(message): |
| 222 |
--- a/afew/MailMover.py |
| 223 |
+++ b/afew/MailMover.py |
| 224 |
@@ -80,7 +80,7 @@ |
| 225 |
shutil.copy2(fname, self.get_new_name(fname, destination)) |
| 226 |
to_delete_fnames.append(fname) |
| 227 |
except shutil.SameFileError: |
| 228 |
- logging.warn("trying to move '{}' onto itself".format(fname)) |
| 229 |
+ logging.warning("trying to move '{}' onto itself".format(fname)) |
| 230 |
continue |
| 231 |
except shutil.Error as e: |
| 232 |
# this is ugly, but shutil does not provide more |
| 233 |
--- a/afew/NotmuchSettings.py |
| 234 |
+++ b/afew/NotmuchSettings.py |
| 235 |
@@ -9,14 +9,15 @@ |
| 236 |
|
| 237 |
|
| 238 |
def read_notmuch_settings(path=None): |
| 239 |
- if path == None: |
| 240 |
+ if path is None: |
| 241 |
path = os.environ.get('NOTMUCH_CONFIG', os.path.expanduser('~/.notmuch-config')) |
| 242 |
|
| 243 |
with open(path) as fp: |
| 244 |
notmuch_settings.read_file(fp) |
| 245 |
|
| 246 |
-def write_notmuch_settings(path = None): |
| 247 |
- if path == None: |
| 248 |
+ |
| 249 |
+def write_notmuch_settings(path=None): |
| 250 |
+ if path is None: |
| 251 |
path = os.environ.get('NOTMUCH_CONFIG', os.path.expanduser('~/.notmuch-config')) |
| 252 |
|
| 253 |
with open(path, 'w+') as fp: |
| 254 |
--- a/afew/configparser.py |
| 255 |
+++ b/afew/configparser.py |
| 256 |
@@ -17,7 +17,9 @@ |
| 257 |
return filter(None, result) |
| 258 |
|
| 259 |
|
| 260 |
-class ConfigParser(configparser.ConfigParser, GetListMixIn): pass |
| 261 |
+class ConfigParser(configparser.ConfigParser, GetListMixIn): |
| 262 |
+ pass |
| 263 |
|
| 264 |
|
| 265 |
-class RawConfigParser(configparser.RawConfigParser, GetListMixIn): pass |
| 266 |
+class RawConfigParser(configparser.RawConfigParser, GetListMixIn): |
| 267 |
+ pass |
| 268 |
--- a/afew/files.py |
| 269 |
+++ b/afew/files.py |
| 270 |
@@ -8,13 +8,14 @@ |
| 271 |
import platform |
| 272 |
import queue |
| 273 |
import threading |
| 274 |
+import notmuch |
| 275 |
+import pyinotify |
| 276 |
+import ctypes |
| 277 |
+import contextlib |
| 278 |
|
| 279 |
if platform.system() != 'Linux': |
| 280 |
raise ImportError('Unsupported platform: {!r}'.format(platform.system())) |
| 281 |
|
| 282 |
-import notmuch |
| 283 |
-import pyinotify |
| 284 |
- |
| 285 |
|
| 286 |
class EventHandler(pyinotify.ProcessEvent): |
| 287 |
def __init__(self, options, database): |
| 288 |
@@ -22,7 +23,7 @@ |
| 289 |
self.database = database |
| 290 |
super().__init__() |
| 291 |
|
| 292 |
- ignore_re = re.compile('(/xapian/.*(base.|tmp)$)|(\.lock$)|(/dovecot)') |
| 293 |
+ ignore_re = re.compile(r'(/xapian/.*(base.|tmp)$)|(\.lock$)|(/dovecot)') |
| 294 |
|
| 295 |
def process_IN_DELETE(self, event): |
| 296 |
if self.ignore_re.search(event.pathname): |
| 297 |
@@ -45,17 +46,17 @@ |
| 298 |
filter_.run('id:"{}"'.format(message.get_message_id())) |
| 299 |
filter_.commit(self.options.dry_run) |
| 300 |
except Exception as e: |
| 301 |
- logging.warn('Error processing mail with filter {!r}: {}'.format(filter_.message, e)) |
| 302 |
+ logging.warning('Error processing mail with filter {!r}: {}'.format(filter_.message, e)) |
| 303 |
|
| 304 |
try: |
| 305 |
self.database.add_message(event.pathname, |
| 306 |
sync_maildir_flags=True, |
| 307 |
new_mail_handler=new_mail) |
| 308 |
except notmuch.FileError as e: |
| 309 |
- logging.warn('Error opening mail file: {}'.format(e)) |
| 310 |
+ logging.warning('Error opening mail file: {}'.format(e)) |
| 311 |
return |
| 312 |
except notmuch.FileNotEmailError as e: |
| 313 |
- logging.warn('File does not look like an email: {}'.format(e)) |
| 314 |
+ logging.warning('File does not look like an email: {}'.format(e)) |
| 315 |
return |
| 316 |
else: |
| 317 |
if src_pathname: |
| 318 |
@@ -67,9 +68,9 @@ |
| 319 |
def watch_for_new_files(options, database, paths, daemonize=False): |
| 320 |
wm = pyinotify.WatchManager() |
| 321 |
mask = ( |
| 322 |
- pyinotify.IN_DELETE | |
| 323 |
- pyinotify.IN_MOVED_FROM | |
| 324 |
- pyinotify.IN_MOVED_TO) |
| 325 |
+ pyinotify.IN_DELETE | |
| 326 |
+ pyinotify.IN_MOVED_FROM | |
| 327 |
+ pyinotify.IN_MOVED_TO) |
| 328 |
handler = EventHandler(options, database) |
| 329 |
notifier = pyinotify.Notifier(wm, handler) |
| 330 |
|
| 331 |
@@ -83,9 +84,6 @@ |
| 332 |
notifier.loop() |
| 333 |
|
| 334 |
|
| 335 |
-import ctypes |
| 336 |
-import contextlib |
| 337 |
- |
| 338 |
try: |
| 339 |
libc = ctypes.CDLL(ctypes.util.find_library("c")) |
| 340 |
except ImportError as e: |
| 341 |
@@ -171,7 +169,7 @@ |
| 342 |
if child not in blacklist): |
| 343 |
try: |
| 344 |
stat_result = os.stat(child_path) |
| 345 |
- except: |
| 346 |
+ except Exception: |
| 347 |
continue |
| 348 |
|
| 349 |
if stat_result.st_mode & stat.S_IFDIR: |
| 350 |
@@ -193,7 +191,7 @@ |
| 351 |
while True: |
| 352 |
result = results.get() |
| 353 |
|
| 354 |
- if result != None: |
| 355 |
+ if result is not None: |
| 356 |
yield result |
| 357 |
else: |
| 358 |
break |
| 359 |
--- a/afew/filters/DMARCReportInspectionFilter.py |
| 360 |
+++ b/afew/filters/DMARCReportInspectionFilter.py |
| 361 |
@@ -81,6 +81,8 @@ |
| 362 |
:param node: XML node holding status as text. |
| 363 |
:returns: Whether the status is reported as "failed". |
| 364 |
""" |
| 365 |
+ if not node or not node.text: |
| 366 |
+ return True |
| 367 |
return (node.text.strip() not in ['pass', 'none']) |
| 368 |
|
| 369 |
|
| 370 |
@@ -118,17 +120,28 @@ |
| 371 |
class DMARCReportInspectionFilter(Filter): |
| 372 |
""" |
| 373 |
Inspect DMARC reports for DKIM and SPF status. |
| 374 |
+ |
| 375 |
+ Config: |
| 376 |
+ |
| 377 |
+ [DMARCReportInspectionFilter] |
| 378 |
+ dkim_ok_tag = "dmarc/dkim-ok" |
| 379 |
+ dkim_fail_tag = "dmarc/dkim-fail" |
| 380 |
+ spf_ok_tag = "dmarc/spf-ok" |
| 381 |
+ spf_fail_tag = "dmarc/spf-fail" |
| 382 |
+ subject_regexp = "^report domain:" |
| 383 |
+ |
| 384 |
""" |
| 385 |
def __init__(self, # pylint: disable=too-many-arguments |
| 386 |
database, |
| 387 |
dkim_ok_tag='dmarc/dkim-ok', |
| 388 |
dkim_fail_tag='dmarc/dkim-fail', |
| 389 |
spf_ok_tag='dmarc/spf-ok', |
| 390 |
- spf_fail_tag='dmarc/spf-fail'): |
| 391 |
+ spf_fail_tag='dmarc/spf-fail', |
| 392 |
+ subject_regexp=r'^report domain:'): |
| 393 |
super().__init__(database) |
| 394 |
self.dkim_tag = {True: dkim_ok_tag, False: dkim_fail_tag} |
| 395 |
self.spf_tag = {True: spf_ok_tag, False: spf_fail_tag} |
| 396 |
- self.dmarc_subject = re.compile(r'^report domain:', |
| 397 |
+ self.dmarc_subject = re.compile(subject_regexp, |
| 398 |
flags=re.IGNORECASE) |
| 399 |
self.log = logging.getLogger('{}.{}'.format( |
| 400 |
self.__module__, self.__class__.__name__)) |
| 401 |
--- a/afew/filters/FolderNameFilter.py |
| 402 |
+++ b/afew/filters/FolderNameFilter.py |
| 403 |
@@ -2,7 +2,6 @@ |
| 404 |
# Copyright (c) dtk <[email protected]> |
| 405 |
|
| 406 |
from afew.filters.BaseFilter import Filter |
| 407 |
-from afew.NotmuchSettings import notmuch_settings |
| 408 |
import re |
| 409 |
import shlex |
| 410 |
|
| 411 |
@@ -15,7 +14,7 @@ |
| 412 |
super().__init__(database) |
| 413 |
|
| 414 |
self.__filename_pattern = '{mail_root}/(?P<maildirs>.*)/(cur|new)/[^/]+'.format( |
| 415 |
- mail_root=notmuch_settings.get('database', 'path').rstrip('/')) |
| 416 |
+ mail_root=database.db_path.rstrip('/')) |
| 417 |
self.__folder_explicit_list = set(shlex.split(folder_explicit_list)) |
| 418 |
self.__folder_blacklist = set(shlex.split(folder_blacklist)) |
| 419 |
self.__folder_transforms = self.__parse_transforms(folder_transforms) |
| 420 |
--- a/afew/filters/HeaderMatchingFilter.py |
| 421 |
+++ b/afew/filters/HeaderMatchingFilter.py |
| 422 |
@@ -6,6 +6,8 @@ |
| 423 |
|
| 424 |
from afew.filters.BaseFilter import Filter |
| 425 |
|
| 426 |
+from notmuch.errors import NullPointerError |
| 427 |
+ |
| 428 |
import re |
| 429 |
|
| 430 |
|
| 431 |
@@ -22,10 +24,13 @@ |
| 432 |
def handle_message(self, message): |
| 433 |
if self.header is not None and self.pattern is not None: |
| 434 |
if not self._tag_blacklist.intersection(message.get_tags()): |
| 435 |
- value = message.get_header(self.header) |
| 436 |
- match = self.pattern.search(value) |
| 437 |
- if match: |
| 438 |
- sub = (lambda tag: |
| 439 |
- tag.format(**match.groupdict()).lower()) |
| 440 |
- self.remove_tags(message, *map(sub, self._tags_to_remove)) |
| 441 |
- self.add_tags(message, *map(sub, self._tags_to_add)) |
| 442 |
+ try: |
| 443 |
+ value = message.get_header(self.header) |
| 444 |
+ match = self.pattern.search(value) |
| 445 |
+ if match: |
| 446 |
+ tagdict = {k: v.lower() for k, v in match.groupdict().items()} |
| 447 |
+ sub = (lambda tag: tag.format(**tagdict)) |
| 448 |
+ self.remove_tags(message, *map(sub, self._tags_to_remove)) |
| 449 |
+ self.add_tags(message, *map(sub, self._tags_to_add)) |
| 450 |
+ except NullPointerError: |
| 451 |
+ pass |
| 452 |
--- a/afew/filters/SentMailsFilter.py |
| 453 |
+++ b/afew/filters/SentMailsFilter.py |
| 454 |
@@ -21,11 +21,11 @@ |
| 455 |
my_addresses.add(other_email) |
| 456 |
|
| 457 |
self.query = ( |
| 458 |
- '(' + |
| 459 |
- ' OR '.join('from:"%s"' % address for address in my_addresses) |
| 460 |
- + ') AND NOT (' + |
| 461 |
- ' OR '.join('to:"%s"' % address for address in my_addresses) |
| 462 |
- + ')' |
| 463 |
+ '(' + |
| 464 |
+ ' OR '.join('from:"%s"' % address for address in my_addresses) + |
| 465 |
+ ') AND NOT (' + |
| 466 |
+ ' OR '.join('to:"%s"' % address for address in my_addresses) + |
| 467 |
+ ')' |
| 468 |
) |
| 469 |
|
| 470 |
self.sent_tag = sent_tag |
| 471 |
@@ -58,7 +58,7 @@ |
| 472 |
return email_to_tags |
| 473 |
|
| 474 |
def __get_bare_email(self, email): |
| 475 |
- if not '<' in email: |
| 476 |
+ if '<' not in email: |
| 477 |
return email |
| 478 |
else: |
| 479 |
match = self._bare_email_re.search(email) |
| 480 |
--- /dev/null |
| 481 |
+++ b/afew/tests/test_headermatchingfilter.py |
| 482 |
@@ -0,0 +1,84 @@ |
| 483 |
+"""Test suite for DKIMValidityFilter. |
| 484 |
+""" |
| 485 |
+import unittest |
| 486 |
+from email.utils import make_msgid |
| 487 |
+from unittest import mock |
| 488 |
+ |
| 489 |
+from afew.Database import Database |
| 490 |
+from afew.filters.HeaderMatchingFilter import HeaderMatchingFilter |
| 491 |
+ |
| 492 |
+from notmuch.errors import NullPointerError |
| 493 |
+ |
| 494 |
+ |
| 495 |
+class _AddTags: # pylint: disable=too-few-public-methods |
| 496 |
+ """Mock for `add_tags` method of base filter. We need to easily collect |
| 497 |
+ tags added by filter for test assertion. |
| 498 |
+ """ |
| 499 |
+ def __init__(self, tags): |
| 500 |
+ self._tags = tags |
| 501 |
+ |
| 502 |
+ def __call__(self, message, *tags): |
| 503 |
+ self._tags.update(tags) |
| 504 |
+ |
| 505 |
+ |
| 506 |
+def _make_header_matching_filter(): |
| 507 |
+ """Make `HeaderMatchingFilter` with mocked `HeaderMatchingFilter.add_tags` |
| 508 |
+ method, so in tests we can easily check what tags were added by filter |
| 509 |
+ without fiddling with db. |
| 510 |
+ """ |
| 511 |
+ tags = set() |
| 512 |
+ add_tags = _AddTags(tags) |
| 513 |
+ header_filter = HeaderMatchingFilter(Database(), header="X-test", pattern="") |
| 514 |
+ header_filter.add_tags = add_tags |
| 515 |
+ return header_filter, tags |
| 516 |
+ |
| 517 |
+ |
| 518 |
+def _make_message(should_fail): |
| 519 |
+ """Make mock email Message. |
| 520 |
+ |
| 521 |
+ Mocked methods: |
| 522 |
+ |
| 523 |
+ - `get_header()` returns non-empty string. When testing with mocked |
| 524 |
+ function for verifying DKIM signature, DKIM signature doesn't matter as |
| 525 |
+ long as it's non-empty string. |
| 526 |
+ |
| 527 |
+ - `get_filenames()` returns list of non-empty string. When testing with |
| 528 |
+ mocked file open, it must just be non-empty string. |
| 529 |
+ |
| 530 |
+ - `get_message_id()` returns some generated message ID. |
| 531 |
+ """ |
| 532 |
+ message = mock.Mock() |
| 533 |
+ if should_fail: |
| 534 |
+ message.get_header.side_effect = NullPointerError |
| 535 |
+ else: |
| 536 |
+ message.get_header.return_value = 'header' |
| 537 |
+ message.get_filenames.return_value = ['a'] |
| 538 |
+ message.get_tags.return_value = ['a'] |
| 539 |
+ message.get_message_id.return_value = make_msgid() |
| 540 |
+ return message |
| 541 |
+ |
| 542 |
+ |
| 543 |
+class TestHeaderMatchingFilter(unittest.TestCase): |
| 544 |
+ """Test suite for `HeaderMatchingFilter`. |
| 545 |
+ """ |
| 546 |
+ @mock.patch('afew.filters.HeaderMatchingFilter.open', |
| 547 |
+ mock.mock_open(read_data=b'')) |
| 548 |
+ def test_header_exists(self): |
| 549 |
+ """Test message with header that exists. |
| 550 |
+ """ |
| 551 |
+ header_filter, tags = _make_header_matching_filter() |
| 552 |
+ message = _make_message(False) |
| 553 |
+ header_filter.handle_message(message) |
| 554 |
+ |
| 555 |
+ self.assertSetEqual(tags, set()) |
| 556 |
+ |
| 557 |
+ @mock.patch('afew.filters.HeaderMatchingFilter.open', |
| 558 |
+ mock.mock_open(read_data=b'')) |
| 559 |
+ def test_header_doesnt_exist(self): |
| 560 |
+ """Test message with header that exists. |
| 561 |
+ """ |
| 562 |
+ header_filter, tags = _make_header_matching_filter() |
| 563 |
+ message = _make_message(True) |
| 564 |
+ header_filter.handle_message(message) |
| 565 |
+ |
| 566 |
+ self.assertSetEqual(tags, set()) |
| 567 |
--- a/afew/tests/test_mailmover.py |
| 568 |
+++ b/afew/tests/test_mailmover.py |
| 569 |
@@ -4,7 +4,6 @@ |
| 570 |
from email.utils import make_msgid |
| 571 |
from freezegun import freeze_time |
| 572 |
import mailbox |
| 573 |
-import notmuch |
| 574 |
import os |
| 575 |
import shutil |
| 576 |
import tempfile |
| 577 |
@@ -13,6 +12,7 @@ |
| 578 |
from afew.Database import Database |
| 579 |
from afew.NotmuchSettings import notmuch_settings, write_notmuch_settings |
| 580 |
|
| 581 |
+ |
| 582 |
def create_mail(msg, maildir, notmuch_db, tags, old=False): |
| 583 |
email_message = email.message.EmailMessage() |
| 584 |
# freezegun doesn't handle time zones properly when generating UNIX |
| 585 |
@@ -83,11 +83,9 @@ |
| 586 |
}, |
| 587 |
} |
| 588 |
|
| 589 |
- |
| 590 |
def tearDown(self): |
| 591 |
shutil.rmtree(self.test_dir) |
| 592 |
|
| 593 |
- |
| 594 |
@staticmethod |
| 595 |
def get_folder_content(db, folder): |
| 596 |
return { |
| 597 |
@@ -95,7 +93,6 @@ |
| 598 |
for msg in db.do_query('folder:{}'.format(folder)).search_messages() |
| 599 |
} |
| 600 |
|
| 601 |
- |
| 602 |
def test_all_rule_cases(self): |
| 603 |
from afew import MailMover |
| 604 |
|
| 605 |
@@ -132,7 +129,6 @@ |
| 606 |
self.assertEqual(expect_archive, self.get_folder_content(db, '.archive')) |
| 607 |
self.assertEqual(expect_spam, self.get_folder_content(db, '.spam')) |
| 608 |
|
| 609 |
- |
| 610 |
def test_max_age(self): |
| 611 |
from afew import MailMover |
| 612 |
|
| 613 |
--- a/afew/tests/test_utils.py |
| 614 |
+++ /dev/null |
| 615 |
@@ -1,12 +0,0 @@ |
| 616 |
-# |
| 617 |
-import unittest |
| 618 |
- |
| 619 |
-from afew import utils |
| 620 |
- |
| 621 |
- |
| 622 |
-class TestUtils(unittest.TestCase): |
| 623 |
- pass |
| 624 |
- |
| 625 |
- |
| 626 |
-if __name__ == '__main__': |
| 627 |
- unittest.main() |
| 628 |
--- a/docs/conf.py |
| 629 |
+++ b/docs/conf.py |
| 630 |
@@ -12,7 +12,8 @@ |
| 631 |
# All configuration values have a default; values that are commented out |
| 632 |
# serve to show the default. |
| 633 |
|
| 634 |
-import sys, os |
| 635 |
+import sys |
| 636 |
+import os |
| 637 |
from pkg_resources import get_distribution |
| 638 |
|
| 639 |
# If extensions (or modules to document with autodoc) are in another directory, |
| 640 |
@@ -42,7 +43,7 @@ |
| 641 |
# -- General configuration ----------------------------------------------------- |
| 642 |
|
| 643 |
# If your documentation needs a minimal Sphinx version, state it here. |
| 644 |
-#needs_sphinx = '1.0' |
| 645 |
+# needs_sphinx = '1.0' |
| 646 |
|
| 647 |
# Add any Sphinx extension module names here, as strings. They can be extensions |
| 648 |
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. |
| 649 |
@@ -79,37 +80,37 @@ |
| 650 |
|
| 651 |
# The language for content autogenerated by Sphinx. Refer to documentation |
| 652 |
# for a list of supported languages. |
| 653 |
-#language = None |
| 654 |
+# language = None |
| 655 |
|
| 656 |
# There are two options for replacing |today|: either, you set today to some |
| 657 |
# non-false value, then it is used: |
| 658 |
-#today = '' |
| 659 |
+# today = '' |
| 660 |
# Else, today_fmt is used as the format for a strftime call. |
| 661 |
-#today_fmt = '%B %d, %Y' |
| 662 |
+# today_fmt = '%B %d, %Y' |
| 663 |
|
| 664 |
# List of patterns, relative to source directory, that match files and |
| 665 |
# directories to ignore when looking for source files. |
| 666 |
exclude_patterns = [] |
| 667 |
|
| 668 |
# The reST default role (used for this markup: `text`) to use for all documents. |
| 669 |
-#default_role = None |
| 670 |
+# default_role = None |
| 671 |
|
| 672 |
# If true, '()' will be appended to :func: etc. cross-reference text. |
| 673 |
-#add_function_parentheses = True |
| 674 |
+# add_function_parentheses = True |
| 675 |
|
| 676 |
# If true, the current module name will be prepended to all description |
| 677 |
# unit titles (such as .. function::). |
| 678 |
-#add_module_names = True |
| 679 |
+# add_module_names = True |
| 680 |
|
| 681 |
# If true, sectionauthor and moduleauthor directives will be shown in the |
| 682 |
# output. They are ignored by default. |
| 683 |
-#show_authors = False |
| 684 |
+# show_authors = False |
| 685 |
|
| 686 |
# The name of the Pygments (syntax highlighting) style to use. |
| 687 |
pygments_style = 'sphinx' |
| 688 |
|
| 689 |
# A list of ignored prefixes for module index sorting. |
| 690 |
-#modindex_common_prefix = [] |
| 691 |
+# modindex_common_prefix = [] |
| 692 |
|
| 693 |
|
| 694 |
# -- Options for HTML output --------------------------------------------------- |
| 695 |
@@ -121,26 +122,26 @@ |
| 696 |
# Theme options are theme-specific and customize the look and feel of a theme |
| 697 |
# further. For a list of options available for each theme, see the |
| 698 |
# documentation. |
| 699 |
-#html_theme_options = {} |
| 700 |
+# html_theme_options = {} |
| 701 |
|
| 702 |
# Add any paths that contain custom themes here, relative to this directory. |
| 703 |
-#html_theme_path = [] |
| 704 |
+# html_theme_path = [] |
| 705 |
|
| 706 |
# The name for this set of Sphinx documents. If None, it defaults to |
| 707 |
# "<project> v<release> documentation". |
| 708 |
-#html_title = None |
| 709 |
+# html_title = None |
| 710 |
|
| 711 |
# A shorter title for the navigation bar. Default is the same as html_title. |
| 712 |
-#html_short_title = None |
| 713 |
+# html_short_title = None |
| 714 |
|
| 715 |
# The name of an image file (relative to this directory) to place at the top |
| 716 |
# of the sidebar. |
| 717 |
-#html_logo = None |
| 718 |
+# html_logo = None |
| 719 |
|
| 720 |
# The name of an image file (within the static path) to use as favicon of the |
| 721 |
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 |
| 722 |
# pixels large. |
| 723 |
-#html_favicon = None |
| 724 |
+# html_favicon = None |
| 725 |
|
| 726 |
# Add any paths that contain custom static files (such as style sheets) here, |
| 727 |
# relative to this directory. They are copied after the builtin static files, |
| 728 |
@@ -149,44 +150,44 @@ |
| 729 |
|
| 730 |
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, |
| 731 |
# using the given strftime format. |
| 732 |
-#html_last_updated_fmt = '%b %d, %Y' |
| 733 |
+# html_last_updated_fmt = '%b %d, %Y' |
| 734 |
|
| 735 |
# If true, SmartyPants will be used to convert quotes and dashes to |
| 736 |
# typographically correct entities. |
| 737 |
-#html_use_smartypants = True |
| 738 |
+# html_use_smartypants = True |
| 739 |
|
| 740 |
# Custom sidebar templates, maps document names to template names. |
| 741 |
-#html_sidebars = {} |
| 742 |
+# html_sidebars = {} |
| 743 |
|
| 744 |
# Additional templates that should be rendered to pages, maps page names to |
| 745 |
# template names. |
| 746 |
-#html_additional_pages = {} |
| 747 |
+# html_additional_pages = {} |
| 748 |
|
| 749 |
# If false, no module index is generated. |
| 750 |
-#html_domain_indices = True |
| 751 |
+# html_domain_indices = True |
| 752 |
|
| 753 |
# If false, no index is generated. |
| 754 |
-#html_use_index = True |
| 755 |
+# html_use_index = True |
| 756 |
|
| 757 |
# If true, the index is split into individual pages for each letter. |
| 758 |
-#html_split_index = False |
| 759 |
+# html_split_index = False |
| 760 |
|
| 761 |
# If true, links to the reST sources are added to the pages. |
| 762 |
-#html_show_sourcelink = True |
| 763 |
+# html_show_sourcelink = True |
| 764 |
|
| 765 |
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. |
| 766 |
-#html_show_sphinx = True |
| 767 |
+# html_show_sphinx = True |
| 768 |
|
| 769 |
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. |
| 770 |
-#html_show_copyright = True |
| 771 |
+# html_show_copyright = True |
| 772 |
|
| 773 |
# If true, an OpenSearch description file will be output, and all pages will |
| 774 |
# contain a <link> tag referring to it. The value of this option must be the |
| 775 |
# base URL from which the finished HTML is served. |
| 776 |
-#html_use_opensearch = '' |
| 777 |
+# html_use_opensearch = '' |
| 778 |
|
| 779 |
# This is the file name suffix for HTML files (e.g. ".xhtml"). |
| 780 |
-#html_file_suffix = None |
| 781 |
+# html_file_suffix = None |
| 782 |
|
| 783 |
# Output file base name for HTML help builder. |
| 784 |
htmlhelp_basename = 'afewdoc' |
| 785 |
@@ -195,10 +196,10 @@ |
| 786 |
# -- Options for LaTeX output -------------------------------------------------- |
| 787 |
|
| 788 |
# The paper size ('letter' or 'a4'). |
| 789 |
-#latex_paper_size = 'letter' |
| 790 |
+# latex_paper_size = 'letter' |
| 791 |
|
| 792 |
# The font size ('10pt', '11pt' or '12pt'). |
| 793 |
-#latex_font_size = '10pt' |
| 794 |
+# latex_font_size = '10pt' |
| 795 |
|
| 796 |
# Grouping the document tree into LaTeX files. List of tuples |
| 797 |
# (source start file, target name, title, author, documentclass [howto/manual]). |
| 798 |
@@ -209,26 +210,26 @@ |
| 799 |
|
| 800 |
# The name of an image file (relative to this directory) to place at the top of |
| 801 |
# the title page. |
| 802 |
-#latex_logo = None |
| 803 |
+# latex_logo = None |
| 804 |
|
| 805 |
# For "manual" documents, if this is true, then toplevel headings are parts, |
| 806 |
# not chapters. |
| 807 |
-#latex_use_parts = False |
| 808 |
+# latex_use_parts = False |
| 809 |
|
| 810 |
# If true, show page references after internal links. |
| 811 |
-#latex_show_pagerefs = False |
| 812 |
+# latex_show_pagerefs = False |
| 813 |
|
| 814 |
# If true, show URL addresses after external links. |
| 815 |
-#latex_show_urls = False |
| 816 |
+# latex_show_urls = False |
| 817 |
|
| 818 |
# Additional stuff for the LaTeX preamble. |
| 819 |
-#latex_preamble = '' |
| 820 |
+# latex_preamble = '' |
| 821 |
|
| 822 |
# Documents to append as an appendix to all manuals. |
| 823 |
-#latex_appendices = [] |
| 824 |
+# latex_appendices = [] |
| 825 |
|
| 826 |
# If false, no module index is generated. |
| 827 |
-#latex_domain_indices = True |
| 828 |
+# latex_domain_indices = True |
| 829 |
|
| 830 |
|
| 831 |
# -- Options for manual page output -------------------------------------------- |
| 832 |
--- a/docs/filters.rst |
| 833 |
+++ b/docs/filters.rst |
| 834 |
@@ -26,7 +26,7 @@ |
| 835 |
DKIMValidityFilter |
| 836 |
------------------ |
| 837 |
|
| 838 |
-This filter verifies DKIM signatures of E-Mails with DKIM header, and adds `dkin-ok` or `dkin-fail` tags. |
| 839 |
+This filter verifies DKIM signatures of E-Mails with DKIM header, and adds `dkim-ok` or `dkim-fail` tags. |
| 840 |
|
| 841 |
DMARCReportInspectionFilter |
| 842 |
--------------------------- |
| 843 |
@@ -145,7 +145,7 @@ |
| 844 |
|
| 845 |
[HeaderMatchingFilter.3] |
| 846 |
header = X-Redmine-Project |
| 847 |
- pattern = (?P<project>.*) |
| 848 |
+ pattern = (?P<project>.+) |
| 849 |
tags = +redmine;+{project} |
| 850 |
|
| 851 |
SpamFilter and ListMailsFilter are implemented using HeaderMatchingFilter, and are |
| 852 |
--- a/setup.py |
| 853 |
+++ b/setup.py |
| 854 |
@@ -12,6 +12,8 @@ |
| 855 |
yield 'chardet' |
| 856 |
yield 'dkimpy' |
| 857 |
|
| 858 |
+with open(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'README.rst'), encoding='utf-8') as f: |
| 859 |
+ long_description = f.read() |
| 860 |
|
| 861 |
setup( |
| 862 |
name='afew', |
| 863 |
@@ -19,6 +21,8 @@ |
| 864 |
description="An initial tagging script for notmuch mail", |
| 865 |
url="https://github.com/afewmail/afew", |
| 866 |
license="ISC", |
| 867 |
+ long_description=long_description, |
| 868 |
+ long_description_content_type="text/x-rst", |
| 869 |
setup_requires=['setuptools_scm'], |
| 870 |
packages=find_packages(), |
| 871 |
test_suite='afew.tests', |