Skip to content

Commit 33e5304

Browse files
authored
Merge pull request #65 from Materials-Data-Science-and-Informatics/fix/non_ascii
json dumps wont force ascii characters
2 parents 891207b + 7648c75 commit 33e5304

9 files changed

Lines changed: 30 additions & 14 deletions

File tree

.somesy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "somesy"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
description = "A CLI tool for synchronizing software project metadata."
55
keywords = ["metadata", "FAIR"]
66
license = "MIT"

CITATION.cff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ type: software
33
message: If you use this software, please cite it using this metadata.
44

55
title: somesy
6-
version: 0.2.1
6+
version: 0.3.0
77
abstract: A CLI tool for synchronizing software project metadata.
88
repository-code: https://github.com/Materials-Data-Science-and-Informatics/somesy
99
license: MIT

codemeta.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
],
2626
"name": "somesy",
2727
"description": "A CLI tool for synchronizing software project metadata.",
28-
"version": "0.2.1",
28+
"version": "0.3.0",
2929
"keywords": [
3030
"metadata",
3131
"FAIR"
@@ -62,7 +62,7 @@
6262
{
6363
"@type": "Person",
6464
"givenName": "Jens",
65-
"familyName": "Br\u00f6der",
65+
"familyName": "Bröder",
6666
"email": "j.broeder@fz-juelich.de",
6767
"@id": "https://orcid.org/0000-0001-7939-226X"
6868
},

src/somesy/codemeta/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Integration with codemetapy (to re-generate codemeta as part of somesy sync)."""
1+
"""Integration with codemeta.json (to re-generate codemeta as part of somesy sync)."""
22
from .writer import Codemeta
33

44
__all__ = ["Codemeta"]

src/somesy/codemeta/writer.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""codemeta.json creation module."""
2-
import json
32
import logging
43
from collections import OrderedDict
54
from pathlib import Path
@@ -9,6 +8,7 @@
98

109
from somesy.core.models import Person, ProjectMetadata
1110
from somesy.core.writer import ProjectMetadataWriter
11+
from somesy.json_wrapper import json
1212

1313
logger = logging.getLogger("somesy")
1414

@@ -87,7 +87,7 @@ def _init_new_file(self) -> None:
8787
}
8888
# dump to file
8989
with self.path.open("w+") as f:
90-
json.dump(data, f, indent=2)
90+
json.dump(data, f)
9191

9292
def save(self, path: Optional[Path] = None) -> None:
9393
"""Save the codemeta.json file."""
@@ -107,15 +107,14 @@ def save(self, path: Optional[Path] = None) -> None:
107107

108108
with path.open("w") as f:
109109
# codemeta.json indentation is 2 spaces
110-
json.dump(data, f, indent=2)
110+
json.dump(data, f)
111111

112112
@staticmethod
113113
def _from_person(person: Person):
114114
"""Convert project metadata person object to codemeta.json dict for person format."""
115115
person_dict = {
116116
"@type": "Person",
117117
}
118-
logger.debug(f"Converting person {person} to codemeta.json format.")
119118
if person.given_names:
120119
person_dict["givenName"] = person.given_names
121120
if person.family_names:
@@ -144,7 +143,6 @@ def _to_person(person) -> Person:
144143
person_obj["orcid"] = person["@id"].strip()
145144
if "address" in person:
146145
person_obj["address"] = person["address"].strip()
147-
logger.debug(f"Converting person {person_obj} to pydantic person instance.")
148146

149147
return Person(**person_obj)
150148

src/somesy/core/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def model_dump_json(self, *args, **kwargs):
115115

116116
if by_alias:
117117
ret = {self.model_fields[k].alias or k: v for k, v in ret.items()}
118-
return json.dumps(ret)
118+
return json.dumps(ret, ensure_ascii=False)
119119

120120

121121
_SOMESY_TARGETS = ["cff", "pyproject", "package_json", "codemeta"]

src/somesy/json_wrapper.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Utility functions for somesy."""
2+
import json
3+
4+
import wrapt
5+
6+
7+
@wrapt.decorator
8+
def json_dump_wrapper(wrapped, instance, args, kwargs):
9+
"""Wrap json.dump to write non-ascii characters with default indentation."""
10+
# Ensure ensure_ascii is set to False
11+
kwargs["ensure_ascii"] = False
12+
# set indent to 2 if not set
13+
kwargs["indent"] = kwargs.get("indent", 2)
14+
return wrapped(*args, **kwargs)
15+
16+
17+
# Apply the wrapper
18+
json.dump = json_dump_wrapper(json.dump)

src/somesy/package_json/writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"""package.json parser and saver."""
2-
import json
32
import logging
43
from collections import OrderedDict
54
from pathlib import Path
@@ -9,6 +8,7 @@
98

109
from somesy.core.models import Person, ProjectMetadata
1110
from somesy.core.writer import ProjectMetadataWriter
11+
from somesy.json_wrapper import json
1212
from somesy.package_json.models import PackageJsonConfig
1313

1414
logger = logging.getLogger("somesy")
@@ -72,7 +72,7 @@ def save(self, path: Optional[Path] = None) -> None:
7272

7373
with path.open("w") as f:
7474
# package.json indentation is 2 spaces
75-
json.dump(self._data, f, indent=2)
75+
json.dump(self._data, f)
7676

7777
@staticmethod
7878
def _from_person(person: Person):

tests/output/test_codemeta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from somesy.codemeta import Codemeta
2-
import json
2+
from somesy.json_wrapper import json
33

44

55
def test_update_codemeta(somesy_input, tmp_path):

0 commit comments

Comments
 (0)