3
# Copyright (C) 2015 Canonical Ltd.
4
# Author: Xavi Garcia <xavi.garcia.mena@canonical.com>
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.
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.
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/>.
19
import xml.etree.ElementTree as ET
20
import getopt, sys, os
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)
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)
30
def process_default(name):
31
print("WARNING: unsupported type found for parameter %s\n" % name)
33
options = {'i' : integer_parameter,
34
's' : string_parameter,
38
print("usage: parse-settings -i INPUT-SCHEMA-FILE -o OUTPUT-FILE")
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"
59
assert False, "unhandled option"
61
if (input == None or output==None):
65
return (input, output)
67
def write_copyright(f):
69
print("* Copyright (C) 2015 Canonical Ltd.", 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)
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)
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)
83
print("* Authored by: This file is autogenerated by the parse-settings.py tool.", file=f)
88
print("#pragma once\n", file=f)
89
print("namespace", file=f)
96
input, output = parse_args()
98
if not os.path.exists(input):
99
print("ERROR, input file %s does not exist\n" % input)
103
tree = ET.parse(input)
104
root = tree.getroot()
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'):
111
options[key.attrib.get('type')](key.attrib.get('name'), default.text, file_output)
114
write_footer(file_output)
116
print("I/O error({0}): {1}".format(e.errno, e.strerror))
118
print("Unexpected error:", sys.exc_info()[0])
122
if __name__ == '__main__':