]>
git.proxmox.com Git - mirror_ovs.git/blob - tests/ovsdb-monitor-sort.py
2 # Copyright (c) 2020 VMware, Inc.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # Breaks lines read from stdin into groups using blank lines as
17 # group separators, then sorts lines within the groups for
24 # This is copied out of the Python Sorting HOWTO at
25 # https://docs.python.org/3/howto/sorting.html#sortinghowto
26 def cmp_to_key(mycmp
):
27 'Convert a cmp= function into a key= function'
30 def __init__(self
, obj
, *args
):
33 def __lt__(self
, other
):
34 return mycmp(self
.obj
, other
.obj
) < 0
36 def __gt__(self
, other
):
37 return mycmp(self
.obj
, other
.obj
) > 0
39 def __eq__(self
, other
):
40 return mycmp(self
.obj
, other
.obj
) == 0
42 def __le__(self
, other
):
43 return mycmp(self
.obj
, other
.obj
) <= 0
45 def __ge__(self
, other
):
46 return mycmp(self
.obj
, other
.obj
) >= 0
48 def __ne__(self
, other
):
49 return mycmp(self
.obj
, other
.obj
) != 0
55 uuid_re
= re
.compile(r
'%s{8}-%s{4}-%s{4}-%s{4}-%s{12}' % ((u
,) * 5))
59 return (a
> b
) - (a
< b
)
62 def compare_lines(a
, b
):
65 return cmp(a
[36:], b
[36:])
68 elif uuid_re
.match(b
):
74 def output_group(group
, dst
):
75 for x
in sorted(group
, key
=cmp_to_key(compare_lines
)):
79 def ovsdb_monitor_sort(src
, dst
):
85 if line
.rstrip() == '':
86 output_group(group
, dst
)
89 elif line
.startswith(',') and group
:
90 group
[len(group
) - 1] += line
94 output_group(group
, dst
)
97 if __name__
== '__main__':
98 ovsdb_monitor_sort(sys
.stdin
, sys
.stdout
)