FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-mvdxml/parser.py at master · opensourceBIM/python-mvdxml · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
opensourceBIM
/
python-mvdxml
Public
Notifications
You must be signed in to change notification settings
Fork
13
Star
32
Code
Issues
4
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-mvdxml
/
parser.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
253 lines (223 loc) · 8.8 KB
Breadcrumbs
python-mvdxml
/
parser.py
Copy path
File metadata and controls
253 lines (223 loc) · 8.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# This file was generated with the assistance of an AI coding tool.
from
__future__
import
annotations
import
os
from
dataclasses
import
replace
from
typing
import
Any
from
xml
.
dom
import
minidom
from
xml
.
dom
.
minidom
import
Document
,
Element
from
.
import
mvdxml_expression
from
.
model
import
concept_or_applicability
,
concept_root
,
rule
,
template
def
_elements
(
node
:
Element
)
->
list
[
Element
]:
return
[
child
for
child
in
node
.
childNodes
if
isinstance
(
child
,
Element
)]
class
_parser
:
def
__init__
(
self
,
dom
:
Document
):
self
.
dom
=
dom
self
.
templates
:
dict
[
str
,
Element
]
=
{}
for
node
in
dom
.
getElementsByTagNameNS
(
"*"
,
"ConceptTemplate"
):
self
.
templates
.
setdefault
(
node
.
attributes
[
"uuid"
].
value
.
casefold
(),
node
)
def
parse_template
(
self
,
template_id
:
str
,
constraints
:
tuple
[
Any
, ...]
=
(),
visited
:
frozenset
[
str
]
=
frozenset
(),
)
->
template
:
template_key
=
template_id
.
casefold
()
try
:
root
=
self
.
templates
[
template_key
]
except
KeyError
as
error
:
raise
ValueError
(
f"Unknown ConceptTemplate reference:
{
template_id
}
"
)
from
error
if
template_key
in
visited
:
raise
ValueError
(
f"Recursive ConceptTemplate reference:
{
template_id
}
"
)
parsed_rules
:
list
[
rule
]
=
[]
next_visited
=
visited
|
{
template_key
}
for
rules_node
in
root
.
getElementsByTagNameNS
(
"*"
,
"Rules"
):
for
node
in
_elements
(
rules_node
):
parsed_rules
.
append
(
self
.
parse_rule
(
node
,
visited
=
next_visited
))
return
template
(
entity
=
(
str
(
root
.
attributes
[
"applicableEntity"
].
value
)
if
"applicableEntity"
in
root
.
attributes
else
""
),
name
=
root
.
attributes
[
"name"
].
value
if
"name"
in
root
.
attributes
else
None
,
rules
=
tuple
(
parsed_rules
),
constraints
=
constraints
,
uuid
=
root
.
attributes
[
"uuid"
].
value
,
)
def
parse_rule
(
self
,
root
:
Element
,
prefix
:
str
=
""
,
visited
:
frozenset
[
str
]
=
frozenset
(),
)
->
rule
:
parsed
=
self
.
_visit_rule
(
root
,
prefix
,
visited
)
if
len
(
parsed
)
!=
1
:
raise
ValueError
(
f"Expected one rule below
{
root
.
localName
}
, found
{
len
(
parsed
)
}
"
)
return
parsed
[
0
]
def
_visit_rule
(
self
,
node
:
Element
,
prefix
:
str
,
visited
:
frozenset
[
str
],
)
->
tuple
[
rule
, ...]:
attribute
:
Any
=
None
bind
:
str
|
None
=
None
optional
=
False
target
=
node
child_prefix
=
prefix
if
node
.
localName
==
"AttributeRule"
:
attribute
=
node
.
attributes
[
"AttributeName"
].
value
bind
=
(
node
.
attributes
[
"RuleID"
].
value
if
"RuleID"
in
node
.
attributes
else
None
)
if
bind
is
None
:
optional
=
(
node
.
parentNode
.
localName
==
"Rules"
or
not
self
.
_child_has_rule_id_or_prefix
(
node
)
)
elif
node
.
localName
==
"EntityRule"
:
attribute
=
node
.
attributes
[
"EntityName"
].
value
elif
node
.
localName
==
"Template"
:
reference
=
node
.
attributes
[
"ref"
].
value
reference_key
=
reference
.
casefold
()
if
reference_key
in
visited
:
return
()
try
:
target
=
self
.
templates
[
reference_key
]
except
KeyError
as
error
:
raise
ValueError
(
f"Unknown ConceptTemplate reference:
{
reference
}
"
)
from
error
if
"IdPrefix"
in
node
.
attributes
:
child_prefix
+=
node
.
attributes
[
"IdPrefix"
].
value
visited
=
visited
|
{
reference_key
}
elif
node
.
localName
==
"Constraint"
:
attribute
=
mvdxml_expression
.
parse
(
node
.
attributes
[
"Expression"
].
value
)
elif
node
.
localName
in
{
"EntityRules"
,
"AttributeRules"
,
"Rules"
,
"Constraints"
,
"References"
,
}:
pass
elif
node
.
localName
in
{
"Definitions"
,
"SubTemplates"
}:
return
()
else
:
raise
ValueError
(
f"Unsupported mvdXML rule element:
{
node
.
localName
}
"
)
children
=
tuple
(
parsed
for
child
in
_elements
(
target
)
for
parsed
in
self
.
_visit_rule
(
child
,
child_prefix
,
visited
)
)
if
attribute
is
not
None
:
return
(
rule
(
tag
=
node
.
localName
,
attribute
=
attribute
,
nodes
=
children
,
bind
=
child_prefix
+
bind
if
bind
else
None
,
optional
=
optional
,
),
)
return
children
def
_child_has_rule_id_or_prefix
(
self
,
node
:
Element
)
->
bool
:
if
"RuleID"
in
node
.
attributes
or
"IdPrefix"
in
node
.
attributes
:
return
True
return
any
(
self
.
_child_has_rule_id_or_prefix
(
child
)
for
child
in
_elements
(
node
)
)
def
parse_template_rules
(
self
,
concept_node
:
Element
)
->
tuple
[
Any
, ...]:
template_rules
=
concept_node
.
getElementsByTagNameNS
(
"*"
,
"TemplateRules"
)
if
not
template_rules
:
return
()
def
visit
(
rules_node
:
Element
)
->
tuple
[
Any
, ...]:
output
:
list
[
Any
]
=
[]
for
index
,
child
in
enumerate
(
_elements
(
rules_node
)):
if
index
:
output
.
append
(
rules_node
.
attributes
[
"operator"
].
value
)
if
child
.
localName
==
"TemplateRules"
:
output
.
append
(
visit
(
child
))
elif
child
.
localName
==
"TemplateRule"
:
output
.
append
(
mvdxml_expression
.
parse
(
child
.
attributes
[
"Parameters"
].
value
)
)
else
:
raise
ValueError
(
f"Unsupported TemplateRules element:
{
child
.
localName
}
"
)
return
tuple
(
output
)
return
visit
(
template_rules
[
0
])
def
parse_concept
(
self
,
concept_node
:
Element
,
root_name
:
str
,
root_entity
:
str
,
is_applicability
:
bool
=
False
,
)
->
concept_or_applicability
:
template_nodes
=
concept_node
.
getElementsByTagNameNS
(
"*"
,
"Template"
)
if
not
template_nodes
:
raise
ValueError
(
f"
{
concept_node
.
localName
}
contains no Template reference"
)
template_id
=
template_nodes
[
0
].
attributes
[
"ref"
].
value
constraints
=
self
.
parse_template_rules
(
concept_node
)
parsed_template
=
replace
(
self
.
parse_template
(
template_id
),
constraints
=
constraints
)
return
concept_or_applicability
(
name
=
(
concept_node
.
attributes
[
"name"
].
value
if
"name"
in
concept_node
.
attributes
else
"Applicability"
),
parsed_template
=
parsed_template
,
template_rules
=
constraints
,
root_name
=
root_name
,
root_entity
=
root_entity
,
is_root_applicability
=
is_applicability
,
)
def
parse_root
(
self
,
root
:
Element
)
->
concept_root
:
name
=
root
.
attributes
[
"name"
].
value
entity
=
str
(
root
.
attributes
[
"applicableRootEntity"
].
value
)
applicability_nodes
=
root
.
getElementsByTagNameNS
(
"*"
,
"Applicability"
)
applicability
=
(
self
.
parse_concept
(
applicability_nodes
[
0
],
name
,
entity
,
is_applicability
=
True
)
if
applicability_nodes
else
None
)
concepts
=
tuple
(
self
.
parse_concept
(
node
,
name
,
entity
)
for
node
in
root
.
getElementsByTagNameNS
(
"*"
,
"Concept"
)
)
return
concept_root
(
name
,
entity
,
concepts
,
applicability
)
def
parse
(
source
:
str
|
os
.
PathLike
[
str
],
*
,
include_templates
:
bool
=
False
)
->
tuple
[
concept_root
|
template
, ...]:
"""Parse an mvdXML document into immutable model objects.
With ``include_templates=True``, standalone template definitions precede
any concept roots in the returned tuple.
"""
try
:
dom
=
minidom
.
parse
(
os
.
fspath
(
source
))
except
Exception
as
error
:
raise
ValueError
(
f"Unable to parse mvdXML document
{
os
.
fspath
(
source
)!r
}
:
{
error
}
"
)
from
error
parser
=
_parser
(
dom
)
roots
=
dom
.
getElementsByTagNameNS
(
"*"
,
"ConceptRoot"
)
parsed_roots
=
tuple
(
parser
.
parse_root
(
root
)
for
root
in
roots
)
if
roots
and
not
include_templates
:
return
parsed_roots
parsed_templates
=
tuple
(
parser
.
parse_template
(
template_id
)
for
template_id
in
parser
.
templates
)
if
include_templates
:
return
parsed_templates
+
parsed_roots
if
parser
.
templates
:
return
parsed_templates
raise
ValueError
(
"mvdXML document contains no ConceptRoot or ConceptTemplate"
)
Back
|
FazBrowse Home
|
New Git URL