~michihenning/thumbnailer/snap

« back to all changes in this revision

Viewing changes to tools/parse-settings.py

Added tool to parse the Settings schema and create a header file with the default values

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python3
 
2
#
 
3
# Copyright (C) 2015 Canonical Ltd.
 
4
# Author: Xavi Garcia <xavi.garcia.mena@canonical.com>
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify
 
7
# it under the terms of the GNU General Public License as published by
 
8
# the Free Software Foundation; version 3.
 
9
#
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU 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 <http://www.gnu.org/licenses/>.
 
17
#
 
18
 
 
19
import xml.etree.ElementTree as ET
 
20
import getopt, sys, os
 
21
 
 
22
def string_parameter(name, default_value, output):
 
23
    line = "    char const %s_DEFAULT[] = %s;" % (name.replace("-", "_").upper(), default_value)
 
24
    print(line, file=output)
 
25
 
 
26
def integer_parameter(name, default_value, output):
 
27
    line = "    int const %s_DEFAULT = %s;" % (name.replace("-", "_").upper(), default_value)
 
28
    print(line, file=output)
 
29
 
 
30
def process_default(name):
 
31
    print("WARNING: unsupported type found for parameter %s\n" % name)
 
32
 
 
33
options = {'i' : integer_parameter,
 
34
           's' : string_parameter,
 
35
}
 
36
 
 
37
def usage():
 
38
    print("usage: parse-settings -i INPUT-SCHEMA-FILE -o OUTPUT-FILE")
 
39
 
 
40
def parse_args():
 
41
    try:
 
42
        opts, args = getopt.getopt(sys.argv[1:], "i:o:h")
 
43
    except getopt.GetoptError as err:
 
44
        # print help information and exit:
 
45
        print(str(err)) # will print something like "option -a not recognized"
 
46
        usage()
 
47
        sys.exit(2)
 
48
    output = None
 
49
    input = None
 
50
    for o, a in opts:
 
51
        if o == "-h":
 
52
            usage()
 
53
            sys.exit()
 
54
        elif o == "-o":
 
55
            output = a
 
56
        elif o == "-i":
 
57
            input = a
 
58
        else:
 
59
            assert False, "unhandled option"
 
60
 
 
61
    if (input == None or output==None):
 
62
        usage()
 
63
        sys.exit()
 
64
 
 
65
    return (input, output)
 
66
 
 
67
def write_copyright(f):
 
68
    print("/*", file=f)
 
69
    print("* Copyright (C) 2015 Canonical Ltd.", file=f)
 
70
    print("*", file=f)
 
71
    print("* This program is free software: you can redistribute it and/or modify", file=f)
 
72
    print("* it under the terms of the GNU Lesser General Public License version 3 as", file=f)
 
73
    print("* published by the Free Software Foundation.", file=f)
 
74
    print("*", file=f)
 
75
    print("* This program is distributed in the hope that it will be useful,", file=f)
 
76
    print("* but WITHOUT ANY WARRANTY; without even the implied warranty of", file=f)
 
77
    print("* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the", file=f)
 
78
    print("* GNU Lesser General Public License for more details.", file=f)
 
79
    print("*", file=f)
 
80
    print("* You should have received a copy of the GNU Lesser General Public License", file=f)
 
81
    print("* along with this program.  If not, see <http://www.gnu.org/licenses/>.", file=f)
 
82
    print("*", file=f)
 
83
    print("* Authored by: This file is autogenerated by the parse-settings.py tool.", file=f)
 
84
    print("*/", file=f)
 
85
 
 
86
def write_header(f):
 
87
    write_copyright(f)
 
88
    print("#pragma once\n", file=f)
 
89
    print("namespace", file=f)
 
90
    print("{", file=f)
 
91
 
 
92
def write_footer(f):
 
93
    print("}", file=f)
 
94
 
 
95
def main():
 
96
    input, output = parse_args()
 
97
 
 
98
    if not os.path.exists(input):
 
99
        print("ERROR, input file %s does not exist\n" % input)
 
100
        usage()
 
101
        sys.exit()
 
102
 
 
103
    tree = ET.parse(input)
 
104
    root = tree.getroot()
 
105
    try:
 
106
        file_output = open(output, "w")
 
107
        write_header(file_output)
 
108
        for key in root.iter('key'):
 
109
            for default in key.iter('default'):
 
110
                try:
 
111
                    options[key.attrib.get('type')](key.attrib.get('name'), default.text, file_output)
 
112
                except KeyError:
 
113
                    process_default()
 
114
        write_footer(file_output)
 
115
    except IOError as e:
 
116
        print("I/O error({0}): {1}".format(e.errno, e.strerror))
 
117
    except:
 
118
        print("Unexpected error:", sys.exc_info()[0])
 
119
    finally:
 
120
        file_output.close()
 
121
 
 
122
if __name__ == '__main__':
 
123
    main()