5 def merge_events(master, localization):
7 Merge two event definition files
10 master_xml = xml.dom.minidom.parse(master)
11 l10n_xml = xml.dom.minidom.parse(localization)
12 impl = xml.dom.minidom.getDOMImplementation()
14 merged = impl.createDocument(None, 'ils_events', None)
17 notes = master_xml.getElementsByTagName('notes')[0]
18 merged.documentElement.appendChild(notes)
20 events = master_xml.getElementsByTagName('event')
23 code = event.getAttribute('code')
24 merged.documentElement.appendChild(merged.createTextNode("\n"))
25 l10n_node = get_l10n_event_desc(l10n_xml, code)
26 for child in event.childNodes:
27 if child.nodeName == 'desc':
28 if child.getAttribute('xml:lang') == l10n_node.getAttribute('xml:lang'):
29 event.removeChild(child)
30 event.appendChild(l10n_node)
31 merged.documentElement.appendChild(event)
32 merged.documentElement.appendChild(merged.createTextNode("\n"))
33 except AttributeError:
34 print("%s probably has an <event> [%s] without a matching <desc> node" % (localization, code))
38 def get_l10n_event_desc(l10n_xml, code):
40 Gets a localized event description
45 events = l10n_xml.getElementsByTagName('event')
47 if event.getAttribute('code') == code:
48 for node in event.childNodes:
49 if node.nodeName == 'desc':
56 Determine what action to take
58 opts = optparse.OptionParser()
59 opts.add_option('-m', '--master', action='store', \
60 help='Master ils_events.xml file into which we are merging our additional localized strings', \
62 opts.add_option('-l', '--localization', action='store', \
63 help='Localized ils_events.xml file', \
65 opts.add_option('-o', '--output', dest='outfile', \
66 help='Write output to FILE (defaults to STDOUT)', metavar='FILE')
67 opts.add_option('-p', '--pretty', action='store', \
68 help='Write pretty XML output')
69 (options, args) = opts.parse_args()
71 if not options.master:
72 opts.error('Must specify the master ils_events file (-m option)')
73 elif not options.localization:
74 opts.error('Must specify the localized ils_events file to merge (-l option)')
76 merged = merge_events(options.master, options.localization)
79 outfile = open(options.outfile, 'w')
81 outfile.write(merged.toprettyxml(encoding='utf-8'))
83 outfile.write(merged.toxml(encoding='utf-8'))
86 print merged.toprettyxml(encoding='utf-8')
88 print merged.toxml(encoding='utf-8')
90 if __name__ == '__main__':