]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/i18n/scripts/merge_ils_events.py
Bring the magic of codecs to the i18n script masses
[working/Evergreen.git] / build / i18n / scripts / merge_ils_events.py
1 #!/usr/bin/env python
2 import codecs
3 import xml.dom.minidom
4 import optparse
5
6 def merge_events(master, localization):
7     """
8     Merge two event definition files
9     """
10
11     master_xml = xml.dom.minidom.parse(master)
12     l10n_xml = xml.dom.minidom.parse(localization)
13     impl = xml.dom.minidom.getDOMImplementation()
14
15     merged = impl.createDocument(None, 'ils_events', None)
16
17     # Add notes
18     notes = master_xml.getElementsByTagName('notes')[0]
19     merged.documentElement.appendChild(notes)
20
21     events = master_xml.getElementsByTagName('event')
22     for event in events:
23         try: 
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))
36
37     return merged
38
39 def get_l10n_event_desc(l10n_xml, code):
40     """
41     Gets a localized event description
42     """
43
44     desc_nodes = ''
45
46     events = l10n_xml.getElementsByTagName('event')
47     for event in events:
48         if event.getAttribute('code') == code:
49             for node in event.childNodes:
50                 if node.nodeName == 'desc':
51                     return node
52         else:
53             continue
54             
55 def main():
56     """
57     Determine what action to take
58     """
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', \
62         metavar='FILE')
63     opts.add_option('-l', '--localization', action='store', \
64         help='Localized ils_events.xml file', \
65         metavar='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()
71
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)')
76     else:
77         merged = merge_events(options.master, options.localization)
78
79     if options.outfile:
80         outfile = codecs.open(options.outfile, encoding='utf-8', mode='w')
81         if options.pretty:
82             outfile.write(merged.toprettyxml(encoding='utf-8'))
83         else:
84             outfile.write(merged.toxml(encoding='utf-8'))
85     else:
86         if options.pretty:
87             print merged.toprettyxml(encoding='utf-8')
88         else:
89             print merged.toxml(encoding='utf-8')
90
91 if __name__ == '__main__':
92     main()
93
94 # vim:et:ts=4:sw=4: