This repository was archived by the owner on Jun 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremovenew.py
executable file
·75 lines (60 loc) · 1.67 KB
/
removenew.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/python
import xml.etree.ElementTree as ET
import glob
import os
import hashlib
import sys
import datetime
# Variables to keep track of progress
fileschecked = 0
issues = 0
xmlerrors = 0
fileschanged = 0
# Calculate md5 hash to check for changes in file.
def md5_for_file(f, block_size=2 ** 20):
md5 = hashlib.md5()
while True:
data = f.read(block_size)
if not data:
break
md5.update(data)
return md5.digest()
# Nicely indents the XML output
def indent(elem, level=0):
i = "\n" + level * "\t"
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + "\t"
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
indent(elem, level + 1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
# Loop over all files and create new data
for filename in glob.glob("systems*/*.xml"):
fileschecked += 1
# Open file
f = open(filename, 'rt')
# Try to parse file
try:
root = ET.parse(f).getroot()
planets = root.findall(".//planet")
except ET.ParseError as error:
print '{}, {}'.format(filename, error)
xmlerrors += 1
issues += 1
continue
finally:
f.close()
for planet in planets:
newtags = planet.findall(".//new")
for newtag in newtags:
planet.remove(newtag)
# Cleanup XML
indent(root)
# Write XML to file.
ET.ElementTree(root).write(filename, encoding="UTF-8", xml_declaration=False)