Skip to content

Commit

Permalink
Use ruff for style and format (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cadair committed Mar 26, 2024
1 parent e005264 commit 597556a
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 73 deletions.
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
# This should be before any formatting hooks like isort
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.2.2"
hooks:
- id: ruff-format
- id: ruff
args: ["--fix"]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-ast
- id: check-case-conflict
- id: trailing-whitespace
- id: check-yaml
- id: debug-statements
- id: check-added-large-files
- id: end-of-file-fixer
- id: mixed-line-ending
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
args: [ "--write-changes" ]
ci:
autofix_prs: true
autoupdate_schedule: "weekly"
66 changes: 66 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
target-version = "py310"
line-length = 110
exclude = [
".git,",
"__pycache__",
"build",
]

[lint]
select = [
"F",
"E",
"W",
"UP",
"C4",
"ICN",
"G",
"INP",
"PT",
"Q",
"RSE",
"RET",
"TID",
"PTH",
"NPY",
"RUF",
]
extend-ignore = [
# pytest (PT)
"PT001", # Always use pytest.fixture()
"PT004", # Fixtures which don't return anything should have leading _
"PT007", # Parametrize should be lists of tuples # TODO! fix
"PT011", # Too broad exception assert # TODO! fix
"PT023", # Always use () on pytest decorators
# pyupgrade
"UP038", # Use | in isinstance - not compatible with models and is slower
# Returns (RET)
"RET502", # Do not implicitly return None in function able to return non-None value
"RET503", # Missing explicit return at the end of function able to return non-None value
# Pathlib (PTH)
"PTH123", # open() should be replaced by Path.open()
# Ruff
"RUF012", # Mutable class attributes should be annotated with `typing.ClassVar`
"RUF013", # PEP 484 prohibits implicit `Optional`
"RUF015", # Prefer `next(iter(...))` over single element slice
]

[lint.per-file-ignores]
# Part of configuration, not a package.
"setup.py" = [
"INP001",
"E402",
"E501",
"E722",
]
"conftest.py" = ["INP001"]
"docs/conf.py" = [
"E402" # Module imports not at top of file
]
"docs/*.py" = [
"INP001", # Implicit-namespace-package. The examples are not a package.
]
"__init__.py" = ["E402", "F401", "F403"]

[lint.pydocstyle]
convention = "numpy"
4 changes: 2 additions & 2 deletions extras/matrix_notifier.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tags:
- notifications
- notification
- matrix
- shapshots
- snapshots

screenshots:
- url: /assets/img/plugins/printstarted.png
Expand All @@ -31,7 +31,7 @@ featuredimage: /assets/img/plugins/printstarted.png
compatibility:
# List of compatible versions
#
# A single version number will be interpretated as a minimum version requirement,
# A single version number will be interpreted as a minimum version requirement,
# e.g. "1.3.1" will show the plugin as compatible to OctoPrint versions 1.3.1 and up.
# More sophisticated version requirements can be modelled too by using PEP440
# compatible version specifiers.
Expand Down
3 changes: 2 additions & 1 deletion octoprint_matrix_notifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from .plugin import MatrixNotifierPlugin

# Set the Python version your plugin is compatible with below. Recommended is Python 3 only for all new plugins.
# Set the Python version your plugin is compatible with below.
# OctoPrint 1.4.0 - 1.7.x run under both Python 3 and the end-of-life Python 2.
# OctoPrint 1.8.0 onwards only supports Python 3.
__plugin_pythoncompat__ = ">=3,<4" # Only Python 3


def __plugin_load__():
global __plugin_implementation__
__plugin_implementation__ = MatrixNotifierPlugin()
Expand Down
21 changes: 9 additions & 12 deletions octoprint_matrix_notifier/matrix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
A *really* barebones matrix client.
"""

import json
import logging
from bs4 import BeautifulSoup
Expand All @@ -25,11 +26,7 @@ def __init__(self, homeserver, access_token=None, logger=None):
def _send(self, method, path, data=None, content_type=None, content_length=None):
url = urljoin(self.homeserver, path)

headers = (
{"Content-Type": content_type}
if content_type
else {"Content-Type": "application/json"}
)
headers = {"Content-Type": content_type} if content_type else {"Content-Type": "application/json"}

if content_length is not None:
headers["Content-Length"] = str(content_length)
Expand All @@ -40,7 +37,9 @@ def _send(self, method, path, data=None, content_type=None, content_length=None)
log_data = data

req = Request(url, data=data, headers=headers, method=method)
self.logger.info("%s %s data=%s headers=%s", method, url.replace(self.access_token, "..."), log_data, headers)
self.logger.info(
"%s %s data=%s headers=%s", method, url.replace(self.access_token, "..."), log_data, headers
)

resp = urlopen(req)
# TODO: Detect matrix errors here
Expand All @@ -56,9 +55,7 @@ def room_send(self, room_id, message_type, content):
Send a message to a room.
"""
uuid = uuid4()
method, path, data = Api.room_send(
self.access_token, room_id, message_type, content, uuid
)
method, path, data = Api.room_send(self.access_token, room_id, message_type, content, uuid)

return self._send(method, path, data)

Expand All @@ -79,11 +76,11 @@ def upload_media(self, media_data, content_type):
)

def room_send_markdown_message(self, room_id, text):
html = markdown.markdown(text, extensions=['nl2br'])
html = markdown.markdown(text, extensions=["nl2br"])
content = {
"msgtype": "m.text",
"body": BeautifulSoup(html, 'html.parser').get_text(),
"body": BeautifulSoup(html, "html.parser").get_text(),
"format": "org.matrix.custom.html",
"formatted_body": html
"formatted_body": html,
}
self.room_send(room_id, "m.room.message", content)

0 comments on commit 597556a

Please sign in to comment.