-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-xml.py
73 lines (58 loc) · 2.44 KB
/
format-xml.py
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
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
import argparse
import os
import pathlib
import sys
import xml.etree.ElementTree as ET
def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input',
required=True,
type=str,
action='store',
metavar='PATH',
help="Path to the input directory.")
parser.add_argument('-o', '--output',
type=str,
action='store',
metavar='PATH',
help="Path to the output directory")
parser.add_argument('-is', '--ignore-subdirs',
action='store_true',
help="Set if you only want to format files from the specified directory and not from subdirectories.")
args = parser.parse_args()
if not os.path.isdir(args.input):
sys.exit("The input directory does not exist!")
input_dir = pathlib.Path(args.input).resolve()
if args.output is None:
args.output = '{}-formatted'.format(input_dir.stem)
output_dir = pathlib.Path(args.output).resolve()
search_pattern = '**/*' # Query subdirs
if args.ignore_subdirs:
search_pattern = '*' # Query only the current dir
paths = []
# Only XML files and ignore hidden files / dirs
for path in input_dir.glob(search_pattern):
if path.suffix.lower() == '.xml' and not path.name.startswith('.') and not os.path.isdir(path):
paths.append(path)
for path in paths:
# Extract the dirs of the input folder
striped_path = path.parts[len(path.parts) - path.parts[::-1].index(input_dir.stem):-1]
# Build the path for the output file
output_path = pathlib.Path(output_dir, *striped_path, path.name)
# Create dir in the output dir
pathlib.Path(output_dir, *striped_path).mkdir(parents=True, exist_ok=True)
# Read file and pretty XML
root = ET.parse(path).getroot()
tree = ET.ElementTree(root)
ET.indent(tree)
with open(output_path, 'w') as output_xml:
tree.write(output_xml, encoding='unicode', xml_declaration=True)
print("{}\n"
"--->\n"
"{}\n".format(path, output_path))
print("+-----------------------+\n"
"Formatted files: {}\n"
"+-----------------------+".format(len(paths)))
if __name__ == '__main__':
main()