configgen.py 4.5 KB
Newer Older
1 2
# python script to generate configoptions.cpp from config.xml
#
Dimitri van Heesch's avatar
Dimitri van Heesch committed
3
# Copyright (C) 1997-2013 by Dimitri van Heesch.
4 5 6 7 8 9 10 11 12 13 14
#
# Permission to use, copy, modify, and distribute this software and its
# documentation under the terms of the GNU General Public License is hereby 
# granted. No representations are made about the suitability of this software 
# for any purpose. It is provided "as is" without express or implied warranty.
# See the GNU General Public License for more details.
#
# Documents produced by Doxygen are derivative works derived from the
# input used in their production; they are not affected by this license.
#
import xml.dom.minidom
15
import sys
16 17 18
from xml.dom import minidom, Node

def addValues(var,node):
Philipp Möller's avatar
Philipp Möller committed
19
	for n in node.childNodes:
20 21
		if n.nodeType == Node.ELEMENT_NODE:
			name = n.getAttribute('name');
Philipp Möller's avatar
Philipp Möller committed
22
			print "	 %s->addValue(\"%s\");" % (var,name)
23 24
	
def parseOption(node):
Philipp Möller's avatar
Philipp Möller committed
25 26 27 28 29
	name	= node.getAttribute('id')
	type	= node.getAttribute('type')
	format	= node.getAttribute('format')
	doc	= node.getAttribute('docs')
	defval	= node.getAttribute('defval')
30 31
	adefval = node.getAttribute('altdefval')
	depends = node.getAttribute('depends')
32
	setting = node.getAttribute('setting')
33
	# replace \ by \\, replace " by \", and '  ' by a newline with end string and start string at next line
34
	docC = doc.strip().replace('\\','\\\\').replace('"','\\"').replace('  ','\\n"\n\t\t\t"')
35
	if len(setting)>0:
Philipp Möller's avatar
Philipp Möller committed
36 37 38 39
		print "#if %s" % (setting)
	print "	 //----"
	if type=='bool':
		if len(adefval)>0:
40
			enabled = adefval
41 42
		elif defval=='1':
			enabled = "TRUE"
43
		else:
44
			enabled = "FALSE"
Philipp Möller's avatar
Philipp Möller committed
45 46 47 48 49
		print "	 cb = cfg->addBool("
		print "			\"%s\"," % (name)
		print "			\"%s\"," % (docC)
		print "			%s"  % (enabled)
		print "		       );"
50
		if depends!='':
Philipp Möller's avatar
Philipp Möller committed
51
			print "	 cb->addDependency(\"%s\");" % (depends)
52
	elif type=='string':
Philipp Möller's avatar
Philipp Möller committed
53 54 55 56
		print "	 cs = cfg->addString("
		print "			\"%s\"," % (name)
		print "			\"%s\""	 % (docC)
		print "		       );"
57
		if defval!='':
Philipp Möller's avatar
Philipp Möller committed
58
			print "	 cs->setDefaultValue(\"%s\");" % (defval)
59
		if format=='file':
Philipp Möller's avatar
Philipp Möller committed
60
			print "	 cs->setWidgetType(ConfigString::File);"
61
		elif format=='dir':
Philipp Möller's avatar
Philipp Möller committed
62
			print "	 cs->setWidgetType(ConfigString::Dir);"
63
		if depends!='':
Philipp Möller's avatar
Philipp Möller committed
64
			print "	 cs->addDependency(\"%s\");" % (depends)
65
	elif type=='enum':
Philipp Möller's avatar
Philipp Möller committed
66 67 68 69 70 71
		print "	 ce = cfg->addEnum("
		print "			\"%s\"," % (name)
		print "			\"%s\"," % (docC)
		print "			\"%s\""	 % (defval)
		print "		       );"
		addValues("ce",node)
72
		if depends!='':
Philipp Möller's avatar
Philipp Möller committed
73
			print "	 ce->addDependency(\"%s\");" % (depends)
74 75 76
	elif type=='int':
		minval = node.getAttribute('minval')
		maxval = node.getAttribute('maxval')
Philipp Möller's avatar
Philipp Möller committed
77 78 79 80 81
		print "	 ci = cfg->addInt("
		print "			\"%s\"," % (name)
		print "			\"%s\"," % (docC)
		print "			%s,%s,%s" % (minval,maxval,defval)
		print "		       );"
82
		if depends!='':
Philipp Möller's avatar
Philipp Möller committed
83
			print "	 ci->addDependency(\"%s\");" % (depends)
84
	elif type=='list':
Philipp Möller's avatar
Philipp Möller committed
85 86 87 88
		print "	 cl = cfg->addList("
		print "			\"%s\"," % (name)
		print "			\"%s\""	 % (docC)
		print "		       );"
89 90
		addValues("cl",node)
		if depends!='':
Philipp Möller's avatar
Philipp Möller committed
91
			print "	 cl->addDependency(\"%s\");" % (depends)
92
		if format=='file':
Philipp Möller's avatar
Philipp Möller committed
93
			print "	 cl->setWidgetType(ConfigList::File);"
94
		elif format=='dir':
Philipp Möller's avatar
Philipp Möller committed
95
			print "	 cl->setWidgetType(ConfigList::Dir);"
96
		elif format=='filedir':
Philipp Möller's avatar
Philipp Möller committed
97
			print "	 cl->setWidgetType(ConfigList::FileAndDir);"
98
	elif type=='obsolete':
Philipp Möller's avatar
Philipp Möller committed
99
		print "	 cfg->addObsolete(\"%s\");" % (name)
100
	if len(setting)>0:
Philipp Möller's avatar
Philipp Möller committed
101 102 103
		print "#else"
		print "	 cfg->addDisabled(\"%s\");" % (name)
		print "#endif"
104 105 106 107 108 109 110
		



def parseGroups(node):
	name = node.getAttribute('name')
	doc  = node.getAttribute('docs')
Philipp Möller's avatar
Philipp Möller committed
111 112 113 114 115
	print "	 //---------------------------------------------------------------------------";
	print "	 cfg->addInfo(\"%s\",\"%s\");" % (name,doc)
	print "	 //---------------------------------------------------------------------------";
	print
	for n in node.childNodes:
116 117 118 119 120
		if n.nodeType == Node.ELEMENT_NODE:
			parseOption(n)
	

def main():
121
	doc = xml.dom.minidom.parse(sys.argv[1])
122
	elem = doc.documentElement
Philipp Möller's avatar
Philipp Möller committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	print "/* WARNING: This file is generated!"
	print " * Do not edit this file, but edit config.xml instead and run"
	print " * python configgen.py to regenerate this file!"
	print " */"
	print ""
	print "#include \"configoptions.h\""
	print "#include \"config.h\""
	print "#include \"portable.h\""
	print "#include \"settings.h\""
	print ""
	print "void addConfigOptions(Config *cfg)"
	print "{"
	print "	 ConfigString *cs;"
	print "	 ConfigEnum   *ce;"
	print "	 ConfigList   *cl;"
	print "	 ConfigInt    *ci;"
	print "	 ConfigBool   *cb;"
	print ""
141 142 143
	for n in elem.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			parseGroups(n)
Philipp Möller's avatar
Philipp Möller committed
144
	print "}"
145 146 147 148

if __name__ == '__main__':
	main()