@flipova/foundation 1.13.0 → 2.0.4
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.
- package/CHANGELOG.md +181 -0
- package/README.md +53 -178
- package/components/ui/accordion/ANIMATION_CUSTOMIZATION.md +162 -0
- package/components/ui/accordion/AccordionAnimatedHeight.tsx +56 -0
- package/components/ui/accordion/AccordionAnimatedIcon.tsx +41 -0
- package/components/ui/accordion/AnimatedHeight.tsx +56 -0
- package/components/ui/accordion/AnimatedIcon.tsx +41 -0
- package/components/ui/accordion/MIGRATION_SUMMARY.md +250 -0
- package/components/ui/accordion/USAGE_EXAMPLE.tsx +179 -0
- package/components/ui/accordion/animation-config.ts +32 -0
- package/components/ui/accordion/index.tsx +253 -0
- package/components/ui/actionsheet/index.tsx +511 -0
- package/components/ui/alert/index.tsx +158 -0
- package/components/ui/alert-dialog/index.tsx +254 -0
- package/components/ui/avatar/index.tsx +139 -0
- package/components/ui/badge/index.tsx +163 -0
- package/components/ui/bottomsheet/index.tsx +477 -0
- package/components/ui/box/index.tsx +19 -0
- package/components/ui/box/index.web.tsx +19 -0
- package/components/ui/box/styles.tsx +10 -0
- package/components/ui/button/index.tsx +264 -0
- package/components/ui/calendar/creator.tsx +366 -0
- package/components/ui/calendar/index.tsx +462 -0
- package/components/ui/calendar/styles.tsx +140 -0
- package/components/ui/card/index.tsx +23 -0
- package/components/ui/card/index.web.tsx +26 -0
- package/components/ui/card/styles.tsx +16 -0
- package/components/ui/center/index.tsx +22 -0
- package/components/ui/center/index.web.tsx +20 -0
- package/components/ui/center/styles.tsx +8 -0
- package/components/ui/checkbox/index.tsx +184 -0
- package/components/ui/date-time-picker/creator.tsx +109 -0
- package/components/ui/date-time-picker/index.tsx +585 -0
- package/components/ui/date-time-picker/index.web.tsx +217 -0
- package/components/ui/date-time-picker/styles.tsx +56 -0
- package/components/ui/divider/index.tsx +40 -0
- package/components/ui/drawer/index.tsx +362 -0
- package/components/ui/fab/index.tsx +215 -0
- package/components/ui/flat-list/index.tsx +21 -0
- package/components/ui/form-control/index.tsx +352 -0
- package/components/ui/gluestack-ui-provider/config.ts +72 -0
- package/components/ui/gluestack-ui-provider/index.tsx +47 -0
- package/components/ui/gluestack-ui-provider/index.web.tsx +96 -0
- package/components/ui/gluestack-ui-provider/script.ts +19 -0
- package/components/ui/gluestack-ui-provider/tokens.d.ts +17 -0
- package/components/ui/gluestack-ui-provider/tokens.js +188 -0
- package/components/ui/gluestack-ui-provider/useGluestackColors.ts +82 -0
- package/components/ui/grid/index.tsx +283 -0
- package/components/ui/grid/index.web.tsx +66 -0
- package/components/ui/grid/styles.tsx +15 -0
- package/components/ui/heading/index.tsx +220 -0
- package/components/ui/heading/index.web.tsx +202 -0
- package/components/ui/heading/styles.tsx +43 -0
- package/components/ui/hstack/index.tsx +27 -0
- package/components/ui/hstack/index.web.tsx +26 -0
- package/components/ui/hstack/styles.tsx +25 -0
- package/components/ui/icon/index.tsx +1588 -0
- package/components/ui/icon/index.web.tsx +1573 -0
- package/components/ui/image/index.tsx +48 -0
- package/components/ui/image-background/index.tsx +25 -0
- package/components/ui/image-viewer/index.tsx +743 -0
- package/components/ui/image-viewer/index.web.tsx +302 -0
- package/components/ui/index.ts +58 -0
- package/components/ui/input/index.tsx +125 -0
- package/components/ui/input-accessory-view/index.tsx +2 -0
- package/components/ui/keyboard-avoiding-view/index.tsx +2 -0
- package/components/ui/link/index.tsx +102 -0
- package/components/ui/liquid-glass/index.tsx +74 -0
- package/components/ui/menu/index.tsx +186 -0
- package/components/ui/modal/index.tsx +237 -0
- package/components/ui/popover/index.tsx +272 -0
- package/components/ui/portal/index.tsx +17 -0
- package/components/ui/pressable/index.tsx +39 -0
- package/components/ui/progress/index.tsx +83 -0
- package/components/ui/radio/index.tsx +217 -0
- package/components/ui/refresh-control/index.tsx +2 -0
- package/components/ui/safe-area-view/index.tsx +2 -0
- package/components/ui/scroll-view/index.tsx +2 -0
- package/components/ui/section-list/index.tsx +2 -0
- package/components/ui/select/index.tsx +277 -0
- package/components/ui/select/select-actionsheet.tsx +562 -0
- package/components/ui/skeleton/index.tsx +105 -0
- package/components/ui/skeleton/index.web.tsx +103 -0
- package/components/ui/skeleton/styles.tsx +35 -0
- package/components/ui/slider/index.tsx +181 -0
- package/components/ui/spinner/index.tsx +40 -0
- package/components/ui/status-bar/index.tsx +2 -0
- package/components/ui/switch/index.tsx +41 -0
- package/components/ui/table/index.tsx +210 -0
- package/components/ui/table/index.web.tsx +157 -0
- package/components/ui/table/styles.tsx +44 -0
- package/components/ui/tabs/TabsAnimatedIndicator.tsx +160 -0
- package/components/ui/tabs/animation-config.ts +3 -0
- package/components/ui/tabs/creator.tsx +206 -0
- package/components/ui/tabs/index.tsx +484 -0
- package/components/ui/text/index.tsx +48 -0
- package/components/ui/text/index.web.tsx +45 -0
- package/components/ui/text/styles.tsx +47 -0
- package/components/ui/textarea/index.tsx +94 -0
- package/components/ui/toast/index.tsx +252 -0
- package/components/ui/tooltip/index.tsx +131 -0
- package/components/ui/view/index.tsx +2 -0
- package/components/ui/virtualized-list/index.tsx +2 -0
- package/components/ui/vstack/index.tsx +28 -0
- package/components/ui/vstack/index.web.tsx +27 -0
- package/components/ui/vstack/styles.tsx +25 -0
- package/design/canonical.index.txt +53 -0
- package/design/documentation.xml +519 -0
- package/design/manifest.xml +90 -0
- package/design/schema.xsd +639 -0
- package/design/themes.xml +204 -0
- package/design/tokens.xml +510 -0
- package/design/tools/design.js +165 -0
- package/design/tools/npm.js +65 -0
- package/design/tools/requirements.txt +35 -0
- package/design/tools/sources/.checker/__init__.py +81 -0
- package/design/tools/sources/.checker/checks.py +198 -0
- package/design/tools/sources/.checker/cli.py +154 -0
- package/design/tools/sources/.checker/io.py +242 -0
- package/design/tools/sources/.checker/model.py +144 -0
- package/design/tools/sources/.checker/registry.py +115 -0
- package/design/tools/sources/.checker/report.py +21 -0
- package/design/tools/sources/.docgen/__init__.py +45 -0
- package/design/tools/sources/.docgen/cli.py +101 -0
- package/design/tools/sources/.docgen/model.py +147 -0
- package/design/tools/sources/.docgen/reference.py +97 -0
- package/design/tools/sources/.docgen/site.py +145 -0
- package/design/tools/sources/.docgen/xsddoc.py +329 -0
- package/design/tools/sources/.generate/__init__.py +35 -0
- package/design/tools/sources/.generate/cli.py +76 -0
- package/design/tools/sources/.generate/common.py +33 -0
- package/design/tools/sources/.generate/config.py +119 -0
- package/design/tools/sources/.generate/model.py +51 -0
- package/design/tools/sources/.generate/tokens.py +141 -0
- package/design/tools/sources/.linter/__init__.py +9 -0
- package/design/tools/sources/.linter/cli.py +44 -0
- package/design/tools/sources/.linter/discovery.py +30 -0
- package/design/tools/sources/.linter/errors.py +45 -0
- package/design/tools/sources/.linter/linter.py +68 -0
- package/design/tools/sources/.xmleditor/__init__.py +74 -0
- package/design/tools/sources/.xmleditor/blocks.py +185 -0
- package/design/tools/sources/.xmleditor/cli.py +141 -0
- package/design/tools/sources/.xmleditor/editor.py +906 -0
- package/design/tools/sources/.xmleditor/model.py +708 -0
- package/design/tools/sources/.xmleditor/widgets.py +1832 -0
- package/design/tools/sources/.xmleditor/xsdmodel.py +753 -0
- package/design/tools/sources/checker.py +55 -0
- package/design/tools/sources/docgen.py +66 -0
- package/design/tools/sources/generate.py +48 -0
- package/design/tools/sources/linter.py +54 -0
- package/design/tools/sources/ruff.toml +12 -0
- package/design/tools/sources/xmleditor.py +62 -0
- package/design/tools/unix/_venv_python.sh +16 -0
- package/design/tools/unix/check.sh +13 -0
- package/design/tools/unix/doc.sh +14 -0
- package/design/tools/unix/gen.sh +14 -0
- package/design/tools/unix/lint.sh +10 -0
- package/design/tools/unix/run.sh +20 -0
- package/design/tools/unix/verify.sh +12 -0
- package/design/tools/unix/xmledit.sh +12 -0
- package/design/tools/windows/check.cmd +12 -0
- package/design/tools/windows/doc.cmd +12 -0
- package/design/tools/windows/gen.cmd +11 -0
- package/design/tools/windows/lint.cmd +9 -0
- package/design/tools/windows/run.cmd +26 -0
- package/design/tools/windows/verify.cmd +10 -0
- package/design/tools/windows/xmledit.cmd +10 -0
- package/dist/chunk-LV4UIEQN.js +9730 -0
- package/dist/chunk-LV4UIEQN.js.map +1 -0
- package/dist/chunk-WNVU3BM2.mjs +9401 -0
- package/dist/chunk-WNVU3BM2.mjs.map +1 -0
- package/dist/index.d.mts +40 -9
- package/dist/index.d.ts +40 -9
- package/dist/index.js +726 -414
- package/dist/index.mjs +1 -5
- package/dist/ui/index.d.mts +6931 -3070
- package/dist/ui/index.d.ts +6931 -3070
- package/dist/ui/index.js +779 -352
- package/dist/ui/index.mjs +1 -2
- package/package.json +66 -142
- package/dist/chunk-2XORADW6.js +0 -24552
- package/dist/chunk-2XORADW6.js.map +0 -1
- package/dist/chunk-52BHHF5M.js +0 -46
- package/dist/chunk-52BHHF5M.js.map +0 -1
- package/dist/chunk-5647JY7Z.mjs +0 -100
- package/dist/chunk-5647JY7Z.mjs.map +0 -1
- package/dist/chunk-5N5HC4ZA.mjs +0 -3
- package/dist/chunk-5N5HC4ZA.mjs.map +0 -1
- package/dist/chunk-7QFZMGL6.js +0 -8583
- package/dist/chunk-7QFZMGL6.js.map +0 -1
- package/dist/chunk-ASZEP37D.js +0 -423
- package/dist/chunk-ASZEP37D.js.map +0 -1
- package/dist/chunk-BH5SU2JQ.mjs +0 -8282
- package/dist/chunk-BH5SU2JQ.mjs.map +0 -1
- package/dist/chunk-BO65GO53.mjs +0 -422
- package/dist/chunk-BO65GO53.mjs.map +0 -1
- package/dist/chunk-BSVXX52E.mjs +0 -413
- package/dist/chunk-BSVXX52E.mjs.map +0 -1
- package/dist/chunk-DNV5UBNF.mjs +0 -3
- package/dist/chunk-DNV5UBNF.mjs.map +0 -1
- package/dist/chunk-EDTAMUO2.js +0 -8310
- package/dist/chunk-EDTAMUO2.js.map +0 -1
- package/dist/chunk-IJTI6EX4.js +0 -4
- package/dist/chunk-IJTI6EX4.js.map +0 -1
- package/dist/chunk-M3YLASM2.js +0 -428
- package/dist/chunk-M3YLASM2.js.map +0 -1
- package/dist/chunk-OXKOGLXE.js +0 -4
- package/dist/chunk-OXKOGLXE.js.map +0 -1
- package/dist/chunk-RQXQK3ZO.mjs +0 -8486
- package/dist/chunk-RQXQK3ZO.mjs.map +0 -1
- package/dist/chunk-SR25V4EA.mjs +0 -40
- package/dist/chunk-SR25V4EA.mjs.map +0 -1
- package/dist/chunk-UB34WFEQ.js +0 -106
- package/dist/chunk-UB34WFEQ.js.map +0 -1
- package/dist/chunk-ZBRWXKDF.mjs +0 -24431
- package/dist/chunk-ZBRWXKDF.mjs.map +0 -1
- package/dist/cli/ds.d.mts +0 -1
- package/dist/cli/ds.d.ts +0 -1
- package/dist/cli/ds.js +0 -602
- package/dist/cli/ds.js.map +0 -1
- package/dist/cli/ds.mjs +0 -579
- package/dist/cli/ds.mjs.map +0 -1
- package/dist/cli/flipova.d.mts +0 -1
- package/dist/cli/flipova.d.ts +0 -1
- package/dist/cli/flipova.js +0 -161
- package/dist/cli/flipova.js.map +0 -1
- package/dist/cli/flipova.mjs +0 -154
- package/dist/cli/flipova.mjs.map +0 -1
- package/dist/config/index.d.mts +0 -123
- package/dist/config/index.d.ts +0 -123
- package/dist/config/index.js +0 -28
- package/dist/config/index.js.map +0 -1
- package/dist/config/index.mjs +0 -3
- package/dist/config/index.mjs.map +0 -1
- package/dist/index-CuaJA3gM.d.mts +0 -78
- package/dist/index-CuaJA3gM.d.ts +0 -78
- package/dist/registry/index.d.mts +0 -10160
- package/dist/registry/index.d.ts +0 -10160
- package/dist/registry/index.js +0 -392
- package/dist/registry/index.js.map +0 -1
- package/dist/registry/index.mjs +0 -3
- package/dist/registry/index.mjs.map +0 -1
- package/dist/theme/index.d.mts +0 -109
- package/dist/theme/index.d.ts +0 -109
- package/dist/theme/index.js +0 -28
- package/dist/theme/index.js.map +0 -1
- package/dist/theme/index.mjs +0 -3
- package/dist/theme/index.mjs.map +0 -1
- package/dist/tokens/index.d.mts +0 -423
- package/dist/tokens/index.d.ts +0 -423
- package/dist/tokens/index.js +0 -44
- package/dist/tokens/index.js.map +0 -1
- package/dist/tokens/index.mjs +0 -3
- package/dist/tokens/index.mjs.map +0 -1
- package/dist/types/index.d.mts +0 -202
- package/dist/types/index.d.ts +0 -202
- package/dist/types/index.js +0 -4
- package/dist/types/index.js.map +0 -1
- package/dist/types/index.mjs +0 -3
- package/dist/types/index.mjs.map +0 -1
|
@@ -0,0 +1,708 @@
|
|
|
1
|
+
"""Editable XML document — the block operations behind the visual editor.
|
|
2
|
+
|
|
3
|
+
The document is kept as an lxml tree and every mutation goes through this
|
|
4
|
+
class, so undo/redo, dirty tracking, structural diagnostics and saving stay
|
|
5
|
+
in one place. Blocks are resolved against ``SchemaModel`` by descending the
|
|
6
|
+
content model from the root, which is the only way to disambiguate element
|
|
7
|
+
names that repeat across types (e.g. ``style``).
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
import sys
|
|
14
|
+
import tempfile
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from pathlib import Path
|
|
17
|
+
|
|
18
|
+
from lxml import etree
|
|
19
|
+
|
|
20
|
+
from .blocks import unfulfilled_required_attributes
|
|
21
|
+
from .xsdmodel import (
|
|
22
|
+
AttributeSpec,
|
|
23
|
+
BlockSpec,
|
|
24
|
+
LeafSpec,
|
|
25
|
+
ParticleSpec,
|
|
26
|
+
SchemaModel,
|
|
27
|
+
localname,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
XML_DECLARATION = '<?xml version="1.0" encoding="UTF-8"?>'
|
|
31
|
+
_SOURCES_DIR = Path(__file__).resolve().parents[1]
|
|
32
|
+
FREEFORM_TYPE = "xs:any"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@dataclass
|
|
36
|
+
class Issue:
|
|
37
|
+
"""A schema-driven diagnostic attached to a document element."""
|
|
38
|
+
|
|
39
|
+
severity: str
|
|
40
|
+
element: etree._Element | None
|
|
41
|
+
message: str
|
|
42
|
+
line: int | None = None
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def label(self) -> str:
|
|
46
|
+
return self.message
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class DocumentModel:
|
|
50
|
+
"""An editable XML document validated against the schema that drives it."""
|
|
51
|
+
|
|
52
|
+
def __init__(self, schema: SchemaModel) -> None:
|
|
53
|
+
self.schema = schema
|
|
54
|
+
self.path: Path | None = None
|
|
55
|
+
self._tree: etree._ElementTree | None = None
|
|
56
|
+
self._undo: list[str] = []
|
|
57
|
+
self._redo: list[str] = []
|
|
58
|
+
self.dirty = False
|
|
59
|
+
|
|
60
|
+
# -- construction ----------------------------------------------------
|
|
61
|
+
|
|
62
|
+
@classmethod
|
|
63
|
+
def new(cls, schema: SchemaModel, root_name: str) -> DocumentModel:
|
|
64
|
+
"""Create an empty document whose root is *root_name*."""
|
|
65
|
+
model = cls(schema)
|
|
66
|
+
root_block = schema.root(root_name)
|
|
67
|
+
if root_block is None:
|
|
68
|
+
raise ValueError(f"unknown root element: {root_name}")
|
|
69
|
+
root = model._make_element(root_block, is_root=True)
|
|
70
|
+
model._tree = etree.ElementTree(root)
|
|
71
|
+
model.dirty = True
|
|
72
|
+
return model
|
|
73
|
+
|
|
74
|
+
@classmethod
|
|
75
|
+
def load(cls, schema: SchemaModel, path: Path) -> DocumentModel:
|
|
76
|
+
model = cls(schema)
|
|
77
|
+
parser = etree.XMLParser(remove_comments=False, resolve_entities=False)
|
|
78
|
+
model._tree = etree.parse(str(path), parser)
|
|
79
|
+
model.path = Path(path)
|
|
80
|
+
model.dirty = False
|
|
81
|
+
return model
|
|
82
|
+
|
|
83
|
+
# -- accessors -------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def tree(self) -> etree._ElementTree:
|
|
87
|
+
if self._tree is None:
|
|
88
|
+
raise RuntimeError("document is empty")
|
|
89
|
+
return self._tree
|
|
90
|
+
|
|
91
|
+
@property
|
|
92
|
+
def root(self) -> etree._Element:
|
|
93
|
+
return self.tree.getroot()
|
|
94
|
+
|
|
95
|
+
@property
|
|
96
|
+
def root_block(self) -> BlockSpec | None:
|
|
97
|
+
return self.schema.block_for_element(str(self.root.tag))
|
|
98
|
+
|
|
99
|
+
@property
|
|
100
|
+
def can_undo(self) -> bool:
|
|
101
|
+
return bool(self._undo)
|
|
102
|
+
|
|
103
|
+
@property
|
|
104
|
+
def can_redo(self) -> bool:
|
|
105
|
+
return bool(self._redo)
|
|
106
|
+
|
|
107
|
+
def qualified(self, name: str, is_root: bool = False) -> str:
|
|
108
|
+
"""Map a schema block name to the tag used in the document."""
|
|
109
|
+
namespace = self.schema.target_namespace
|
|
110
|
+
if not namespace:
|
|
111
|
+
return name
|
|
112
|
+
if self.schema.element_form_default == "qualified" or is_root:
|
|
113
|
+
return f"{{{namespace}}}{name}"
|
|
114
|
+
return name
|
|
115
|
+
|
|
116
|
+
# -- block resolution ------------------------------------------------
|
|
117
|
+
|
|
118
|
+
def block_chain(
|
|
119
|
+
self, element: etree._Element
|
|
120
|
+
) -> list[tuple[BlockSpec, ParticleSpec | None]]:
|
|
121
|
+
"""Walk root -> element collecting (block, particle) for each level."""
|
|
122
|
+
lineage: list[etree._Element] = []
|
|
123
|
+
node: etree._Element | None = element
|
|
124
|
+
while node is not None:
|
|
125
|
+
lineage.append(node)
|
|
126
|
+
node = node.getparent()
|
|
127
|
+
lineage.reverse()
|
|
128
|
+
|
|
129
|
+
chain: list[tuple[BlockSpec, ParticleSpec | None]] = []
|
|
130
|
+
block = self.root_block
|
|
131
|
+
if block is None or not lineage or lineage[0] is not self.root:
|
|
132
|
+
return chain
|
|
133
|
+
chain.append((block, None))
|
|
134
|
+
for child in lineage[1:]:
|
|
135
|
+
name = localname(str(child.tag))
|
|
136
|
+
particle = block.child(name)
|
|
137
|
+
if particle is None:
|
|
138
|
+
if block.allows_any:
|
|
139
|
+
block = self.freeform_block(block, name)
|
|
140
|
+
chain.append((block, None))
|
|
141
|
+
continue
|
|
142
|
+
return chain
|
|
143
|
+
block = particle.block
|
|
144
|
+
chain.append((block, particle))
|
|
145
|
+
return chain
|
|
146
|
+
|
|
147
|
+
def block_of(self, element: etree._Element) -> BlockSpec | None:
|
|
148
|
+
"""Resolve the block definition governing *element* (context-aware)."""
|
|
149
|
+
chain = self.block_chain(element)
|
|
150
|
+
return chain[-1][0] if chain else None
|
|
151
|
+
|
|
152
|
+
def particle_of(self, element: etree._Element) -> ParticleSpec | None:
|
|
153
|
+
"""Resolve the particle (occurrence range) that allowed *element*."""
|
|
154
|
+
chain = self.block_chain(element)
|
|
155
|
+
return chain[-1][1] if chain else None
|
|
156
|
+
|
|
157
|
+
def child_names(self, element: etree._Element) -> list[str]:
|
|
158
|
+
return [localname(str(child.tag)) for child in element if isinstance(child.tag, str)]
|
|
159
|
+
|
|
160
|
+
def is_known(self, element: etree._Element) -> bool:
|
|
161
|
+
"""True when the element sits on a schema-valid path from the root."""
|
|
162
|
+
chain = self.block_chain(element)
|
|
163
|
+
if not chain:
|
|
164
|
+
return False
|
|
165
|
+
return localname(str(element.tag)) == chain[-1][0].name or element is self.root
|
|
166
|
+
|
|
167
|
+
# -- serialisation ---------------------------------------------------
|
|
168
|
+
|
|
169
|
+
def to_string(self) -> str:
|
|
170
|
+
"""Deterministic pretty XML, matching the repository's file style.
|
|
171
|
+
|
|
172
|
+
The default namespace lives in the root's nsmap, so qualified tags are
|
|
173
|
+
serialised as ``<themes xmlns="urn:...">`` rather than ``ns0:...``.
|
|
174
|
+
"""
|
|
175
|
+
body = etree.tostring(self.root, pretty_print=True, encoding="unicode")
|
|
176
|
+
return XML_DECLARATION + "\n" + body
|
|
177
|
+
|
|
178
|
+
def save(self, path: Path | None = None) -> Path:
|
|
179
|
+
target = Path(path) if path is not None else self.path
|
|
180
|
+
if target is None:
|
|
181
|
+
raise ValueError("no path given and the document was never saved")
|
|
182
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
183
|
+
with open(target, "w", encoding="utf-8", newline="\n") as handle:
|
|
184
|
+
handle.write(self.to_string())
|
|
185
|
+
self.path = target
|
|
186
|
+
self.dirty = False
|
|
187
|
+
return target
|
|
188
|
+
|
|
189
|
+
def write_temp(self) -> Path:
|
|
190
|
+
"""Write the current buffer to a temp file (for XSD validation)."""
|
|
191
|
+
with tempfile.NamedTemporaryFile(
|
|
192
|
+
mode="w", suffix=".xml", delete=False, encoding="utf-8", newline="\n") as handle:
|
|
193
|
+
handle.write(self.to_string())
|
|
194
|
+
return Path(handle.name)
|
|
195
|
+
|
|
196
|
+
# -- undo / redo -----------------------------------------------------
|
|
197
|
+
|
|
198
|
+
def snapshot(self) -> None:
|
|
199
|
+
"""Record the current state so the next mutation can be undone."""
|
|
200
|
+
if getattr(self, "_suspend_history", False):
|
|
201
|
+
return # batched mutation (auto_fix) records a single step itself
|
|
202
|
+
self._undo.append(etree.tostring(self.root, encoding="unicode"))
|
|
203
|
+
del self._redo[:]
|
|
204
|
+
if len(self._undo) > 200:
|
|
205
|
+
del self._undo[0]
|
|
206
|
+
|
|
207
|
+
def _restore(self, payload: str) -> None:
|
|
208
|
+
self._tree = etree.ElementTree(etree.fromstring(payload.encode("utf-8")))
|
|
209
|
+
|
|
210
|
+
def undo(self) -> bool:
|
|
211
|
+
if not self._undo:
|
|
212
|
+
return False
|
|
213
|
+
self._redo.append(etree.tostring(self.root, encoding="unicode"))
|
|
214
|
+
self._restore(self._undo.pop())
|
|
215
|
+
self.dirty = True
|
|
216
|
+
return True
|
|
217
|
+
|
|
218
|
+
def redo(self) -> bool:
|
|
219
|
+
if not self._redo:
|
|
220
|
+
return False
|
|
221
|
+
self._undo.append(etree.tostring(self.root, encoding="unicode"))
|
|
222
|
+
self._restore(self._redo.pop())
|
|
223
|
+
self.dirty = True
|
|
224
|
+
return True
|
|
225
|
+
|
|
226
|
+
# -- element creation ------------------------------------------------
|
|
227
|
+
|
|
228
|
+
def _make_element(self, block: BlockSpec, is_root: bool = False) -> etree._Element:
|
|
229
|
+
"""Create an element carrying the schema-mandated fixed values."""
|
|
230
|
+
tag = self.qualified(block.name, is_root)
|
|
231
|
+
namespace = self.schema.target_namespace
|
|
232
|
+
if is_root and namespace:
|
|
233
|
+
# Declare the default namespace on the root so serialisation keeps
|
|
234
|
+
# the repository's `<themes xmlns="urn:...">` form.
|
|
235
|
+
element = etree.Element(tag, nsmap={None: namespace})
|
|
236
|
+
else:
|
|
237
|
+
element = etree.Element(tag)
|
|
238
|
+
for attr in block.attributes:
|
|
239
|
+
if attr.read_only and attr.initial:
|
|
240
|
+
element.set(attr.name, attr.initial)
|
|
241
|
+
if block.leaf is not None and block.leaf.enumerations and not block.children:
|
|
242
|
+
element.text = block.leaf.enumerations[0]
|
|
243
|
+
return element
|
|
244
|
+
|
|
245
|
+
def freeform_block(self, parent_block: BlockSpec, name: str) -> BlockSpec:
|
|
246
|
+
"""Synthetic block for xs:any content: the element name is a free key.
|
|
247
|
+
|
|
248
|
+
Open groups (e.g. ``StylePropsType``) accept any child in the target
|
|
249
|
+
namespace; the editor still needs a block to display and edit, so one
|
|
250
|
+
is synthesised from the parent's context instead of hardcoded.
|
|
251
|
+
"""
|
|
252
|
+
return BlockSpec(
|
|
253
|
+
name=name,
|
|
254
|
+
key=f"{parent_block.key or parent_block.name}.{name}",
|
|
255
|
+
type_name=FREEFORM_TYPE,
|
|
256
|
+
documentation=f"open content (xs:any in {parent_block.name}): the element name is a "
|
|
257
|
+
"free key, the value follows the style contract",
|
|
258
|
+
leaf=LeafSpec(base="xs:string"),
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def sort_children(self, parent: etree._Element) -> None:
|
|
262
|
+
"""Re-order children to the schema's sequence order (stable).
|
|
263
|
+
|
|
264
|
+
``xs:sequence`` is positional, so a block editor must keep siblings in
|
|
265
|
+
declaration order; equal elements keep their relative order, which is
|
|
266
|
+
what makes reordering repeated blocks meaningful.
|
|
267
|
+
"""
|
|
268
|
+
block = self.block_of(parent)
|
|
269
|
+
if block is None or not block.children:
|
|
270
|
+
return
|
|
271
|
+
order = {particle.name: index for index, particle in enumerate(block.children)}
|
|
272
|
+
children = list(parent)
|
|
273
|
+
|
|
274
|
+
def rank(child: etree._Element) -> int:
|
|
275
|
+
if not isinstance(child.tag, str):
|
|
276
|
+
return len(order) + 1
|
|
277
|
+
return order.get(localname(str(child.tag)), len(order))
|
|
278
|
+
|
|
279
|
+
ranked = sorted(range(len(children)), key=lambda i: rank(children[i]))
|
|
280
|
+
for position, original in enumerate(ranked):
|
|
281
|
+
if position != original:
|
|
282
|
+
parent.insert(position, children[original])
|
|
283
|
+
|
|
284
|
+
# -- block mutations -------------------------------------------------
|
|
285
|
+
|
|
286
|
+
def add_child(
|
|
287
|
+
self, parent: etree._Element, name: str, record: bool = True
|
|
288
|
+
) -> etree._Element | None:
|
|
289
|
+
"""Insert a new block under *parent*; None when the schema forbids it."""
|
|
290
|
+
block = self.block_of(parent)
|
|
291
|
+
if block is None:
|
|
292
|
+
return None
|
|
293
|
+
particle = block.child(name)
|
|
294
|
+
if particle is None:
|
|
295
|
+
if not block.allows_any:
|
|
296
|
+
return None
|
|
297
|
+
# Open content (xs:any): the caller supplies the free key.
|
|
298
|
+
if record:
|
|
299
|
+
self.snapshot()
|
|
300
|
+
element = etree.Element(self.qualified(name))
|
|
301
|
+
parent.append(element)
|
|
302
|
+
self.dirty = True
|
|
303
|
+
return element
|
|
304
|
+
existing = self.child_names(parent).count(name)
|
|
305
|
+
if particle.max_occurs is not None and existing >= particle.max_occurs:
|
|
306
|
+
return None
|
|
307
|
+
if record:
|
|
308
|
+
self.snapshot()
|
|
309
|
+
element = self._make_element(particle.block)
|
|
310
|
+
parent.append(element)
|
|
311
|
+
self.sort_children(parent)
|
|
312
|
+
self.dirty = True
|
|
313
|
+
return element
|
|
314
|
+
|
|
315
|
+
def can_remove(self, element: etree._Element) -> tuple[bool, str]:
|
|
316
|
+
"""Check the minimum-occurrence constraint before deleting."""
|
|
317
|
+
particle = self.particle_of(element)
|
|
318
|
+
parent = element.getparent()
|
|
319
|
+
if particle is None:
|
|
320
|
+
return True, ""
|
|
321
|
+
if parent is None:
|
|
322
|
+
return False, "the document root cannot be removed"
|
|
323
|
+
name = localname(str(element.tag))
|
|
324
|
+
present = self.child_names(parent).count(name)
|
|
325
|
+
if present <= particle.min_occurs:
|
|
326
|
+
return False, (
|
|
327
|
+
f"{name} requires at least {particle.min_occurs} occurrence(s) "
|
|
328
|
+
f"({particle.occurs_label})")
|
|
329
|
+
return True, ""
|
|
330
|
+
|
|
331
|
+
def remove(self, element: etree._Element) -> bool:
|
|
332
|
+
"""Delete a block when the content model allows it."""
|
|
333
|
+
allowed, _ = self.can_remove(element)
|
|
334
|
+
parent = element.getparent()
|
|
335
|
+
if not allowed or parent is None:
|
|
336
|
+
return False
|
|
337
|
+
self.snapshot()
|
|
338
|
+
parent.remove(element)
|
|
339
|
+
self.dirty = True
|
|
340
|
+
return True
|
|
341
|
+
|
|
342
|
+
def duplicate(self, element: etree._Element) -> etree._Element | None:
|
|
343
|
+
"""Clone a block right after itself when its cardinality allows it."""
|
|
344
|
+
parent = element.getparent()
|
|
345
|
+
if parent is None:
|
|
346
|
+
return None
|
|
347
|
+
particle = self.particle_of(element)
|
|
348
|
+
if particle is not None and particle.max_occurs is not None:
|
|
349
|
+
name = localname(str(element.tag))
|
|
350
|
+
if self.child_names(parent).count(name) >= particle.max_occurs:
|
|
351
|
+
return None
|
|
352
|
+
self.snapshot()
|
|
353
|
+
clone = etree.fromstring(etree.tostring(element))
|
|
354
|
+
parent.insert(list(parent).index(element) + 1, clone)
|
|
355
|
+
self.sort_children(parent)
|
|
356
|
+
self.dirty = True
|
|
357
|
+
return clone
|
|
358
|
+
|
|
359
|
+
def move(self, element: etree._Element, delta: int) -> bool:
|
|
360
|
+
"""Swap a block with its neighbour (meaningful for repeated blocks)."""
|
|
361
|
+
parent = element.getparent()
|
|
362
|
+
if parent is None:
|
|
363
|
+
return False
|
|
364
|
+
siblings = [child for child in parent if isinstance(child.tag, str)]
|
|
365
|
+
try:
|
|
366
|
+
position = siblings.index(element)
|
|
367
|
+
except ValueError:
|
|
368
|
+
return False
|
|
369
|
+
target = position + delta
|
|
370
|
+
if target < 0 or target >= len(siblings):
|
|
371
|
+
return False
|
|
372
|
+
self.snapshot()
|
|
373
|
+
other = siblings[target]
|
|
374
|
+
parent.insert(list(parent).index(element), other)
|
|
375
|
+
self.dirty = True
|
|
376
|
+
return True
|
|
377
|
+
|
|
378
|
+
# -- values ----------------------------------------------------------
|
|
379
|
+
|
|
380
|
+
def attribute_map(self, element: etree._Element) -> dict:
|
|
381
|
+
"""Attributes actually present on the element, in document order."""
|
|
382
|
+
return {name: (value or "") for name, value in element.attrib.items()}
|
|
383
|
+
|
|
384
|
+
def set_attribute(self, element: etree._Element, name: str, value: str) -> bool:
|
|
385
|
+
"""Set/replace an attribute; empty value removes an optional one."""
|
|
386
|
+
block = self.block_of(element)
|
|
387
|
+
spec = block.attribute(name) if block is not None else None
|
|
388
|
+
if spec is not None and spec.read_only:
|
|
389
|
+
value = spec.fixed or ""
|
|
390
|
+
if value == "" and spec is not None and not spec.required:
|
|
391
|
+
return self.remove_attribute(element, name)
|
|
392
|
+
if element.get(name) == value:
|
|
393
|
+
return False
|
|
394
|
+
self.snapshot()
|
|
395
|
+
element.set(name, value)
|
|
396
|
+
self.dirty = True
|
|
397
|
+
return True
|
|
398
|
+
|
|
399
|
+
def remove_attribute(self, element: etree._Element, name: str) -> bool:
|
|
400
|
+
block = self.block_of(element)
|
|
401
|
+
spec = block.attribute(name) if block is not None else None
|
|
402
|
+
if spec is not None and spec.required:
|
|
403
|
+
return False
|
|
404
|
+
if name not in element.attrib:
|
|
405
|
+
return False
|
|
406
|
+
self.snapshot()
|
|
407
|
+
del element.attrib[name]
|
|
408
|
+
self.dirty = True
|
|
409
|
+
return True
|
|
410
|
+
|
|
411
|
+
def set_text(self, element: etree._Element, text: str) -> bool:
|
|
412
|
+
"""Replace the text content of a value block (preserving whitespace)."""
|
|
413
|
+
current = element.text or ""
|
|
414
|
+
if current == text:
|
|
415
|
+
return False
|
|
416
|
+
self.snapshot()
|
|
417
|
+
element.text = text
|
|
418
|
+
self.dirty = True
|
|
419
|
+
return True
|
|
420
|
+
|
|
421
|
+
def text_of(self, element: etree._Element) -> str:
|
|
422
|
+
return element.text or ""
|
|
423
|
+
|
|
424
|
+
# -- diagnostics -----------------------------------------------------
|
|
425
|
+
|
|
426
|
+
def structural_issues(self) -> list[Issue]:
|
|
427
|
+
"""Fast schema-only diagnostics, recomputed live by the editor.
|
|
428
|
+
|
|
429
|
+
Covers the constraints a block editor can express without a full XSD
|
|
430
|
+
pass: required children and attributes, fixed values, enumerations,
|
|
431
|
+
patterns and maxOccurs. The authoritative check remains the XSD
|
|
432
|
+
validation below (``xsd_issues``), which reuses the project linter.
|
|
433
|
+
"""
|
|
434
|
+
issues: list[Issue] = []
|
|
435
|
+
self._walk(self.root, self.root_block, issues)
|
|
436
|
+
return issues
|
|
437
|
+
|
|
438
|
+
def _walk(
|
|
439
|
+
self,
|
|
440
|
+
element: etree._Element,
|
|
441
|
+
block: BlockSpec | None,
|
|
442
|
+
issues: list[Issue],
|
|
443
|
+
) -> None:
|
|
444
|
+
line = element.sourceline
|
|
445
|
+
if block is None:
|
|
446
|
+
issues.append(Issue(
|
|
447
|
+
"error", element,
|
|
448
|
+
f"unresolved element <{localname(str(element.tag))}>: not reachable from the document root", line))
|
|
449
|
+
return
|
|
450
|
+
|
|
451
|
+
for name in unfulfilled_required_attributes(block, list(element.attrib)):
|
|
452
|
+
issues.append(Issue("error", element, f"missing required attribute @{name}", line))
|
|
453
|
+
|
|
454
|
+
for attr in block.attributes:
|
|
455
|
+
value = element.get(attr.name)
|
|
456
|
+
if value is None:
|
|
457
|
+
continue
|
|
458
|
+
if attr.fixed is not None and value != attr.fixed:
|
|
459
|
+
issues.append(Issue(
|
|
460
|
+
"error", element,
|
|
461
|
+
f"@{attr.name} must be {attr.fixed!r} (fixed by the schema)", line))
|
|
462
|
+
elif attr.enumerations and value not in attr.enumerations:
|
|
463
|
+
issues.append(Issue(
|
|
464
|
+
"error", element,
|
|
465
|
+
"@{}={!r} is not one of: {}".format(attr.name, value, ", ".join(attr.enumerations)),
|
|
466
|
+
line))
|
|
467
|
+
elif attr.pattern and not re.fullmatch(attr.pattern, value):
|
|
468
|
+
issues.append(Issue(
|
|
469
|
+
"warning", element,
|
|
470
|
+
f"@{attr.name}={value!r} does not match the schema pattern {attr.pattern}",
|
|
471
|
+
line))
|
|
472
|
+
|
|
473
|
+
counts: dict = {}
|
|
474
|
+
for child in element:
|
|
475
|
+
if isinstance(child.tag, str):
|
|
476
|
+
name = localname(str(child.tag))
|
|
477
|
+
counts[name] = counts.get(name, 0) + 1
|
|
478
|
+
|
|
479
|
+
for particle in block.children:
|
|
480
|
+
present = counts.get(particle.name, 0)
|
|
481
|
+
if present < particle.min_occurs:
|
|
482
|
+
issues.append(Issue(
|
|
483
|
+
"error", element,
|
|
484
|
+
f"missing required block <{particle.name}> ({particle.occurs_label})",
|
|
485
|
+
line))
|
|
486
|
+
elif particle.max_occurs is not None and present > particle.max_occurs:
|
|
487
|
+
issues.append(Issue(
|
|
488
|
+
"error", element,
|
|
489
|
+
f"too many <{particle.name}>: {present} occurrences, "
|
|
490
|
+
f"the schema allows {particle.max_occurs}", line))
|
|
491
|
+
|
|
492
|
+
if block.leaf is not None and block.leaf.enumerations:
|
|
493
|
+
value = (element.text or "").strip()
|
|
494
|
+
if value and value not in block.leaf.enumerations:
|
|
495
|
+
issues.append(Issue(
|
|
496
|
+
"error", element,
|
|
497
|
+
"value {!r} is not one of: {}".format(value, ", ".join(block.leaf.enumerations)), line))
|
|
498
|
+
|
|
499
|
+
for child in element:
|
|
500
|
+
if not isinstance(child.tag, str):
|
|
501
|
+
continue
|
|
502
|
+
particle = block.child(localname(str(child.tag)))
|
|
503
|
+
if particle is None and block.allows_any:
|
|
504
|
+
continue # open content (xs:any): keys are user-defined
|
|
505
|
+
self._walk(child, particle.block if particle is not None else None, issues)
|
|
506
|
+
|
|
507
|
+
def elements_by_line(self) -> list[etree._Element]:
|
|
508
|
+
"""Every element node in document order (used to map XSD line errors)."""
|
|
509
|
+
return [node for node in self.root.iter() if isinstance(node.tag, str)]
|
|
510
|
+
|
|
511
|
+
def closest_element(self, line: int | None) -> etree._Element | None:
|
|
512
|
+
"""Element whose start line is the nearest at or before *line*."""
|
|
513
|
+
if line is None:
|
|
514
|
+
return None
|
|
515
|
+
best: etree._Element | None = None
|
|
516
|
+
best_line = -1
|
|
517
|
+
for node in self.elements_by_line():
|
|
518
|
+
node_line = node.sourceline or -1
|
|
519
|
+
if best_line < node_line <= line:
|
|
520
|
+
best, best_line = node, node_line
|
|
521
|
+
return best
|
|
522
|
+
|
|
523
|
+
def xsd_issues(self, schema_path: Path) -> list[Issue]:
|
|
524
|
+
"""Authoritative XSD validation of the current buffer.
|
|
525
|
+
|
|
526
|
+
The buffer is written to a temp file and handed to the project's
|
|
527
|
+
agnostic linter (``design/tools/sources/linter.py``) so the editor and
|
|
528
|
+
the CLI pipeline can never disagree about validity.
|
|
529
|
+
"""
|
|
530
|
+
if str(_SOURCES_DIR) not in sys.path:
|
|
531
|
+
sys.path.insert(0, str(_SOURCES_DIR))
|
|
532
|
+
from linter import XSDLinter
|
|
533
|
+
|
|
534
|
+
temp_path = self.write_temp()
|
|
535
|
+
try:
|
|
536
|
+
result = XSDLinter(schema_path=str(schema_path)).validate_file(str(temp_path))
|
|
537
|
+
finally:
|
|
538
|
+
try:
|
|
539
|
+
temp_path.unlink()
|
|
540
|
+
except OSError:
|
|
541
|
+
pass
|
|
542
|
+
|
|
543
|
+
issues: list[Issue] = []
|
|
544
|
+
for error in result.errors:
|
|
545
|
+
issue_line = getattr(error, "line", None)
|
|
546
|
+
issues.append(Issue(
|
|
547
|
+
getattr(error, "severity", None) or "error",
|
|
548
|
+
self.closest_element(issue_line),
|
|
549
|
+
getattr(error, "message", str(error)),
|
|
550
|
+
issue_line))
|
|
551
|
+
return issues
|
|
552
|
+
|
|
553
|
+
def validate(self, schema_path: Path) -> list[Issue]:
|
|
554
|
+
"""Structural diagnostics first, then the authoritative XSD pass."""
|
|
555
|
+
return self.structural_issues() + self.xsd_issues(schema_path)
|
|
556
|
+
|
|
557
|
+
# -- one-click auto-fix -----------------------------------------------
|
|
558
|
+
|
|
559
|
+
def auto_fix(self, max_passes: int = 12) -> dict[str, int]:
|
|
560
|
+
"""Repair every issue the block model can fix itself, in one click.
|
|
561
|
+
|
|
562
|
+
Adds missing required blocks (recursively, to a fixpoint), sets
|
|
563
|
+
missing required attributes (@fixed, @default, first enumeration or a
|
|
564
|
+
type-based guess), corrects values violating ``@fixed`` or an
|
|
565
|
+
enumeration, and trims children beyond ``maxOccurs``. What cannot be
|
|
566
|
+
guessed safely (patterns, elements unreachable from the root) is
|
|
567
|
+
counted under ``unfixed`` so the caller can point at the diagnostics
|
|
568
|
+
that still need a human. Recorded as a single undo step.
|
|
569
|
+
"""
|
|
570
|
+
before = etree.tostring(self.root, encoding="unicode")
|
|
571
|
+
stats = {"blocks": 0, "attributes": 0, "values": 0,
|
|
572
|
+
"removed": 0, "unfixed": 0}
|
|
573
|
+
self._suspend_history = True
|
|
574
|
+
try:
|
|
575
|
+
for _ in range(max_passes):
|
|
576
|
+
if not self._auto_fix_pass(stats):
|
|
577
|
+
break
|
|
578
|
+
stats["unfixed"] = self._auto_fix_audit()
|
|
579
|
+
finally:
|
|
580
|
+
self._suspend_history = False
|
|
581
|
+
if etree.tostring(self.root, encoding="unicode") != before:
|
|
582
|
+
self._undo.append(before)
|
|
583
|
+
del self._redo[:]
|
|
584
|
+
if len(self._undo) > 200:
|
|
585
|
+
del self._undo[0]
|
|
586
|
+
self.dirty = True
|
|
587
|
+
return stats
|
|
588
|
+
|
|
589
|
+
def _auto_fix_pass(self, stats: dict) -> bool:
|
|
590
|
+
"""One repair sweep over the document; True when it changed anything."""
|
|
591
|
+
changed = False
|
|
592
|
+
for element in self.elements_by_line():
|
|
593
|
+
if not isinstance(element.tag, str):
|
|
594
|
+
continue
|
|
595
|
+
block = self.block_of(element)
|
|
596
|
+
if block is None:
|
|
597
|
+
continue
|
|
598
|
+
|
|
599
|
+
for attr in block.attributes:
|
|
600
|
+
if attr.required and element.get(attr.name) is None:
|
|
601
|
+
value = _guess_attribute_value(attr)
|
|
602
|
+
if value is not None:
|
|
603
|
+
element.set(attr.name, value)
|
|
604
|
+
stats["attributes"] += 1
|
|
605
|
+
changed = True
|
|
606
|
+
|
|
607
|
+
for attr in block.attributes:
|
|
608
|
+
value = element.get(attr.name)
|
|
609
|
+
if value is None:
|
|
610
|
+
continue
|
|
611
|
+
if attr.fixed is not None and value != attr.fixed:
|
|
612
|
+
element.set(attr.name, attr.fixed)
|
|
613
|
+
stats["values"] += 1
|
|
614
|
+
changed = True
|
|
615
|
+
elif attr.enumerations and value not in attr.enumerations:
|
|
616
|
+
element.set(attr.name, attr.enumerations[0])
|
|
617
|
+
stats["values"] += 1
|
|
618
|
+
changed = True
|
|
619
|
+
|
|
620
|
+
counts: dict = {}
|
|
621
|
+
for child in element:
|
|
622
|
+
if isinstance(child.tag, str):
|
|
623
|
+
name = localname(str(child.tag))
|
|
624
|
+
counts[name] = counts.get(name, 0) + 1
|
|
625
|
+
for particle in block.children:
|
|
626
|
+
have = counts.get(particle.name, 0)
|
|
627
|
+
if have < particle.min_occurs:
|
|
628
|
+
added = 0
|
|
629
|
+
for _ in range(particle.min_occurs - have):
|
|
630
|
+
if self.add_child(element, particle.name) is None:
|
|
631
|
+
break
|
|
632
|
+
added += 1
|
|
633
|
+
if added:
|
|
634
|
+
stats["blocks"] += added
|
|
635
|
+
have += added
|
|
636
|
+
changed = True
|
|
637
|
+
if particle.max_occurs is not None and have > particle.max_occurs:
|
|
638
|
+
victims = [c for c in element
|
|
639
|
+
if isinstance(c.tag, str)
|
|
640
|
+
and localname(str(c.tag)) == particle.name]
|
|
641
|
+
for victim in victims[particle.max_occurs:]:
|
|
642
|
+
if self.remove(victim):
|
|
643
|
+
stats["removed"] += 1
|
|
644
|
+
changed = True
|
|
645
|
+
else:
|
|
646
|
+
break
|
|
647
|
+
return changed
|
|
648
|
+
|
|
649
|
+
def _auto_fix_audit(self) -> int:
|
|
650
|
+
"""Count what auto-fix could not repair (needs a human decision)."""
|
|
651
|
+
unfixed = 0
|
|
652
|
+
for element in self.elements_by_line():
|
|
653
|
+
block = self.block_of(element)
|
|
654
|
+
if block is None:
|
|
655
|
+
unfixed += 1
|
|
656
|
+
continue
|
|
657
|
+
for attr in block.attributes:
|
|
658
|
+
if (attr.required and element.get(attr.name) is None
|
|
659
|
+
and _guess_attribute_value(attr) is None):
|
|
660
|
+
unfixed += 1
|
|
661
|
+
return unfixed
|
|
662
|
+
|
|
663
|
+
|
|
664
|
+
# ---------------------------------------------------------------------------
|
|
665
|
+
# One-click auto-fix: plausible values for required attributes
|
|
666
|
+
# ---------------------------------------------------------------------------
|
|
667
|
+
|
|
668
|
+
_POSITIVE_INT = {"xs:positiveInteger", "xs:unsignedByte", "xs:unsignedShort",
|
|
669
|
+
"xs:unsignedInt", "xs:unsignedLong"}
|
|
670
|
+
_INT = {"xs:byte", "xs:short", "xs:int", "xs:long", "xs:integer",
|
|
671
|
+
"xs:nonPositiveInteger"}
|
|
672
|
+
_NUM = {"xs:decimal", "xs:float", "xs:double"}
|
|
673
|
+
|
|
674
|
+
|
|
675
|
+
def _guess_attribute_value(spec: AttributeSpec) -> str | None:
|
|
676
|
+
"""A plausible value for a required attribute; None when unsafe.
|
|
677
|
+
|
|
678
|
+
Preference order: @fixed, @default, first enumeration, boolean, then a
|
|
679
|
+
type-based guess (integer/decimal -> a neutral in-range value, anything
|
|
680
|
+
else -> the empty string, which satisfies ``use="required"`` presence for
|
|
681
|
+
plain strings). A value that cannot satisfy @pattern is not guessed.
|
|
682
|
+
"""
|
|
683
|
+
if spec.fixed is not None:
|
|
684
|
+
value = spec.fixed
|
|
685
|
+
elif spec.default is not None:
|
|
686
|
+
value = spec.default
|
|
687
|
+
elif spec.enumerations:
|
|
688
|
+
value = spec.enumerations[0]
|
|
689
|
+
elif spec.is_boolean:
|
|
690
|
+
value = "true"
|
|
691
|
+
else:
|
|
692
|
+
base = spec.type_name or ""
|
|
693
|
+
if base in _POSITIVE_INT:
|
|
694
|
+
value = "1"
|
|
695
|
+
elif base == "xs:negativeInteger":
|
|
696
|
+
value = "-1"
|
|
697
|
+
elif base in _INT or "Integer" in base or base in _NUM:
|
|
698
|
+
value = "0"
|
|
699
|
+
else:
|
|
700
|
+
value = ""
|
|
701
|
+
if spec.pattern and re.fullmatch(spec.pattern, value) is None:
|
|
702
|
+
return None
|
|
703
|
+
return value
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|