]> git.proxmox.com Git - mirror_ovs.git/blob - tests/uuidfilt.py
ofp-print: Abbreviate lists of fields in table features output.
[mirror_ovs.git] / tests / uuidfilt.py
1 #!/usr/bin/env python
2
3 import re
4 import sys
5
6
7 def lookup_uuid(uuids, match):
8 return "<%s>" % uuids.setdefault(match.group(0), len(uuids))
9
10
11 int_re = re.compile(r'\d+')
12
13
14 def sort_set(match):
15 s = match.group(0)
16 uuids = sorted([int(x) for x in int_re.findall(s)])
17 return '["set",[' + ','.join('["uuid","<%s>"]' % x for x in uuids) + ']]'
18
19
20 u = '[0-9a-fA-F]'
21 uuid_re = re.compile(r'%s{8}-%s{4}-%s{4}-%s{4}-%s{12}' % ((u,) * 5))
22 set_re = re.compile(r'(\["set",\[(,?\["uuid","<\d+>"\])+\]\])')
23
24
25 def filter_uuids(src, dst):
26 uuids = {}
27
28 def lf(match):
29 return lookup_uuid(uuids, match)
30
31 while True:
32 line = src.readline()
33 if not line:
34 break
35 line = uuid_re.sub(lf, line)
36
37 # Sort sets like this:
38 # [["uuid","<1>"],["uuid","<0>"]]
39 # to look like this:
40 # [["uuid","<0>"],["uuid","<1>"]]
41 line = set_re.sub(sort_set, line)
42 dst.write(line)
43
44
45 if __name__ == '__main__':
46 if len(sys.argv) > 1:
47 for src in sys.argv[1:]:
48 filter_uuids(open(src), sys.stdout)
49 else:
50 filter_uuids(sys.stdin, sys.stdout)