-
Notifications
You must be signed in to change notification settings - Fork 1
/
serialize_tmx.py
55 lines (47 loc) · 1.8 KB
/
serialize_tmx.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
# -*- coding: utf-8 -*-
import gtk
import re
def convert(tmx):
inputtmx = open(tmx, 'r').read()
# pseudoconvert to TMX 1.4 - change header only
tmx14_1 = re.sub(r'<!DOCTYPE tmx SYSTEM "tmx11.dtd">',
r'<!DOCTYPE tmx SYSTEM "tmx14.dtd">', inputtmx)
tmx14 = re.sub(r'<tmx version = "1.1">', r'<tmx version = "1.4">', tmx14_1)
tmx14split = tmx14.splitlines()
tmx14lines = []
for line in tmx14split:
if re.match(r'<seg>', line):
phid = 1
newline = line
for tag in re.findall(r'<(.*?)>', line):
newline = re.sub(r'<(.*?)>', r'<ph x="' + str(phid)
+ r'">&lt;\1&gt;</ph>', newline, 1)
phid = phid + 1
tmx14lines.append(newline)
else:
tmx14lines.append(line)
outputtmx = open(tmx, 'w')
for item in tmx14lines:
outputtmx.write(item + '\n')
outputtmx.close()
def ChooseDialog():
choosedialog = gtk.FileChooserDialog('Show me the TMX to process.', None,
gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK)
)
filter = gtk.FileFilter()
filter.set_name('TMX files')
filter.add_pattern('*.tmx')
choosedialog.add_filter(filter)
choosedialog.set_current_name('.tmx')
choosedialog.set_default_response(gtk.RESPONSE_OK)
choosedialog.show_all()
chooseresponse = choosedialog.run()
if chooseresponse == gtk.RESPONSE_OK:
convert(choosedialog.get_filename())
print "TMX converted."
else:
print "So long."
choosedialog.destroy()
ChooseDialog()