]> git.evergreen-ils.org Git - working/Evergreen.git/blob - build/i18n/scripts/merge_ils_events.py
LP#1709932: recognize more strings from oils_i18n_gettext()
[working/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         try: 
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))
35
36     return merged
37
38 def get_l10n_event_desc(l10n_xml, code):
39     """
40     Gets a localized event description
41     """
42
43     desc_nodes = ''
44
45     events = l10n_xml.getElementsByTagName('event')
46     for event in events:
47         if event.getAttribute('code') == code:
48             for node in event.childNodes:
49                 if node.nodeName == 'desc':
50                     return node
51         else:
52             continue
53             
54 def main():
55     """
56     Determine what action to take
57     """
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', \
61         metavar='FILE')
62     opts.add_option('-l', '--localization', action='store', \
63         help='Localized ils_events.xml file', \
64         metavar='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()
70
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)')
75     else:
76         merged = merge_events(options.master, options.localization)
77
78     if options.outfile:
79         outfile = open(options.outfile, 'w')
80         if options.pretty:
81             outfile.write(merged.toprettyxml(encoding='utf-8'))
82         else:
83             outfile.write(merged.toxml(encoding='utf-8'))
84     else:
85         if options.pretty:
86             print merged.toprettyxml(encoding='utf-8')
87         else:
88             print merged.toxml(encoding='utf-8')
89
90 if __name__ == '__main__':
91     main()
92
93 # vim:et:ts=4:sw=4: