6 def merge_events(master, localization):
8 Merge two event definition files
11 master_xml = xml.dom.minidom.parse(master)
12 l10n_xml = xml.dom.minidom.parse(localization)
13 impl = xml.dom.minidom.getDOMImplementation()
15 merged = impl.createDocument(None, 'ils_events', None)
18 notes = master_xml.getElementsByTagName('notes')[0]
19 merged.documentElement.appendChild(notes)
21 events = master_xml.getElementsByTagName('event')
24 code = event.getAttribute('code')
25 merged.documentElement.appendChild(merged.createTextNode("\n"))
26 l10n_node = get_l10n_event_desc(l10n_xml, code)
27 for child in event.childNodes:
28 if child.nodeName == 'desc':
29 if child.getAttribute('xml:lang') == l10n_node.getAttribute('xml:lang'):
30 event.removeChild(child)
31 event.appendChild(l10n_node)
32 merged.documentElement.appendChild(event)
33 merged.documentElement.appendChild(merged.createTextNode("\n"))
34 except AttributeError:
35 print("%s probably has an <event> [%s] without a matching <desc> node" % (localization, code))
39 def get_l10n_event_desc(l10n_xml, code):
41 Gets a localized event description
46 events = l10n_xml.getElementsByTagName('event')
48 if event.getAttribute('code') == code:
49 for node in event.childNodes:
50 if node.nodeName == 'desc':
57 Determine what action to take
59 opts = optparse.OptionParser()
60 opts.add_option('-m', '--master', action='store', \
61 help='Master ils_events.xml file into which we are merging our additional localized strings', \
63 opts.add_option('-l', '--localization', action='store', \
64 help='Localized ils_events.xml file', \
66 opts.add_option('-o', '--output', dest='outfile', \
67 help='Write output to FILE (defaults to STDOUT)', metavar='FILE')
68 opts.add_option('-p', '--pretty', action='store', \
69 help='Write pretty XML output')
70 (options, args) = opts.parse_args()
72 if not options.master:
73 opts.error('Must specify the master ils_events file (-m option)')
74 elif not options.localization:
75 opts.error('Must specify the localized ils_events file to merge (-l option)')
77 merged = merge_events(options.master, options.localization)
80 outfile = codecs.open(options.outfile, encoding='utf-8', mode='w')
82 outfile.write(merged.toprettyxml(encoding='utf-8'))
84 outfile.write(merged.toxml(encoding='utf-8'))
87 print merged.toprettyxml(encoding='utf-8')
89 print merged.toxml(encoding='utf-8')
91 if __name__ == '__main__':