Skip to content

Commit

Permalink
chore: pre-commit auto fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrinux committed Aug 16, 2022
1 parent e4a24dc commit 0764f94
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ env/
venv/
ENV/
env.bak/
venv.bak/
venv.bak/
39 changes: 39 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-docstring-first
- id: check-json
- id: check-added-large-files
- id: check-yaml
- id: debug-statements
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v1.5.7
hooks:
- id: autopep8
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.6.0
hooks:
- id: reorder-python-imports
args: [--py38-plus, --add-import, "from __future__ import annotations"]
exclude: ^(\.pdbrc|\.pythonrc.py)$
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.2.0
hooks:
- id: add-trailing-comma
args: [--py36-plus]
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
hooks:
- id: pyupgrade
args: [--py38-plus]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910-1
hooks:
- id: mypy
3 changes: 1 addition & 2 deletions config/settings.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ folder = "INBOX"
# Number of seconds to wait for IDLE notifications
# Default is 5 minutes, IMAP RFC's states never more
# than 29 minutes as a recommendation
# timeout = 300
# timeout = 300


[rules]
Expand All @@ -34,4 +34,3 @@ folder = "INBOX"
subject = "[urlwatch]"
priority = 3
token = "MyOtherToken"

13 changes: 9 additions & 4 deletions gotify.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf8 -*-
from __future__ import annotations

import requests
import simplejson as json
Expand Down Expand Up @@ -48,7 +48,9 @@ def _importance_to_priority(importance):

def _send_to_gotify(self, message, rule):
if rule.priority:
self.logger.debug("Using rule override priority: %s", rule.priority)
self.logger.debug(
"Using rule override priority: %s", rule.priority,
)
priority = rule.priority
else:
priority = Gotify._importance_to_priority(message.importance)
Expand All @@ -65,13 +67,16 @@ def _send_to_gotify(self, message, rule):
else:
extras = None

body = {"title": message.subject, "message": message.body, "priority": priority}
body = {
"title": message.subject,
"message": message.body, "priority": priority,
}

if extras:
body["extras"] = extras

self.logger.debug("Gotify post message body: %s", json.dumps(body))
url = "{0}/message?token={1}".format(self.server, token)
url = f"{self.server}/message?token={token}"
self.logger.debug("Gotify message url: %s", url)
response = requests.post(url=url, json=body)

Expand Down
6 changes: 5 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
from collections.abc import MutableMapping
from logging import Logger
Expand All @@ -7,7 +9,9 @@ def get_logger(name: str, config: MutableMapping = None) -> Logger:
"""Obtaints a logger instance of the given name, optionally configured to
have an output level of debug via toml config [main][verbose]
Returns the logger instance"""
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(name)
if config and "verbose" in config["main"]:
logger.setLevel(logging.DEBUG)
Expand Down
30 changes: 20 additions & 10 deletions imap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# -*- coding: utf8 -*-
from __future__ import annotations

from collections import namedtuple
from collections.abc import MutableMapping
from email import message_from_bytes, policy
from email.header import decode_header, make_header
from email import message_from_bytes
from email import policy
from email.header import decode_header
from email.header import make_header
from email.message import Message

import html2text
Expand All @@ -13,7 +15,7 @@
from helpers import get_logger

EmailStruct = namedtuple(
"EmailStruct", ["msgid", "from_", "subject", "body", "importance"]
"EmailStruct", ["msgid", "from_", "subject", "body", "importance"],
)


Expand Down Expand Up @@ -67,7 +69,7 @@ def _get_body_from_message(self, msg: Message) -> str:
return msg.get_body(preferencelist=("plain")).get_content()
# Html version, try to convert to plain text
elif "html" in msg.get("content-type") and msg.get_body(
preferencelist=("html")
preferencelist=("html"),
):
h = html2text.HTML2Text()
h.ignore_links = False
Expand All @@ -84,15 +86,21 @@ def _get_messages(self, msg_ids: list) -> list:
results = self._client.fetch(msg_ids, "RFC822")
for msgid, raw_msg in results.items():
self.logger.debug("Decoding message headers and body")
msg = message_from_bytes(raw_msg[b"RFC822"], policy=policy.default)
msg = message_from_bytes(
raw_msg[b"RFC822"], policy=policy.default,
)
emails.append(
EmailStruct(
msgid=msgid,
from_=msg.get("from"),
subject=str(make_header(decode_header(msg.get("subject")))),
subject=str(
make_header(
decode_header(msg.get("subject")),
),
),
body=self._get_body_from_message(msg),
importance=msg.get("importance"),
)
),
)
return emails

Expand All @@ -111,7 +119,9 @@ def get_unread(self) -> list:
self.logger.info("No unread flagged messages found")
else:
self.logger.info(
"%d unread flagged message(s) found, getting headers", len(msg_ids)
"%d unread flagged message(s) found, getting headers", len(
msg_ids,
),
)

return self._get_messages(msg_ids)
Expand All @@ -134,7 +144,7 @@ def wait_for_new(self) -> bool:
self._client.idle()
new_exist = False
self.logger.info(
"Waiting idle for new messages to arrive, %d second timeout", self.timeout
"Waiting idle for new messages to arrive, %d second timeout", self.timeout,
)
while True:
# todo add an overall breakout time check
Expand Down
4 changes: 2 additions & 2 deletions imap2gotify.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
# -*- coding: utf8 -*-
from __future__ import annotations

import logging
import os
Expand Down Expand Up @@ -34,7 +34,7 @@ def run(self):
# then marked as read
new_messages = client.get_unread()
(matched_results, not_matched_results) = rules.check_matches(
new_messages
new_messages,
)
client.mark_as_read([r.message for r in not_matched_results])
if matched_results:
Expand Down
11 changes: 9 additions & 2 deletions rules.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
from __future__ import annotations

from collections import namedtuple
from collections.abc import MutableMapping

from helpers import get_logger
from imap import EmailStruct

Rule = namedtuple("Rule", ["name", "from_", "subject", "priority", "token", "extras"])
Rule = namedtuple(
"Rule", [
"name", "from_", "subject",
"priority", "token", "extras",
],
)
MatchResult = namedtuple("MatchResult", ["matched", "rule", "message"])


Expand Down Expand Up @@ -56,7 +63,7 @@ def check_matches(self, messages: list) -> (list, list):
)

self.logger.info(
"%d of %d messages matched rules", len(matches), len(messages)
"%d of %d messages matched rules", len(matches), len(messages),
)
return (matches, misses)

Expand Down
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ deps =
commands =
pytest --flake8

[testenv:py310]
skip_install = True
deps =
black
pytest
pytest-flake8
commands =
black --diff --check imap2gotify.py imap.py gotify.py
pytest --flake8

[testenv:py39]
skip_install = True
deps =
Expand Down

0 comments on commit 0764f94

Please sign in to comment.