-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix_xml.py
More file actions
65 lines (54 loc) · 2.1 KB
/
Copy pathfix_xml.py
File metadata and controls
65 lines (54 loc) · 2.1 KB
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
import sys
from bs4 import BeautifulSoup
def fix_xml(in_path, out_path):
"""Programmatic entry point to fix_xml.
:param in_path: path to input xml file
:param out_path: path to output fixed xml file
"""
with open(in_path, 'r', encoding='utf-8') as infile:
soup: BeautifulSoup = Fixer(infile, 'xml')
with open(out_path, 'w', encoding='utf-8') as outfile:
outfile.write(str(soup))
class Fixer(BeautifulSoup):
NESTABLE_TAGS: dict = {
'suite': ['robot', 'suite', 'statistics'],
'doc': ['suite', 'test', 'kw'],
'metadata': ['suite'],
'item': ['metadata'],
'status': ['suite', 'test', 'kw'],
'test': ['suite'],
'tags': ['test'],
'tag': ['tags'],
'kw': ['suite', 'test', 'kw'],
'msg': ['kw', 'errors'],
'arguments': ['kw'],
'arg': ['arguments'],
'statistics': ['robot'],
'errors': ['robot']
}
__close_on_open = None
def handle_starttag(self, name, namespace, nsprefix, attrs, sourceline=None, sourcepos=None, namespaces=None):
if name == 'robot':
attrs = [(key, value) if key != 'generator' else ('generator', 'robotfix_xml.py') for key, value in
attrs.items()]
if name == 'kw' and ('type', 'teardown') in attrs:
while self.tagStack[-1].name not in ['test', 'suite']:
self.handle_endtag(self.tagStack[-1].name)
if self.__close_on_open:
self.handle_endtag(self.__close_on_open)
self.__close_on_open = None
super().handle_starttag(name, namespace, nsprefix, attrs, sourceline=sourceline, sourcepos=sourcepos,
namespaces=namespaces)
def handle_endtag(self, name, nsprefix=None):
super().handle_endtag(name, nsprefix=nsprefix)
if name == 'status':
self.__close_on_open = self.tagStack[-1].name
else:
self.__close_on_open = None
if __name__ == '__main__':
try:
fix_xml(*sys.argv[1:])
except TypeError:
print(__doc__)
else:
print(sys.argv[2])