fairspec-library 0.1.1__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.
- fairspec_library-0.1.1/.gitignore +229 -0
- fairspec_library-0.1.1/PKG-INFO +22 -0
- fairspec_library-0.1.1/README.md +3 -0
- fairspec_library-0.1.1/fairspec_library/__init__.py +29 -0
- fairspec_library-0.1.1/fairspec_library/actions/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/data/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/data/load.py +27 -0
- fairspec_library-0.1.1/fairspec_library/actions/data/load_spec.py +38 -0
- fairspec_library-0.1.1/fairspec_library/actions/data/validate.py +41 -0
- fairspec_library-0.1.1/fairspec_library/actions/data/validate_spec.py +46 -0
- fairspec_library-0.1.1/fairspec_library/actions/data_schema/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/data_schema/infer.py +23 -0
- fairspec_library-0.1.1/fairspec_library/actions/data_schema/infer_spec.py +40 -0
- fairspec_library-0.1.1/fairspec_library/actions/data_schema/render.py +19 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/foreign_key.py +98 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/foreign_key_spec.py +114 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/infer.py +17 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/infer_spec.py +32 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/load.py +20 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/load_spec.py +26 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/render.py +17 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/save.py +20 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/save_spec.py +19 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/validate.py +57 -0
- fairspec_library-0.1.1/fairspec_library/actions/dataset/validate_spec.py +54 -0
- fairspec_library-0.1.1/fairspec_library/actions/file_dialect/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/file_dialect/infer.py +22 -0
- fairspec_library-0.1.1/fairspec_library/actions/file_dialect/infer_spec.py +35 -0
- fairspec_library-0.1.1/fairspec_library/actions/resource/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/resource/infer.py +42 -0
- fairspec_library-0.1.1/fairspec_library/actions/resource/infer_spec.py +37 -0
- fairspec_library-0.1.1/fairspec_library/actions/resource/validate.py +34 -0
- fairspec_library-0.1.1/fairspec_library/actions/resource/validate_spec.py +42 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/infer.py +36 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/infer_spec.py +22 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/load.py +24 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/load_spec.py +41 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/save.py +18 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/save_spec.py +26 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/validate.py +37 -0
- fairspec_library-0.1.1/fairspec_library/actions/table/validate_spec.py +43 -0
- fairspec_library-0.1.1/fairspec_library/actions/table_schema/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/actions/table_schema/infer.py +28 -0
- fairspec_library-0.1.1/fairspec_library/actions/table_schema/infer_spec.py +25 -0
- fairspec_library-0.1.1/fairspec_library/actions/table_schema/render.py +19 -0
- fairspec_library-0.1.1/fairspec_library/models/__init__.py +0 -0
- fairspec_library-0.1.1/fairspec_library/models/table.py +6 -0
- fairspec_library-0.1.1/fairspec_library/plugin.py +3 -0
- fairspec_library-0.1.1/fairspec_library/py.typed +0 -0
- fairspec_library-0.1.1/fairspec_library/system.py +48 -0
- fairspec_library-0.1.1/pyproject.toml +43 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
# .vscode/
|
|
203
|
+
|
|
204
|
+
# Ruff stuff:
|
|
205
|
+
.ruff_cache/
|
|
206
|
+
|
|
207
|
+
# PyPI configuration file
|
|
208
|
+
.pypirc
|
|
209
|
+
|
|
210
|
+
# Marimo
|
|
211
|
+
marimo/_static/
|
|
212
|
+
marimo/_lsp/
|
|
213
|
+
__marimo__/
|
|
214
|
+
|
|
215
|
+
# Streamlit
|
|
216
|
+
.streamlit/secrets.toml
|
|
217
|
+
|
|
218
|
+
# Node
|
|
219
|
+
node_modules/
|
|
220
|
+
jspm_packages/
|
|
221
|
+
.lock-wscript
|
|
222
|
+
build/Release
|
|
223
|
+
.node_repl_history
|
|
224
|
+
*.tgz
|
|
225
|
+
.npm
|
|
226
|
+
*.so
|
|
227
|
+
|
|
228
|
+
# User
|
|
229
|
+
/.claude/settings.local.json
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fairspec-library
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Fairspec Python is a fast data management framework built on top of the Fairspec standard and Polars DataFrames
|
|
5
|
+
Project-URL: homepage, https://github.com/fairspec/fairspec-python
|
|
6
|
+
Project-URL: repository, https://github.com/fairspec/fairspec-python
|
|
7
|
+
Author: Evgeny Karev
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Keywords: data,dataframe,fair,fairspec,jsonschema,library,metadata,polars,python,quality,tableschema,validation
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
12
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Requires-Dist: fairspec-dataset
|
|
15
|
+
Requires-Dist: fairspec-metadata
|
|
16
|
+
Requires-Dist: fairspec-table
|
|
17
|
+
Requires-Dist: genson>=1.3
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
# fairspec-library
|
|
21
|
+
|
|
22
|
+
Fairspec Python is a fast data management framework built on top of the Fairspec standard and Polars DataFrames. It supports various formats like CSV, JSON, and Parquet and integrates with data platforms such as CKAN, Zenodo, and GitHub. For more information, please read the [project's documentation](https://python.fairspec.org).
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
# fairspec-library
|
|
2
|
+
|
|
3
|
+
Fairspec Python is a fast data management framework built on top of the Fairspec standard and Polars DataFrames. It supports various formats like CSV, JSON, and Parquet and integrates with data platforms such as CKAN, Zenodo, and GitHub. For more information, please read the [project's documentation](https://python.fairspec.org).
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from fairspec_dataset import * # noqa: F403
|
|
2
|
+
from fairspec_metadata import * # noqa: F403
|
|
3
|
+
from fairspec_table import * # noqa: F403
|
|
4
|
+
|
|
5
|
+
from .actions.data.load import load_data as load_data
|
|
6
|
+
from .actions.data.validate import validate_data as validate_data
|
|
7
|
+
from .actions.data_schema.infer import infer_data_schema as infer_data_schema
|
|
8
|
+
from .actions.data_schema.render import render_data_schema_as as render_data_schema_as
|
|
9
|
+
from .actions.dataset.foreign_key import (
|
|
10
|
+
validate_dataset_foreign_keys as validate_dataset_foreign_keys,
|
|
11
|
+
)
|
|
12
|
+
from .actions.dataset.infer import infer_dataset as infer_dataset
|
|
13
|
+
from .actions.dataset.load import load_dataset as load_dataset
|
|
14
|
+
from .actions.dataset.render import render_dataset_as as render_dataset_as
|
|
15
|
+
from .actions.dataset.save import save_dataset as save_dataset
|
|
16
|
+
from .actions.dataset.validate import validate_dataset as validate_dataset
|
|
17
|
+
from .actions.file_dialect.infer import infer_file_dialect as infer_file_dialect
|
|
18
|
+
from .actions.resource.infer import infer_resource as infer_resource
|
|
19
|
+
from .actions.resource.validate import validate_resource as validate_resource
|
|
20
|
+
from .actions.table.infer import infer_table as infer_table
|
|
21
|
+
from .actions.table.load import load_table as load_table
|
|
22
|
+
from .actions.table.save import save_table as save_table
|
|
23
|
+
from .actions.table.validate import validate_table as validate_table
|
|
24
|
+
from .actions.table_schema.infer import infer_table_schema as infer_table_schema
|
|
25
|
+
from .actions.table_schema.render import render_table_schema_as as render_table_schema_as
|
|
26
|
+
from .models.table import ValidateTableOptions as ValidateTableOptions
|
|
27
|
+
from .plugin import Plugin as Plugin
|
|
28
|
+
from .system import System as System
|
|
29
|
+
from .system import system as system
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from fairspec_metadata import (
|
|
6
|
+
get_data_first_path,
|
|
7
|
+
get_data_value,
|
|
8
|
+
get_supported_file_dialect,
|
|
9
|
+
load_descriptor,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from fairspec_metadata import Resource
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def load_data(resource: Resource) -> object | None:
|
|
17
|
+
data_value = get_data_value(resource)
|
|
18
|
+
if data_value:
|
|
19
|
+
return data_value
|
|
20
|
+
|
|
21
|
+
first_path = get_data_first_path(resource)
|
|
22
|
+
if first_path:
|
|
23
|
+
dialect = get_supported_file_dialect(resource, ["json"])
|
|
24
|
+
if dialect:
|
|
25
|
+
return load_descriptor(first_path)
|
|
26
|
+
|
|
27
|
+
return None
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from fairspec_dataset import write_temp_file
|
|
6
|
+
from fairspec_metadata import Resource
|
|
7
|
+
|
|
8
|
+
from .load import load_data
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestLoadData:
|
|
12
|
+
def test_should_return_inline_data(self):
|
|
13
|
+
resource = Resource(data=[{"id": 1}, {"id": 2}])
|
|
14
|
+
result = load_data(resource)
|
|
15
|
+
assert result == [{"id": 1}, {"id": 2}]
|
|
16
|
+
|
|
17
|
+
def test_should_return_inline_object(self):
|
|
18
|
+
resource = Resource(data={"key": "value"})
|
|
19
|
+
result = load_data(resource)
|
|
20
|
+
assert result == {"key": "value"}
|
|
21
|
+
|
|
22
|
+
def test_should_load_json_file(self):
|
|
23
|
+
data = {"key": "value"}
|
|
24
|
+
path = write_temp_file(json.dumps(data), format="json")
|
|
25
|
+
resource = Resource(data=path)
|
|
26
|
+
result = load_data(resource)
|
|
27
|
+
assert result == data
|
|
28
|
+
|
|
29
|
+
def test_should_return_none_for_non_json_file(self):
|
|
30
|
+
path = write_temp_file("id,name\n1,english", format="csv")
|
|
31
|
+
resource = Resource(data=path)
|
|
32
|
+
result = load_data(resource)
|
|
33
|
+
assert result is None
|
|
34
|
+
|
|
35
|
+
def test_should_return_none_for_empty_resource(self):
|
|
36
|
+
resource = Resource()
|
|
37
|
+
result = load_data(resource)
|
|
38
|
+
assert result is None
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from fairspec_metadata import (
|
|
6
|
+
DataError,
|
|
7
|
+
FairspecError,
|
|
8
|
+
Report,
|
|
9
|
+
create_report,
|
|
10
|
+
inspect_json,
|
|
11
|
+
resolve_data_schema,
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
from .load import load_data
|
|
15
|
+
|
|
16
|
+
if TYPE_CHECKING:
|
|
17
|
+
from fairspec_metadata import Resource
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def validate_data(resource: Resource) -> Report:
|
|
21
|
+
errors: list[FairspecError] = []
|
|
22
|
+
|
|
23
|
+
data_schema = resolve_data_schema(resource.dataSchema)
|
|
24
|
+
if not data_schema:
|
|
25
|
+
return create_report()
|
|
26
|
+
|
|
27
|
+
data = load_data(resource)
|
|
28
|
+
if data is None:
|
|
29
|
+
return create_report()
|
|
30
|
+
|
|
31
|
+
notes = inspect_json(data, json_schema=data_schema)
|
|
32
|
+
for note in notes:
|
|
33
|
+
errors.append(
|
|
34
|
+
DataError(
|
|
35
|
+
type="data",
|
|
36
|
+
message=note["message"],
|
|
37
|
+
jsonPointer=note["jsonPointer"],
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
return create_report(errors)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from fairspec_metadata import Resource
|
|
4
|
+
|
|
5
|
+
from .validate import validate_data
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestValidateData:
|
|
9
|
+
def test_should_return_valid_when_no_schema(self):
|
|
10
|
+
resource = Resource(data=[{"id": 1}])
|
|
11
|
+
report = validate_data(resource)
|
|
12
|
+
assert report.valid is True
|
|
13
|
+
|
|
14
|
+
def test_should_validate_inline_data(self):
|
|
15
|
+
resource = Resource(
|
|
16
|
+
data={"name": "test", "age": 25},
|
|
17
|
+
dataSchema={
|
|
18
|
+
"type": "object",
|
|
19
|
+
"properties": {
|
|
20
|
+
"name": {"type": "string"},
|
|
21
|
+
"age": {"type": "integer"},
|
|
22
|
+
},
|
|
23
|
+
"required": ["name", "age"],
|
|
24
|
+
},
|
|
25
|
+
)
|
|
26
|
+
report = validate_data(resource)
|
|
27
|
+
assert report.valid is True
|
|
28
|
+
|
|
29
|
+
def test_should_detect_invalid_data(self):
|
|
30
|
+
resource = Resource(
|
|
31
|
+
data={"name": 123},
|
|
32
|
+
dataSchema={
|
|
33
|
+
"type": "object",
|
|
34
|
+
"properties": {"name": {"type": "string"}},
|
|
35
|
+
},
|
|
36
|
+
)
|
|
37
|
+
report = validate_data(resource)
|
|
38
|
+
assert report.valid is False
|
|
39
|
+
assert len(report.errors) > 0
|
|
40
|
+
|
|
41
|
+
def test_should_return_valid_for_no_data(self):
|
|
42
|
+
resource = Resource(
|
|
43
|
+
dataSchema={"type": "object"},
|
|
44
|
+
)
|
|
45
|
+
report = validate_data(resource)
|
|
46
|
+
assert report.valid is True
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from genson import SchemaBuilder
|
|
6
|
+
|
|
7
|
+
from fairspec_library.actions.data.load import load_data
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from fairspec_metadata import JsonSchema, Resource
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def infer_data_schema(resource: Resource) -> JsonSchema | None:
|
|
14
|
+
data = load_data(resource)
|
|
15
|
+
if not data:
|
|
16
|
+
return None
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
builder = SchemaBuilder()
|
|
20
|
+
builder.add_object(data)
|
|
21
|
+
return builder.to_schema()
|
|
22
|
+
except Exception:
|
|
23
|
+
return None
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
|
|
5
|
+
from fairspec_dataset import write_temp_file
|
|
6
|
+
from fairspec_metadata import Resource
|
|
7
|
+
|
|
8
|
+
from .infer import infer_data_schema
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class TestInferDataSchema:
|
|
12
|
+
def test_should_infer_schema_from_inline_data(self):
|
|
13
|
+
resource = Resource(data={"name": "test", "age": 25})
|
|
14
|
+
schema = infer_data_schema(resource)
|
|
15
|
+
assert schema is not None
|
|
16
|
+
assert schema.get("type") == "object"
|
|
17
|
+
|
|
18
|
+
def test_should_infer_schema_from_inline_array(self):
|
|
19
|
+
resource = Resource(data=[{"id": 1}, {"id": 2}])
|
|
20
|
+
schema = infer_data_schema(resource)
|
|
21
|
+
assert schema is not None
|
|
22
|
+
|
|
23
|
+
def test_should_infer_schema_from_json_file(self):
|
|
24
|
+
data = {"name": "test", "value": 42}
|
|
25
|
+
path = write_temp_file(json.dumps(data), format="json")
|
|
26
|
+
resource = Resource(data=path)
|
|
27
|
+
schema = infer_data_schema(resource)
|
|
28
|
+
assert schema is not None
|
|
29
|
+
assert schema.get("type") == "object"
|
|
30
|
+
|
|
31
|
+
def test_should_return_none_for_no_data(self):
|
|
32
|
+
resource = Resource()
|
|
33
|
+
schema = infer_data_schema(resource)
|
|
34
|
+
assert schema is None
|
|
35
|
+
|
|
36
|
+
def test_should_return_none_for_csv_file(self):
|
|
37
|
+
path = write_temp_file("id,name\n1,english", format="csv")
|
|
38
|
+
resource = Resource(data=path)
|
|
39
|
+
schema = infer_data_schema(resource)
|
|
40
|
+
assert schema is None
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
from fairspec_library.system import system
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from fairspec_metadata import DataSchema, RenderDataSchemaOptions
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def render_data_schema_as(
|
|
12
|
+
data_schema: DataSchema, options: RenderDataSchemaOptions
|
|
13
|
+
) -> str | None:
|
|
14
|
+
for plugin in system.plugins:
|
|
15
|
+
result = plugin.render_data_schema_as(data_schema, options)
|
|
16
|
+
if result is not None:
|
|
17
|
+
return result
|
|
18
|
+
|
|
19
|
+
return None
|
|
File without changes
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING, Unpack
|
|
4
|
+
|
|
5
|
+
import polars as pl
|
|
6
|
+
from fairspec_metadata import (
|
|
7
|
+
FairspecError,
|
|
8
|
+
ForeignKey,
|
|
9
|
+
ForeignKeyError,
|
|
10
|
+
Report,
|
|
11
|
+
create_report,
|
|
12
|
+
resolve_table_schema,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
from fairspec_library.actions.table.load import load_table
|
|
16
|
+
from fairspec_library.models.table import ValidateTableOptions
|
|
17
|
+
|
|
18
|
+
if TYPE_CHECKING:
|
|
19
|
+
from fairspec_metadata import Dataset, Resource
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def validate_dataset_foreign_keys(
|
|
23
|
+
dataset: Dataset, **options: Unpack[ValidateTableOptions]
|
|
24
|
+
) -> Report:
|
|
25
|
+
errors: list[FairspecError] = []
|
|
26
|
+
|
|
27
|
+
for resource in dataset.resources or []:
|
|
28
|
+
table_schema = resolve_table_schema(resource.tableSchema)
|
|
29
|
+
if not table_schema:
|
|
30
|
+
continue
|
|
31
|
+
|
|
32
|
+
for foreign_key in table_schema.foreignKeys or []:
|
|
33
|
+
fk_errors = _validate_foreign_key(
|
|
34
|
+
resource, foreign_key, dataset, **options
|
|
35
|
+
)
|
|
36
|
+
errors.extend(fk_errors)
|
|
37
|
+
|
|
38
|
+
return create_report(errors)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _validate_foreign_key(
|
|
42
|
+
resource: Resource,
|
|
43
|
+
foreign_key: ForeignKey,
|
|
44
|
+
dataset: Dataset,
|
|
45
|
+
**options: Unpack[ValidateTableOptions],
|
|
46
|
+
) -> list[ForeignKeyError]:
|
|
47
|
+
reference = foreign_key.reference
|
|
48
|
+
columns = foreign_key.columns
|
|
49
|
+
ref_columns = reference.columns
|
|
50
|
+
|
|
51
|
+
if not columns or not ref_columns or len(columns) != len(ref_columns):
|
|
52
|
+
return []
|
|
53
|
+
|
|
54
|
+
ref_resource = _find_resource(dataset, reference.resource)
|
|
55
|
+
if not ref_resource:
|
|
56
|
+
return []
|
|
57
|
+
|
|
58
|
+
table = load_table(resource, denormalized=True, **options)
|
|
59
|
+
if table is None:
|
|
60
|
+
return []
|
|
61
|
+
|
|
62
|
+
ref_table = load_table(ref_resource, denormalized=True, **options)
|
|
63
|
+
if ref_table is None:
|
|
64
|
+
return []
|
|
65
|
+
|
|
66
|
+
rename_mapping = dict(zip(ref_columns, columns))
|
|
67
|
+
ref_selected = ref_table.select(
|
|
68
|
+
[pl.col(name).alias(rename_mapping[name]) for name in ref_columns]
|
|
69
|
+
).unique()
|
|
70
|
+
|
|
71
|
+
violations: pl.DataFrame = table.select(columns).join( # ty: ignore[invalid-assignment] https://github.com/astral-sh/ty/issues/2278
|
|
72
|
+
ref_selected, on=columns, how="anti"
|
|
73
|
+
).unique().collect()
|
|
74
|
+
|
|
75
|
+
errors: list[ForeignKeyError] = []
|
|
76
|
+
for row in violations.to_dicts():
|
|
77
|
+
cells = [str(row[c]) for c in columns]
|
|
78
|
+
errors.append(
|
|
79
|
+
ForeignKeyError(
|
|
80
|
+
type="foreignKey",
|
|
81
|
+
resourceName=resource.name,
|
|
82
|
+
foreignKey=foreign_key,
|
|
83
|
+
cells=cells,
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
return errors
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _find_resource(dataset: Dataset, name: str | None) -> Resource | None:
|
|
91
|
+
if not name:
|
|
92
|
+
return None
|
|
93
|
+
|
|
94
|
+
for resource in dataset.resources or []:
|
|
95
|
+
if resource.name == name:
|
|
96
|
+
return resource
|
|
97
|
+
|
|
98
|
+
return None
|