Skip to content

Commit

Permalink
refactor: Use tomllib from standard library and tomli on Python l…
Browse files Browse the repository at this point in the history
…ess than 3.11 instead of `toml`
  • Loading branch information
pawamoy committed Oct 12, 2023
1 parent 67ec274 commit b5115ed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
3 changes: 1 addition & 2 deletions project/pyproject.toml.jinja
Expand Up @@ -141,7 +141,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 @@ -161,7 +161,6 @@ typing = [
"mypy>=0.910",
"types-markdown>=3.3",
"types-pyyaml>=6.0",
"types-toml>=0.10",
]
security = [
"safety>=2",
Expand Down
16 changes: 12 additions & 4 deletions project/scripts/gen_credits.py.jinja
Expand Up @@ -3,21 +3,29 @@
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

# TODO: Remove once support for Python 3.10 is dropped.
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.joinpath("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.joinpath("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 All @@ -31,7 +39,7 @@ def _get_license(pkg_name: str) -> str:
license_name = cast(dict, data).get("License", "").strip()
multiple_lines = bool(license_name.count("\n"))
{%- if author_username == "pawamoy" %}
# TODO: remove author logic once all my packages licenses are fixed
# TODO: Remove author logic once all my packages licenses are fixed.
author = ""
{%- endif %}
if multiple_lines or not license_name or license_name == "UNKNOWN":
Expand Down

0 comments on commit b5115ed

Please sign in to comment.