Skip to content

writer

codemeta.json creation module.

CodeMeta

Bases: ProjectMetadataWriter

Codemeta.json parser and saver.

Source code in src/somesy/codemeta/writer.py
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
class CodeMeta(ProjectMetadataWriter):
    """Codemeta.json parser and saver."""

    def __init__(
        self,
        path: Path,
        merge: Optional[bool] = False,
        pass_validation: Optional[bool] = False,
    ):
        """Codemeta.json parser.

        See [somesy.core.writer.ProjectMetadataWriter.__init__][].
        """
        self.merge = merge
        self._default_context = [
            "https://doi.org/10.5063/schema/codemeta-2.0",
            "https://w3id.org/software-iodata",
            "https://raw.githubusercontent.com/jantman/repostatus.org/master/badges/latest/ontology.jsonld",
            "https://schema.org",
            "https://w3id.org/software-types",
        ]
        mappings: FieldKeyMapping = {
            "repository": ["codeRepository"],
            "homepage": ["softwareHelp"],
            "documentation": ["buildInstructions"],
            "keywords": ["keywords"],
            "authors": ["author"],
            "maintainers": ["maintainer"],
            "contributors": ["contributor"],
        }
        # delete the file if it exists
        if path.is_file() and not self.merge:
            logger.verbose("Deleting existing codemeta.json file.")
            path.unlink()
        super().__init__(
            path,
            create_if_not_exists=True,
            direct_mappings=mappings,
            pass_validation=pass_validation,
        )

        # if merge is True, add necessary keys to the codemeta.json file
        if self.merge:
            # check if the context exists but is not a list
            if isinstance(self._data["@context"], str):
                self._data["@context"] = [self._data["@context"]]
            # finally add each item in the context to the codemeta.json file if it does not exist in the list
            for item in self._default_context:
                if item not in self._data["@context"]:
                    self._data["@context"].append(item)

            # add (or overwrite) the type
            self._data["@type"] = "SoftwareSourceCode"

            # overwrite authors, maintainers, contributors
            self._data["author"] = []
            self._data["maintainer"] = []
            self._data["contributor"] = []

    @property
    def authors(self):
        """Return the only author of the codemeta.json file as list."""
        return self._get_property(self._get_key("publication_authors")) or []

    @authors.setter
    def authors(self, authors: List[Union[Person, Entity]]) -> None:
        """Set the authors of the project."""
        authors_dict = [self._from_person(a) for a in authors]
        self._set_property(self._get_key("authors"), authors_dict)

    @property
    def maintainers(self):
        """Return the maintainers of the codemeta.json file."""
        return self._get_property(self._get_key("maintainers"))

    @maintainers.setter
    def maintainers(self, maintainers: List[Union[Person, Entity]]) -> None:
        """Set the maintainers of the project."""
        maintainers_dict = [self._from_person(m) for m in maintainers]
        self._set_property(self._get_key("maintainers"), maintainers_dict)

    @property
    def contributors(self):
        """Return the contributors of the codemeta.json file."""
        return self._get_property(self._get_key("contributors"))

    @contributors.setter
    def contributors(self, contributors: List[Union[Person, Entity]]) -> None:
        """Set the contributors of the project."""
        contributors_dict = [self._from_person(c) for c in contributors]
        self._set_property(self._get_key("contributors"), contributors_dict)

    def _load(self) -> None:
        """Load codemeta.json file."""
        with self.path.open() as f:
            self._data = json.load(f, object_pairs_hook=OrderedDict)

    def _validate(self) -> None:
        """Validate codemeta.json content using pydantic class."""
        if self.pass_validation:
            return
        invalid_fields = validate_codemeta(self._data)
        if invalid_fields and self.merge:
            raise ValueError(
                f"Invalid fields in codemeta.json: {invalid_fields}. Cannot merge with invalid fields."
            )

    def _init_new_file(self) -> None:
        """Create a new codemeta.json file with bare minimum generic data."""
        data = {
            "@context": [
                "https://doi.org/10.5063/schema/codemeta-2.0",
                "https://w3id.org/software-iodata",
                "https://raw.githubusercontent.com/jantman/repostatus.org/master/badges/latest/ontology.jsonld",
                "https://schema.org",
                "https://w3id.org/software-types",
            ],
            "@type": "SoftwareSourceCode",
            "author": [],
        }
        # dump to file
        with self.path.open("w+") as f:
            json.dump(data, f)

    def save(self, path: Optional[Path] = None) -> None:
        """Save the codemeta.json file."""
        path = path or self.path
        logger.debug(f"Saving codemeta.json to {path}")

        # copy the _data
        data = self._data.copy()

        # set license
        if "license" in data:
            data["license"] = (f"https://spdx.org/licenses/{data['license']}",)

        # if softwareHelp is set, set url to softwareHelp
        if "softwareHelp" in data:
            data["url"] = data["softwareHelp"]

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

    @staticmethod
    def _from_person(person: Union[Person, Entity]) -> dict:
        """Convert project metadata person object to codemeta.json dict for person format."""
        if isinstance(person, Person):
            person_dict = {
                "@type": "Person",
            }
            if person.given_names:
                person_dict["givenName"] = person.given_names
            if person.family_names:
                person_dict["familyName"] = person.family_names
            if person.email:
                person_dict["email"] = person.email
            if person.orcid:
                person_dict["@id"] = str(person.orcid)
                person_dict["identifier"] = str(person.orcid)
            if person.address:
                person_dict["address"] = person.address
            if person.affiliation:
                person_dict["affiliation"] = person.affiliation
            return person_dict
        else:
            entity_dict = {"@type": "Organization", "name": person.name}
            if person.address:
                entity_dict["address"] = person.address
            if person.email:
                entity_dict["email"] = person.email
            if person.date_start:
                entity_dict["startDate"] = person.date_start.isoformat()
            if person.date_end:
                entity_dict["endDate"] = person.date_end.isoformat()
            if person.website:
                entity_dict["@id"] = str(person.website)
                entity_dict["identifier"] = str(person.website)
            if person.rorid:
                entity_dict["@id"] = str(person.rorid)
                entity_dict["identifier"] = str(person.rorid)
            return entity_dict

    @staticmethod
    def _to_person(person) -> Union[Person, Entity]:
        """Convert codemeta.json dict or str for person/entity format to project metadata person object."""
        if "name" in person:
            entity_obj = {"name": person["name"]}
            return Entity(**entity_obj)
        else:
            person_obj = {}
            if "givenName" in person:
                person_obj["given_names"] = person["givenName"].strip()
            if "familyName" in person:
                person_obj["family_names"] = person["familyName"].strip()
            if "email" in person:
                person_obj["email"] = person["email"].strip()
            if "@id" in person:
                person_obj["orcid"] = person["@id"].strip()
            if "address" in person:
                person_obj["address"] = person["address"].strip()

            return Person(**person_obj)

    def _sync_person_list(
        self, old: List[Any], new: List[Union[Person, Entity]]
    ) -> List[Any]:
        """Override the _sync_person_list function from ProjectMetadataWriter.

        This method wont care about existing persons in codemeta.json file.

        Args:
            old (List[Any]): existing persons in codemeta.json file, in this case ignored in the output. However, it is necessary to make the function compatible with the parent class.
            new (List[Person]): new persons to add to codemeta.json file

        Returns:
            List[Any]: list of new persons to add to codemeta.json file

        """
        return new

    def sync(self, metadata: ProjectMetadata) -> None:
        """Sync codemeta.json with project metadata.

        Use existing sync function from ProjectMetadataWriter but update repository and contributors.
        """
        super().sync(metadata)
        self.contributors = metadata.contributors()

        # add the default context items if they are not already in the codemeta.json file
        for item in self._default_context:
            if item not in self._data["@context"]:
                self._data["@context"].append(item)

authors property writable

authors

Return the only author of the codemeta.json file as list.

maintainers property writable

maintainers

Return the maintainers of the codemeta.json file.

contributors property writable

contributors

Return the contributors of the codemeta.json file.

__init__

__init__(
    path: Path,
    merge: Optional[bool] = False,
    pass_validation: Optional[bool] = False,
)

Codemeta.json parser.

See somesy.core.writer.ProjectMetadataWriter.init.

Source code in src/somesy/codemeta/writer.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def __init__(
    self,
    path: Path,
    merge: Optional[bool] = False,
    pass_validation: Optional[bool] = False,
):
    """Codemeta.json parser.

    See [somesy.core.writer.ProjectMetadataWriter.__init__][].
    """
    self.merge = merge
    self._default_context = [
        "https://doi.org/10.5063/schema/codemeta-2.0",
        "https://w3id.org/software-iodata",
        "https://raw.githubusercontent.com/jantman/repostatus.org/master/badges/latest/ontology.jsonld",
        "https://schema.org",
        "https://w3id.org/software-types",
    ]
    mappings: FieldKeyMapping = {
        "repository": ["codeRepository"],
        "homepage": ["softwareHelp"],
        "documentation": ["buildInstructions"],
        "keywords": ["keywords"],
        "authors": ["author"],
        "maintainers": ["maintainer"],
        "contributors": ["contributor"],
    }
    # delete the file if it exists
    if path.is_file() and not self.merge:
        logger.verbose("Deleting existing codemeta.json file.")
        path.unlink()
    super().__init__(
        path,
        create_if_not_exists=True,
        direct_mappings=mappings,
        pass_validation=pass_validation,
    )

    # if merge is True, add necessary keys to the codemeta.json file
    if self.merge:
        # check if the context exists but is not a list
        if isinstance(self._data["@context"], str):
            self._data["@context"] = [self._data["@context"]]
        # finally add each item in the context to the codemeta.json file if it does not exist in the list
        for item in self._default_context:
            if item not in self._data["@context"]:
                self._data["@context"].append(item)

        # add (or overwrite) the type
        self._data["@type"] = "SoftwareSourceCode"

        # overwrite authors, maintainers, contributors
        self._data["author"] = []
        self._data["maintainer"] = []
        self._data["contributor"] = []

save

save(path: Optional[Path] = None) -> None

Save the codemeta.json file.

Source code in src/somesy/codemeta/writer.py
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def save(self, path: Optional[Path] = None) -> None:
    """Save the codemeta.json file."""
    path = path or self.path
    logger.debug(f"Saving codemeta.json to {path}")

    # copy the _data
    data = self._data.copy()

    # set license
    if "license" in data:
        data["license"] = (f"https://spdx.org/licenses/{data['license']}",)

    # if softwareHelp is set, set url to softwareHelp
    if "softwareHelp" in data:
        data["url"] = data["softwareHelp"]

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

sync

sync(metadata: ProjectMetadata) -> None

Sync codemeta.json with project metadata.

Use existing sync function from ProjectMetadataWriter but update repository and contributors.

Source code in src/somesy/codemeta/writer.py
237
238
239
240
241
242
243
244
245
246
247
248
def sync(self, metadata: ProjectMetadata) -> None:
    """Sync codemeta.json with project metadata.

    Use existing sync function from ProjectMetadataWriter but update repository and contributors.
    """
    super().sync(metadata)
    self.contributors = metadata.contributors()

    # add the default context items if they are not already in the codemeta.json file
    for item in self._default_context:
        if item not in self._data["@context"]:
            self._data["@context"].append(item)