Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
badw-it
DHParser
Commits
30e6dd80
Commit
30e6dd80
authored
Mar 25, 2019
by
di68kap
Browse files
- syntaxtree: Node.attr_active renamed to Node.has_attr and semantics changed.
parent
e7cfb370
Changes
3
Hide whitespace changes
Inline
Side-by-side
DHParser/syntaxtree.pxd
View file @
30e6dd80
...
...
@@ -16,7 +16,7 @@ cdef class Node:
cpdef
is_anonymous
(
self
)
cpdef
_content
(
self
)
cpdef
with_pos
(
self
,
pos
)
cpdef
attr
_active
(
self
)
cpdef
has_
attr
(
self
)
# cpdef compare_attr(self, other)
# cpdef _tree_repr(self, tab, open_fn, close_fn, data_fn, density, inline, inline_fn)
# cpdef as_sxpr(self, src, indentation, compact)
...
...
DHParser/syntaxtree.py
View file @
30e6dd80
...
...
@@ -198,7 +198,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
else
:
duplicate
=
self
.
__class__
(
self
.
tag_name
,
self
.
result
,
True
)
duplicate
.
_pos
=
self
.
_pos
if
self
.
attr
_active
():
if
self
.
has_
attr
():
duplicate
.
attr
.
update
(
copy
.
deepcopy
(
self
.
_xml_attr
))
# duplicate._xml_attr = copy.deepcopy(self._xml_attr) # this is not cython compatible
return
duplicate
...
...
@@ -437,6 +437,24 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
def
attr
(
self
):
"""
Returns a dictionary of XML-attr attached to the node.
Examples:
>>> node = Node(None, '')
>>> print('Any attributes present?', node.has_attr())
Any attributes present? False
>>> node.attr['id'] = 'identificator'
>>> node.attr
OrderedDict([('id', 'identificator')])
>>> node.attr['id']
'identificator'
>>> del node['id']
>>> node.attr
OrderedDict()
NOTE: Use `node.attr_active()` rather than bool(node.attr) to check the
presence of any attributes. Attribute dictionaries are created lazily
and node.attr would create a dictionary, even though it may never be
needed any more.
"""
try
:
if
self
.
_xml_attr
is
None
:
# cython compatibility
...
...
@@ -446,14 +464,18 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
return
self
.
_xml_attr
def
attr
_active
(
self
)
->
bool
:
def
has_
attr
(
self
)
->
bool
:
"""
Returns True, if XML-Attributes of this node have ever been set
or queried, even if unsuccessfully.
Returns `True`, if the node has any attributes, `False` otherwise.
This function does not create an attribute dictionary, therefore
it should be prefered to querying node.attr when testing for the
existence of any attributes.
"""
try
:
if
self
.
_xml_attr
is
not
None
:
return
True
# if self._xml_attr is not None:
# return True
return
bool
(
self
.
_xml_attr
)
except
AttributeError
:
pass
return
False
...
...
@@ -464,12 +486,12 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
Returns True, if `self` and `other` have the same attributes with the
same attribute values.
"""
if
self
.
attr
_active
():
if
other
.
attr
_active
():
if
self
.
has_
attr
():
if
other
.
has_
attr
():
return
self
.
attr
==
other
.
attr
return
len
(
self
.
attr
)
==
0
# self has empty dictionary and other has no attributes
elif
other
.
attr
_active
():
elif
other
.
has_
attr
():
return
len
(
other
.
attr
)
==
0
# other has empty attribute dictionary and self as no attributes
return
True
# neither self nor other have any attributes
...
...
@@ -661,7 +683,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
txt
=
[
left_bracket
,
node
.
tag_name
]
# s += " '(pos %i)" % node.add_pos
# txt.append(str(id(node))) # for debugging
if
node
.
attr
_active
():
if
node
.
has_
attr
():
txt
.
extend
(
' `(%s "%s")'
%
(
k
,
v
)
for
k
,
v
in
node
.
attr
.
items
())
if
src
:
line
,
col
=
line_col
(
lbreaks
,
node
.
pos
)
...
...
@@ -715,13 +737,13 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
if
node
.
tag_name
in
omit_tags
:
return
''
txt
=
[
'<'
,
node
.
tag_name
]
has_reserved_attrs
=
node
.
attr
_active
()
\
has_reserved_attrs
=
node
.
has_
attr
()
\
and
any
(
r
in
node
.
attr
for
r
in
{
'err'
,
'line'
,
'col'
})
if
node
.
attr
_active
():
if
node
.
has_
attr
():
txt
.
extend
(
' %s="%s"'
%
(
k
,
v
)
for
k
,
v
in
node
.
attr
.
items
())
if
src
and
not
has_reserved_attrs
:
txt
.
append
(
' line="%i" col="%i"'
%
line_col
(
line_breaks
,
node
.
pos
))
if
src
==
''
and
not
(
node
.
attr
_active
()
and
'_pos'
in
node
.
attr
)
and
node
.
pos
>=
0
:
if
src
==
''
and
not
(
node
.
has_
attr
()
and
'_pos'
in
node
.
attr
)
and
node
.
pos
>=
0
:
txt
.
append
(
' _pos="%i"'
%
node
.
pos
)
if
root
and
id
(
node
)
in
root
.
error_nodes
and
not
has_reserved_attrs
:
txt
.
append
(
' err="%s"'
%
''
.
join
(
str
(
err
).
replace
(
'"'
,
r
'\"'
)
...
...
@@ -752,7 +774,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
printed on several lines to avoid unwanted gaps in the output.
"""
return
node
.
tag_name
in
inline_tags
\
or
(
node
.
attr
_active
()
or
(
node
.
has_
attr
()
and
node
.
attr
.
get
(
'xml:space'
,
'default'
)
==
'preserve'
)
line_breaks
=
linebreaks
(
src
)
if
src
else
[]
...
...
@@ -765,7 +787,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
data
=
[
self
.
tag_name
,
[
child
.
to_json_obj
()
for
child
in
self
.
children
]
if
self
.
children
else
str
(
self
.
_result
)]
has_attr
=
self
.
attr
_active
()
has_attr
=
self
.
has_
attr
()
if
self
.
_pos
>=
0
or
has_attr
:
data
.
append
(
self
.
_pos
)
if
has_attr
:
...
...
@@ -938,7 +960,7 @@ class RootNode(Node):
duplicate
.
children
=
NoChildren
duplicate
.
_result
=
self
.
_result
duplicate
.
_pos
=
self
.
_pos
if
self
.
attr
_active
():
if
self
.
has_
attr
():
duplicate
.
attr
.
update
(
copy
.
deepcopy
(
self
.
_xml_attr
))
# duplicate._xml_attr = copy.deepcopy(self._xml_attr) # this is blocked by cython
duplicate
.
errors
=
copy
.
copy
(
self
.
errors
)
...
...
@@ -968,7 +990,7 @@ class RootNode(Node):
self
.
children
=
node
.
children
self
.
_pos
=
node
.
_pos
self
.
tag_name
=
node
.
tag_name
if
node
.
attr
_active
():
if
node
.
has_
attr
():
self
.
_xml_attr
=
node
.
_xml_attr
# self._content = node._content
if
id
(
node
)
in
self
.
error_nodes
:
...
...
DHParser/transform.py
View file @
30e6dd80
...
...
@@ -531,8 +531,8 @@ def swap_attributes(node: Node, other: Node):
Exchanges the attributes between node and other. This might be
needed when rearanging trees.
"""
NA
=
node
.
attr
_active
()
OA
=
other
.
attr
_active
()
NA
=
node
.
has_
attr
()
OA
=
other
.
has_
attr
()
if
NA
or
OA
:
save
=
node
.
_xml_attr
if
NA
else
None
if
OA
:
...
...
@@ -564,7 +564,7 @@ def _reduce_child(node: Node, child: Node):
"""
node
.
result
=
child
.
result
update_attr
(
child
,
node
)
if
child
.
attr
_active
():
if
child
.
has_
attr
():
node
.
_xml_attr
=
child
.
_xml_attr
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment