Skip to content

Commit

Permalink
refactor: Use tomli, and tomllib on Python 3.11+
Browse files Browse the repository at this point in the history
  • Loading branch information
mifluder authored Oct 10, 2023
1 parent 7d25d16 commit 8effdac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 8effdac

Please sign in to comment.