Skip to content

Commit

Permalink
Merge pull request #65 from Materials-Data-Science-and-Informatics/fi…
Browse files Browse the repository at this point in the history
…x/non_ascii

json dumps wont force ascii characters
  • Loading branch information
mustafasoylu authored Jan 16, 2024
2 parents 891207b + 7648c75 commit 33e5304
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .somesy.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "somesy"
version = "0.2.1"
version = "0.3.0"
description = "A CLI tool for synchronizing software project metadata."
keywords = ["metadata", "FAIR"]
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type: software
message: If you use this software, please cite it using this metadata.

title: somesy
version: 0.2.1
version: 0.3.0
abstract: A CLI tool for synchronizing software project metadata.
repository-code: https://github.com/Materials-Data-Science-and-Informatics/somesy
license: MIT
Expand Down
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
],
"name": "somesy",
"description": "A CLI tool for synchronizing software project metadata.",
"version": "0.2.1",
"version": "0.3.0",
"keywords": [
"metadata",
"FAIR"
Expand Down Expand Up @@ -62,7 +62,7 @@
{
"@type": "Person",
"givenName": "Jens",
"familyName": "Br\u00f6der",
"familyName": "Bröder",
"email": "[email protected]",
"@id": "https://orcid.org/0000-0001-7939-226X"
},
Expand Down
2 changes: 1 addition & 1 deletion src/somesy/codemeta/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Integration with codemetapy (to re-generate codemeta as part of somesy sync)."""
"""Integration with codemeta.json (to re-generate codemeta as part of somesy sync)."""
from .writer import Codemeta

__all__ = ["Codemeta"]
8 changes: 3 additions & 5 deletions src/somesy/codemeta/writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""codemeta.json creation module."""
import json
import logging
from collections import OrderedDict
from pathlib import Path
Expand All @@ -9,6 +8,7 @@

from somesy.core.models import Person, ProjectMetadata
from somesy.core.writer import ProjectMetadataWriter
from somesy.json_wrapper import json

logger = logging.getLogger("somesy")

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

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

with path.open("w") as f:
# codemeta.json indentation is 2 spaces
json.dump(data, f, indent=2)
json.dump(data, f)

@staticmethod
def _from_person(person: Person):
"""Convert project metadata person object to codemeta.json dict for person format."""
person_dict = {
"@type": "Person",
}
logger.debug(f"Converting person {person} to codemeta.json format.")
if person.given_names:
person_dict["givenName"] = person.given_names
if person.family_names:
Expand Down Expand Up @@ -144,7 +143,6 @@ def _to_person(person) -> Person:
person_obj["orcid"] = person["@id"].strip()
if "address" in person:
person_obj["address"] = person["address"].strip()
logger.debug(f"Converting person {person_obj} to pydantic person instance.")

return Person(**person_obj)

Expand Down
2 changes: 1 addition & 1 deletion src/somesy/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def model_dump_json(self, *args, **kwargs):

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


_SOMESY_TARGETS = ["cff", "pyproject", "package_json", "codemeta"]
Expand Down
18 changes: 18 additions & 0 deletions src/somesy/json_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Utility functions for somesy."""
import json

import wrapt


@wrapt.decorator
def json_dump_wrapper(wrapped, instance, args, kwargs):
"""Wrap json.dump to write non-ascii characters with default indentation."""
# Ensure ensure_ascii is set to False
kwargs["ensure_ascii"] = False
# set indent to 2 if not set
kwargs["indent"] = kwargs.get("indent", 2)
return wrapped(*args, **kwargs)


# Apply the wrapper
json.dump = json_dump_wrapper(json.dump)
4 changes: 2 additions & 2 deletions src/somesy/package_json/writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""package.json parser and saver."""
import json
import logging
from collections import OrderedDict
from pathlib import Path
Expand All @@ -9,6 +8,7 @@

from somesy.core.models import Person, ProjectMetadata
from somesy.core.writer import ProjectMetadataWriter
from somesy.json_wrapper import json
from somesy.package_json.models import PackageJsonConfig

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

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

@staticmethod
def _from_person(person: Person):
Expand Down
2 changes: 1 addition & 1 deletion tests/output/test_codemeta.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from somesy.codemeta import Codemeta
import json
from somesy.json_wrapper import json


def test_update_codemeta(somesy_input, tmp_path):
Expand Down

0 comments on commit 33e5304

Please sign in to comment.