summaryrefslogtreecommitdiffstats
path: root/share/extensions/tools/generate_argparse_conf.py
blob: d99337b7d3be517cd76c2a0be2245fa8753c1124 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# coding=utf-8
import os
from string import Template
import xml.etree.ElementTree as ET
import argparse

parser = argparse.ArgumentParser(
    description="Reads an *.inx file and generates initialization code for argparse"
)
parser.add_argument("input")
args = parser.parse_args()

if os.path.isabs(args.input):
    inpath = args.input
else:
    folder = os.path.dirname(os.path.realpath(__file__))
    inpath = os.path.normpath(os.path.join(folder, args.input))

templateWithType = Template(
    'self.arg_parser.add_argument("--$param",  type=$type, dest="$param", default=$default)'
)
templateWithoutType = Template(
    'self.arg_parser.add_argument("--$param",  dest="$param", default=$default)'
)


def handle_param_node(node):
    if node.attrib["type"] == "float":
        cmd = templateWithType.substitute(
            param=node.attrib["name"], type="float", default=node.text
        )
        print(cmd)
    elif node.attrib["type"] == "int":
        cmd = templateWithType.substitute(
            param=node.attrib["name"], type="int", default=node.text
        )
        print(cmd)
    elif node.attrib["type"] == "boolean":
        cmd = templateWithType.substitute(
            param=node.attrib["name"],
            type="inkex.inkbool",
            default='"' + node.text + '"',
        )
        print(cmd)
    elif node.attrib["type"] == "enum":
        cmd = templateWithoutType.substitute(
            param=node.attrib["name"], default='"' + node[0].text + '"'
        )
        print(cmd)
    elif node.attrib["type"] == "notebook":
        cmd = templateWithoutType.substitute(
            param=node.attrib["name"], default='"' + node[0].attrib["name"] + '"'
        )
        print(cmd)
    else:
        # TODO: Implement other types of args
        raise NotImplementedError


def process_node(node):
    for child in node:
        if child.tag.endswith("param"):
            handle_param_node(child)
        process_node(child)


tree = ET.parse(inpath)
root = tree.getroot()
process_node(root)