puxti-bi-core 0.21.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.
- puxti_bi_core-0.21.0/.gitignore +49 -0
- puxti_bi_core-0.21.0/LICENSE +201 -0
- puxti_bi_core-0.21.0/PKG-INFO +27 -0
- puxti_bi_core-0.21.0/README.md +10 -0
- puxti_bi_core-0.21.0/pyproject.toml +31 -0
- puxti_bi_core-0.21.0/src/puxti_bi_core/__init__.py +46 -0
- puxti_bi_core-0.21.0/src/puxti_bi_core/loader.py +205 -0
- puxti_bi_core-0.21.0/src/puxti_bi_core/py.typed +0 -0
- puxti_bi_core-0.21.0/src/puxti_bi_core/schema.py +69 -0
- puxti_bi_core-0.21.0/src/puxti_bi_core/schemas/v1alpha1.json +1185 -0
- puxti_bi_core-0.21.0/src/puxti_bi_core/spec.py +325 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.pyd
|
|
6
|
+
.Python
|
|
7
|
+
*.egg-info/
|
|
8
|
+
dist/
|
|
9
|
+
build/
|
|
10
|
+
.eggs/
|
|
11
|
+
|
|
12
|
+
# Virtual environments
|
|
13
|
+
.venv/
|
|
14
|
+
venv/
|
|
15
|
+
env/
|
|
16
|
+
|
|
17
|
+
# uv
|
|
18
|
+
uv.lock
|
|
19
|
+
|
|
20
|
+
# Environment
|
|
21
|
+
.env
|
|
22
|
+
.env.local
|
|
23
|
+
.env.*.local
|
|
24
|
+
|
|
25
|
+
# Editor
|
|
26
|
+
.DS_Store
|
|
27
|
+
Thumbs.db
|
|
28
|
+
.idea/
|
|
29
|
+
.vscode/
|
|
30
|
+
*.swp
|
|
31
|
+
*.swo
|
|
32
|
+
|
|
33
|
+
# Pytest
|
|
34
|
+
.pytest_cache/
|
|
35
|
+
.coverage
|
|
36
|
+
htmlcov/
|
|
37
|
+
|
|
38
|
+
# Ruff
|
|
39
|
+
.ruff_cache/
|
|
40
|
+
|
|
41
|
+
# Neo4j local data (if ever mounted locally)
|
|
42
|
+
neo4j_data/
|
|
43
|
+
neo4j_logs/
|
|
44
|
+
|
|
45
|
+
# Plan / scratch files
|
|
46
|
+
fix_plan.md
|
|
47
|
+
|
|
48
|
+
# enola architectural snapshots (local, per-repo)
|
|
49
|
+
.enola/
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: puxti-bi-core
|
|
3
|
+
Version: 0.21.0
|
|
4
|
+
Summary: BI assets as code: the spec schema for datasets, charts and dashboards used by Puxti.
|
|
5
|
+
Project-URL: Homepage, https://github.com/puxti-labs/puxti
|
|
6
|
+
Project-URL: Repository, https://github.com/puxti-labs/puxti
|
|
7
|
+
Project-URL: Documentation, https://github.com/puxti-labs/puxti/blob/main/docs/bi-specs.md
|
|
8
|
+
License-Expression: Apache-2.0
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Python: >=3.12
|
|
14
|
+
Requires-Dist: pydantic>=2.7.0
|
|
15
|
+
Requires-Dist: pyyaml>=6.0.0
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# puxti-bi-core
|
|
19
|
+
|
|
20
|
+
The spec schema for BI assets as code, used by [Puxti](https://github.com/puxti-labs/puxti).
|
|
21
|
+
|
|
22
|
+
Datasets, charts and dashboards are short YAML files in git, one asset per file. This package
|
|
23
|
+
parses and validates them and publishes their JSON Schema. It depends on no Puxti package;
|
|
24
|
+
Puxti resolves the dbt names the specs refer to.
|
|
25
|
+
|
|
26
|
+
The schema is `puxti/v1alpha1` and may still change in breaking ways. See
|
|
27
|
+
[docs/bi-specs.md](https://github.com/puxti-labs/puxti/blob/main/docs/bi-specs.md) for the format.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# puxti-bi-core
|
|
2
|
+
|
|
3
|
+
The spec schema for BI assets as code, used by [Puxti](https://github.com/puxti-labs/puxti).
|
|
4
|
+
|
|
5
|
+
Datasets, charts and dashboards are short YAML files in git, one asset per file. This package
|
|
6
|
+
parses and validates them and publishes their JSON Schema. It depends on no Puxti package;
|
|
7
|
+
Puxti resolves the dbt names the specs refer to.
|
|
8
|
+
|
|
9
|
+
The schema is `puxti/v1alpha1` and may still change in breaking ways. See
|
|
10
|
+
[docs/bi-specs.md](https://github.com/puxti-labs/puxti/blob/main/docs/bi-specs.md) for the format.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "puxti-bi-core"
|
|
3
|
+
version = "0.21.0"
|
|
4
|
+
description = "BI assets as code: the spec schema for datasets, charts and dashboards used by Puxti."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.12"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
classifiers = [
|
|
9
|
+
"License :: OSI Approved :: Apache Software License",
|
|
10
|
+
"Programming Language :: Python :: 3",
|
|
11
|
+
"Programming Language :: Python :: 3.12",
|
|
12
|
+
]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"pydantic>=2.7.0",
|
|
15
|
+
"pyyaml>=6.0.0",
|
|
16
|
+
]
|
|
17
|
+
|
|
18
|
+
[project.urls]
|
|
19
|
+
Homepage = "https://github.com/puxti-labs/puxti"
|
|
20
|
+
Repository = "https://github.com/puxti-labs/puxti"
|
|
21
|
+
Documentation = "https://github.com/puxti-labs/puxti/blob/main/docs/bi-specs.md"
|
|
22
|
+
|
|
23
|
+
[tool.hatch.build.targets.wheel]
|
|
24
|
+
packages = ["src/puxti_bi_core"]
|
|
25
|
+
|
|
26
|
+
[tool.hatch.build.targets.sdist]
|
|
27
|
+
exclude = ["tests"]
|
|
28
|
+
|
|
29
|
+
[build-system]
|
|
30
|
+
requires = ["hatchling"]
|
|
31
|
+
build-backend = "hatchling.build"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""puxti-bi-core: the spec schema for BI assets as code."""
|
|
2
|
+
|
|
3
|
+
from puxti_bi_core.loader import SpecError, SpecSet, load_spec_file, load_specs
|
|
4
|
+
from puxti_bi_core.spec import (
|
|
5
|
+
API_VERSION,
|
|
6
|
+
GRID_COLUMNS,
|
|
7
|
+
Aggregation,
|
|
8
|
+
BarChart,
|
|
9
|
+
BigNumberChart,
|
|
10
|
+
Chart,
|
|
11
|
+
Dashboard,
|
|
12
|
+
DashboardCell,
|
|
13
|
+
DashboardRow,
|
|
14
|
+
Dataset,
|
|
15
|
+
Grain,
|
|
16
|
+
LineChart,
|
|
17
|
+
Metric,
|
|
18
|
+
PieChart,
|
|
19
|
+
Spec,
|
|
20
|
+
TableChart,
|
|
21
|
+
XAxis,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"API_VERSION",
|
|
26
|
+
"GRID_COLUMNS",
|
|
27
|
+
"Aggregation",
|
|
28
|
+
"BarChart",
|
|
29
|
+
"BigNumberChart",
|
|
30
|
+
"Chart",
|
|
31
|
+
"Dashboard",
|
|
32
|
+
"DashboardCell",
|
|
33
|
+
"DashboardRow",
|
|
34
|
+
"Dataset",
|
|
35
|
+
"Grain",
|
|
36
|
+
"LineChart",
|
|
37
|
+
"Metric",
|
|
38
|
+
"PieChart",
|
|
39
|
+
"Spec",
|
|
40
|
+
"SpecError",
|
|
41
|
+
"SpecSet",
|
|
42
|
+
"TableChart",
|
|
43
|
+
"XAxis",
|
|
44
|
+
"load_spec_file",
|
|
45
|
+
"load_specs",
|
|
46
|
+
]
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
"""Load spec files from a folder, reporting every problem with its file and line."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections.abc import Sequence
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import yaml
|
|
11
|
+
from pydantic import BaseModel, ConfigDict, Field, ValidationError
|
|
12
|
+
from yaml.nodes import MappingNode, Node, ScalarNode, SequenceNode
|
|
13
|
+
|
|
14
|
+
from puxti_bi_core.spec import SPEC_ADAPTER, Dashboard, Dataset
|
|
15
|
+
|
|
16
|
+
SPEC_SUFFIXES = (".yml", ".yaml")
|
|
17
|
+
|
|
18
|
+
_BOOL = "tag:yaml.org,2002:bool"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _SpecLoader(yaml.SafeLoader):
|
|
22
|
+
"""PyYAML's safe loader with two changes for hand-written specs.
|
|
23
|
+
|
|
24
|
+
Only ``true`` and ``false`` are booleans, as in YAML 1.2, so ``title: yes`` or a key
|
|
25
|
+
``on:`` stay text. Aliases are rejected: specs have no use for them, and expanding
|
|
26
|
+
them can hang the loader (a recursive alias) or take exponential time.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
yaml_implicit_resolvers = {
|
|
30
|
+
first: [(tag, regexp) for tag, regexp in resolvers if tag != _BOOL]
|
|
31
|
+
for first, resolvers in yaml.SafeLoader.yaml_implicit_resolvers.items()
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
def compose_node(self, parent: Node | None, index: Any) -> Node | None:
|
|
35
|
+
if self.check_event(yaml.AliasEvent):
|
|
36
|
+
event = self.peek_event()
|
|
37
|
+
raise yaml.composer.ComposerError(
|
|
38
|
+
None, None, "aliases (*name) are not supported in specs", event.start_mark
|
|
39
|
+
)
|
|
40
|
+
return super().compose_node(parent, index)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
_SpecLoader.add_implicit_resolver(
|
|
44
|
+
_BOOL, re.compile(r"^(?:true|True|TRUE|false|False|FALSE)$"), list("tTfF")
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class SpecError(BaseModel):
|
|
49
|
+
"""A problem in one spec file. ``line`` is 1-based, None when the file has no position."""
|
|
50
|
+
|
|
51
|
+
model_config = ConfigDict(frozen=True)
|
|
52
|
+
|
|
53
|
+
path: Path
|
|
54
|
+
line: int | None = None
|
|
55
|
+
message: str
|
|
56
|
+
|
|
57
|
+
def __str__(self) -> str:
|
|
58
|
+
where = f"{self.path}:{self.line}" if self.line else str(self.path)
|
|
59
|
+
return f"{where}: {self.message}"
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class SpecSet(BaseModel):
|
|
63
|
+
"""Every spec that loaded, keyed by name within its kind, with the file each came from."""
|
|
64
|
+
|
|
65
|
+
datasets: dict[str, Dataset] = Field(default_factory=dict)
|
|
66
|
+
charts: dict[str, Any] = Field(default_factory=dict) # name -> one of the chart models
|
|
67
|
+
dashboards: dict[str, Dashboard] = Field(default_factory=dict)
|
|
68
|
+
paths: dict[str, Path] = Field(default_factory=dict) # "<kind>:<name>" -> file
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def load_specs(root: Path) -> tuple[SpecSet, list[SpecError]]:
|
|
72
|
+
"""Load every ``.yml``/``.yaml`` file under ``root``, skipping hidden folders.
|
|
73
|
+
|
|
74
|
+
Files that fail to parse or validate are left out of the returned set and reported
|
|
75
|
+
as errors; the rest still load, so one bad file does not hide problems in others.
|
|
76
|
+
"""
|
|
77
|
+
specs = SpecSet()
|
|
78
|
+
errors: list[SpecError] = []
|
|
79
|
+
if not root.is_dir():
|
|
80
|
+
return specs, [SpecError(path=root, message="spec folder not found")]
|
|
81
|
+
for path in _spec_files(root):
|
|
82
|
+
spec, file_errors = load_spec_file(path)
|
|
83
|
+
errors.extend(file_errors)
|
|
84
|
+
if spec is None:
|
|
85
|
+
continue
|
|
86
|
+
key = f"{spec.kind}:{spec.name}"
|
|
87
|
+
if key in specs.paths:
|
|
88
|
+
errors.append(
|
|
89
|
+
SpecError(
|
|
90
|
+
path=path,
|
|
91
|
+
line=1,
|
|
92
|
+
message=f"{spec.kind} '{spec.name}' is also defined in {specs.paths[key]}",
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
continue
|
|
96
|
+
specs.paths[key] = path
|
|
97
|
+
{"dataset": specs.datasets, "chart": specs.charts, "dashboard": specs.dashboards}[
|
|
98
|
+
spec.kind
|
|
99
|
+
][spec.name] = spec
|
|
100
|
+
return specs, errors
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def load_spec_file(path: Path) -> tuple[Any | None, list[SpecError]]:
|
|
104
|
+
"""Parse and validate one spec file. Returns the spec, or None with the errors."""
|
|
105
|
+
try:
|
|
106
|
+
text = path.read_text(encoding="utf-8")
|
|
107
|
+
except (OSError, UnicodeDecodeError) as exc:
|
|
108
|
+
return None, [SpecError(path=path, message=f"cannot read file: {exc}")]
|
|
109
|
+
try:
|
|
110
|
+
nodes = list(yaml.compose_all(text, Loader=_SpecLoader))
|
|
111
|
+
except yaml.MarkedYAMLError as exc:
|
|
112
|
+
mark = exc.problem_mark or exc.context_mark
|
|
113
|
+
line = mark.line + 1 if mark else None
|
|
114
|
+
return None, [SpecError(path=path, line=line, message=f"invalid YAML: {exc.problem}")]
|
|
115
|
+
except yaml.YAMLError as exc:
|
|
116
|
+
return None, [SpecError(path=path, message=f"invalid YAML: {exc}")]
|
|
117
|
+
|
|
118
|
+
if not nodes:
|
|
119
|
+
return None, [SpecError(path=path, message="file is empty")]
|
|
120
|
+
if len(nodes) > 1:
|
|
121
|
+
line = nodes[1].start_mark.line + 1
|
|
122
|
+
return None, [SpecError(path=path, line=line, message="one asset per file")]
|
|
123
|
+
if not isinstance(nodes[0], MappingNode):
|
|
124
|
+
return None, [SpecError(path=path, line=1, message="a spec must be a YAML mapping")]
|
|
125
|
+
duplicates = [
|
|
126
|
+
SpecError(path=path, line=line, message=f"{key}: key appears twice")
|
|
127
|
+
for line, key in _duplicate_keys(nodes[0])
|
|
128
|
+
]
|
|
129
|
+
if duplicates:
|
|
130
|
+
return None, duplicates
|
|
131
|
+
|
|
132
|
+
try:
|
|
133
|
+
data = _SpecLoader("").construct_document(nodes[0])
|
|
134
|
+
except yaml.MarkedYAMLError as exc: # e.g. an unknown tag such as !foo
|
|
135
|
+
mark = exc.problem_mark or exc.context_mark
|
|
136
|
+
line = mark.line + 1 if mark else None
|
|
137
|
+
return None, [SpecError(path=path, line=line, message=f"invalid YAML: {exc.problem}")]
|
|
138
|
+
try:
|
|
139
|
+
return SPEC_ADAPTER.validate_python(data), []
|
|
140
|
+
except ValidationError as exc:
|
|
141
|
+
return None, [_spec_error(path, nodes[0], err) for err in exc.errors()]
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _spec_files(root: Path) -> list[Path]:
|
|
145
|
+
return sorted(
|
|
146
|
+
p
|
|
147
|
+
for p in root.rglob("*")
|
|
148
|
+
if p.suffix.lower() in SPEC_SUFFIXES
|
|
149
|
+
and p.is_file()
|
|
150
|
+
and not any(part.startswith(".") for part in p.relative_to(root).parts)
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _duplicate_keys(node: Node) -> list[tuple[int, str]]:
|
|
155
|
+
"""Keys repeated within one mapping, which PyYAML would silently collapse to the last."""
|
|
156
|
+
found: list[tuple[int, str]] = []
|
|
157
|
+
stack = [node]
|
|
158
|
+
while stack:
|
|
159
|
+
current = stack.pop()
|
|
160
|
+
if isinstance(current, MappingNode):
|
|
161
|
+
seen: set[str] = set()
|
|
162
|
+
for key, value in current.value:
|
|
163
|
+
if isinstance(key, ScalarNode):
|
|
164
|
+
if key.value in seen:
|
|
165
|
+
found.append((key.start_mark.line + 1, key.value))
|
|
166
|
+
seen.add(key.value)
|
|
167
|
+
stack.append(value)
|
|
168
|
+
elif isinstance(current, SequenceNode):
|
|
169
|
+
stack.extend(current.value)
|
|
170
|
+
return sorted(found)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _spec_error(path: Path, root: Node, err: Any) -> SpecError:
|
|
174
|
+
line, field = _locate(root, err["loc"], missing=err["type"] == "missing")
|
|
175
|
+
message = err["msg"].removeprefix("Value error, ")
|
|
176
|
+
if field:
|
|
177
|
+
message = f"{field}: {message}"
|
|
178
|
+
return SpecError(path=path, line=line, message=message)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def _locate(node: Node, loc: Sequence[str | int], missing: bool) -> tuple[int, str]:
|
|
182
|
+
"""Follow a pydantic error location through the YAML nodes.
|
|
183
|
+
|
|
184
|
+
Location parts that name no key are union tags added by pydantic (the ``kind`` or
|
|
185
|
+
chart ``type`` value) and are skipped. For a missing field, the last part is the
|
|
186
|
+
field's name: it is kept in the path and the line is its parent mapping's.
|
|
187
|
+
"""
|
|
188
|
+
line = node.start_mark.line + 1
|
|
189
|
+
path: list[str] = []
|
|
190
|
+
for i, part in enumerate(loc):
|
|
191
|
+
if isinstance(node, MappingNode) and isinstance(part, str):
|
|
192
|
+
for key, value in node.value:
|
|
193
|
+
if isinstance(key, ScalarNode) and key.value == part:
|
|
194
|
+
node, line = value, value.start_mark.line + 1
|
|
195
|
+
path.append(part)
|
|
196
|
+
break
|
|
197
|
+
else:
|
|
198
|
+
if missing and i == len(loc) - 1:
|
|
199
|
+
path.append(part)
|
|
200
|
+
elif isinstance(node, SequenceNode) and isinstance(part, int):
|
|
201
|
+
if 0 <= part < len(node.value):
|
|
202
|
+
node = node.value[part]
|
|
203
|
+
line = node.start_mark.line + 1
|
|
204
|
+
path.append(f"[{part}]")
|
|
205
|
+
return line, ".".join(path).replace(".[", "[")
|
|
File without changes
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"""The published JSON Schema for spec files.
|
|
2
|
+
|
|
3
|
+
The committed file ``schemas/v1alpha1.json`` is generated from the models in ``spec.py``;
|
|
4
|
+
a test fails when it goes stale. Regenerate it with ``make bi-schema``, which runs
|
|
5
|
+
``python -m puxti_bi_core.schema <output path>``.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import sys
|
|
12
|
+
from importlib.resources import files
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from puxti_bi_core.spec import API_VERSION, SPEC_ADAPTER
|
|
17
|
+
|
|
18
|
+
SCHEMA_ID = (
|
|
19
|
+
"https://raw.githubusercontent.com/puxti-labs/puxti/main/"
|
|
20
|
+
"packages/puxti-bi-core/src/puxti_bi_core/schemas/v1alpha1.json"
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def json_schema() -> dict[str, Any]:
|
|
25
|
+
"""The JSON Schema for one spec file, generated from the models."""
|
|
26
|
+
return {
|
|
27
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
28
|
+
"$id": SCHEMA_ID,
|
|
29
|
+
"title": f"Puxti BI spec ({API_VERSION})",
|
|
30
|
+
**_drop_discriminators(SPEC_ADAPTER.json_schema()),
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _drop_discriminators(node: Any) -> Any:
|
|
35
|
+
"""Remove pydantic's OpenAPI ``discriminator`` hints.
|
|
36
|
+
|
|
37
|
+
They are not JSON Schema keywords, and for the nested chart union pydantic writes a
|
|
38
|
+
mapping value that is an object, which OpenAPI tools reject. The ``oneOf`` branches
|
|
39
|
+
already pin ``kind`` and ``type`` with ``const``, so validation is unchanged.
|
|
40
|
+
"""
|
|
41
|
+
if isinstance(node, dict):
|
|
42
|
+
return {k: _drop_discriminators(v) for k, v in node.items() if k != "discriminator"}
|
|
43
|
+
if isinstance(node, list):
|
|
44
|
+
return [_drop_discriminators(v) for v in node]
|
|
45
|
+
return node
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def render() -> str:
|
|
49
|
+
return json.dumps(json_schema(), indent=2, sort_keys=True) + "\n"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def committed_schema() -> str:
|
|
53
|
+
"""The schema file shipped in the package."""
|
|
54
|
+
return files("puxti_bi_core").joinpath("schemas/v1alpha1.json").read_text(encoding="utf-8")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def main(argv: list[str]) -> int:
|
|
58
|
+
if len(argv) > 1:
|
|
59
|
+
sys.stderr.write("usage: python -m puxti_bi_core.schema [output.json]\n")
|
|
60
|
+
return 2
|
|
61
|
+
if argv:
|
|
62
|
+
Path(argv[0]).write_text(render(), encoding="utf-8")
|
|
63
|
+
else:
|
|
64
|
+
sys.stdout.write(render())
|
|
65
|
+
return 0
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
if __name__ == "__main__":
|
|
69
|
+
raise SystemExit(main(sys.argv[1:]))
|