Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tomllib and tomli instead of toml #127

Merged
merged 7 commits into from Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions pyproject.toml
Expand Up @@ -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",
]

Expand Down Expand Up @@ -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",
Expand All @@ -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 = [
Expand Down
13 changes: 10 additions & 3 deletions scripts/gen_credits.py
Expand Up @@ -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<dist>[\w.-]+)(?P<spec>.*)$")
Expand Down
12 changes: 9 additions & 3 deletions src/aria2p/utils.py
Expand Up @@ -6,19 +6,24 @@
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

if TYPE_CHECKING:
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."""
Expand Down Expand Up @@ -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:
Expand Down