citesync 0.2.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {citesync-0.2.0 → citesync-0.3.0}/PKG-INFO +15 -7
- {citesync-0.2.0 → citesync-0.3.0}/README.md +11 -4
- {citesync-0.2.0 → citesync-0.3.0}/pyproject.toml +5 -3
- {citesync-0.2.0 → citesync-0.3.0}/pyproject.toml.orig +5 -3
- citesync-0.3.0/src/citesync/MAPPING_SOURCE +5 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/check.py +11 -2
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/config.py +38 -0
- citesync-0.3.0/src/citesync/core_metadata.py +131 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/formats.py +51 -11
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/mapping-local.json +206 -104
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/mapping.schema.json +4 -0
- citesync-0.3.0/src/citesync/playground.py +198 -0
- citesync-0.3.0/src/citesync/playground_exports.py +153 -0
- citesync-0.3.0/src/citesync/research_metadata.py +316 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/rules.py +13 -3
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/write.py +121 -20
- citesync-0.2.0/src/citesync/MAPPING_SOURCE +0 -5
- {citesync-0.2.0 → citesync-0.3.0}/LICENSE +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/NOTICE +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/__init__.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/__main__.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/cli.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/codecs.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/errors.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/jsonsplice.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/mapping.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/positions.py +0 -0
- {citesync-0.2.0 → citesync-0.3.0}/src/citesync/py.typed +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: citesync
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Generate and synchronize software citation files from package metadata. CLI, pre-commit hook and browser playground.
|
|
5
5
|
Keywords: citation,CITATION.cff,codemeta,zenodo,software-metadata,pre-commit
|
|
6
6
|
Author: Simon van Lierde
|
|
7
7
|
Author-email: Simon van Lierde <code@duinlab.nl>
|
|
@@ -22,7 +22,8 @@ Requires-Dist: packaging>=26.3
|
|
|
22
22
|
Requires-Dist: ruamel-yaml>=0.19
|
|
23
23
|
Requires-Dist: tomlkit>=0.15.2
|
|
24
24
|
Requires-Python: >=3.12
|
|
25
|
-
Project-URL: Homepage, https://
|
|
25
|
+
Project-URL: Homepage, https://simonvanlierde.com/citesync/
|
|
26
|
+
Project-URL: Source, https://github.com/simonvanlierde/citesync
|
|
26
27
|
Project-URL: Issues, https://github.com/simonvanlierde/citesync/issues
|
|
27
28
|
Project-URL: Changelog, https://github.com/simonvanlierde/citesync/blob/main/CHANGELOG.md
|
|
28
29
|
Description-Content-Type: text/markdown
|
|
@@ -40,7 +41,7 @@ Status: experimental. It may change in ways that break your code, without notice
|
|
|
40
41
|
|
|
41
42
|
Questions and bugs: see [CONTRIBUTING.md](https://github.com/simonvanlierde/citesync/blob/main/.github/CONTRIBUTING.md).
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
Generate software citation files from package metadata, then keep them in sync across CITATION.cff, codemeta.json, .zenodo.json and package manifests. Use the CLI, a pre-commit hook or the [browser playground](https://simonvanlierde.com/citesync/). Field mappings come from CodeMeta's crosswalks, as exported by [codemeta-drift](https://github.com/simonvanlierde/codemeta-drift).
|
|
44
45
|
|
|
45
46
|
## Usage
|
|
46
47
|
|
|
@@ -70,12 +71,18 @@ A config is optional. A one-line `citesync.toml` is `ignore = ["description-mism
|
|
|
70
71
|
|
|
71
72
|
- [How check works](https://github.com/simonvanlierde/citesync/blob/main/docs/check.md): the fields, files, options and exit codes.
|
|
72
73
|
- [How write works](https://github.com/simonvanlierde/citesync/blob/main/docs/write.md): what it changes and what it leaves alone.
|
|
74
|
+
- [Supported files](https://github.com/simonvanlierde/citesync/blob/main/docs/formats.md): which files citesync reads, updates and creates, including read-only Python distribution, R and Julia metadata.
|
|
73
75
|
- [How init works](https://github.com/simonvanlierde/citesync/blob/main/docs/init.md): where a new CITATION.cff takes each field from.
|
|
74
76
|
- [Rules](https://github.com/simonvanlierde/citesync/blob/main/docs/rules.md): each rule id, what it checks and how to fix it.
|
|
75
77
|
- [Configuration](https://github.com/simonvanlierde/citesync/blob/main/docs/config.md): where config goes, and every key.
|
|
78
|
+
- [Use citation exports with Pandoc](https://github.com/simonvanlierde/citesync/blob/main/docs/pandoc.md): cite software from the playground’s CSL-JSON or BibTeX files.
|
|
76
79
|
- [Output formats](https://github.com/simonvanlierde/citesync/blob/main/docs/output.md): JSON, GitHub annotations and SARIF.
|
|
77
80
|
- [Tested on real repositories](https://github.com/simonvanlierde/citesync/blob/main/docs/tested-repositories.md): what citesync reports on five public repositories.
|
|
78
81
|
|
|
82
|
+
## Browser playground
|
|
83
|
+
|
|
84
|
+
Generate citation files from existing metadata or basic project details, without installing Python. [Open the playground](https://simonvanlierde.com/citesync/) or [run it locally](https://github.com/simonvanlierde/citesync/blob/main/playground/README.md). Files are processed in the browser; downloading the converter requires a network connection.
|
|
85
|
+
|
|
79
86
|
## Pre-commit hook
|
|
80
87
|
|
|
81
88
|
For [prek](https://prek.j178.dev) or [pre-commit](https://pre-commit.com), in `.pre-commit-config.yaml`:
|
|
@@ -83,20 +90,21 @@ For [prek](https://prek.j178.dev) or [pre-commit](https://pre-commit.com), in `.
|
|
|
83
90
|
```yaml
|
|
84
91
|
repos:
|
|
85
92
|
- repo: https://github.com/simonvanlierde/citesync
|
|
86
|
-
rev: v0.
|
|
93
|
+
rev: v0.3.0
|
|
87
94
|
hooks:
|
|
88
95
|
- id: citesync
|
|
89
96
|
- id: citesync-write
|
|
90
97
|
```
|
|
91
98
|
|
|
92
|
-
The `citesync` hook runs when a commit changes
|
|
99
|
+
The `citesync` hook runs when a commit changes a supported metadata file or a citesync config, at any depth. It checks every file, not only the staged ones, and fails the commit on an error finding. The `citesync-write` hook runs only when you ask for it: `prek run citesync-write --stage manual --all-files`.
|
|
93
100
|
|
|
94
101
|
## Development
|
|
95
102
|
|
|
96
|
-
You need [uv](https://docs.astral.sh/uv/)
|
|
103
|
+
You need [uv](https://docs.astral.sh/uv/), [just](https://just.systems), Node.js 26 and [pnpm](https://pnpm.io/).
|
|
97
104
|
|
|
98
105
|
```sh
|
|
99
106
|
uv sync
|
|
107
|
+
pnpm install --frozen-lockfile
|
|
100
108
|
uv tool install prek # once per machine
|
|
101
109
|
prek install # git hooks
|
|
102
110
|
```
|
|
@@ -11,7 +11,7 @@ Status: experimental. It may change in ways that break your code, without notice
|
|
|
11
11
|
|
|
12
12
|
Questions and bugs: see [CONTRIBUTING.md](https://github.com/simonvanlierde/citesync/blob/main/.github/CONTRIBUTING.md).
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
Generate software citation files from package metadata, then keep them in sync across CITATION.cff, codemeta.json, .zenodo.json and package manifests. Use the CLI, a pre-commit hook or the [browser playground](https://simonvanlierde.com/citesync/). Field mappings come from CodeMeta's crosswalks, as exported by [codemeta-drift](https://github.com/simonvanlierde/codemeta-drift).
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
17
17
|
|
|
@@ -41,12 +41,18 @@ A config is optional. A one-line `citesync.toml` is `ignore = ["description-mism
|
|
|
41
41
|
|
|
42
42
|
- [How check works](https://github.com/simonvanlierde/citesync/blob/main/docs/check.md): the fields, files, options and exit codes.
|
|
43
43
|
- [How write works](https://github.com/simonvanlierde/citesync/blob/main/docs/write.md): what it changes and what it leaves alone.
|
|
44
|
+
- [Supported files](https://github.com/simonvanlierde/citesync/blob/main/docs/formats.md): which files citesync reads, updates and creates, including read-only Python distribution, R and Julia metadata.
|
|
44
45
|
- [How init works](https://github.com/simonvanlierde/citesync/blob/main/docs/init.md): where a new CITATION.cff takes each field from.
|
|
45
46
|
- [Rules](https://github.com/simonvanlierde/citesync/blob/main/docs/rules.md): each rule id, what it checks and how to fix it.
|
|
46
47
|
- [Configuration](https://github.com/simonvanlierde/citesync/blob/main/docs/config.md): where config goes, and every key.
|
|
48
|
+
- [Use citation exports with Pandoc](https://github.com/simonvanlierde/citesync/blob/main/docs/pandoc.md): cite software from the playground’s CSL-JSON or BibTeX files.
|
|
47
49
|
- [Output formats](https://github.com/simonvanlierde/citesync/blob/main/docs/output.md): JSON, GitHub annotations and SARIF.
|
|
48
50
|
- [Tested on real repositories](https://github.com/simonvanlierde/citesync/blob/main/docs/tested-repositories.md): what citesync reports on five public repositories.
|
|
49
51
|
|
|
52
|
+
## Browser playground
|
|
53
|
+
|
|
54
|
+
Generate citation files from existing metadata or basic project details, without installing Python. [Open the playground](https://simonvanlierde.com/citesync/) or [run it locally](https://github.com/simonvanlierde/citesync/blob/main/playground/README.md). Files are processed in the browser; downloading the converter requires a network connection.
|
|
55
|
+
|
|
50
56
|
## Pre-commit hook
|
|
51
57
|
|
|
52
58
|
For [prek](https://prek.j178.dev) or [pre-commit](https://pre-commit.com), in `.pre-commit-config.yaml`:
|
|
@@ -54,20 +60,21 @@ For [prek](https://prek.j178.dev) or [pre-commit](https://pre-commit.com), in `.
|
|
|
54
60
|
```yaml
|
|
55
61
|
repos:
|
|
56
62
|
- repo: https://github.com/simonvanlierde/citesync
|
|
57
|
-
rev: v0.
|
|
63
|
+
rev: v0.3.0
|
|
58
64
|
hooks:
|
|
59
65
|
- id: citesync
|
|
60
66
|
- id: citesync-write
|
|
61
67
|
```
|
|
62
68
|
|
|
63
|
-
The `citesync` hook runs when a commit changes
|
|
69
|
+
The `citesync` hook runs when a commit changes a supported metadata file or a citesync config, at any depth. It checks every file, not only the staged ones, and fails the commit on an error finding. The `citesync-write` hook runs only when you ask for it: `prek run citesync-write --stage manual --all-files`.
|
|
64
70
|
|
|
65
71
|
## Development
|
|
66
72
|
|
|
67
|
-
You need [uv](https://docs.astral.sh/uv/)
|
|
73
|
+
You need [uv](https://docs.astral.sh/uv/), [just](https://just.systems), Node.js 26 and [pnpm](https://pnpm.io/).
|
|
68
74
|
|
|
69
75
|
```sh
|
|
70
76
|
uv sync
|
|
77
|
+
pnpm install --frozen-lockfile
|
|
71
78
|
uv tool install prek # once per machine
|
|
72
79
|
prek install # git hooks
|
|
73
80
|
```
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "citesync"
|
|
3
|
-
version = "0.
|
|
4
|
-
description = "
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "Generate and synchronize software citation files from package metadata. CLI, pre-commit hook and browser playground."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
7
7
|
license = "Apache-2.0"
|
|
@@ -43,7 +43,8 @@ email = "code@duinlab.nl"
|
|
|
43
43
|
citesync = "citesync.cli:main"
|
|
44
44
|
|
|
45
45
|
[project.urls]
|
|
46
|
-
Homepage = "https://
|
|
46
|
+
Homepage = "https://simonvanlierde.com/citesync/"
|
|
47
|
+
Source = "https://github.com/simonvanlierde/citesync"
|
|
47
48
|
Issues = "https://github.com/simonvanlierde/citesync/issues"
|
|
48
49
|
Changelog = "https://github.com/simonvanlierde/citesync/blob/main/CHANGELOG.md"
|
|
49
50
|
|
|
@@ -53,6 +54,7 @@ dev = [
|
|
|
53
54
|
"jsonschema>=4.25",
|
|
54
55
|
"memray>=1.20.0 ; sys_platform != 'win32'",
|
|
55
56
|
"mutmut==3.8.0",
|
|
57
|
+
"pybtex>=0.25",
|
|
56
58
|
"pyinstrument>=5.1.3",
|
|
57
59
|
"pytest>=9.1.1",
|
|
58
60
|
"pytest-cov>=7.1.0",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "citesync"
|
|
3
|
-
version = "0.
|
|
4
|
-
description = "
|
|
3
|
+
version = "0.3.0"
|
|
4
|
+
description = "Generate and synchronize software citation files from package metadata. CLI, pre-commit hook and browser playground."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
7
7
|
authors = [{ name = "Simon van Lierde", email = "code@duinlab.nl" }]
|
|
@@ -30,7 +30,8 @@ dependencies = [
|
|
|
30
30
|
citesync = "citesync.cli:main"
|
|
31
31
|
|
|
32
32
|
[project.urls]
|
|
33
|
-
Homepage = "https://
|
|
33
|
+
Homepage = "https://simonvanlierde.com/citesync/"
|
|
34
|
+
Source = "https://github.com/simonvanlierde/citesync"
|
|
34
35
|
Issues = "https://github.com/simonvanlierde/citesync/issues"
|
|
35
36
|
Changelog = "https://github.com/simonvanlierde/citesync/blob/main/CHANGELOG.md"
|
|
36
37
|
|
|
@@ -40,6 +41,7 @@ dev = [
|
|
|
40
41
|
"jsonschema>=4.25",
|
|
41
42
|
"memray>=1.20.0 ; sys_platform != 'win32'",
|
|
42
43
|
"mutmut==3.8.0",
|
|
44
|
+
"pybtex>=0.25",
|
|
43
45
|
"pyinstrument>=5.1.3",
|
|
44
46
|
"pytest>=9.1.1",
|
|
45
47
|
"pytest-cov>=7.1.0",
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
# Vendored from simonvanlierde/codemeta-drift; tests/test_mapping.py checks the sha256 lines.
|
|
2
|
+
tag v0.4.0
|
|
3
|
+
commit 2028ca26bd29ce7a64e6c741aed65a92a63f3f44
|
|
4
|
+
sha256 80468ba82523afb3611457a77cd6eafc955720f9a95b0dafca63dd97d5502e43 mapping-local.json examples/mapping-3.1.json
|
|
5
|
+
sha256 0551a57279c0d8cd3b1bdafcdd8e47865067432c5652fed7efea35994f695330 mapping.schema.json src/codemeta_drift/mapping.schema.json
|
|
@@ -105,7 +105,14 @@ def read_docs(root: Path, config: Config, mapping: Mapping) -> tuple[list[Doc],
|
|
|
105
105
|
docs, unreadable = [], [Finding(p, None, "file-missing", "listed under files") for p in missing(root, config)]
|
|
106
106
|
for path in discover(root, config):
|
|
107
107
|
try:
|
|
108
|
-
docs.append(
|
|
108
|
+
docs.append(
|
|
109
|
+
read_doc(
|
|
110
|
+
root,
|
|
111
|
+
path,
|
|
112
|
+
mapping,
|
|
113
|
+
keep_empty_contributors=config.zenodo_contributor_type is not None and path != config.source,
|
|
114
|
+
)
|
|
115
|
+
)
|
|
109
116
|
except UnreadableError as err:
|
|
110
117
|
unreadable.append(Finding(err.path, None, "unreadable", err.message, err.line, err.column))
|
|
111
118
|
fmt = format_of(path)
|
|
@@ -195,13 +202,15 @@ def findings(docs: list[Doc], extra: list[Finding], config: Config) -> tuple[set
|
|
|
195
202
|
source = docs[0]
|
|
196
203
|
for doc in docs:
|
|
197
204
|
configured = config.codemeta_version
|
|
198
|
-
if configured and doc.readable and doc.format == "codemeta" and doc.version != configured:
|
|
205
|
+
if configured and doc.readable and doc.is_citation and doc.format == "codemeta" and doc.version != configured:
|
|
199
206
|
detail = f"{doc.version}, configured {configured}"
|
|
200
207
|
found.add(Finding(doc.path, "@context", "context-mismatch", detail))
|
|
201
208
|
for key in dict.fromkeys(key for key, _ in doc.dangling_roles):
|
|
202
209
|
orcids = ", ".join(orcid for k, orcid in doc.dangling_roles if k == key)
|
|
203
210
|
detail = f"a Role points at {orcids}, which no Person in author or contributor has"
|
|
204
211
|
found.add(Finding(doc.path, key, "role-dangling", detail))
|
|
212
|
+
for term, message in doc.import_notes:
|
|
213
|
+
found.add(Finding(doc.path, term, "metadata-omitted", message))
|
|
205
214
|
for key, rule in doc.zenodo_gaps:
|
|
206
215
|
found.add(Finding(doc.path, key, rule, ZENODO_GAPS[rule]))
|
|
207
216
|
for term in TERMS:
|
|
@@ -24,6 +24,7 @@ class Config:
|
|
|
24
24
|
files: tuple[str, ...] | None = None
|
|
25
25
|
manifests: tuple[str, ...] | None = None
|
|
26
26
|
write_manifests: tuple[str, ...] = ()
|
|
27
|
+
zenodo_contributor_type: str | None = None
|
|
27
28
|
keywords_case_sensitive: bool = False
|
|
28
29
|
keywords_ignore_separators: bool = False
|
|
29
30
|
ignore: tuple[str, ...] = ()
|
|
@@ -110,6 +111,39 @@ def _codemeta_version(value: object, origin: str) -> str:
|
|
|
110
111
|
return str(value)
|
|
111
112
|
|
|
112
113
|
|
|
114
|
+
# Zenodo deposition metadata: https://developers.zenodo.org/#representation
|
|
115
|
+
ZENODO_CONTRIBUTOR_TYPES = (
|
|
116
|
+
"ContactPerson",
|
|
117
|
+
"DataCollector",
|
|
118
|
+
"DataCurator",
|
|
119
|
+
"DataManager",
|
|
120
|
+
"Distributor",
|
|
121
|
+
"Editor",
|
|
122
|
+
"HostingInstitution",
|
|
123
|
+
"Other",
|
|
124
|
+
"Producer",
|
|
125
|
+
"ProjectLeader",
|
|
126
|
+
"ProjectManager",
|
|
127
|
+
"ProjectMember",
|
|
128
|
+
"RegistrationAgency",
|
|
129
|
+
"RegistrationAuthority",
|
|
130
|
+
"RelatedPerson",
|
|
131
|
+
"ResearchGroup",
|
|
132
|
+
"RightsHolder",
|
|
133
|
+
"Researcher",
|
|
134
|
+
"Sponsor",
|
|
135
|
+
"Supervisor",
|
|
136
|
+
"WorkPackageLeader",
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def _zenodo_contributor_type(value: object, origin: str) -> str:
|
|
141
|
+
if not isinstance(value, str) or value not in ZENODO_CONTRIBUTOR_TYPES:
|
|
142
|
+
msg = f"{origin}: zenodo-contributor-type must be one of {', '.join(ZENODO_CONTRIBUTOR_TYPES)}"
|
|
143
|
+
raise CheckError(msg)
|
|
144
|
+
return value
|
|
145
|
+
|
|
146
|
+
|
|
113
147
|
def _bool(value: object, origin: str, key: str) -> bool:
|
|
114
148
|
if not isinstance(value, bool):
|
|
115
149
|
msg = f"{origin}: {key} must be true or false"
|
|
@@ -191,6 +225,7 @@ _PARSE: dict[str, Callable[[object, str], Any]] = {
|
|
|
191
225
|
"files": lambda value, origin: _paths(value, CITATION_FILES, origin, "files"),
|
|
192
226
|
"manifests": lambda value, origin: _paths(value, MANIFESTS, origin, "manifests"),
|
|
193
227
|
"write-manifests": _write_manifests,
|
|
228
|
+
"zenodo-contributor-type": _zenodo_contributor_type,
|
|
194
229
|
"keywords-case-sensitive": lambda value, origin: _bool(value, origin, "keywords-case-sensitive"),
|
|
195
230
|
"keywords-ignore-separators": lambda value, origin: _bool(value, origin, "keywords-ignore-separators"),
|
|
196
231
|
"ignore": _ignore,
|
|
@@ -212,6 +247,9 @@ def render(config: Config) -> str:
|
|
|
212
247
|
if config.manifests is not None
|
|
213
248
|
else "# manifests: not set, so every release manifest at the repo root",
|
|
214
249
|
f"write-manifests = {json.dumps(list(config.write_manifests))}",
|
|
250
|
+
f"zenodo-contributor-type = {json.dumps(config.zenodo_contributor_type)}"
|
|
251
|
+
if config.zenodo_contributor_type is not None
|
|
252
|
+
else "# zenodo-contributor-type: not set, so new contributors need a hand edit",
|
|
215
253
|
f"keywords-case-sensitive = {json.dumps(config.keywords_case_sensitive)}",
|
|
216
254
|
f"keywords-ignore-separators = {json.dumps(config.keywords_ignore_separators)}",
|
|
217
255
|
f"ignore = {json.dumps(list(config.ignore))}",
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"""Read distribution headers into the existing PEP 621 field shapes, without loading package code."""
|
|
2
|
+
|
|
3
|
+
import csv
|
|
4
|
+
import unicodedata
|
|
5
|
+
from email.header import decode_header, make_header
|
|
6
|
+
from email.message import Message
|
|
7
|
+
from email.parser import Parser
|
|
8
|
+
from email.policy import compat32
|
|
9
|
+
from email.utils import getaddresses
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from packaging.licenses import InvalidLicenseExpression, canonicalize_license_expression
|
|
13
|
+
from packaging.utils import InvalidName, canonicalize_name
|
|
14
|
+
from packaging.version import InvalidVersion, Version
|
|
15
|
+
|
|
16
|
+
from citesync.errors import UnreadableError
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def parse(text: str, rel: str) -> dict[str, Any]: # noqa: C901 - independent optional headers
|
|
20
|
+
"""Normalize UTF-8 Core Metadata 1.x to 2.6; retain import notices for fields that cannot be represented.
|
|
21
|
+
|
|
22
|
+
Repeated author/address and Project-URL headers are read in full. Conflicting
|
|
23
|
+
scalar fields and repeated URL labels are errors. No build or package import runs.
|
|
24
|
+
"""
|
|
25
|
+
message = Parser(policy=compat32).parsestr(text)
|
|
26
|
+
if message.defects:
|
|
27
|
+
raise UnreadableError(rel, f"malformed metadata headers: {type(message.defects[0]).__name__}")
|
|
28
|
+
|
|
29
|
+
def one(key: str, *, required: bool = False) -> str:
|
|
30
|
+
values = message.get_all(key, [])
|
|
31
|
+
if len(values) > 1:
|
|
32
|
+
raise UnreadableError(rel, f"{key} must appear only once")
|
|
33
|
+
value = str(values[0]).strip() if values else ""
|
|
34
|
+
if required and not value:
|
|
35
|
+
raise UnreadableError(rel, f"missing {key} header")
|
|
36
|
+
return value
|
|
37
|
+
|
|
38
|
+
version = one("Metadata-Version", required=True)
|
|
39
|
+
if version not in {"1.0", "1.1", "1.2", "2.1", "2.2", "2.3", "2.4", "2.5", "2.6"}:
|
|
40
|
+
raise UnreadableError(rel, f"unsupported Metadata-Version {version!r}; citesync reads 1.0 to 2.6")
|
|
41
|
+
project: dict[str, Any] = {"name": one("Name", required=True), "version": one("Version", required=True)}
|
|
42
|
+
try:
|
|
43
|
+
canonicalize_name(project["name"], validate=True)
|
|
44
|
+
Version(project["version"])
|
|
45
|
+
except (InvalidName, InvalidVersion) as err:
|
|
46
|
+
raise UnreadableError(rel, f"invalid Name or Version: {err}") from err
|
|
47
|
+
description = one("Summary") or one("Description") or message.get_payload()
|
|
48
|
+
if isinstance(description, str) and description.strip():
|
|
49
|
+
project["description"] = description.strip()
|
|
50
|
+
if authors := _authors(message, rel):
|
|
51
|
+
project["authors"] = authors
|
|
52
|
+
if urls := _urls(message, rel):
|
|
53
|
+
project["urls"] = urls
|
|
54
|
+
if words := one("Keywords"):
|
|
55
|
+
project["keywords"] = [word.strip() for word in words.split(",") if word.strip()]
|
|
56
|
+
notes = _license(project, one("License-Expression"), one("License"), rel)
|
|
57
|
+
return {"project": project, "build-system": {}, "_import_notes": notes}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _license(project: dict[str, Any], expression: str, legacy: str, rel: str) -> list[tuple[str, str]]:
|
|
61
|
+
notes = []
|
|
62
|
+
if expression or legacy:
|
|
63
|
+
try:
|
|
64
|
+
canonical = canonicalize_license_expression(expression or legacy)
|
|
65
|
+
if "LicenseRef-" in canonical:
|
|
66
|
+
notes.append(
|
|
67
|
+
("license", "LicenseRef has no public SPDX licence URL; review this licence in each citation file.")
|
|
68
|
+
)
|
|
69
|
+
else:
|
|
70
|
+
project["license"] = canonical
|
|
71
|
+
except InvalidLicenseExpression as err:
|
|
72
|
+
if expression:
|
|
73
|
+
raise UnreadableError(rel, f"invalid License-Expression: {err}") from err
|
|
74
|
+
notes.append(
|
|
75
|
+
(
|
|
76
|
+
"license",
|
|
77
|
+
"Legacy License text is not an SPDX expression; set License-Expression before converting it.",
|
|
78
|
+
)
|
|
79
|
+
)
|
|
80
|
+
return notes
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _authors(message: Message, rel: str) -> list[dict[str, str]]:
|
|
84
|
+
contacts: dict[str, dict[str, str]] = {}
|
|
85
|
+
for raw_name, address in getaddresses(message.get_all("Author-email", [])):
|
|
86
|
+
if not address or "@" not in address or any(c.isspace() for c in address):
|
|
87
|
+
raise UnreadableError(rel, "Author-email must contain valid addresses with optional display names")
|
|
88
|
+
if raw_name:
|
|
89
|
+
name = _header(raw_name, rel)
|
|
90
|
+
key = name.casefold()
|
|
91
|
+
if key in contacts and contacts[key].get("email") != address:
|
|
92
|
+
raise UnreadableError(rel, f"Author-email gives conflicting addresses for {name!r}")
|
|
93
|
+
contacts[key] = {"name": name, "email": address}
|
|
94
|
+
authors: dict[str, dict[str, str]] = {}
|
|
95
|
+
for header in message.get_all("Author", []):
|
|
96
|
+
try:
|
|
97
|
+
names = next(csv.reader([_header(str(header), rel)], skipinitialspace=True, strict=True))
|
|
98
|
+
except csv.Error as err:
|
|
99
|
+
raise UnreadableError(rel, f"cannot read Author names: {err}") from err
|
|
100
|
+
for raw_name in names:
|
|
101
|
+
name = raw_name.strip()
|
|
102
|
+
if name:
|
|
103
|
+
authors.setdefault(name.casefold(), contacts.get(name.casefold(), {"name": name}))
|
|
104
|
+
for key, contact in contacts.items():
|
|
105
|
+
authors.setdefault(key, contact)
|
|
106
|
+
return list(authors.values())
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _urls(message: Message, rel: str) -> dict[str, str]:
|
|
110
|
+
urls = {}
|
|
111
|
+
for raw in message.get_all("Project-URL", []):
|
|
112
|
+
label, separator, address = str(raw).partition(",")
|
|
113
|
+
label, address = label.strip(), address.strip()
|
|
114
|
+
if not separator or not label or not address:
|
|
115
|
+
raise UnreadableError(rel, "Project-URL must contain a label, comma and URL")
|
|
116
|
+
if label in urls and urls[label] != address:
|
|
117
|
+
raise UnreadableError(rel, f"Project-URL has conflicting values for {label!r}")
|
|
118
|
+
urls[label] = address
|
|
119
|
+
homes = message.get_all("Home-page", [])
|
|
120
|
+
if len(homes) > 1:
|
|
121
|
+
raise UnreadableError(rel, "Home-page must appear only once")
|
|
122
|
+
if homes:
|
|
123
|
+
urls.setdefault("Homepage", str(homes[0]).strip())
|
|
124
|
+
return urls
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _header(value: str, rel: str) -> str:
|
|
128
|
+
try:
|
|
129
|
+
return unicodedata.normalize("NFC", " ".join(str(make_header(decode_header(value))).split()))
|
|
130
|
+
except (LookupError, UnicodeError) as err:
|
|
131
|
+
raise UnreadableError(rel, f"invalid encoded author header: {err}") from err
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"""Read
|
|
1
|
+
"""Read citation, package and distribution metadata into one record keyed by CodeMeta terms.
|
|
2
2
|
|
|
3
3
|
The mapping says where a term sits. The adapters here only reshape what a path
|
|
4
4
|
can't express (Cargo workspace inheritance, npm strings that may be objects,
|
|
@@ -22,7 +22,14 @@ from citesync.mapping import PEOPLE, PERSON_TERMS, TERMS, Mapping, person_terms,
|
|
|
22
22
|
|
|
23
23
|
CITATION_FILES = {"CITATION.cff": "cff-1.2.0", "codemeta.json": "codemeta", ".zenodo.json": "zenodo"}
|
|
24
24
|
MANIFESTS = {"pyproject.toml": "pep621", "Cargo.toml": "cargo", "package.json": "npm"}
|
|
25
|
-
|
|
25
|
+
IMPORTS = {
|
|
26
|
+
"PKG-INFO": "pep621",
|
|
27
|
+
"METADATA": "pep621",
|
|
28
|
+
"DESCRIPTION": "codemeta",
|
|
29
|
+
"Project.toml": "codemeta",
|
|
30
|
+
"JuliaProject.toml": "codemeta",
|
|
31
|
+
}
|
|
32
|
+
FORMAT_OF = CITATION_FILES | MANIFESTS | IMPORTS
|
|
26
33
|
CODEMETA_VERSIONS = ("2.0", "3.0", "3.1", "4.0")
|
|
27
34
|
CFF_VERSIONS = ("1.2.0",)
|
|
28
35
|
|
|
@@ -78,6 +85,7 @@ class Doc:
|
|
|
78
85
|
``[build-system]``, a private package.json.
|
|
79
86
|
readable: False for a file citesync couldn't read, whose record is empty.
|
|
80
87
|
dangling_roles: For codemeta.json, (list, ORCID) for each Role whose person isn't listed.
|
|
88
|
+
import_notes: (term, message) for distribution values omitted during import.
|
|
81
89
|
zenodo_gaps: For a CITATION.cff with no .zenodo.json beside it, (key, rule id) for each
|
|
82
90
|
key Zenodo refuses or drops when it archives a release.
|
|
83
91
|
"""
|
|
@@ -91,18 +99,19 @@ class Doc:
|
|
|
91
99
|
readable: bool = True
|
|
92
100
|
dangling_roles: tuple[tuple[str, str], ...] = ()
|
|
93
101
|
zenodo_gaps: tuple[tuple[str, str], ...] = ()
|
|
102
|
+
import_notes: tuple[tuple[str, str], ...] = ()
|
|
94
103
|
|
|
95
104
|
@property
|
|
96
105
|
def is_citation(self) -> bool:
|
|
97
106
|
"""CITATION.cff, codemeta.json or .zenodo.json."""
|
|
98
|
-
return self.format in CITATION_FILES.values()
|
|
107
|
+
return PurePosixPath(self.path).name not in IMPORTS and self.format in CITATION_FILES.values()
|
|
99
108
|
|
|
100
109
|
|
|
101
110
|
def format_of(path: str) -> str:
|
|
102
111
|
"""The format id of a file, from its name.
|
|
103
112
|
|
|
104
113
|
Raises:
|
|
105
|
-
CheckError: when the
|
|
114
|
+
CheckError: when the filename is not supported.
|
|
106
115
|
"""
|
|
107
116
|
fmt = FORMAT_OF.get(PurePosixPath(path).name)
|
|
108
117
|
if fmt is None:
|
|
@@ -116,9 +125,12 @@ def inside(root: Path, rel: str) -> bool:
|
|
|
116
125
|
return (root / rel).resolve().is_relative_to(root.resolve())
|
|
117
126
|
|
|
118
127
|
|
|
119
|
-
def read_doc(root: Path, rel: str, mapping: Mapping) -> Doc:
|
|
128
|
+
def read_doc(root: Path, rel: str, mapping: Mapping, *, keep_empty_contributors: bool = False) -> Doc:
|
|
120
129
|
"""Read ``rel`` under ``root`` into its record.
|
|
121
130
|
|
|
131
|
+
``keep_empty_contributors`` preserves a Zenodo target's explicit empty contributors
|
|
132
|
+
list, so a configured contributor type can fill it. Never set it for the source.
|
|
133
|
+
|
|
122
134
|
Raises:
|
|
123
135
|
UnreadableError: when the file resolves outside ``root``, can't be read or parsed, has a format version
|
|
124
136
|
citesync doesn't read, or has a key holding a shape its format doesn't allow.
|
|
@@ -145,7 +157,19 @@ def read_doc(root: Path, rel: str, mapping: Mapping) -> Doc:
|
|
|
145
157
|
record[term] = value
|
|
146
158
|
except (AttributeError, TypeError) as err: # a key holding a shape its format doesn't allow
|
|
147
159
|
raise UnreadableError(rel, f"unexpected shape ({err})") from err
|
|
148
|
-
|
|
160
|
+
if keep_empty_contributors and fmt == "zenodo" and data.get("contributors") == []:
|
|
161
|
+
record["contributor"] = ()
|
|
162
|
+
return Doc(
|
|
163
|
+
rel,
|
|
164
|
+
fmt,
|
|
165
|
+
record,
|
|
166
|
+
slots(fmt, mapping),
|
|
167
|
+
version,
|
|
168
|
+
release,
|
|
169
|
+
dangling_roles=roles,
|
|
170
|
+
zenodo_gaps=gaps,
|
|
171
|
+
import_notes=tuple(data.get("_import_notes", ())) if PurePosixPath(rel).name in IMPORTS else (),
|
|
172
|
+
)
|
|
149
173
|
|
|
150
174
|
|
|
151
175
|
def zenodo_gaps(cff: dict[str, Any]) -> tuple[tuple[str, str], ...]:
|
|
@@ -177,11 +201,11 @@ def dangling_roles(data: dict[str, Any]) -> tuple[tuple[str, str], ...]:
|
|
|
177
201
|
for entry in (data[key] if isinstance(data.get(key), list) else [data.get(key)])
|
|
178
202
|
if isinstance(entry, dict)
|
|
179
203
|
]
|
|
180
|
-
people = {
|
|
204
|
+
people = {codemeta_orcid(_ld(e, "id")) for _, e in entries if _ld(e, "type") != "Role"}
|
|
181
205
|
found = []
|
|
182
206
|
for key, entry in entries:
|
|
183
207
|
target = entry.get("schema:author")
|
|
184
|
-
orcid =
|
|
208
|
+
orcid = codemeta_orcid(_ld(target, "id") if isinstance(target, dict) else target)
|
|
185
209
|
if _ld(entry, "type") == "Role" and orcid and orcid not in people:
|
|
186
210
|
found.append((key, orcid))
|
|
187
211
|
return tuple(found)
|
|
@@ -192,18 +216,26 @@ def _ld(node: dict[str, Any], key: str) -> object:
|
|
|
192
216
|
return node.get(f"@{key}", node.get(key))
|
|
193
217
|
|
|
194
218
|
|
|
219
|
+
def codemeta_orcid(value: object) -> str | None:
|
|
220
|
+
"""The ORCID in a CodeMeta node identifier, or None for other URLs, fragments and blank nodes."""
|
|
221
|
+
bare = codecs.orcid_decode(value)
|
|
222
|
+
return bare if bare and re.fullmatch(r"[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]", bare) else None
|
|
223
|
+
|
|
224
|
+
|
|
195
225
|
def slots(fmt: str, mapping: Mapping) -> frozenset[str]:
|
|
196
226
|
"""The terms and person sub-terms ``fmt`` has a place for."""
|
|
197
227
|
return frozenset(term for term in TERMS + PERSON_TERMS if mapping[fmt, term] is not None)
|
|
198
228
|
|
|
199
229
|
|
|
200
|
-
def load(path: Path, rel: str) -> dict[str, Any]: # noqa: C901 - one branch per way a file fails
|
|
201
|
-
"""Parse
|
|
230
|
+
def load(path: Path, rel: str) -> dict[str, Any]: # noqa: C901, PLR0912 - one branch per way a file fails
|
|
231
|
+
"""Parse metadata at ``path`` as TOML, JSON, YAML or distribution headers, named ``rel`` in errors.
|
|
202
232
|
|
|
203
233
|
Raises:
|
|
204
234
|
UnreadableError: when it can't be read or parsed, or its top level isn't a mapping.
|
|
205
235
|
"""
|
|
206
236
|
try:
|
|
237
|
+
if path.name in IMPORTS and path.stat().st_size > 1_000_000:
|
|
238
|
+
raise UnreadableError(rel, "use a metadata file smaller than 1 MB")
|
|
207
239
|
text = path.read_text(encoding="utf-8")
|
|
208
240
|
except OSError as err:
|
|
209
241
|
raise UnreadableError(rel, f"cannot read: {err.strerror}") from err
|
|
@@ -211,6 +243,13 @@ def load(path: Path, rel: str) -> dict[str, Any]: # noqa: C901 - one branch per
|
|
|
211
243
|
raise UnreadableError(rel, f"not UTF-8 at byte {err.start}") from err
|
|
212
244
|
if text.startswith("\ufeff") and path.suffix in {".json", ".toml"}: # YAML skips one; JSON and TOML refuse it
|
|
213
245
|
raise UnreadableError(rel, "starts with a byte order mark: save it as UTF-8 without one")
|
|
246
|
+
if path.name in IMPORTS:
|
|
247
|
+
if IMPORTS[path.name] == "pep621":
|
|
248
|
+
from citesync.core_metadata import parse as parse_import # noqa: PLC0415 - import-only adapter
|
|
249
|
+
else:
|
|
250
|
+
from citesync.research_metadata import parse as parse_import # noqa: PLC0415 - import-only adapter
|
|
251
|
+
|
|
252
|
+
return parse_import(text, rel)
|
|
214
253
|
try:
|
|
215
254
|
if path.suffix == ".toml":
|
|
216
255
|
data = tomllib.loads(text)
|
|
@@ -429,12 +468,13 @@ def person(fmt: str, element: object, parent: str, mapping: Mapping, term: str)
|
|
|
429
468
|
return None
|
|
430
469
|
rel = relative(path, parent)
|
|
431
470
|
values = [element] if rel == "" else resolve(element, rel)
|
|
471
|
+
values = [_ld(element, "id")] if fmt == "codemeta" and rel == "@id" and isinstance(element, dict) else values
|
|
432
472
|
return values[0] if values else None
|
|
433
473
|
|
|
434
474
|
def text(value: object) -> str | None:
|
|
435
475
|
return (codecs.clean(value) or None) if isinstance(value, str) else None
|
|
436
476
|
|
|
437
|
-
orcid = codecs.orcid_decode(sub(orcid_term))
|
|
477
|
+
orcid = (codemeta_orcid if fmt == "codemeta" else codecs.orcid_decode)(sub(orcid_term))
|
|
438
478
|
given, family = None, None
|
|
439
479
|
if mapping[fmt, given_term] == mapping[fmt, family_term]:
|
|
440
480
|
raw = sub(given_term)
|