Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
M
mio
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Blomdell
mio
Commits
0e910d37
Commit
0e910d37
authored
Aug 26, 2019
by
Anders Blomdell
Browse files
Options
Downloads
Patches
Plain Diff
Adjust sorting from cmp= to key= syntax
parent
324a38be
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/mio/parser.py
+8
-61
8 additions, 61 deletions
src/mio/parser.py
with
8 additions
and
61 deletions
src/mio/parser.py
+
8
−
61
View file @
0e910d37
...
@@ -136,58 +136,6 @@ def xml_escape(s):
...
@@ -136,58 +136,6 @@ def xml_escape(s):
return
result
return
result
class
AttrSortWrapper
:
"""
Wrapper class for sorting attributes
[see Node._xml(...)]
"""
def
__init__
(
self
,
node
,
sort
):
self
.
node
=
node
self
.
sort
=
sort
def
__call__
(
self
,
a
,
b
):
"""
Return the sort order between attributes
'
a
'
and
'
b
'
.
If sort returns None, a standard Python comparison is used.
"""
result
=
None
if
self
.
sort
:
result
=
self
.
sort
(
self
.
node
,
a
,
b
)
if
result
==
None
:
if
a
<
b
:
result
=
-
1
elif
a
>
b
:
result
=
1
else
:
result
=
0
return
result
class
TagSortWrapper
:
"""
Wrapper class for sorting tags
[see Node._xml(...)]
"""
def
__init__
(
self
,
parent
,
sort
):
self
.
parent
=
parent
self
.
sort
=
sort
def
__call__
(
self
,
a
,
b
):
"""
Return the sort order between tags
'
a
'
and
'
b
'
.
If sort returns None, a standard Python comparison is used.
"""
result
=
None
if
self
.
sort
:
result
=
self
.
sort
(
self
.
parent
,
a
,
b
)
if
result
==
None
:
if
a
.
_tag
<
b
.
_tag
:
result
=
-
1
elif
a
.
_tag
>
b
.
_tag
:
result
=
1
else
:
result
=
0
return
result
class
ChildAccessor
:
class
ChildAccessor
:
"""
Helper class for iterating over xml trees
"""
Helper class for iterating over xml trees
...
@@ -374,14 +322,13 @@ class Node:
...
@@ -374,14 +322,13 @@ class Node:
else
:
else
:
return
AttributeAccessor
(
self
,
attr
)
return
AttributeAccessor
(
self
,
attr
)
def
_xml
(
self
,
indent
=
0
,
attr_
sort
=
None
,
tag_sort
=
None
,
width
=
80
):
def
_xml
(
self
,
indent
=
0
,
attr_
key
=
None
,
node_key
=
None
,
width
=
80
):
"""
Generate a prettyprinted xml of the tree rooted in this node
"""
"""
Generate a prettyprinted xml of the tree rooted in this node
"""
result
=
""
result
=
""
line
=
"
%s<%s
"
%
(
"
"
*
indent
,
self
.
_tag
)
line
=
"
%s<%s
"
%
(
"
"
*
indent
,
self
.
_tag
)
blanks
=
"
"
*
len
(
line
)
blanks
=
"
"
*
len
(
line
)
for
k
in
sorted
(
self
.
_attribute
.
keys
(),
for
k
in
sorted
(
self
.
_attribute
.
keys
(),
key
=
(
attr_sort
and
AttrSortWrapper
(
self
,
attr_sort
)
key
=
attr_key
and
(
lambda
a
:
attr_key
(
self
,
a
))):
or
None
)):
s
=
"
%s=
'
%s
'"
%
(
k
,
xml_escape
(
self
.
_attribute
[
k
]))
s
=
"
%s=
'
%s
'"
%
(
k
,
xml_escape
(
self
.
_attribute
[
k
]))
if
len
(
line
)
+
len
(
s
)
>
width
:
if
len
(
line
)
+
len
(
s
)
>
width
:
result
+=
"
%s
\n
"
%
line
result
+=
"
%s
\n
"
%
line
...
@@ -396,12 +343,9 @@ class Node:
...
@@ -396,12 +343,9 @@ class Node:
result
+=
'
\n
'
.
join
(
self
.
_char
)
result
+=
'
\n
'
.
join
(
self
.
_char
)
result
+=
'
\n
'
result
+=
'
\n
'
pass
pass
print
(
"
YYY
"
,
(
tag_sort
and
TagSortWrapper
(
self
,
tag_sort
)
or
None
),
"
\n
"
)
for
c
in
sorted
(
self
.
_children
,
for
c
in
sorted
(
self
.
_children
,
key
=
(
tag_sort
and
TagSortWrapper
(
self
,
tag_sort
)
key
=
node_key
and
(
lambda
c
:
node_key
(
self
,
c
))):
or
None
)):
result
+=
c
.
_xml
(
indent
+
1
,
attr_key
,
node_key
,
width
)
result
+=
c
.
_xml
(
indent
+
1
,
attr_sort
,
tag_sort
,
width
)
result
+=
"
%s</%s>
\n
"
%
(
"
"
*
indent
,
self
.
_tag
)
result
+=
"
%s</%s>
\n
"
%
(
"
"
*
indent
,
self
.
_tag
)
return
result
return
result
...
@@ -453,6 +397,9 @@ class Comment:
...
@@ -453,6 +397,9 @@ class Comment:
self
.
_tag
=
''
self
.
_tag
=
''
self
.
_attribute
=
{
''
:
content
}
self
.
_attribute
=
{
''
:
content
}
def
__lt__
(
self
,
other
):
return
self
.
_tag
<
other
.
_tag
def
__copy__
(
self
):
def
__copy__
(
self
):
"""
Copy self
"""
"""
Copy self
"""
result
=
Comment
(
self
.
_content
,
self
.
_line
)
result
=
Comment
(
self
.
_content
,
self
.
_line
)
...
@@ -462,7 +409,7 @@ class Comment:
...
@@ -462,7 +409,7 @@ class Comment:
"""
Copy node and all children
"""
"""
Copy node and all children
"""
return
self
.
__copy__
()
return
self
.
__copy__
()
def
_xml
(
self
,
indent
=
0
,
attr_
sort
=
None
,
tag_sort
=
None
,
width
=
80
):
def
_xml
(
self
,
indent
=
0
,
attr_
key
=
None
,
node_key
=
None
,
width
=
80
):
"""
Generate a prettyprinted xml of the tree rooted in this node
"""
"""
Generate a prettyprinted xml of the tree rooted in this node
"""
result
=
"
%s<!--
"
%
(
"
"
*
indent
)
result
=
"
%s<!--
"
%
(
"
"
*
indent
)
result
+=
self
.
_attribute
[
''
]
result
+=
self
.
_attribute
[
''
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment