From 5a1c7c613122a80cf651fe8791add9e7b41faaec Mon Sep 17 00:00:00 2001 From: Michal Fluder Date: Tue, 3 Oct 2023 07:46:10 +0200 Subject: [PATCH 1/5] refactor: Use tomllib and tomli instead of toml --- pyproject.toml | 5 ++--- scripts/gen_credits.py | 13 ++++++++++--- src/aria2p/utils.py | 12 +++++++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e66604d..cb91a74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,7 @@ dependencies = [ "appdirs>=1.4", "loguru>=0.5", "requests>=2.19", - "toml>=0.10", + "tomli>=2.0.1", "websocket-client>=0.58", ] @@ -72,7 +72,7 @@ docs = [ "mkdocstrings[python]>=0.18", "markdown-callouts>=0.2", "markdown-exec>=0.5", - "toml>=0.10", + "tomli>=2.0.1", ] format = [ "autoflake>=1.4", @@ -114,7 +114,6 @@ tests = [ typing = [ "mypy>=0.910", "types-markdown>=3.3", - "types-toml>=0.10", "types-setuptools", "types-requests", ] diff --git a/scripts/gen_credits.py b/scripts/gen_credits.py index e5bdd7a..43da0db 100644 --- a/scripts/gen_credits.py +++ b/scripts/gen_credits.py @@ -1,9 +1,9 @@ import re from itertools import chain from pathlib import Path +import sys from textwrap import dedent -import toml from jinja2 import StrictUndefined from jinja2.sandbox import SandboxedEnvironment @@ -12,11 +12,18 @@ except ImportError: from importlib_metadata import metadata, PackageNotFoundError +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib + project_dir = Path(".") -pyproject = toml.load(project_dir / "pyproject.toml") +with (project_dir / "pyproject.toml").open("rb") as pyproject_file: + pyproject = tomllib.load(pyproject_file) project = pyproject["project"] pdm = pyproject["tool"]["pdm"] -lock_data = toml.load(project_dir / "pdm.lock") +with (project_dir / "pdm.lock").open("rb") as lock_file: + lock_data = tomllib.load(lock_file) lock_pkgs = {pkg["name"].lower(): pkg for pkg in lock_data["package"]} project_name = project["name"] regex = re.compile(r"(?P[\w.-]+)(?P.*)$") diff --git a/src/aria2p/utils.py b/src/aria2p/utils.py index a01ec8b..36a68fc 100644 --- a/src/aria2p/utils.py +++ b/src/aria2p/utils.py @@ -7,6 +7,7 @@ from __future__ import annotations import signal +import sys import textwrap from datetime import timedelta from pathlib import Path @@ -14,12 +15,16 @@ from typing import Any, Dict import pkg_resources -import toml from appdirs import user_config_dir from loguru import logger from aria2p.types import PathOrStr +if sys.version_info >= (3, 11): + import tomllib +else: + import tomli as tomllib # noqa: WPS440 (variables overlap) + class SignalHandler: """A helper class to handle signals.""" @@ -243,14 +248,15 @@ def load_configuration() -> Dict[str, Any]: """ config_dict = {} - config_dict["DEFAULT"] = toml.loads(default_config) + config_dict["DEFAULT"] = tomllib.loads(default_config) # Check for configuration file config_file_path = Path(user_config_dir("aria2p")) / "config.toml" if config_file_path.exists(): try: - config_dict["USER"] = toml.load(config_file_path) + with config_file_path.open("rb") as config_file: + config_dict["USER"] = tomllib.load(config_file) except Exception as error: # noqa: W0703 (too broad exception) logger.error(f"Failed to load configuration file: {error}") else: From e5bd2dd48b46576111c7ea9f1fe7acbfa91611ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Tue, 10 Oct 2023 16:22:14 +0200 Subject: [PATCH 2/5] fixup --- scripts/gen_credits.py | 2 +- src/aria2p/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/gen_credits.py b/scripts/gen_credits.py index de0c267..b62283f 100644 --- a/scripts/gen_credits.py +++ b/scripts/gen_credits.py @@ -3,10 +3,10 @@ from __future__ import annotations import re +import sys from importlib.metadata import PackageNotFoundError, metadata from itertools import chain from pathlib import Path -import sys from textwrap import dedent from typing import Mapping, cast diff --git a/src/aria2p/utils.py b/src/aria2p/utils.py index a447ed4..4a2adfb 100644 --- a/src/aria2p/utils.py +++ b/src/aria2p/utils.py @@ -247,7 +247,7 @@ def load_configuration() -> dict[str, Any]: try: with config_file_path.open("rb") as config_file: config_dict["USER"] = tomllib.load(config_file) - except Exception as error: # noqa: W0703 (too broad exception) + except Exception as error: logger.error(f"Failed to load configuration file: {error}") else: # Write initial configuration file if it does not exist From 23ca74075b9cb19edf3dfd74f4555448553b65d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Tue, 10 Oct 2023 16:22:48 +0200 Subject: [PATCH 3/5] fixup --- src/aria2p/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aria2p/utils.py b/src/aria2p/utils.py index 4a2adfb..9b64795 100644 --- a/src/aria2p/utils.py +++ b/src/aria2p/utils.py @@ -247,7 +247,7 @@ def load_configuration() -> dict[str, Any]: try: with config_file_path.open("rb") as config_file: config_dict["USER"] = tomllib.load(config_file) - except Exception as error: + except Exception as error: # noqa: BLE001 logger.error(f"Failed to load configuration file: {error}") else: # Write initial configuration file if it does not exist From c1f3c82f4cef48067ca9ccbc9d8e0875675d879a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Tue, 10 Oct 2023 16:25:07 +0200 Subject: [PATCH 4/5] fixup --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index f2636be..68cea6d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ dependencies = [ "appdirs>=1.4", "loguru>=0.5", "requests>=2.19", - "tomli>=2.0.1", + "tomli>=2.0.1; python_version < '3.11'", "websocket-client>=0.58", ] From d856ce407ba524b183f44a13440754514a60ec38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Tue, 10 Oct 2023 16:25:35 +0200 Subject: [PATCH 5/5] fixup --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 68cea6d..17a1cd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ dependencies = [ "appdirs>=1.4", "loguru>=0.5", "requests>=2.19", - "tomli>=2.0.1; python_version < '3.11'", + "tomli>=2.0; python_version < '3.11'", "websocket-client>=0.58", ]