Skip to content

Commit

Permalink
Merge pull request #3831 from neutrinoceros/migrate2tomli
Browse files Browse the repository at this point in the history
MNT: migrate from toml to tomli + tomli_w
  • Loading branch information
cphyc authored Mar 3, 2022
2 parents ff5e808 + 9446e89 commit 564450a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ install_requires =
pyyaml>=4.2b1
setuptools>=19.6
sympy!=1.9,>=1.2 # see https://github.com/sympy/sympy/issues/22241
toml>=0.10.2
tomli>=1.2.3
tomli-w>=0.4.0
tqdm>=3.4.0
unyt>=2.8.0
python_requires = >=3.7,<3.12
Expand Down Expand Up @@ -103,6 +104,8 @@ minimal =
matplotlib==2.2.3
more-itertools==8.4
numpy==1.14.5
tomli==1.2.3
tomli-w==0.4.0
unyt==2.8.0
test =
codecov~=2.0.15
Expand Down
14 changes: 9 additions & 5 deletions yt/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import warnings

import toml
# TODO: import tomllib from the standard library instead in Python >= 3.11
import tomli as tomllib
import tomli_w
from more_itertools import always_iterable

from yt.utilities.configuration_tree import ConfigLeaf, ConfigNode
Expand Down Expand Up @@ -144,14 +146,16 @@ def read(self, file_names):
if not os.path.exists(fname):
continue
metadata = {"source": f"file: {fname}"}
self.update(toml.load(fname), metadata=metadata)
with open(fname, "rb") as fh:
data = tomllib.load(fh)
self.update(data, metadata=metadata)
file_names_read.append(fname)

return file_names_read

def write(self, file_handler):
value = self.config_root.as_dict()
config_as_str = toml.dumps(value)
config_as_str = tomli_w.dumps(value)

try:
# Assuming file_handler has a write attribute
Expand Down Expand Up @@ -192,8 +196,8 @@ def _repr_json_(self):
if not os.path.exists(_global_config_file):
cfg = {"yt": {}} # type: ignore
try:
with open(_global_config_file, mode="w") as fd:
toml.dump(cfg, fd)
with open(_global_config_file, mode="wb") as fd:
tomli_w.dump(cfg, fd)
except OSError:
warnings.warn("unable to write new config file")

Expand Down

0 comments on commit 564450a

Please sign in to comment.