Skip to content

Commit

Permalink
use tomllib/tomli/tomli-w instead of tomlkit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcflugen committed Mar 5, 2024
1 parent aefed4b commit 171ff3a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
17 changes: 11 additions & 6 deletions babelizer/metadata.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
"""Library metadata used by the babelizer to wrap libraries."""

import pathlib
import re
import sys
import warnings
from collections import OrderedDict, defaultdict
from collections import defaultdict
from contextlib import suppress

import tomlkit as toml
import tomli_w
import yaml

if sys.version_info >= (3, 11): # pragma: no cover (PY11+)
import tomllib
else: # pragma: no cover (<PY311)
import tomli as tomllib

from .errors import ScanError, ValidationError


Expand Down Expand Up @@ -62,7 +67,7 @@ def _norm_os(name):
class BabelMetadata:
"""Library metadata."""

LOADERS = {"yaml": yaml.safe_load, "toml": toml.parse}
LOADERS = {"yaml": yaml.safe_load, "toml": tomllib.loads}

def __init__(
self, library=None, build=None, package=None, info=None, plugin=None, ci=None
Expand Down Expand Up @@ -129,7 +134,7 @@ def from_stream(cls, stream, fmt="toml"):
meta = loader(stream.read())
except yaml.scanner.ScannerError as error:
raise ScanError(f"unable to scan yaml-formatted metadata file:\n{error}")
except toml.exceptions.ParseError as error:
except tomllib.TOMLDecodeError as error:
raise ScanError(f"unable to scan toml-formatted metadata file:\n{error}")
else:
if not isinstance(meta, dict):
Expand Down Expand Up @@ -367,7 +372,7 @@ def format_toml(self):
str
Serialized metadata as a TOML-formatted string
"""
return toml.dumps(self._meta)
return tomli_w.dumps(self._meta, multiline_strings=True)

@staticmethod
def parse_entry_point(specifier):
Expand Down
7 changes: 5 additions & 2 deletions babelizer/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import black as blk
import git
import isort
import tomlkit as toml
from cookiecutter.exceptions import OutputDirExistsException
from cookiecutter.main import cookiecutter

if sys.version_info >= (3, 11): # pragma: no cover (PY11+)
import tomllib
else: # pragma: no cover (<PY311)
import tomli as tomllib
if sys.version_info >= (3, 12): # pragma: no cover (PY12+)
import importlib.resources as importlib_resources
else: # pragma: no cover (<PY312)
Expand Down Expand Up @@ -160,7 +163,7 @@ def prettify_python(path_to_repo):
"""
path_to_repo = pathlib.Path(path_to_repo)
with open(path_to_repo / "babel.toml") as fp:
meta = toml.parse(fp.read())
meta = tomllib.loads(fp.read())
module_name = meta["package"]["name"]

files_to_fix = [
Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ dependencies = [
"gitpython",
"importlib-resources; python_version < '3.12'",
"isort>=5",
"logoizer @ git+https://github.com/mcflugen/logoizer",
"logoizer@ git+https://github.com/mcflugen/logoizer",
"pyyaml",
"tomlkit",
"tomli-w",
"tomli; python_version < '3.11'",
]
dynamic = [
"readme",
Expand Down

0 comments on commit 171ff3a

Please sign in to comment.