pydantic-codemeta 0.1.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.
- pydantic_codemeta-0.1.0/CITATION.cff +23 -0
- pydantic_codemeta-0.1.0/LICENSE +21 -0
- pydantic_codemeta-0.1.0/MANIFEST.in +2 -0
- pydantic_codemeta-0.1.0/PKG-INFO +136 -0
- pydantic_codemeta-0.1.0/README.md +123 -0
- pydantic_codemeta-0.1.0/codemeta.json +59 -0
- pydantic_codemeta-0.1.0/pyproject.toml +48 -0
- pydantic_codemeta-0.1.0/setup.cfg +4 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta/__init__.py +53 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta/base.py +179 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta/codemeta.py +89 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta/normalization.py +59 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta/types.py +318 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta.egg-info/PKG-INFO +136 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta.egg-info/SOURCES.txt +21 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta.egg-info/dependency_links.txt +1 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta.egg-info/requires.txt +2 -0
- pydantic_codemeta-0.1.0/src/pydantic_codemeta.egg-info/top_level.txt +1 -0
- pydantic_codemeta-0.1.0/tests/fixtures/full_codemeta.json +102 -0
- pydantic_codemeta-0.1.0/tests/fixtures/minimal_codemeta.json +14 -0
- pydantic_codemeta-0.1.0/tests/test_codemeta_contracts.py +617 -0
- pydantic_codemeta-0.1.0/tests/test_codemeta_fixtures.py +171 -0
- pydantic_codemeta-0.1.0/tests/test_schemaorg_models.py +530 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use pydantic-codemeta, please cite it using these metadata."
|
|
3
|
+
title: pydantic-codemeta
|
|
4
|
+
type: software
|
|
5
|
+
authors:
|
|
6
|
+
- family-names: Lee
|
|
7
|
+
given-names: Allen
|
|
8
|
+
email: allen.lee@asu.edu
|
|
9
|
+
orcid: "https://orcid.org/0000-0002-6523-6079"
|
|
10
|
+
affiliation: "Arizona State University"
|
|
11
|
+
abstract: >-
|
|
12
|
+
Pydantic v2 models for parsing, validating, and serializing CodeMeta JSON-LD
|
|
13
|
+
metadata while preserving unknown JSON-compatible properties.
|
|
14
|
+
version: 0.1.0
|
|
15
|
+
repository-code: "https://github.com/SciCodes/pydantic-codemeta"
|
|
16
|
+
url: "https://github.com/SciCodes/pydantic-codemeta"
|
|
17
|
+
license: MIT
|
|
18
|
+
keywords:
|
|
19
|
+
- CodeMeta
|
|
20
|
+
- JSON-LD
|
|
21
|
+
- metadata
|
|
22
|
+
- Pydantic
|
|
23
|
+
- research software
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Consortium of Scientific Software Registries and Repositories
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pydantic-codemeta
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: CodeMeta-focused schema.org models built with Pydantic v2
|
|
5
|
+
Author-email: Allen Lee <allen.lee@asu.edu>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Requires-Dist: pydantic<3,>=2
|
|
11
|
+
Requires-Dist: typing-extensions>=4.7
|
|
12
|
+
Dynamic: license-file
|
|
13
|
+
|
|
14
|
+
# pydantic-codemeta
|
|
15
|
+
|
|
16
|
+
Pydantic v2 models for parsing, validating, and serializing [CodeMeta](https://codemeta.github.io/) JSON-LD metadata, with typed Python APIs and round-trip preservation of unknown properties.
|
|
17
|
+
|
|
18
|
+
The package provides a typed representation of common CodeMeta and schema.org terms while preserving unknown JSON-LD properties during round trips.
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- Typed CodeMeta and schema.org models built on Pydantic v2
|
|
23
|
+
- JSON-LD aliases such as `@context`, `@type`, and `codeRepository`
|
|
24
|
+
- Preservation of unknown JSON-compatible properties
|
|
25
|
+
- Explicit legacy CodeMeta migration without implicit document rewriting
|
|
26
|
+
- Typed CodeMeta 3.0 models with strict @context validation
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
|
|
30
|
+
- Python 3.11 or newer
|
|
31
|
+
- Pydantic 2.x
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
python -m pip install pydantic-codemeta
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
Parse a CodeMeta document into a validated model and serialize it back to
|
|
42
|
+
JSON-LD:
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from pydantic_codemeta import CodeMeta
|
|
46
|
+
|
|
47
|
+
document = {
|
|
48
|
+
"@context": "https://w3id.org/codemeta/3.0",
|
|
49
|
+
"@type": "SoftwareSourceCode",
|
|
50
|
+
"name": "Example",
|
|
51
|
+
"codeRepository": "https://example.org/repository",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
metadata = CodeMeta.from_jsonld(document)
|
|
55
|
+
|
|
56
|
+
assert metadata.code_repository == "https://example.org/repository"
|
|
57
|
+
assert metadata.to_jsonld()["codeRepository"] == document["codeRepository"]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Python attributes use snake_case names. `to_jsonld()` emits the corresponding
|
|
61
|
+
CodeMeta and schema.org aliases.
|
|
62
|
+
|
|
63
|
+
### Parsing Behavior
|
|
64
|
+
|
|
65
|
+
- `CodeMeta.from_jsonld()` validates canonical CodeMeta 3.0 without rewriting it.
|
|
66
|
+
- `CodeMeta.from_legacy_jsonld()` explicitly migrates supported legacy forms.
|
|
67
|
+
- Unknown JSON-compatible properties survive parse and serialization round trips.
|
|
68
|
+
|
|
69
|
+
See the
|
|
70
|
+
[model specification](https://github.com/SciCodes/pydantic-codemeta/blob/main/docs/specs/codemeta-pydantic-v2.md)
|
|
71
|
+
for the complete field, context, migration, date, collision, and round-trip
|
|
72
|
+
contracts.
|
|
73
|
+
|
|
74
|
+
## Development
|
|
75
|
+
|
|
76
|
+
Install [uv](https://docs.astral.sh/uv/), then create the development
|
|
77
|
+
environment and run the test suite:
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
uv sync
|
|
81
|
+
uv run python -m pytest -q -s
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Build and validate the distribution artifacts:
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
uv build
|
|
88
|
+
uv run twine check dist/*
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Docker-based checks are also available:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
make docker-test
|
|
95
|
+
make docker-package
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## TestPyPI rehearsal
|
|
99
|
+
|
|
100
|
+
A full TestPyPI rehearsal builds, validates, and uploads the package without
|
|
101
|
+
touching production PyPI. Configure `~/.pypirc` with TestPyPI credentials:
|
|
102
|
+
|
|
103
|
+
```ini
|
|
104
|
+
[testpypi]
|
|
105
|
+
username = __token__
|
|
106
|
+
password = pypi-<your-testpypi-token>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Then run:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
make release-test
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
This runs `clean`, `test`, `package`, `check`, and `publish-test` (uploads to
|
|
116
|
+
TestPyPI). After uploading, verify the package installs from TestPyPI:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
make smoke-test
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Production PyPI releases are published by the GitHub Actions release workflow
|
|
123
|
+
using PyPI Trusted Publishing. Do not use `make publish` for production
|
|
124
|
+
releases.
|
|
125
|
+
|
|
126
|
+
## Contributing
|
|
127
|
+
|
|
128
|
+
Contributions are welcome. See
|
|
129
|
+
[CONTRIBUTING.md](https://github.com/SciCodes/pydantic-codemeta/blob/main/CONTRIBUTING.md)
|
|
130
|
+
for development setup, testing, formatting, pull request guidelines, and the
|
|
131
|
+
release process.
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
`pydantic-codemeta` is distributed under the
|
|
136
|
+
[MIT License](https://github.com/SciCodes/pydantic-codemeta/blob/main/LICENSE).
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# pydantic-codemeta
|
|
2
|
+
|
|
3
|
+
Pydantic v2 models for parsing, validating, and serializing [CodeMeta](https://codemeta.github.io/) JSON-LD metadata, with typed Python APIs and round-trip preservation of unknown properties.
|
|
4
|
+
|
|
5
|
+
The package provides a typed representation of common CodeMeta and schema.org terms while preserving unknown JSON-LD properties during round trips.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Typed CodeMeta and schema.org models built on Pydantic v2
|
|
10
|
+
- JSON-LD aliases such as `@context`, `@type`, and `codeRepository`
|
|
11
|
+
- Preservation of unknown JSON-compatible properties
|
|
12
|
+
- Explicit legacy CodeMeta migration without implicit document rewriting
|
|
13
|
+
- Typed CodeMeta 3.0 models with strict @context validation
|
|
14
|
+
|
|
15
|
+
## Requirements
|
|
16
|
+
|
|
17
|
+
- Python 3.11 or newer
|
|
18
|
+
- Pydantic 2.x
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
python -m pip install pydantic-codemeta
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
Parse a CodeMeta document into a validated model and serialize it back to
|
|
29
|
+
JSON-LD:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from pydantic_codemeta import CodeMeta
|
|
33
|
+
|
|
34
|
+
document = {
|
|
35
|
+
"@context": "https://w3id.org/codemeta/3.0",
|
|
36
|
+
"@type": "SoftwareSourceCode",
|
|
37
|
+
"name": "Example",
|
|
38
|
+
"codeRepository": "https://example.org/repository",
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
metadata = CodeMeta.from_jsonld(document)
|
|
42
|
+
|
|
43
|
+
assert metadata.code_repository == "https://example.org/repository"
|
|
44
|
+
assert metadata.to_jsonld()["codeRepository"] == document["codeRepository"]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Python attributes use snake_case names. `to_jsonld()` emits the corresponding
|
|
48
|
+
CodeMeta and schema.org aliases.
|
|
49
|
+
|
|
50
|
+
### Parsing Behavior
|
|
51
|
+
|
|
52
|
+
- `CodeMeta.from_jsonld()` validates canonical CodeMeta 3.0 without rewriting it.
|
|
53
|
+
- `CodeMeta.from_legacy_jsonld()` explicitly migrates supported legacy forms.
|
|
54
|
+
- Unknown JSON-compatible properties survive parse and serialization round trips.
|
|
55
|
+
|
|
56
|
+
See the
|
|
57
|
+
[model specification](https://github.com/SciCodes/pydantic-codemeta/blob/main/docs/specs/codemeta-pydantic-v2.md)
|
|
58
|
+
for the complete field, context, migration, date, collision, and round-trip
|
|
59
|
+
contracts.
|
|
60
|
+
|
|
61
|
+
## Development
|
|
62
|
+
|
|
63
|
+
Install [uv](https://docs.astral.sh/uv/), then create the development
|
|
64
|
+
environment and run the test suite:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
uv sync
|
|
68
|
+
uv run python -m pytest -q -s
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Build and validate the distribution artifacts:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
uv build
|
|
75
|
+
uv run twine check dist/*
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Docker-based checks are also available:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
make docker-test
|
|
82
|
+
make docker-package
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## TestPyPI rehearsal
|
|
86
|
+
|
|
87
|
+
A full TestPyPI rehearsal builds, validates, and uploads the package without
|
|
88
|
+
touching production PyPI. Configure `~/.pypirc` with TestPyPI credentials:
|
|
89
|
+
|
|
90
|
+
```ini
|
|
91
|
+
[testpypi]
|
|
92
|
+
username = __token__
|
|
93
|
+
password = pypi-<your-testpypi-token>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Then run:
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
make release-test
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
This runs `clean`, `test`, `package`, `check`, and `publish-test` (uploads to
|
|
103
|
+
TestPyPI). After uploading, verify the package installs from TestPyPI:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
make smoke-test
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Production PyPI releases are published by the GitHub Actions release workflow
|
|
110
|
+
using PyPI Trusted Publishing. Do not use `make publish` for production
|
|
111
|
+
releases.
|
|
112
|
+
|
|
113
|
+
## Contributing
|
|
114
|
+
|
|
115
|
+
Contributions are welcome. See
|
|
116
|
+
[CONTRIBUTING.md](https://github.com/SciCodes/pydantic-codemeta/blob/main/CONTRIBUTING.md)
|
|
117
|
+
for development setup, testing, formatting, pull request guidelines, and the
|
|
118
|
+
release process.
|
|
119
|
+
|
|
120
|
+
## License
|
|
121
|
+
|
|
122
|
+
`pydantic-codemeta` is distributed under the
|
|
123
|
+
[MIT License](https://github.com/SciCodes/pydantic-codemeta/blob/main/LICENSE).
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"@context": "https://w3id.org/codemeta/3.0",
|
|
3
|
+
"@type": "SoftwareSourceCode",
|
|
4
|
+
"name": "pydantic-codemeta",
|
|
5
|
+
"description": "Pydantic v2 models for parsing, validating, and serializing CodeMeta JSON-LD metadata while preserving unknown JSON-compatible properties.",
|
|
6
|
+
"version": "0.1.0",
|
|
7
|
+
"author": {
|
|
8
|
+
"@id": "https://orcid.org/0000-0002-6523-6079",
|
|
9
|
+
"@type": "Person",
|
|
10
|
+
"givenName": "Allen",
|
|
11
|
+
"familyName": "Lee",
|
|
12
|
+
"email": "allen.lee@asu.edu",
|
|
13
|
+
"affiliation": [
|
|
14
|
+
{
|
|
15
|
+
"@type": "Organization",
|
|
16
|
+
"@id": "https://ror.org/03efmqc40",
|
|
17
|
+
"name": "Arizona State University"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"@type": "Organization",
|
|
21
|
+
"@id": "https://ror.org/01p0n4966",
|
|
22
|
+
"name": "Open Modeling Foundation"
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"maintainer": {
|
|
27
|
+
"@type": "Person",
|
|
28
|
+
"givenName": "Allen",
|
|
29
|
+
"familyName": "Lee",
|
|
30
|
+
"email": "allen.lee@asu.edu"
|
|
31
|
+
},
|
|
32
|
+
"copyrightHolder": {
|
|
33
|
+
"@type": "Organization",
|
|
34
|
+
"name": "Consortium of Scientific Software Registries and Repositories"
|
|
35
|
+
},
|
|
36
|
+
"copyrightYear": 2026,
|
|
37
|
+
"license": "https://spdx.org/licenses/MIT.html",
|
|
38
|
+
"codeRepository": "https://github.com/SciCodes/pydantic-codemeta",
|
|
39
|
+
"issueTracker": "https://github.com/SciCodes/pydantic-codemeta/issues",
|
|
40
|
+
"readme": "https://github.com/SciCodes/pydantic-codemeta/blob/main/README.md",
|
|
41
|
+
"buildInstructions": "https://github.com/SciCodes/pydantic-codemeta#development",
|
|
42
|
+
"continuousIntegration": "https://github.com/SciCodes/pydantic-codemeta/actions/workflows/test.yml",
|
|
43
|
+
"programmingLanguage": {
|
|
44
|
+
"@type": "ComputerLanguage",
|
|
45
|
+
"name": "Python"
|
|
46
|
+
},
|
|
47
|
+
"runtimePlatform": "Python >= 3.11",
|
|
48
|
+
"softwareRequirements": [
|
|
49
|
+
"pydantic >= 2, < 3",
|
|
50
|
+
"typing-extensions >= 4.7"
|
|
51
|
+
],
|
|
52
|
+
"keywords": [
|
|
53
|
+
"CodeMeta",
|
|
54
|
+
"JSON-LD",
|
|
55
|
+
"metadata",
|
|
56
|
+
"Pydantic",
|
|
57
|
+
"research software"
|
|
58
|
+
]
|
|
59
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pydantic-codemeta"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "CodeMeta-focused schema.org models built with Pydantic v2"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [
|
|
14
|
+
{ name = "Allen Lee", email = "allen.lee@asu.edu" },
|
|
15
|
+
]
|
|
16
|
+
dependencies = [
|
|
17
|
+
"pydantic>=2,<3",
|
|
18
|
+
"typing-extensions>=4.7",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[dependency-groups]
|
|
22
|
+
dev = [
|
|
23
|
+
"build>=1.2.2",
|
|
24
|
+
"twine>=6,<7",
|
|
25
|
+
"pytest>=9.0.3",
|
|
26
|
+
"ruff>=0.6",
|
|
27
|
+
"mdformat>=0.7",
|
|
28
|
+
"mdformat-gfm>=0.3",
|
|
29
|
+
"mdformat-frontmatter>=2",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
[tool.pytest.ini_options]
|
|
33
|
+
testpaths = ["tests"]
|
|
34
|
+
|
|
35
|
+
[tool.ruff]
|
|
36
|
+
line-length = 88
|
|
37
|
+
|
|
38
|
+
[tool.ruff.lint]
|
|
39
|
+
select = ["E", "F", "W", "I", "UP", "B", "DTZ"]
|
|
40
|
+
|
|
41
|
+
[tool.ruff.lint.per-file-ignores]
|
|
42
|
+
"tests/**/*.py" = ["DTZ001"]
|
|
43
|
+
|
|
44
|
+
[tool.setuptools]
|
|
45
|
+
package-dir = {"" = "src"}
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.packages.find]
|
|
48
|
+
where = ["src"]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Minimal schema.org models used for CodeMeta metadata."""
|
|
2
|
+
|
|
3
|
+
from .base import JsonLdContext, PropertyValue, SchemaOrgBase, Thing
|
|
4
|
+
from .codemeta import (
|
|
5
|
+
CODEMETA_V3_CONTEXT,
|
|
6
|
+
CodeMeta,
|
|
7
|
+
CodeMetaV3,
|
|
8
|
+
codemeta_to_codemeta_v3,
|
|
9
|
+
codemeta_v3_to_codemeta,
|
|
10
|
+
)
|
|
11
|
+
from .normalization import migrate_legacy_codemeta, normalize_jsonld_context
|
|
12
|
+
from .types import (
|
|
13
|
+
ComputerLanguage,
|
|
14
|
+
ContactPoint,
|
|
15
|
+
CreativeWork,
|
|
16
|
+
DataFeed,
|
|
17
|
+
MediaObject,
|
|
18
|
+
Organization,
|
|
19
|
+
Person,
|
|
20
|
+
PostalAddress,
|
|
21
|
+
Review,
|
|
22
|
+
Role,
|
|
23
|
+
ScholarlyArticle,
|
|
24
|
+
SoftwareApplication,
|
|
25
|
+
SoftwareSourceCode,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"CodeMeta",
|
|
30
|
+
"CodeMetaV3",
|
|
31
|
+
"CODEMETA_V3_CONTEXT",
|
|
32
|
+
"codemeta_v3_to_codemeta",
|
|
33
|
+
"codemeta_to_codemeta_v3",
|
|
34
|
+
"normalize_jsonld_context",
|
|
35
|
+
"migrate_legacy_codemeta",
|
|
36
|
+
"JsonLdContext",
|
|
37
|
+
"ComputerLanguage",
|
|
38
|
+
"ContactPoint",
|
|
39
|
+
"CreativeWork",
|
|
40
|
+
"DataFeed",
|
|
41
|
+
"MediaObject",
|
|
42
|
+
"Organization",
|
|
43
|
+
"Person",
|
|
44
|
+
"PostalAddress",
|
|
45
|
+
"PropertyValue",
|
|
46
|
+
"Review",
|
|
47
|
+
"Role",
|
|
48
|
+
"SchemaOrgBase",
|
|
49
|
+
"ScholarlyArticle",
|
|
50
|
+
"SoftwareApplication",
|
|
51
|
+
"SoftwareSourceCode",
|
|
52
|
+
"Thing",
|
|
53
|
+
]
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""Shared JSON-LD behavior and base schema.org models."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from math import isfinite
|
|
7
|
+
from typing import Annotated, Literal, Self, TypeAlias, cast
|
|
8
|
+
|
|
9
|
+
from pydantic import (
|
|
10
|
+
BaseModel,
|
|
11
|
+
BeforeValidator,
|
|
12
|
+
ConfigDict,
|
|
13
|
+
Field,
|
|
14
|
+
JsonValue,
|
|
15
|
+
StrictBool,
|
|
16
|
+
StrictFloat,
|
|
17
|
+
StrictInt,
|
|
18
|
+
model_validator,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _check_raw_json_value(value: object, path: str) -> None:
|
|
23
|
+
"""Reject Python-only values before Pydantic can coerce them."""
|
|
24
|
+
|
|
25
|
+
value_type = type(value)
|
|
26
|
+
if value is None or value_type in (bool, int, str):
|
|
27
|
+
return
|
|
28
|
+
if value_type is float:
|
|
29
|
+
if not isfinite(value):
|
|
30
|
+
raise ValueError(f"{path} must contain only finite JSON numbers")
|
|
31
|
+
return
|
|
32
|
+
if value_type is list:
|
|
33
|
+
for index, item in enumerate(value):
|
|
34
|
+
_check_raw_json_value(item, f"{path}[{index}]")
|
|
35
|
+
return
|
|
36
|
+
if value_type is dict:
|
|
37
|
+
for key, item in value.items():
|
|
38
|
+
if type(key) is not str:
|
|
39
|
+
raise ValueError(f"{path} must contain only string object keys")
|
|
40
|
+
_check_raw_json_value(item, f"{path}.{key}")
|
|
41
|
+
return
|
|
42
|
+
raise ValueError(f"{path} must be a raw JSON value")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _validate_raw_json_value(value: object) -> object:
|
|
46
|
+
_check_raw_json_value(value, "$")
|
|
47
|
+
return value
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _declared_input_keys(model: type[BaseModel]) -> set[str]:
|
|
51
|
+
"""Return field names and string aliases accepted as declared input."""
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
key
|
|
55
|
+
for name, field in model.model_fields.items()
|
|
56
|
+
for key in (name, field.alias)
|
|
57
|
+
if isinstance(key, str)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
RawJsonValue: TypeAlias = Annotated[
|
|
62
|
+
JsonValue, BeforeValidator(_validate_raw_json_value)
|
|
63
|
+
]
|
|
64
|
+
RawJsonObject: TypeAlias = Annotated[
|
|
65
|
+
dict[str, JsonValue], BeforeValidator(_validate_raw_json_value)
|
|
66
|
+
]
|
|
67
|
+
JsonLdContextObject: TypeAlias = RawJsonObject
|
|
68
|
+
JsonLdContextEntry: TypeAlias = str | JsonLdContextObject | None
|
|
69
|
+
JsonLdContext: TypeAlias = JsonLdContextEntry | list[JsonLdContextEntry]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class SchemaOrgBase(BaseModel):
|
|
73
|
+
"""Common JSON-LD behavior for the package's typed resources.
|
|
74
|
+
|
|
75
|
+
Declared fields are typed vocabulary terms. Extra fields are retained as
|
|
76
|
+
raw JSON-compatible values so future terms and extensions can round-trip.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
model_config = ConfigDict(
|
|
80
|
+
allow_inf_nan=False,
|
|
81
|
+
populate_by_name=True,
|
|
82
|
+
extra="allow",
|
|
83
|
+
use_enum_values=True,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
__pydantic_extra__: dict[str, object] = Field(init=False)
|
|
87
|
+
|
|
88
|
+
id: str | None = Field(None, alias="@id")
|
|
89
|
+
type: str = Field(..., alias="@type")
|
|
90
|
+
|
|
91
|
+
@model_validator(mode="before")
|
|
92
|
+
@classmethod
|
|
93
|
+
def reject_alias_name_collisions(cls, data: object) -> object:
|
|
94
|
+
"""Reject ambiguous aliases and Python-only extra values."""
|
|
95
|
+
|
|
96
|
+
if not isinstance(data, Mapping):
|
|
97
|
+
return data
|
|
98
|
+
|
|
99
|
+
field_keys = _declared_input_keys(cls)
|
|
100
|
+
collisions = [
|
|
101
|
+
(name, field.alias)
|
|
102
|
+
for name, field in cls.model_fields.items()
|
|
103
|
+
if isinstance(field.alias, str)
|
|
104
|
+
and field.alias != name
|
|
105
|
+
and name in data
|
|
106
|
+
and field.alias in data
|
|
107
|
+
]
|
|
108
|
+
if collisions:
|
|
109
|
+
rendered = ", ".join(
|
|
110
|
+
f"{name!r} and {alias!r}" for name, alias in collisions
|
|
111
|
+
)
|
|
112
|
+
raise ValueError(f"Conflicting field name and JSON-LD alias: {rendered}")
|
|
113
|
+
|
|
114
|
+
for key, value in data.items():
|
|
115
|
+
if key not in field_keys:
|
|
116
|
+
_check_raw_json_value(value, key)
|
|
117
|
+
return data
|
|
118
|
+
|
|
119
|
+
def to_jsonld(self) -> dict[str, object]:
|
|
120
|
+
"""Serialize the model using JSON-LD aliases."""
|
|
121
|
+
|
|
122
|
+
payload = cast(
|
|
123
|
+
dict[str, object],
|
|
124
|
+
self.model_dump(
|
|
125
|
+
by_alias=True,
|
|
126
|
+
mode="json",
|
|
127
|
+
),
|
|
128
|
+
)
|
|
129
|
+
return cast(dict[str, object], _prune_declared_none(self, payload))
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def from_jsonld(cls, data: dict[str, object]) -> Self:
|
|
133
|
+
"""Validate a JSON-LD dictionary without normalization or migration."""
|
|
134
|
+
|
|
135
|
+
return cls.model_validate(data)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _prune_declared_none(value: object, serialized: object) -> object:
|
|
139
|
+
"""Omit null typed fields while retaining nulls inside raw JSON extras."""
|
|
140
|
+
|
|
141
|
+
if isinstance(value, SchemaOrgBase) and isinstance(serialized, dict):
|
|
142
|
+
for name, field in type(value).model_fields.items():
|
|
143
|
+
alias = field.serialization_alias or field.alias or name
|
|
144
|
+
field_value = getattr(value, name)
|
|
145
|
+
if field_value is None:
|
|
146
|
+
preserve_null_context = (
|
|
147
|
+
name == "context" and name in value.model_fields_set
|
|
148
|
+
)
|
|
149
|
+
if not preserve_null_context:
|
|
150
|
+
serialized.pop(alias, None)
|
|
151
|
+
elif alias in serialized:
|
|
152
|
+
serialized[alias] = _prune_declared_none(field_value, serialized[alias])
|
|
153
|
+
return serialized
|
|
154
|
+
if isinstance(value, list) and isinstance(serialized, list):
|
|
155
|
+
return [
|
|
156
|
+
_prune_declared_none(item, dumped)
|
|
157
|
+
for item, dumped in zip(value, serialized, strict=True)
|
|
158
|
+
]
|
|
159
|
+
return serialized
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class Thing(SchemaOrgBase):
|
|
163
|
+
"""Base schema.org Thing model."""
|
|
164
|
+
|
|
165
|
+
type: Literal["Thing"] = Field("Thing", alias="@type")
|
|
166
|
+
name: str | None = None
|
|
167
|
+
description: str | None = None
|
|
168
|
+
url: str | list[str] | None = None
|
|
169
|
+
identifier: str | PropertyValue | list[str | PropertyValue] | None = None
|
|
170
|
+
related_link: str | list[str] | None = Field(None, alias="relatedLink")
|
|
171
|
+
same_as: str | list[str] | None = Field(None, alias="sameAs")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
class PropertyValue(Thing):
|
|
175
|
+
"""A schema.org structured property value."""
|
|
176
|
+
|
|
177
|
+
type: Literal["PropertyValue"] = Field("PropertyValue", alias="@type")
|
|
178
|
+
value: StrictBool | str | StrictInt | StrictFloat | RawJsonObject | None = None
|
|
179
|
+
property_id: str | None = Field(None, alias="propertyID")
|