Debian Codegraph @

scripts/apertium-metalrx

raw
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2018 Xavi Ivars <[email protected]>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License as
7 # published by the Free Software Foundation; either version 2 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, see <https://www.gnu.org/licenses/>.
17 #
18
19 import sys
20 import xml.etree.ElementTree as ET
21
22 source = sys.argv[1]
23 target = sys.argv[2]
24
25 tree = ET.ElementTree()
26 tree.parse(source)
27
28 rules = tree.find('rules')
29
30 macros = []
31
32 for macro in tree.iter(tag='macro'):
33
34 defmacro = tree.find('.//def-macro[@name="' + macro.attrib['name'] + '"]')
35
36 macrostr = ET.tostring(defmacro, encoding="unicode")
37 for k,v in macro.attrib.items():
38 if k.startswith('p'):
39 kstr = '{{' + k + '}}'
40 macrostr = macrostr.replace(kstr, v)
41
42 newtree = ET.fromstring('<root>' + macrostr + '</root>')
43
44 for r in newtree.iter(tag='rule'):
45 rules.append(r)
46
47 macros.append(macro)
48
49 allmacrodefs = tree.getroot().find('def-macros')
50 if allmacrodefs is not None:
51 tree.getroot().remove(allmacrodefs)
52
53 for macro in macros:
54 rules.remove(macro)
55
56 if tree.getroot().tag == 'metalrx':
57 tree.getroot().tag = 'lrx'
58
59 tree.write(target)