forked from philipmat/discogs-xml2db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-xml.py
67 lines (50 loc) · 1.46 KB
/
fix-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
from __future__ import with_statement
import re
import sys
import os
from contextlib import nested
def clean(filename):
# delete any byte that's between 0x00 and 0x1F except 0x09 (tab), 0x0A (LF), and 0x0D (CR).
ctrlregex = re.compile(r'[\x01-\x08|\x0B|\x0C|\x0E-\x1F]')
try:
os.rename(filename, "%s.old" % filename)
except:
print("Did not rename")
with nested(open(filename, "wb"), open(filename + ".old", "rb")) as (destination, source):
counter = 0
for line in source:
rObj = re.search(ctrlregex, line)
counter += 1
if rObj is not None:
print(counter)
newLine = re.sub(ctrlregex, '', line)
destination.write(newLine)
else:
destination.write(line)
os.remove("%s.old" % filename)
def usage():
print("Usage: python fix-xml.py release directory, where release is for example 20091101")
sys.exit()
def main(argv):
if len(argv) == 0 or len(argv[0]) != 8:
usage()
try:
int(argv[0])
except ValueError:
usage()
sys.exit()
if len(argv) > 1 and os.path.isdir(argv[1]):
path = argv[1] + '/'
else:
path = ''
release = argv[0]
filename = path + 'discogs_%s_labels.xml' % release
clean(filename)
filename = path + 'discogs_%s_releases.xml' % release
clean(filename)
filename = path + 'discogs_%s_masters.xml' % release
clean(filename)
filename = path + 'discogs_%s_artists.xml' % release
clean(filename)
if __name__ == '__main__':
main(sys.argv[1:])