perucompras-cli 0.1.0__py3-none-any.whl
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.
- perucompras_cli-0.1.0.dist-info/METADATA +429 -0
- perucompras_cli-0.1.0.dist-info/RECORD +50 -0
- perucompras_cli-0.1.0.dist-info/WHEEL +4 -0
- perucompras_cli-0.1.0.dist-info/entry_points.txt +2 -0
- perucompras_cli-0.1.0.dist-info/licenses/LICENSE +16 -0
- piloto_ai_cli/__init__.py +51 -0
- piloto_ai_cli/__main__.py +5 -0
- piloto_ai_cli/api.py +394 -0
- piloto_ai_cli/attribute_text.py +106 -0
- piloto_ai_cli/attributes_service.py +250 -0
- piloto_ai_cli/auto_tag.py +65 -0
- piloto_ai_cli/backoff.py +25 -0
- piloto_ai_cli/cards.py +871 -0
- piloto_ai_cli/cli.py +602 -0
- piloto_ai_cli/connectors.py +106 -0
- piloto_ai_cli/database.py +491 -0
- piloto_ai_cli/enrichment.py +557 -0
- piloto_ai_cli/errors.py +92 -0
- piloto_ai_cli/excel.py +186 -0
- piloto_ai_cli/extraction.py +1316 -0
- piloto_ai_cli/health.py +398 -0
- piloto_ai_cli/health_workflow.py +1060 -0
- piloto_ai_cli/http_client.py +610 -0
- piloto_ai_cli/llm_client.py +259 -0
- piloto_ai_cli/llm_extraction.py +444 -0
- piloto_ai_cli/llm_output.py +90 -0
- piloto_ai_cli/pane.py +303 -0
- piloto_ai_cli/parsers.py +477 -0
- piloto_ai_cli/payload_store.py +227 -0
- piloto_ai_cli/pdf_parser.py +12 -0
- piloto_ai_cli/py.typed +0 -0
- piloto_ai_cli/query_compiler.py +1112 -0
- piloto_ai_cli/query_result.py +175 -0
- piloto_ai_cli/query_service.py +199 -0
- piloto_ai_cli/reextract.py +129 -0
- piloto_ai_cli/review.py +217 -0
- piloto_ai_cli/rules.py +67 -0
- piloto_ai_cli/run_log.py +230 -0
- piloto_ai_cli/schema/__init__.py +25 -0
- piloto_ai_cli/schema/contract-v1.schema.json +261 -0
- piloto_ai_cli/schema/contract.py +257 -0
- piloto_ai_cli/schema/migration_filters.py +59 -0
- piloto_ai_cli/schema/models.py +747 -0
- piloto_ai_cli/schema/snowflake_autogen.py +134 -0
- piloto_ai_cli/search_form.py +82 -0
- piloto_ai_cli/settings.py +174 -0
- piloto_ai_cli/snowflake_url.py +90 -0
- piloto_ai_cli/stamp_service.py +53 -0
- piloto_ai_cli/sync.py +1157 -0
- piloto_ai_cli/unit_converter.py +81 -0
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"title": "Perú Compras predicate contract v1",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"required": ["contract_version", "scope", "predicates"],
|
|
6
|
+
"additionalProperties": false,
|
|
7
|
+
"properties": {
|
|
8
|
+
"contract_version": {
|
|
9
|
+
"type": "integer",
|
|
10
|
+
"const": 1
|
|
11
|
+
},
|
|
12
|
+
"scope": {
|
|
13
|
+
"type": "object",
|
|
14
|
+
"additionalProperties": false,
|
|
15
|
+
"properties": {
|
|
16
|
+
"agreement": {
|
|
17
|
+
"type": "string",
|
|
18
|
+
"minLength": 1
|
|
19
|
+
},
|
|
20
|
+
"catalogue": {
|
|
21
|
+
"type": "string",
|
|
22
|
+
"minLength": 1
|
|
23
|
+
},
|
|
24
|
+
"category": {
|
|
25
|
+
"type": "string",
|
|
26
|
+
"minLength": 1
|
|
27
|
+
},
|
|
28
|
+
"department": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"minLength": 1
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"predicates": {
|
|
35
|
+
"type": "array",
|
|
36
|
+
"items": {
|
|
37
|
+
"$ref": "#/$defs/predicate"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"include": {
|
|
41
|
+
"type": "object",
|
|
42
|
+
"additionalProperties": false,
|
|
43
|
+
"properties": {
|
|
44
|
+
"rows": {
|
|
45
|
+
"type": "boolean"
|
|
46
|
+
},
|
|
47
|
+
"facets": {
|
|
48
|
+
"oneOf": [
|
|
49
|
+
{
|
|
50
|
+
"type": "array",
|
|
51
|
+
"items": {
|
|
52
|
+
"type": "string",
|
|
53
|
+
"minLength": 1
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
"const": "all"
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"max_items": {
|
|
64
|
+
"type": "integer",
|
|
65
|
+
"minimum": 1
|
|
66
|
+
},
|
|
67
|
+
"confidence_threshold": {
|
|
68
|
+
"type": "number",
|
|
69
|
+
"minimum": 0,
|
|
70
|
+
"maximum": 1
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
"$defs": {
|
|
74
|
+
"scalar": {
|
|
75
|
+
"type": ["string", "number", "boolean"]
|
|
76
|
+
},
|
|
77
|
+
"predicate": {
|
|
78
|
+
"type": "object",
|
|
79
|
+
"required": ["attribute", "op"],
|
|
80
|
+
"additionalProperties": false,
|
|
81
|
+
"properties": {
|
|
82
|
+
"attribute": {
|
|
83
|
+
"type": "string",
|
|
84
|
+
"minLength": 1
|
|
85
|
+
},
|
|
86
|
+
"op": {
|
|
87
|
+
"enum": [
|
|
88
|
+
"=",
|
|
89
|
+
"!=",
|
|
90
|
+
"<",
|
|
91
|
+
"<=",
|
|
92
|
+
">",
|
|
93
|
+
">=",
|
|
94
|
+
"between",
|
|
95
|
+
"in",
|
|
96
|
+
"contains",
|
|
97
|
+
"exists",
|
|
98
|
+
"text"
|
|
99
|
+
]
|
|
100
|
+
},
|
|
101
|
+
"value": {
|
|
102
|
+
"$ref": "#/$defs/scalar"
|
|
103
|
+
},
|
|
104
|
+
"values": {
|
|
105
|
+
"type": "array",
|
|
106
|
+
"items": {
|
|
107
|
+
"$ref": "#/$defs/scalar"
|
|
108
|
+
}
|
|
109
|
+
},
|
|
110
|
+
"unit": {
|
|
111
|
+
"type": "string",
|
|
112
|
+
"minLength": 1
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
"allOf": [
|
|
116
|
+
{
|
|
117
|
+
"if": {
|
|
118
|
+
"properties": {
|
|
119
|
+
"op": {
|
|
120
|
+
"enum": ["=", "!=", "<", "<=", ">", ">=", "contains"]
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
"required": ["op"]
|
|
124
|
+
},
|
|
125
|
+
"then": {
|
|
126
|
+
"required": ["value"],
|
|
127
|
+
"not": {
|
|
128
|
+
"required": ["values"]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
"if": {
|
|
134
|
+
"properties": {
|
|
135
|
+
"op": {
|
|
136
|
+
"enum": ["<", "<=", ">", ">="]
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
"required": ["op"]
|
|
140
|
+
},
|
|
141
|
+
"then": {
|
|
142
|
+
"properties": {
|
|
143
|
+
"value": {
|
|
144
|
+
"type": ["string", "number"]
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
"if": {
|
|
151
|
+
"properties": {
|
|
152
|
+
"op": {
|
|
153
|
+
"const": "between"
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
"required": ["op"]
|
|
157
|
+
},
|
|
158
|
+
"then": {
|
|
159
|
+
"required": ["values"],
|
|
160
|
+
"properties": {
|
|
161
|
+
"values": {
|
|
162
|
+
"type": "array",
|
|
163
|
+
"minItems": 2,
|
|
164
|
+
"maxItems": 2,
|
|
165
|
+
"oneOf": [
|
|
166
|
+
{
|
|
167
|
+
"items": {
|
|
168
|
+
"type": "number"
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"items": {
|
|
173
|
+
"type": "string"
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
]
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
"not": {
|
|
180
|
+
"required": ["value"]
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
"if": {
|
|
186
|
+
"properties": {
|
|
187
|
+
"op": {
|
|
188
|
+
"const": "in"
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
"required": ["op"]
|
|
192
|
+
},
|
|
193
|
+
"then": {
|
|
194
|
+
"required": ["values"],
|
|
195
|
+
"properties": {
|
|
196
|
+
"values": {
|
|
197
|
+
"minItems": 1
|
|
198
|
+
}
|
|
199
|
+
},
|
|
200
|
+
"not": {
|
|
201
|
+
"required": ["value"]
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
"if": {
|
|
207
|
+
"properties": {
|
|
208
|
+
"op": {
|
|
209
|
+
"const": "exists"
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
"required": ["op"]
|
|
213
|
+
},
|
|
214
|
+
"then": {
|
|
215
|
+
"not": {
|
|
216
|
+
"anyOf": [
|
|
217
|
+
{
|
|
218
|
+
"required": ["value"]
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
"required": ["values"]
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
"required": ["unit"]
|
|
225
|
+
}
|
|
226
|
+
]
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
{
|
|
231
|
+
"if": {
|
|
232
|
+
"properties": {
|
|
233
|
+
"op": {
|
|
234
|
+
"const": "text"
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
"required": ["op"]
|
|
238
|
+
},
|
|
239
|
+
"then": {
|
|
240
|
+
"required": ["value"],
|
|
241
|
+
"properties": {
|
|
242
|
+
"value": {
|
|
243
|
+
"type": "string"
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
"not": {
|
|
247
|
+
"anyOf": [
|
|
248
|
+
{
|
|
249
|
+
"required": ["values"]
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
"required": ["unit"]
|
|
253
|
+
}
|
|
254
|
+
]
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
]
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""Validation for versioned predicate documents."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from collections.abc import Mapping
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from decimal import Decimal
|
|
7
|
+
from importlib.resources import files
|
|
8
|
+
from importlib.resources.abc import Traversable
|
|
9
|
+
from typing import Any, Literal, cast
|
|
10
|
+
|
|
11
|
+
from jsonschema import Draft202012Validator, ValidationError
|
|
12
|
+
from sqlalchemy import select
|
|
13
|
+
from sqlalchemy.orm import Session
|
|
14
|
+
|
|
15
|
+
from piloto_ai_cli.attribute_text import normalize_label
|
|
16
|
+
from piloto_ai_cli.errors import ContractError, ContractErrorCode
|
|
17
|
+
from piloto_ai_cli.schema.models import Alias, Attribute, UnitRule
|
|
18
|
+
|
|
19
|
+
STRUCTURAL_FIELDS = ("agreement", "catalogue", "category", "department")
|
|
20
|
+
AttributeKind = Literal["quantity", "set", "categoric", "flag"]
|
|
21
|
+
PredicateOperator = Literal[
|
|
22
|
+
"=", "!=", "<", "<=", ">", ">=", "between", "in", "contains", "exists", "text"
|
|
23
|
+
]
|
|
24
|
+
PredicateValue = str | int | float | bool
|
|
25
|
+
|
|
26
|
+
_KIND_OPERATORS: dict[AttributeKind, frozenset[PredicateOperator]] = {
|
|
27
|
+
"quantity": frozenset({"=", "!=", "<", "<=", ">", ">=", "between", "in", "contains", "exists"}),
|
|
28
|
+
"set": frozenset({"in", "contains", "exists"}),
|
|
29
|
+
"categoric": frozenset({"=", "!=", "in", "contains", "exists", "text"}),
|
|
30
|
+
"flag": frozenset({"=", "!=", "exists"}),
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
CONTRACT_SCHEMA_PATH: Traversable = files(__package__).joinpath("contract-v1.schema.json")
|
|
34
|
+
"""The JSON Schema of predicate documents (``contract_version`` 1), as a package resource.
|
|
35
|
+
|
|
36
|
+
Read it with ``read_text()`` or ``open()``; ``importlib.resources.as_file``
|
|
37
|
+
gives a filesystem path where one is needed.
|
|
38
|
+
"""
|
|
39
|
+
CONTRACT_V1_SCHEMA: dict[str, Any] = json.loads(CONTRACT_SCHEMA_PATH.read_text(encoding="utf-8"))
|
|
40
|
+
_CONTRACT_V1_VALIDATOR = Draft202012Validator(CONTRACT_V1_SCHEMA)
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True)
|
|
44
|
+
class ValidatedPredicate:
|
|
45
|
+
"""A predicate with its canonical attribute and stored type resolved."""
|
|
46
|
+
|
|
47
|
+
attribute: str
|
|
48
|
+
kind: AttributeKind
|
|
49
|
+
canonical_unit: str | None
|
|
50
|
+
op: PredicateOperator
|
|
51
|
+
value: PredicateValue | None
|
|
52
|
+
values: tuple[PredicateValue, ...] | None
|
|
53
|
+
unit: str | None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class ValidatedContract:
|
|
58
|
+
"""A schema-valid query contract with resolved, typed predicates."""
|
|
59
|
+
|
|
60
|
+
contract_version: Literal[1]
|
|
61
|
+
scope: dict[str, str]
|
|
62
|
+
predicates: tuple[ValidatedPredicate, ...]
|
|
63
|
+
include: dict[str, bool | list[str] | Literal["all"]] | None
|
|
64
|
+
max_items: int | None
|
|
65
|
+
confidence_threshold: float | int | None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class ContractValidator:
|
|
69
|
+
"""Validate a query contract against the accepted database vocabulary."""
|
|
70
|
+
|
|
71
|
+
def __init__(self, session: Session) -> None:
|
|
72
|
+
self._session = session
|
|
73
|
+
|
|
74
|
+
def validate(self, document: object) -> ValidatedContract:
|
|
75
|
+
"""Return a resolved contract or raise a typed refusal."""
|
|
76
|
+
validate_contract(document)
|
|
77
|
+
if not isinstance(document, dict):
|
|
78
|
+
raise TypeError("schema-valid contract must be a dictionary")
|
|
79
|
+
|
|
80
|
+
# Imported here: unit_converter imports schema.models, whose package
|
|
81
|
+
# __init__ imports this module, so a module-level import is circular.
|
|
82
|
+
from piloto_ai_cli.unit_converter import UnitConverter
|
|
83
|
+
|
|
84
|
+
# Validation is read-only: never flush the caller's pending changes.
|
|
85
|
+
with self._session.no_autoflush:
|
|
86
|
+
attributes = {
|
|
87
|
+
attribute.attribute_id: attribute
|
|
88
|
+
for attribute in self._session.scalars(select(Attribute)).all()
|
|
89
|
+
}
|
|
90
|
+
aliases = {
|
|
91
|
+
normalize_label(alias.raw_label): alias.attribute_id
|
|
92
|
+
for alias in self._session.scalars(
|
|
93
|
+
select(Alias).where(Alias.status == "accepted")
|
|
94
|
+
).all()
|
|
95
|
+
}
|
|
96
|
+
unit_converter = UnitConverter(self._session.scalars(select(UnitRule)).all())
|
|
97
|
+
scope = cast(dict[str, str], document["scope"])
|
|
98
|
+
category = scope.get("category", "*")
|
|
99
|
+
predicates: list[ValidatedPredicate] = []
|
|
100
|
+
|
|
101
|
+
for index, predicate in enumerate(cast(list[dict[str, Any]], document["predicates"])):
|
|
102
|
+
attribute_id = predicate["attribute"]
|
|
103
|
+
attribute = self._resolve_attribute(attribute_id, attributes, aliases)
|
|
104
|
+
if attribute is None:
|
|
105
|
+
raise ContractError(
|
|
106
|
+
ContractErrorCode.UNKNOWN_ATTRIBUTE,
|
|
107
|
+
index,
|
|
108
|
+
f"Unknown attribute {attribute_id!r}.",
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
if attribute.kind not in _KIND_OPERATORS:
|
|
112
|
+
allowed_operators: frozenset[PredicateOperator] = frozenset()
|
|
113
|
+
else:
|
|
114
|
+
kind = attribute.kind
|
|
115
|
+
allowed_operators = _KIND_OPERATORS[kind]
|
|
116
|
+
operator = cast(PredicateOperator, predicate["op"])
|
|
117
|
+
if operator not in allowed_operators:
|
|
118
|
+
raise ContractError(
|
|
119
|
+
ContractErrorCode.BAD_OP_FOR_KIND,
|
|
120
|
+
index,
|
|
121
|
+
f"Operator {operator!r} is not allowed for attribute "
|
|
122
|
+
f"{attribute.attribute_id!r} of kind {attribute.kind!r}.",
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
unit = cast(str | None, predicate.get("unit"))
|
|
126
|
+
if unit is not None:
|
|
127
|
+
if attribute.canonical_unit is None:
|
|
128
|
+
raise ContractError(
|
|
129
|
+
ContractErrorCode.UNIT_NOT_CONVERTIBLE,
|
|
130
|
+
index,
|
|
131
|
+
f"Unit {unit!r} has no canonical unit for attribute "
|
|
132
|
+
f"{attribute.attribute_id!r}.",
|
|
133
|
+
)
|
|
134
|
+
unit_converter.convert(
|
|
135
|
+
Decimal(0),
|
|
136
|
+
unit,
|
|
137
|
+
attribute.canonical_unit,
|
|
138
|
+
category,
|
|
139
|
+
location=index,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
predicates.append(
|
|
143
|
+
ValidatedPredicate(
|
|
144
|
+
attribute=attribute.attribute_id,
|
|
145
|
+
kind=cast(AttributeKind, attribute.kind),
|
|
146
|
+
canonical_unit=attribute.canonical_unit,
|
|
147
|
+
op=operator,
|
|
148
|
+
value=cast(PredicateValue | None, predicate.get("value")),
|
|
149
|
+
values=(
|
|
150
|
+
tuple(cast(list[PredicateValue], predicate["values"]))
|
|
151
|
+
if "values" in predicate
|
|
152
|
+
else None
|
|
153
|
+
),
|
|
154
|
+
unit=unit,
|
|
155
|
+
)
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
return ValidatedContract(
|
|
159
|
+
contract_version=1,
|
|
160
|
+
scope=dict(scope),
|
|
161
|
+
predicates=tuple(predicates),
|
|
162
|
+
include=cast(
|
|
163
|
+
dict[str, bool | list[str] | Literal["all"]] | None,
|
|
164
|
+
document.get("include"),
|
|
165
|
+
),
|
|
166
|
+
max_items=_optional_int(document.get("max_items")),
|
|
167
|
+
confidence_threshold=cast(float | int | None, document.get("confidence_threshold")),
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
@staticmethod
|
|
171
|
+
def _resolve_attribute(
|
|
172
|
+
attribute_id: str,
|
|
173
|
+
attributes: dict[str, Attribute],
|
|
174
|
+
aliases: dict[str, str],
|
|
175
|
+
) -> Attribute | None:
|
|
176
|
+
"""Resolve an id or accepted alias through any attribute merges.
|
|
177
|
+
|
|
178
|
+
The direct id wins only when its merge chain reaches an accepted
|
|
179
|
+
attribute; otherwise an accepted alias with the same label is tried.
|
|
180
|
+
"""
|
|
181
|
+
direct = ContractValidator._follow_merges(attribute_id, attributes)
|
|
182
|
+
if direct is not None:
|
|
183
|
+
return direct
|
|
184
|
+
alias_target = aliases.get(normalize_label(attribute_id))
|
|
185
|
+
if alias_target is None:
|
|
186
|
+
return None
|
|
187
|
+
return ContractValidator._follow_merges(alias_target, attributes)
|
|
188
|
+
|
|
189
|
+
@staticmethod
|
|
190
|
+
def _follow_merges(attribute_id: str, attributes: dict[str, Attribute]) -> Attribute | None:
|
|
191
|
+
visited: set[str] = set()
|
|
192
|
+
attribute = attributes.get(attribute_id)
|
|
193
|
+
while attribute is not None and attribute.merged_into is not None:
|
|
194
|
+
if attribute.attribute_id in visited:
|
|
195
|
+
return None
|
|
196
|
+
visited.add(attribute.attribute_id)
|
|
197
|
+
attribute = attributes.get(attribute.merged_into)
|
|
198
|
+
if attribute is None or attribute.status != "accepted":
|
|
199
|
+
return None
|
|
200
|
+
return attribute
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _optional_int(value: object) -> int | None:
|
|
204
|
+
"""JSON Schema accepts integral floats such as 5.0 as integers: normalize them."""
|
|
205
|
+
if value is None:
|
|
206
|
+
return None
|
|
207
|
+
return int(cast(int | float, value))
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
def _error_location(error: ValidationError) -> int | str:
|
|
211
|
+
path = list(error.absolute_path)
|
|
212
|
+
if (
|
|
213
|
+
error.validator == "additionalProperties"
|
|
214
|
+
and isinstance(error.instance, dict)
|
|
215
|
+
and isinstance(error.schema, Mapping)
|
|
216
|
+
):
|
|
217
|
+
properties = error.schema.get("properties", {})
|
|
218
|
+
unexpected = (
|
|
219
|
+
sorted(set(error.instance) - set(properties)) if isinstance(properties, Mapping) else []
|
|
220
|
+
)
|
|
221
|
+
if unexpected:
|
|
222
|
+
path.append(unexpected[0])
|
|
223
|
+
elif (
|
|
224
|
+
error.validator == "required"
|
|
225
|
+
and isinstance(error.instance, dict)
|
|
226
|
+
and isinstance(error.validator_value, list)
|
|
227
|
+
):
|
|
228
|
+
missing = sorted(set(error.validator_value) - set(error.instance))
|
|
229
|
+
if missing:
|
|
230
|
+
path.append(missing[0])
|
|
231
|
+
|
|
232
|
+
if "predicates" in path:
|
|
233
|
+
predicate_index = path.index("predicates") + 1
|
|
234
|
+
if predicate_index < len(path) and isinstance(path[predicate_index], int):
|
|
235
|
+
return path[predicate_index]
|
|
236
|
+
if "scope" in path:
|
|
237
|
+
return ".".join(str(part) for part in path)
|
|
238
|
+
return ".".join(str(part) for part in path) or "contract"
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def validate_contract(document: object) -> None:
|
|
242
|
+
"""Raise a typed schema violation if a predicate document is invalid."""
|
|
243
|
+
errors = sorted(
|
|
244
|
+
_CONTRACT_V1_VALIDATOR.iter_errors(document),
|
|
245
|
+
key=lambda error: (
|
|
246
|
+
tuple(str(part) for part in error.absolute_path),
|
|
247
|
+
error.validator or "",
|
|
248
|
+
error.message,
|
|
249
|
+
),
|
|
250
|
+
)
|
|
251
|
+
if errors:
|
|
252
|
+
error = errors[0]
|
|
253
|
+
raise ContractError(
|
|
254
|
+
ContractErrorCode.SCHEMA_VIOLATION,
|
|
255
|
+
_error_location(error),
|
|
256
|
+
error.message,
|
|
257
|
+
)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Alembic autogenerate filters that keep comparisons inside the ``cli`` schema."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Callable, Mapping
|
|
4
|
+
from typing import Literal
|
|
5
|
+
|
|
6
|
+
from piloto_ai_cli.schema.models import SCHEMA
|
|
7
|
+
|
|
8
|
+
NameType = Literal[
|
|
9
|
+
"schema",
|
|
10
|
+
"table",
|
|
11
|
+
"column",
|
|
12
|
+
"index",
|
|
13
|
+
"unique_constraint",
|
|
14
|
+
"foreign_key_constraint",
|
|
15
|
+
]
|
|
16
|
+
|
|
17
|
+
IncludeName = Callable[[str | None, NameType, Mapping[NameType, str | None]], bool]
|
|
18
|
+
|
|
19
|
+
VERSION_TABLE = "alembic_version"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def is_cli_schema(name: str | None) -> bool:
|
|
23
|
+
return name is not None and name.lower() == SCHEMA
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def make_include_name(default_schema: str | None) -> IncludeName:
|
|
27
|
+
"""Return an ``include_name`` hook that reflects only the ``cli`` schema.
|
|
28
|
+
|
|
29
|
+
Alembic represents the connection's default schema as ``None`` and
|
|
30
|
+
normalises model tables in that schema to ``None`` too, so the default
|
|
31
|
+
schema is included only when it is ``cli``; then ``cli`` itself is never
|
|
32
|
+
offered by name. Snowflake may report names in upper case, so the
|
|
33
|
+
comparison ignores case.
|
|
34
|
+
"""
|
|
35
|
+
default_is_cli = is_cli_schema(default_schema)
|
|
36
|
+
|
|
37
|
+
def include_name(
|
|
38
|
+
name: str | None,
|
|
39
|
+
type_: NameType,
|
|
40
|
+
parent_names: Mapping[NameType, str | None],
|
|
41
|
+
) -> bool:
|
|
42
|
+
if type_ == "schema":
|
|
43
|
+
return default_is_cli if name is None else is_cli_schema(name)
|
|
44
|
+
if type_ == "table":
|
|
45
|
+
return name != VERSION_TABLE
|
|
46
|
+
return True
|
|
47
|
+
|
|
48
|
+
return include_name
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def include_object(
|
|
52
|
+
object: object,
|
|
53
|
+
name: str | None,
|
|
54
|
+
object_type: str,
|
|
55
|
+
reflected: bool,
|
|
56
|
+
compare_to: object | None,
|
|
57
|
+
) -> bool:
|
|
58
|
+
"""Exclude Alembic's version table from autogenerate comparisons."""
|
|
59
|
+
return object_type != "table" or name != VERSION_TABLE
|