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