From 879cffc0564c7af2f5caf9b2647544500dd9a0c0 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 6 May 2024 13:02:10 +0000 Subject: [PATCH] Configure more lints and switch from `black` to `ruff format` --- chatmaild/pyproject.toml | 15 +++++++-- chatmaild/src/chatmaild/database.py | 2 +- chatmaild/src/chatmaild/doveauth.py | 10 +++--- chatmaild/src/chatmaild/echo.py | 4 +-- chatmaild/src/chatmaild/filedict.py | 7 ++-- chatmaild/src/chatmaild/filtermail.py | 6 ++-- chatmaild/src/chatmaild/metadata.py | 15 ++++----- chatmaild/src/chatmaild/metrics.py | 4 +-- chatmaild/src/chatmaild/newemail.py | 4 +-- chatmaild/src/chatmaild/notifier.py | 11 ++++--- chatmaild/src/chatmaild/tests/plugin.py | 12 +++---- .../src/chatmaild/tests/test_doveauth.py | 8 ++--- .../src/chatmaild/tests/test_filtermail.py | 5 ++- .../src/chatmaild/tests/test_metadata.py | 8 ++--- cmdeploy/pyproject.toml | 11 ++++++- cmdeploy/src/cmdeploy/__init__.py | 26 +++++++-------- cmdeploy/src/cmdeploy/acmetool/__init__.py | 2 +- cmdeploy/src/cmdeploy/cmdeploy.py | 33 +++++++++---------- cmdeploy/src/cmdeploy/deploy.py | 4 ++- cmdeploy/src/cmdeploy/dns.py | 10 +++--- cmdeploy/src/cmdeploy/genqr.py | 7 ++-- .../src/cmdeploy/tests/online/test_0_login.py | 5 +-- .../src/cmdeploy/tests/online/test_1_basic.py | 1 + .../cmdeploy/tests/online/test_2_deltachat.py | 8 ++--- cmdeploy/src/cmdeploy/tests/plugin.py | 17 +++++----- cmdeploy/src/cmdeploy/tests/test_cmdeploy.py | 1 + cmdeploy/src/cmdeploy/www.py | 12 ++++--- 27 files changed, 136 insertions(+), 112 deletions(-) diff --git a/chatmaild/pyproject.toml b/chatmaild/pyproject.toml index 64416f8..861ee26 100644 --- a/chatmaild/pyproject.toml +++ b/chatmaild/pyproject.toml @@ -36,6 +36,16 @@ log_format = "%(asctime)s %(levelname)s %(message)s" log_date_format = "%Y-%m-%d %H:%M:%S" log_level = "INFO" +[tool.ruff] +lint.select = [ + "F", # Pyflakes + "I", # isort + + "PLC", # Pylint Convention + "PLE", # Pylint Error + "PLW", # Pylint Warning +] + [tool.tox] legacy_tox_ini = """ [tox] @@ -47,10 +57,9 @@ skipdist = True skip_install = True deps = ruff - black commands = - black --quiet --check --diff src/ - ruff src/ + ruff format --quiet --diff src/ + ruff check src/ [testenv] deps = pytest diff --git a/chatmaild/src/chatmaild/database.py b/chatmaild/src/chatmaild/database.py index a7197e1..1e232c1 100644 --- a/chatmaild/src/chatmaild/database.py +++ b/chatmaild/src/chatmaild/database.py @@ -1,5 +1,5 @@ -import sqlite3 import contextlib +import sqlite3 import time from pathlib import Path diff --git a/chatmaild/src/chatmaild/doveauth.py b/chatmaild/src/chatmaild/doveauth.py index f6ffb4c..74e1f78 100644 --- a/chatmaild/src/chatmaild/doveauth.py +++ b/chatmaild/src/chatmaild/doveauth.py @@ -1,18 +1,18 @@ +import crypt +import json import logging import os -import time import sys -import json -import crypt +import time from pathlib import Path from socketserver import ( - UnixStreamServer, StreamRequestHandler, ThreadingMixIn, + UnixStreamServer, ) +from .config import Config, read_config from .database import Database -from .config import read_config, Config NOCREATE_FILE = "/etc/chatmail-nocreate" diff --git a/chatmaild/src/chatmaild/echo.py b/chatmaild/src/chatmaild/echo.py index 32049eb..4a09809 100644 --- a/chatmaild/src/chatmaild/echo.py +++ b/chatmaild/src/chatmaild/echo.py @@ -6,11 +6,11 @@ it will echo back any message that has non-empty text and also supports the /hel import logging import os -import sys import subprocess +import sys +from pathlib import Path from deltachat_rpc_client import Bot, DeltaChat, EventType, Rpc, events -from pathlib import Path from chatmaild.config import read_config from chatmaild.newemail import create_newemail_dict diff --git a/chatmaild/src/chatmaild/filedict.py b/chatmaild/src/chatmaild/filedict.py index 5be5830..c86b94a 100644 --- a/chatmaild/src/chatmaild/filedict.py +++ b/chatmaild/src/chatmaild/filedict.py @@ -1,9 +1,10 @@ -import os -import logging import json -import filelock +import logging +import os from contextlib import contextmanager +import filelock + class FileDict: """Concurrency-safe multi-reader/single-writer persistent dict.""" diff --git a/chatmaild/src/chatmaild/filtermail.py b/chatmaild/src/chatmaild/filtermail.py index 0a0a8cd..87231fd 100644 --- a/chatmaild/src/chatmaild/filtermail.py +++ b/chatmaild/src/chatmaild/filtermail.py @@ -1,14 +1,14 @@ #!/usr/bin/env python3 import asyncio import logging -import time import sys -from email.parser import BytesParser +import time from email import policy +from email.parser import BytesParser from email.utils import parseaddr +from smtplib import SMTP as SMTPClient from aiosmtpd.controller import Controller -from smtplib import SMTP as SMTPClient from .config import read_config diff --git a/chatmaild/src/chatmaild/metadata.py b/chatmaild/src/chatmaild/metadata.py index e3f7d7f..582cd78 100644 --- a/chatmaild/src/chatmaild/metadata.py +++ b/chatmaild/src/chatmaild/metadata.py @@ -1,17 +1,16 @@ -from pathlib import Path -from socketserver import ( - UnixStreamServer, - StreamRequestHandler, - ThreadingMixIn, -) -import sys import logging import os +import sys +from pathlib import Path +from socketserver import ( + StreamRequestHandler, + ThreadingMixIn, + UnixStreamServer, +) from .filedict import FileDict from .notifier import Notifier - DICTPROXY_HELLO_CHAR = "H" DICTPROXY_LOOKUP_CHAR = "L" DICTPROXY_ITERATE_CHAR = "I" diff --git a/chatmaild/src/chatmaild/metrics.py b/chatmaild/src/chatmaild/metrics.py index e037724..af7c1ab 100644 --- a/chatmaild/src/chatmaild/metrics.py +++ b/chatmaild/src/chatmaild/metrics.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -from pathlib import Path -import time import sys +import time +from pathlib import Path def main(vmail_dir=None): diff --git a/chatmaild/src/chatmaild/newemail.py b/chatmaild/src/chatmaild/newemail.py index 2c14458..fbf976a 100644 --- a/chatmaild/src/chatmaild/newemail.py +++ b/chatmaild/src/chatmaild/newemail.py @@ -1,13 +1,13 @@ #!/usr/local/lib/chatmaild/venv/bin/python3 -""" CGI script for creating new accounts. """ +"""CGI script for creating new accounts.""" import json import random import secrets import string -from chatmaild.config import read_config, Config +from chatmaild.config import Config, read_config CONFIG_PATH = "/usr/local/lib/chatmaild/chatmail.ini" ALPHANUMERIC = string.ascii_lowercase + string.digits diff --git a/chatmaild/src/chatmaild/notifier.py b/chatmaild/src/chatmaild/notifier.py index 2f49fe1..6800cce 100644 --- a/chatmaild/src/chatmaild/notifier.py +++ b/chatmaild/src/chatmaild/notifier.py @@ -25,15 +25,16 @@ The meaning and format of tokens is basically a matter of Delta-Chat Core and the `notification.delta.chat` service. """ +import logging +import math import os import time -import math -import logging -from uuid import uuid4 -from threading import Thread +from dataclasses import dataclass from pathlib import Path from queue import PriorityQueue -from dataclasses import dataclass +from threading import Thread +from uuid import uuid4 + import requests diff --git a/chatmaild/src/chatmaild/tests/plugin.py b/chatmaild/src/chatmaild/tests/plugin.py index 98dc83b..7f34fa7 100644 --- a/chatmaild/src/chatmaild/tests/plugin.py +++ b/chatmaild/src/chatmaild/tests/plugin.py @@ -1,14 +1,14 @@ -import random -from pathlib import Path -import os import importlib.resources import itertools -from email.parser import BytesParser +import os +import random from email import policy -import pytest +from email.parser import BytesParser +from pathlib import Path -from chatmaild.database import Database +import pytest from chatmaild.config import read_config, write_initial_config +from chatmaild.database import Database @pytest.fixture diff --git a/chatmaild/src/chatmaild/tests/test_doveauth.py b/chatmaild/src/chatmaild/tests/test_doveauth.py index e9a4563..b2649f1 100644 --- a/chatmaild/src/chatmaild/tests/test_doveauth.py +++ b/chatmaild/src/chatmaild/tests/test_doveauth.py @@ -1,18 +1,18 @@ import io import json -import pytest import queue import threading import traceback import chatmaild.doveauth +import pytest +from chatmaild.database import DBError from chatmaild.doveauth import ( get_user_data, - lookup_passdb, - handle_dovecot_request, handle_dovecot_protocol, + handle_dovecot_request, + lookup_passdb, ) -from chatmaild.database import DBError def test_basic(db, example_config): diff --git a/chatmaild/src/chatmaild/tests/test_filtermail.py b/chatmaild/src/chatmaild/tests/test_filtermail.py index 9421844..091e035 100644 --- a/chatmaild/src/chatmaild/tests/test_filtermail.py +++ b/chatmaild/src/chatmaild/tests/test_filtermail.py @@ -1,12 +1,11 @@ +import pytest from chatmaild.filtermail import ( - check_encrypted, BeforeQueueHandler, SendRateLimiter, + check_encrypted, check_mdn, ) -import pytest - @pytest.fixture def maildomain(): diff --git a/chatmaild/src/chatmaild/tests/test_metadata.py b/chatmaild/src/chatmaild/tests/test_metadata.py index 23028d8..c24bfbf 100644 --- a/chatmaild/src/chatmaild/tests/test_metadata.py +++ b/chatmaild/src/chatmaild/tests/test_metadata.py @@ -1,12 +1,12 @@ import io -import pytest -import requests import time +import pytest +import requests from chatmaild.metadata import ( - handle_dovecot_request, - handle_dovecot_protocol, Metadata, + handle_dovecot_protocol, + handle_dovecot_request, ) from chatmaild.notifier import ( Notifier, diff --git a/cmdeploy/pyproject.toml b/cmdeploy/pyproject.toml index 3502a30..807b245 100644 --- a/cmdeploy/pyproject.toml +++ b/cmdeploy/pyproject.toml @@ -16,7 +16,6 @@ dependencies = [ "build", "tox", "ruff", - "black", "pytest", "pytest-xdist", "imap_tools", @@ -31,3 +30,13 @@ cmdeploy = "cmdeploy.cmdeploy:main" [tool.pytest.ini_options] addopts = "-v -ra --strict-markers" + +[tool.ruff] +lint.select = [ + "F", # Pyflakes + "I", # isort + + "PLC", # Pylint Convention + "PLE", # Pylint Error + "PLW", # Pylint Warning +] diff --git a/cmdeploy/src/cmdeploy/__init__.py b/cmdeploy/src/cmdeploy/__init__.py index 6ecab52..db5cad6 100644 --- a/cmdeploy/src/cmdeploy/__init__.py +++ b/cmdeploy/src/cmdeploy/__init__.py @@ -2,20 +2,20 @@ Chat Mail pyinfra deploy. """ -import sys import importlib.resources -import subprocess -import shutil import io +import shutil +import subprocess +import sys from pathlib import Path +from chatmaild.config import Config, read_config from pyinfra import host -from pyinfra.operations import apt, files, server, systemd, pip from pyinfra.facts.files import File from pyinfra.facts.systemd import SystemdEnabled -from .acmetool import deploy_acmetool +from pyinfra.operations import apt, files, pip, server, systemd -from chatmaild.config import read_config, Config +from .acmetool import deploy_acmetool def _build_chatmaild(dist_dir) -> None: @@ -486,19 +486,19 @@ def deploy_chatmail(config_path: Path) -> None: # Add our OBS repository for dovecot_no_delay files.put( - name = "Add Deltachat OBS GPG key to apt keyring", - src = importlib.resources.files(__package__).joinpath("obs-home-deltachat.gpg"), - dest = "/etc/apt/keyrings/obs-home-deltachat.gpg", + name="Add Deltachat OBS GPG key to apt keyring", + src=importlib.resources.files(__package__).joinpath("obs-home-deltachat.gpg"), + dest="/etc/apt/keyrings/obs-home-deltachat.gpg", user="root", group="root", mode="644", ) files.line( - name = "Add DeltaChat OBS home repository to sources.list", - path = "/etc/apt/sources.list", - line = "deb [signed-by=/etc/apt/keyrings/obs-home-deltachat.gpg] https://download.opensuse.org/repositories/home:/deltachat/Debian_12/ ./", - ensure_newline = True, + name="Add DeltaChat OBS home repository to sources.list", + path="/etc/apt/sources.list", + line="deb [signed-by=/etc/apt/keyrings/obs-home-deltachat.gpg] https://download.opensuse.org/repositories/home:/deltachat/Debian_12/ ./", + ensure_newline=True, ) apt.update(name="apt update", cache_time=24 * 3600) diff --git a/cmdeploy/src/cmdeploy/acmetool/__init__.py b/cmdeploy/src/cmdeploy/acmetool/__init__.py index 2034a4f..4e47d46 100644 --- a/cmdeploy/src/cmdeploy/acmetool/__init__.py +++ b/cmdeploy/src/cmdeploy/acmetool/__init__.py @@ -1,8 +1,8 @@ import importlib.resources -from pyinfra.operations import apt, files, systemd, server from pyinfra import host from pyinfra.facts.systemd import SystemdStatus +from pyinfra.operations import apt, files, server, systemd def deploy_acmetool(email="", domains=[]): diff --git a/cmdeploy/src/cmdeploy/cmdeploy.py b/cmdeploy/src/cmdeploy/cmdeploy.py index bde9b75..8675434 100644 --- a/cmdeploy/src/cmdeploy/cmdeploy.py +++ b/cmdeploy/src/cmdeploy/cmdeploy.py @@ -4,19 +4,18 @@ along with command line option and subcommand parsing. """ import argparse -import shutil -import subprocess import importlib.resources import importlib.util import os +import shutil +import subprocess import sys from pathlib import Path - -from termcolor import colored from chatmaild.config import read_config, write_initial_config -from cmdeploy.dns import show_dns, check_necessary_dns +from termcolor import colored +from cmdeploy.dns import check_necessary_dns, show_dns # # cmdeploy sub commands and options @@ -157,26 +156,26 @@ def fmt_cmd_options(parser): def fmt_cmd(args, out): - """Run formattting fixes (ruff and black) on all chatmail source code.""" + """Run formattting fixes on all chatmail source code.""" sources = [str(importlib.resources.files(x)) for x in ("chatmaild", "cmdeploy")] - black_args = [shutil.which("black")] - ruff_args = [shutil.which("ruff")] + format_args = [shutil.which("ruff"), "format"] + check_args = [shutil.which("ruff"), "check"] if args.check: - black_args.append("--check") + format_args.append("--diff") else: - ruff_args.append("--fix") + check_args.append("--fix") if not args.verbose: - black_args.append("-q") - ruff_args.append("-q") + check_args.append("--quiet") + format_args.append("--quiet") - black_args.extend(sources) - ruff_args.extend(sources) + format_args.extend(sources) + check_args.extend(sources) - out.check_call(" ".join(black_args), quiet=not args.verbose) - out.check_call(" ".join(ruff_args), quiet=not args.verbose) + out.check_call(" ".join(format_args), quiet=not args.verbose) + out.check_call(" ".join(check_args), quiet=not args.verbose) return 0 @@ -232,7 +231,7 @@ class Out: if not quiet: cmdstring = " ".join(args) self(f"[$ {cmdstring}]", file=sys.stderr) - proc = subprocess.run(args, env=env) + proc = subprocess.run(args, env=env, check=False) return proc.returncode diff --git a/cmdeploy/src/cmdeploy/deploy.py b/cmdeploy/src/cmdeploy/deploy.py index d7d4e03..ef259ad 100644 --- a/cmdeploy/src/cmdeploy/deploy.py +++ b/cmdeploy/src/cmdeploy/deploy.py @@ -1,6 +1,8 @@ -import os import importlib.resources +import os + import pyinfra + from cmdeploy import deploy_chatmail diff --git a/cmdeploy/src/cmdeploy/dns.py b/cmdeploy/src/cmdeploy/dns.py index a3e0e85..46b20e0 100644 --- a/cmdeploy/src/cmdeploy/dns.py +++ b/cmdeploy/src/cmdeploy/dns.py @@ -1,9 +1,9 @@ +import datetime +import importlib +import subprocess import sys import requests -import importlib -import subprocess -import datetime class DNS: @@ -104,8 +104,8 @@ def show_dns(args, out) -> int: return 0 except TypeError: pass - for line in zonefile.splitlines(): - line = line.format( + for raw_line in zonefile.splitlines(): + line = raw_line.format( acme_account_url=acme_account_url, sts_id=datetime.datetime.now().strftime("%Y%m%d%H%M"), chatmail_domain=args.config.mail_domain, diff --git a/cmdeploy/src/cmdeploy/genqr.py b/cmdeploy/src/cmdeploy/genqr.py index 5d84cf5..35c3a97 100644 --- a/cmdeploy/src/cmdeploy/genqr.py +++ b/cmdeploy/src/cmdeploy/genqr.py @@ -1,8 +1,9 @@ import importlib -import qrcode -import os -from PIL import ImageFont, ImageDraw, Image import io +import os + +import qrcode +from PIL import Image, ImageDraw, ImageFont def gen_qr_png_data(maildomain): diff --git a/cmdeploy/src/cmdeploy/tests/online/test_0_login.py b/cmdeploy/src/cmdeploy/tests/online/test_0_login.py index 0b7a861..f2eb8ea 100644 --- a/cmdeploy/src/cmdeploy/tests/online/test_0_login.py +++ b/cmdeploy/src/cmdeploy/tests/online/test_0_login.py @@ -1,9 +1,10 @@ -import pytest -import threading import queue import socket +import threading +import pytest from chatmaild.config import read_config + from cmdeploy.cmdeploy import main diff --git a/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py b/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py index 198df6e..63dea5f 100644 --- a/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py +++ b/cmdeploy/src/cmdeploy/tests/online/test_1_basic.py @@ -1,4 +1,5 @@ import smtplib + import pytest diff --git a/cmdeploy/src/cmdeploy/tests/online/test_2_deltachat.py b/cmdeploy/src/cmdeploy/tests/online/test_2_deltachat.py index cb11a42..a75ab28 100644 --- a/cmdeploy/src/cmdeploy/tests/online/test_2_deltachat.py +++ b/cmdeploy/src/cmdeploy/tests/online/test_2_deltachat.py @@ -1,11 +1,11 @@ -import time -import re +import ipaddress import random +import re +import time +import imap_tools import pytest import requests -import ipaddress -import imap_tools @pytest.fixture diff --git a/cmdeploy/src/cmdeploy/tests/plugin.py b/cmdeploy/src/cmdeploy/tests/plugin.py index 943a0dd..c78866f 100644 --- a/cmdeploy/src/cmdeploy/tests/plugin.py +++ b/cmdeploy/src/cmdeploy/tests/plugin.py @@ -1,17 +1,16 @@ -import os -import io -import time -import random -import subprocess import imaplib -import smtplib +import io import itertools +import os +import random +import smtplib +import subprocess +import time from pathlib import Path + import pytest - -from chatmaild.database import Database from chatmaild.config import read_config - +from chatmaild.database import Database conftestdir = Path(__file__).parent diff --git a/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py b/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py index 215a1d1..ff9c516 100644 --- a/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py +++ b/cmdeploy/src/cmdeploy/tests/test_cmdeploy.py @@ -1,6 +1,7 @@ import os import pytest + from cmdeploy.cmdeploy import get_parser, main diff --git a/cmdeploy/src/cmdeploy/www.py b/cmdeploy/src/cmdeploy/www.py index 2a92133..9dd404b 100644 --- a/cmdeploy/src/cmdeploy/www.py +++ b/cmdeploy/src/cmdeploy/www.py @@ -1,13 +1,14 @@ -import importlib.resources -import webbrowser import hashlib +import importlib.resources import time import traceback +import webbrowser import markdown -from jinja2 import Template -from .genqr import gen_qr_png_data from chatmaild.config import read_config +from jinja2 import Template + +from .genqr import gen_qr_png_data def snapshot_dir_stats(somedir): @@ -120,7 +121,8 @@ def main(): print(f"watching {src_path} directory for changes") changenum = 0 - for count in range(0, 1000000): + count = 0 + while True: newstats = snapshot_dir_stats(src_path) if newstats == stats and count % 60 != 0: count += 1