diff --git a/pyproject.toml b/pyproject.toml index ce439dd..17a1cd8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ dependencies = [ "appdirs>=1.4", "loguru>=0.5", "requests>=2.19", - "toml>=0.10", + "tomli>=2.0; python_version < '3.11'", "websocket-client>=0.58", ] @@ -78,7 +78,7 @@ docs = [ "mkdocs-material>=7.3", "mkdocs-minify-plugin>=0.6.4", "mkdocstrings[python]>=0.18", - "toml>=0.10", + "tomli>=2.0; python_version < '3.11'", ] maintain = [ "black>=23.1", @@ -103,8 +103,6 @@ typing = [ "mypy>=0.910", "types-markdown>=3.3", "types-pyyaml>=6.0", - "types-toml>=0.10", - "types-setuptools", "types-requests", ] security = [ diff --git a/scripts/gen_credits.py b/scripts/gen_credits.py index 459f293..b62283f 100644 --- a/scripts/gen_credits.py +++ b/scripts/gen_credits.py @@ -3,21 +3,28 @@ from __future__ import annotations import re +import sys from importlib.metadata import PackageNotFoundError, metadata from itertools import chain from pathlib import Path from textwrap import dedent from typing import Mapping, cast -import toml from jinja2 import StrictUndefined from jinja2.sandbox import SandboxedEnvironment +if sys.version_info < (3, 11): + import tomli as tomllib +else: + import 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 9c808f1..9b64795 100644 --- a/src/aria2p/utils.py +++ b/src/aria2p/utils.py @@ -6,12 +6,12 @@ from __future__ import annotations import signal +import sys import textwrap from pathlib import Path from typing import TYPE_CHECKING, Any import pkg_resources -import toml from appdirs import user_config_dir from loguru import logger @@ -19,6 +19,11 @@ from datetime import timedelta from types import FrameType +if sys.version_info < (3, 11): + import tomli as tomllib +else: + import tomllib + class SignalHandler: """A helper class to handle signals.""" @@ -233,14 +238,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: BLE001 logger.error(f"Failed to load configuration file: {error}") else: