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

Switch from deprecated "toml" library #452

Merged
merged 1 commit into from
May 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"picosvg>=0.20.4",
"pillow>=7.2.0",
"regex>=2020.4.4",
"toml>=0.10.1",
"tomlkit",
"ufo2ft[cffsubr]>=2.24.0",
"ufoLib2>=0.6.2",
"resvg-cli>=0.22.0.post3",
Expand Down
27 changes: 22 additions & 5 deletions src/nanoemoji/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
import importlib_resources as resources # pytype: disable=import-error

import itertools
import tomlkit
from pathlib import Path
from picosvg.svg_transform import Affine2D
import toml
from typing import Any, Iterable, MutableMapping, NamedTuple, Optional, Tuple, Sequence

from nanoemoji import util
Expand Down Expand Up @@ -247,6 +247,20 @@ def default(self) -> MasterConfig:
raise ValueError("Must have a default master")


def toml_strip(data):
"""
Workaround for:
https://github.com/sdispater/tomlkit/issues/240
anthrotype marked this conversation as resolved.
Show resolved Hide resolved
"""
new_data = {}
for key, val in data.items():
if isinstance(val, dict):
val = toml_strip(val)
if val is not None:
new_data[key] = val
return new_data


def write(dest: Path, config: FontConfig):
toml_cfg = {
"family": config.family,
Expand Down Expand Up @@ -288,17 +302,20 @@ def write(dest: Path, config: FontConfig):
for m in config.masters
},
}
dest.write_text(toml.dumps(toml_cfg))
with open(dest, "w") as toml_file:
tomlkit.dump(toml_strip(toml_cfg), toml_file)


def _resolve_config(
config_file: Optional[Path] = None,
) -> Tuple[Optional[Path], MutableMapping[str, Any]]:
if config_file is None:
with resources.path("nanoemoji.data", _DEFAULT_CONFIG_FILE) as config_file:
# no config_dir in this context; bad input if we need it
return None, toml.load(config_file)
return config_file.parent, toml.load(config_file)
with open(config_file) as file:
# no config_dir in this context; bad input if we need it
return None, tomlkit.load(file)
with open(config_file) as file:
return config_file.parent, tomlkit.load(file)


def _resolve_src(relative_base: Optional[Path], src: str) -> Iterable[Path]:
Expand Down