8f02505635477c7d33450bab969838ca741882cc
[Evergreen.git] / build / i18n / scripts / merge_ils_events.py
1 #!/usr/bin/env python
2 import xml.dom.minidom
3 import optparse
4
5 def merge_events(master, localization):
6     """
7     Merge two event definition files
8     """
9
10     master_xml = xml.dom.minidom.parse(master)
11     l10n_xml = xml.dom.minidom.parse(localization)
12     impl = xml.dom.minidom.getDOMImplementation()
13
14     merged = impl.createDocument(None, 'ils_events', None)
15
16     # Add notes
17     notes = master_xml.getElementsByTagName('notes')[0]
18     merged.documentElement.appendChild(notes)
19
20     events = master_xml.getElementsByTagName('event')
21     for event in events:
22         merged.documentElement.appendChild(merged.createTextNode("\n"))
23         l10n_node = get_l10n_event_desc(l10n_xml, event.getAttribute('code'))
24         for child in event.childNodes:
25             if child.nodeName == 'desc':
26                 if child.getAttribute('xml:lang') == l10n_node.getAttribute('xml:lang'):
27                     event.removeChild(child)
28         event.appendChild(l10n_node)
29         merged.documentElement.appendChild(event)
30         merged.documentElement.appendChild(merged.createTextNode("\n"))
31
32     return merged
33
34 def get_l10n_event_desc(l10n_xml, code):
35     """
36     Gets a localized event description
37     """
38
39     desc_nodes = ''
40
41     events = l10n_xml.getElementsByTagName('event')
42     for event in events:
43         if event.getAttribute('code') == code:
44             for node in event.childNodes:
45                 if node.nodeName == 'desc':
46                     return node
47         else:
48             continue
49             
50 def main():
51     """
52     Determine what action to take
53     """
54     opts = optparse.OptionParser()
55     opts.add_option('-m', '--master', action='store', \
56         help='Master ils_events.xml file into which we are merging our additional localized strings', \
57         metavar='FILE')
58     opts.add_option('-l', '--localization', action='store', \
59         help='Localized ils_events.xml file', \
60         metavar='FILE')
61     opts.add_option('-o', '--output', dest='outfile', \
62         help='Write output to FILE (defaults to STDOUT)', metavar='FILE')
63     opts.add_option('-p', '--pretty', action='store', \
64         help='Write pretty XML output')
65     (options, args) = opts.parse_args()
66
67     if not options.master:
68         opts.error('Must specify the master ils_events file (-m option)')
69     elif not options.localization:
70         opts.error('Must specify the localized ils_events file to merge (-l option)')
71     else:
72         merged = merge_events(options.master, options.localization)
73
74     if options.outfile:
75         outfile = open(options.outfile, 'w')
76         if options.pretty:
77             outfile.write(merged.toprettyxml(encoding='utf-8'))
78         else:
79             outfile.write(merged.toxml(encoding='utf-8'))
80     else:
81         if options.pretty:
82             print merged.toprettyxml(encoding='utf-8')
83         else:
84             print merged.toxml(encoding='utf-8')
85
86 if __name__ == '__main__':
87     main()
88
89 # vim:et:ts=4:sw=4: