Skip to content

Commit bf7c3db

Browse files
committed
overwrite existing people info
update person conversion for schema.org
1 parent df1989f commit bf7c3db

1 file changed

Lines changed: 49 additions & 19 deletions

File tree

src/somesy/codemeta/create.py

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
from collections import OrderedDict
55
from pathlib import Path
6-
from typing import List, Optional
6+
from typing import Any, List, Optional
77

88
from rich.pretty import pretty_repr
99

@@ -32,11 +32,15 @@ def __init__(
3232
"maintainers": ["maintainer"],
3333
"contributors": ["contributor"],
3434
}
35+
# delete the file if it exists
36+
if path.is_file():
37+
logger.verbose("Deleting existing codemeta.json file.")
38+
path.unlink()
3539
super().__init__(path, create_if_not_exists=True, direct_mappings=mappings)
3640

3741
@property
3842
def authors(self):
39-
"""Return the only author of the package.json file as list."""
43+
"""Return the only author of the codemeta.json file as list."""
4044
return self._get_property(self._get_key("publication_authors")) or []
4145

4246
@authors.setter
@@ -47,7 +51,7 @@ def authors(self, authors: List[Person]) -> None:
4751

4852
@property
4953
def contributors(self):
50-
"""Return the contributors of the package.json file."""
54+
"""Return the contributors of the codemeta.json file."""
5155
return self._get_property(self._get_key("contributors"))
5256

5357
@contributors.setter
@@ -57,16 +61,16 @@ def contributors(self, contributors: List[Person]) -> None:
5761
self._set_property(self._get_key("contributors"), contributors)
5862

5963
def _load(self) -> None:
60-
"""Load package.json file."""
64+
"""Load codemeta.json file."""
6165
with self.path.open() as f:
6266
self._data = json.load(f, object_pairs_hook=OrderedDict)
6367

6468
def _validate(self) -> None:
65-
"""Validate package.json content using pydantic class."""
69+
"""Validate codemeta.json content using pydantic class."""
6670
config = dict(self._get_property([]))
6771

6872
logger.debug(
69-
f"No validation for codemeta {Codemeta.__name__}: {pretty_repr(config)}"
73+
f"No validation for codemeta.json files {Codemeta.__name__}: {pretty_repr(config)}"
7074
)
7175

7276
def _init_new_file(self) -> None:
@@ -82,13 +86,13 @@ def _init_new_file(self) -> None:
8286
"author": [],
8387
}
8488
# dump to file
85-
with self.path.open("w") as f:
89+
with self.path.open("w+") as f:
8690
json.dump(data, f, indent=2)
8791

8892
def save(self, path: Optional[Path] = None) -> None:
89-
"""Save the package.json file."""
93+
"""Save the codemeta.json file."""
9094
path = path or self.path
91-
logger.debug(f"Saving package.json to {path}")
95+
logger.debug(f"Saving codemeta.json to {path}")
9296

9397
# copy the _data
9498
data = self._data.copy()
@@ -102,38 +106,64 @@ def save(self, path: Optional[Path] = None) -> None:
102106
data["url"] = data["softwareHelp"]
103107

104108
with path.open("w") as f:
105-
# package.json indentation is 2 spaces
109+
# codemeta.json indentation is 2 spaces
106110
json.dump(data, f, indent=2)
107111

108112
@staticmethod
109113
def _from_person(person: Person):
110-
"""Convert project metadata person object to package.json dict for person format."""
114+
"""Convert project metadata person object to codemeta.json dict for person format."""
111115
person_dict = {
112-
"givenName": person.given_names,
113-
"familyName": person.family_names,
114116
"@type": "Person",
115117
}
118+
logger.debug(f"Converting person {person} to codemeta.json format.")
119+
if person.given_names:
120+
person_dict["givenName"] = person.given_names
121+
if person.family_names:
122+
person_dict["familyName"] = person.family_names
116123
if person.email:
117124
person_dict["email"] = person.email
118125
if person.orcid:
119126
person_dict["@id"] = str(person.orcid)
127+
if person.address:
128+
person_dict["address"] = person.address
129+
if person.affiliation:
130+
person_dict["affiliation"] = person.affiliation
120131
return person_dict
121132

122133
@staticmethod
123134
def _to_person(person) -> Person:
124-
"""Convert package.json dict or str for person format to project metadata person object."""
125-
person_obj = {
126-
"given-names": person["givenName"],
127-
"family-names": person["familyName"],
128-
}
135+
"""Convert codemeta.json dict or str for person format to project metadata person object."""
136+
person_obj = {}
137+
if "givenName" in person:
138+
person_obj["given_names"] = person["givenName"].strip()
139+
if "familyName" in person:
140+
person_obj["family_names"] = person["familyName"].strip()
129141
if "email" in person:
130142
person_obj["email"] = person["email"].strip()
131143
if "@id" in person:
132144
person_obj["orcid"] = person["@id"].strip()
145+
if "address" in person:
146+
person_obj["address"] = person["address"].strip()
147+
logger.debug(f"Converting person {person_obj} to pydantic person instance.")
148+
133149
return Person(**person_obj)
134150

151+
def _sync_person_list(self, old: List[Any], new: List[Person]) -> List[Any]:
152+
"""Override the _sync_person_list function from ProjectMetadataWriter.
153+
154+
This method wont care about existing persons in codemeta.json file.
155+
156+
Args:
157+
old (List[Any]): _description_
158+
new (List[Person]): _description_
159+
160+
Returns:
161+
List[Any]: _description_
162+
"""
163+
return new
164+
135165
def sync(self, metadata: ProjectMetadata) -> None:
136-
"""Sync package.json with project metadata.
166+
"""Sync codemeta.json with project metadata.
137167
138168
Use existing sync function from ProjectMetadataWriter but update repository and contributors.
139169
"""

0 commit comments

Comments
 (0)