| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #! /usr/bin/env python3 | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # | 
 | 3 | # SPDX-License-Identifier: GPL-2.0-only | 
 | 4 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 5 |  | 
 | 6 | import sys | 
 | 7 | try: | 
 | 8 |     import xml.etree.cElementTree as etree | 
 | 9 | except: | 
 | 10 |     import xml.etree.ElementTree as etree | 
 | 11 |  | 
 | 12 | def child (elem, name): | 
 | 13 |     for e in elem.getchildren(): | 
 | 14 |         if e.tag == name: | 
 | 15 |             return e | 
 | 16 |     return None | 
 | 17 |  | 
 | 18 | def children (elem, name=None): | 
 | 19 |     l = elem.getchildren() | 
 | 20 |     if name: | 
 | 21 |         l = [e for e in l if e.tag == name] | 
 | 22 |     return l | 
 | 23 |  | 
| Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 24 | if len(sys.argv) < 2 or sys.argv[1] in ('-h', '--help'): | 
 | 25 |     print('oe-trim-schemas: error: the following arguments are required: schema\n' | 
 | 26 |           'Usage: oe-trim-schemas schema\n\n' | 
 | 27 |           'OpenEmbedded trim schemas - remove unneeded schema locale translations\n' | 
 | 28 |           '                            from gconf schema files\n\n' | 
 | 29 |           'arguments:\n' | 
 | 30 |           '  schema                gconf schema file to trim\n') | 
 | 31 |     sys.exit(2) | 
 | 32 |  | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 33 | xml = etree.parse(sys.argv[1]) | 
 | 34 |  | 
 | 35 | for schema in child(xml.getroot(), "schemalist").getchildren(): | 
 | 36 |     e = child(schema, "short") | 
 | 37 |     if e is not None: | 
 | 38 |         schema.remove(e) | 
 | 39 |  | 
 | 40 |     e = child(schema, "long") | 
 | 41 |     if e is not None: | 
 | 42 |         schema.remove(e) | 
 | 43 |  | 
 | 44 |     for locale in children(schema, "locale"): | 
 | 45 |         # One locale must exist so leave C locale... | 
 | 46 |         a = locale.attrib.get("name") | 
 | 47 |         if a == 'C': | 
 | 48 |             continue | 
 | 49 |         e = child(locale, "default") | 
 | 50 |         if e is None: | 
 | 51 |             schema.remove(locale) | 
 | 52 |         else: | 
 | 53 |             e = child(locale, "short") | 
 | 54 |             if e is not None: | 
 | 55 |                 locale.remove(e) | 
 | 56 |             e = child(locale, "long") | 
 | 57 |             if e is not None: | 
 | 58 |                 locale.remove(e) | 
 | 59 |  | 
 | 60 | xml.write(sys.stdout, "UTF-8") | 
 | 61 |  |