summaryrefslogtreecommitdiffstats
path: root/src/configgen.py
blob: 40239304105f176cc76374ab4e4daa4df95751ae (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
#!/usr/bin/python
# python script to generate configoptions.cpp and config.doc from config.xml
#
# Copyright (C) 1997-2014 by Dimitri van Heesch.
#
# 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
import sys
import re
import textwrap
from xml.dom import minidom, Node


def transformDocs(doc):
	# join lines, unless it is an empty line
	# remove doxygen layout constructs
	doc = doc.strip()
	doc = doc.replace("\n", " ")
	doc = doc.replace("\r", " ")
	doc = doc.replace("\t", " ")
	doc = doc.replace("\\&", "&")
	doc = doc.replace("\\c ", " ")
	doc = doc.replace("\\b ", " ")
	doc = doc.replace("\\e ", " ")
	doc = doc.replace("\\$", "$")
	doc = doc.replace("\\#include ", "#include ")
	doc = doc.replace("\\#undef ", "#undef ")
	doc = doc.replace("-# ", "\n - ")
	doc = doc.replace(" - ", "\n - ")
	doc = doc.replace("\\sa", "\nSee also: ")
	doc = doc.replace("\\par", "\n")
	doc = doc.replace("@note", "\nNote:")
	doc = doc.replace("\\note", "\nNote:")
	doc = doc.replace("\\verbatim", "\n")
	doc = doc.replace("\\endverbatim", "\n")
	doc = doc.replace("<code>", "")
	doc = doc.replace("</code>", "")
	doc = doc.replace("`", "")
	doc = doc.replace("\\<", "<")
	doc = doc.replace("\\>", ">")
	doc = doc.replace("\\@", "@")
	doc = doc.replace("\\\\", "\\")
	# \ref name "description" -> description
	doc = re.sub('\\\\ref +[^ ]* +"([^"]*)"', '\\1', doc)
	# \ref specials
	# \ref <key> -> description
	doc = re.sub('\\\\ref +doxygen_usage', '"Doxygen usage"', doc)
	doc = re.sub('\\\\ref +extsearch', '"External Indexing and Searching"',
				 doc)
	doc = re.sub('\\\\ref +external', '"Linking to external documentation"',
				 doc)
	# fallback for not handled
	doc = re.sub('\\\\ref', '', doc)
	#<a href="address">description</a> -> description (see: address)
	doc = re.sub('<a +href="([^"]*)" *>([^<]*)</a>', '\\2 (see: \\1)', doc)
	# LaTeX name as formula -> LaTeX
	doc = doc.replace("\\f$\\mbox{\\LaTeX}\\f$", "LaTeX")
	# Other forula's (now just 2) so explicitely mentioned.
	doc = doc.replace("\\f$2^{(16+\\mbox{LOOKUP\\_CACHE\\_SIZE})}\\f$",
					  "2^(16+LOOKUP_CACHE_SIZE)")
	doc = doc.replace("\\f$2^{16} = 65536\\f$", "2^16=65536")
	# remove consecutive spaces
	doc = re.sub(" +", " ", doc)
	# a dirty trick to get an extra empty line in Doxyfile documentation.
	# <br> will be removed later on again, we need it here otherwise splitlines
	# will filter the extra line.
	doc = doc.replace("<br>", "\n<br>\n")
	# a dirty trick to go to the next line in Doxyfile documentation.
	# <br/> will be removed later on again, we need it here otherwise splitlines
	# will filter the line break.
	doc = doc.replace("<br/>", "\n<br/>\n")
	#
	doc = doc.splitlines()
	split_doc = []
	for line in doc:
		split_doc += textwrap.wrap(line, 78)
	# replace \ by \\, replace " by \", and '  ' by a newline with end string
	# and start string at next line
	docC = []
	for line in split_doc:
		if (line.strip() != "<br/>"):
			docC.append(line.strip().replace('\\', '\\\\').
					replace('"', '\\"').replace("<br>", ""))
	return docC


def collectValues(node):
	values = []
	for n in node.childNodes:
		if (n.nodeName == "value"):
			if n.nodeType == Node.ELEMENT_NODE:
				if n.getAttribute('name') != "":
					if n.getAttribute('show_docu') != "NO":
						name = "<code>" + n.getAttribute('name') + "</code>"
						desc = n.getAttribute('desc')
						if (desc != ""):
							name += " " + desc
						values.append(name)
	return values


def addValues(var, node):
	for n in node.childNodes:
		if (n.nodeName == "value"):
			if n.nodeType == Node.ELEMENT_NODE:
				name = n.getAttribute('name')
				print "  %s->addValue(\"%s\");" % (var, name)


def parseHeader(node,objName):
	doc = ""
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			if (n.nodeName == "docs"):
				if (n.getAttribute('doxyfile') != "0"):
					doc += parseDocs(n)
	docC = transformDocs(doc)
	print "  %s->setHeader(" % (objName)
	rng = len(docC)
	for i in range(rng):
		line = docC[i]
		if i != rng - 1:  # since we go from 0 to rng-1
			print "              \"%s\\n\"" % (line)
		else:
			print "              \"%s\"" % (line)
	print "             );"


def prepCDocs(node):
	type = node.getAttribute('type')
	format = node.getAttribute('format')
	defval = node.getAttribute('defval')
	adefval = node.getAttribute('altdefval')
	doc = "";
	if (type != 'obsolete'):
		for n in node.childNodes:
			if (n.nodeName == "docs"):
				if (n.getAttribute('doxyfile') != "0"):
					if n.nodeType == Node.ELEMENT_NODE:
						doc += parseDocs(n)
		if (type == 'enum'):
			values = collectValues(node)
			doc += "<br/>Possible values are: "
			rng = len(values)
			for i in range(rng):
				val = values[i]
				if i == rng - 2:
					doc += "%s and " % (val)
				elif i == rng - 1:
					doc += "%s." % (val)
				else:
					doc += "%s, " % (val)
			if (defval != ""):
				doc += "<br/>The default value is: <code>%s</code>." % (defval)
		elif (type == 'int'):
			minval = node.getAttribute('minval')
			maxval = node.getAttribute('maxval')
			doc += "<br/>%s: %s, %s: %s, %s: %s." % (" Minimum value", minval, 
					 "maximum value", maxval,
					 "default value", defval)
		elif (type == 'bool'):
			if (node.hasAttribute('altdefval')):
			  doc += "<br/>%s: %s." % ("The default value is", "system dependent")
			else:
			  doc += "<br/>%s: %s." % ("The default value is", "YES" if (defval == "1") else "NO")
		elif (type == 'list'):
			if format == 'string':
				values = collectValues(node)
				rng = len(values)
				for i in range(rng):
					val = values[i]
					if i == rng - 2:
						doc += "%s and " % (val)
					elif i == rng - 1:
						doc += "%s." % (val)
					else:
						doc += "%s, " % (val)
		elif (type == 'string'):
			if format == 'dir':
				if defval != '':
					doc += "<br/>The default directory is: <code>%s</code>." % (
						defval)
			elif format == 'file':
				abspath = node.getAttribute('abspath')
				if defval != '':
					if abspath != '1':
						doc += "<br/>The default file is: <code>%s</code>." % (
							defval)
					else:
						doc += "<br/>%s: %s%s%s." % (
							"The default file (with absolute path) is",
							"<code>",defval,"</code>")
				else:
					if abspath == '1':
						doc += "<br/>The file has to be specified with full path."
			else: # format == 'string':
				if defval != '':
					doc += "<br/>The default value is: <code>%s</code>." % (
						defval)
		# depends handling
		if (node.hasAttribute('depends')):
			depends = node.getAttribute('depends')
			doc += "<br/>%s \\ref cfg_%s \"%s\" is set to \\c YES." % (
				"This tag requires that the tag", depends.lower(), depends.upper())

	docC = transformDocs(doc)
	return docC;

def parseOption(node):
	# Handling part for Doxyfile
	name = node.getAttribute('id')
	type = node.getAttribute('type')
	format = node.getAttribute('format')
	defval = node.getAttribute('defval')
	adefval = node.getAttribute('altdefval')
	depends = node.getAttribute('depends')
	setting = node.getAttribute('setting')
	docC = prepCDocs(node);
	if len(setting) > 0:
		print "#if %s" % (setting)
	print "  //----"
	if type == 'bool':
		if len(adefval) > 0:
			enabled = adefval
		elif defval == '1':
			enabled = "TRUE"
		else:
			enabled = "FALSE"
		print "  cb = cfg->addBool("
		print "             \"%s\"," % (name)
		rng = len(docC)
		for i in range(rng):
			line = docC[i]
			if i != rng - 1:  # since we go from 0 to rng-1
				print "              \"%s\\n\"" % (line)
			else:
				print "              \"%s\"," % (line)
		print "              %s" % (enabled)
		print "             );"
		if depends != '':
			print "  cb->addDependency(\"%s\");" % (depends)
	elif type == 'string':
		print "  cs = cfg->addString("
		print "              \"%s\"," % (name)
		rng = len(docC)
		for i in range(rng):
			line = docC[i]
			if i != rng - 1:  # since we go from 0 to rng-1
				print "              \"%s\\n\"" % (line)
			else:
				print "              \"%s\"" % (line)
		print "             );"
		if defval != '':
			print "  cs->setDefaultValue(\"%s\");" % (defval)
		if format == 'file':
			print "  cs->setWidgetType(ConfigString::File);"
		elif format == 'dir':
			print "  cs->setWidgetType(ConfigString::Dir);"
		if depends != '':
			print "  cs->addDependency(\"%s\");" % (depends)
	elif type == 'enum':
		print "  ce = cfg->addEnum("
		print "              \"%s\"," % (name)
		rng = len(docC)
		for i in range(rng):
			line = docC[i]
			if i != rng - 1:  # since we go from 0 to rng-1
				print "              \"%s\\n\"" % (line)
			else:
				print "              \"%s\"," % (line)
		print "              \"%s\"" % (defval)
		print "             );"
		addValues("ce", node)
		if depends != '':
			print "  ce->addDependency(\"%s\");" % (depends)
	elif type == 'int':
		minval = node.getAttribute('minval')
		maxval = node.getAttribute('maxval')
		print "  ci = cfg->addInt("
		print "              \"%s\"," % (name)
		rng = len(docC)
		for i in range(rng):
			line = docC[i]
			if i != rng - 1:  # since we go from 0 to rng-1
				print "              \"%s\\n\"" % (line)
			else:
				print "              \"%s\"," % (line)
		print "              %s,%s,%s" % (minval, maxval, defval)
		print "             );"
		if depends != '':
			print "  ci->addDependency(\"%s\");" % (depends)
	elif type == 'list':
		print "  cl = cfg->addList("
		print "              \"%s\"," % (name)
		rng = len(docC)
		for i in range(rng):
			line = docC[i]
			if i != rng - 1:  # since we go from 0 to rng-1
				print "              \"%s\\n\"" % (line)
			else:
				print "              \"%s\"" % (line)
		print "             );"
		addValues("cl", node)
		if depends != '':
			print "  cl->addDependency(\"%s\");" % (depends)
		if format == 'file':
			print "  cl->setWidgetType(ConfigList::File);"
		elif format == 'dir':
			print "  cl->setWidgetType(ConfigList::Dir);"
		elif format == 'filedir':
			print "  cl->setWidgetType(ConfigList::FileAndDir);"
	elif type == 'obsolete':
		print "  cfg->addObsolete(\"%s\");" % (name)
	if len(setting) > 0:
		print "#else"
		print "  cfg->addDisabled(\"%s\");" % (name)
		print "#endif"


def parseGroups(node):
	name = node.getAttribute('name')
	doc = node.getAttribute('docs')
	print "%s%s" % ("  //-----------------------------------------",
					"----------------------------------")
	print "  cfg->addInfo(\"%s\",\"%s\");" % (name, doc)
	print "%s%s" % ("  //-----------------------------------------",
					"----------------------------------")
	print
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			parseOption(n)

def parseGroupCDocs(node):
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			type = n.getAttribute('type')
			name = n.getAttribute('id')
			docC = prepCDocs(n);
			if type != 'obsolete':
				print "  doc->add("
				print "              \"%s\"," % (name)
				rng = len(docC)
				for i in range(rng):
					line = docC[i]
					if i != rng - 1:  # since we go from 0 to rng-1
						print "              \"%s\\n\"" % (line)
					else:
						print "              \"%s\"" % (line)
				print "          );"

def parseOptionDoc(node, first):
	# Handling part for documentation
	name = node.getAttribute('id')
	type = node.getAttribute('type')
	format = node.getAttribute('format')
	defval = node.getAttribute('defval')
	adefval = node.getAttribute('altdefval')
	depends = node.getAttribute('depends')
	setting = node.getAttribute('setting')
	doc = ""
	if (type != 'obsolete'):
		for n in node.childNodes:
			if (n.nodeName == "docs"):
				if (n.getAttribute('documentation') != "0"):
					if n.nodeType == Node.ELEMENT_NODE:
						doc += parseDocs(n)
		if (first):
			print " \\anchor cfg_%s" % (name.lower())
			print "<dl>"
			print ""
			print "<dt>\\c %s <dd>" % (name)
		else:
			print " \\anchor cfg_%s" % (name.lower())
			print "<dt>\\c %s <dd>" % (name)
		print " \\addindex %s" % (name)
		print doc
		if (type == 'enum'):
			values = collectValues(node)
			print ""
			print "Possible values are: "
			rng = len(values)
			for i in range(rng):
				val = values[i]
				if i == rng - 2:
					print "%s and " % (val)
				elif i == rng - 1:
					print "%s." % (val)
				else:
					print "%s, " % (val)
			if (defval != ""):
				print ""
				print ""
				print "The default value is: <code>%s</code>." % (defval)
			print ""
		elif (type == 'int'):
			minval = node.getAttribute('minval')
			maxval = node.getAttribute('maxval')
			print ""
			print ""
			print "%s: %s%s%s, %s: %s%s%s, %s: %s%s%s." % (
					 " Minimum value", "<code>", minval, "</code>", 
					 "maximum value", "<code>", maxval, "</code>",
					 "default value", "<code>", defval, "</code>")
			print ""
		elif (type == 'bool'):
			print ""
			print ""
			if (node.hasAttribute('altdefval')):
				print "The default value is: system dependent."
			else:
				print "The default value is: <code>%s</code>." % (
					"YES" if (defval == "1") else "NO")
			print ""
		elif (type == 'list'):
			if format == 'string':
				values = collectValues(node)
				rng = len(values)
				for i in range(rng):
					val = values[i]
					if i == rng - 2:
						print "%s and " % (val)
					elif i == rng - 1:
						print "%s." % (val)
					else:
						print "%s, " % (val)
			print ""
		elif (type == 'string'):
			if format == 'dir':
				if defval != '':
					print ""
					print "The default directory is: <code>%s</code>." % (
						defval)
			elif format == 'file':
				abspath = node.getAttribute('abspath')
				if defval != '':
					print ""
					if abspath != '1':
						print "The default file is: <code>%s</code>." % (
							defval)
					else:
						print "%s: %s%s%s." % (
							"The default file (with absolute path) is",
							"<code>",defval,"</code>")
				else:
					if abspath == '1':
						print ""
						print "The file has to be specified with full path."
			else: # format == 'string':
				if defval != '':
					print ""
					print "The default value is: <code>%s</code>." % (
						defval)
			print ""
		# depends handling
		if (node.hasAttribute('depends')):
			depends = node.getAttribute('depends')
			print ""
			print "%s \\ref cfg_%s \"%s\" is set to \\c YES." % (
				"This tag requires that the tag", depends.lower(), depends.upper())
		return False


def parseGroupsDoc(node):
	name = node.getAttribute('name')
	doc = node.getAttribute('docs')
	print "\section config_%s %s" % (name.lower(), doc)
	# Start of list has been moved to the first option for better
	# anchor placement
	#  print "<dl>"
	#  print ""
	first = True
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			first = parseOptionDoc(n, first)
	if (not first):
		print "</dl>"


def parseGroupsList(node, commandsList):
	list = ()
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			type = n.getAttribute('type')
			if type != 'obsolete':
				commandsList = commandsList + (n.getAttribute('id'),)
	return commandsList


def parseDocs(node):
	doc = ""
	for n in node.childNodes:
		if n.nodeType == Node.TEXT_NODE:
			doc += n.nodeValue.strip()
		if n.nodeType == Node.CDATA_SECTION_NODE:
			doc += n.nodeValue.rstrip("\r\n ").lstrip("\r\n")
	#doc += "<br>"
	return doc


def parseHeaderDoc(node):
	doc = ""
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			if (n.nodeName == "docs"):
				if (n.getAttribute('documentation') != "0"):
					doc += parseDocs(n)
	print doc


def parseFooterDoc(node):
	doc = ""
	for n in node.childNodes:
		if n.nodeType == Node.ELEMENT_NODE:
			if (n.nodeName == "docs"):
				if (n.getAttribute('documentation') != "0"):
					doc += parseDocs(n)
	print doc


def main():
	if len(sys.argv)<3 or (not sys.argv[1] in ['-doc','-cpp','-wiz']):
		sys.exit('Usage: %s -doc|-cpp|-wiz config.xml' % sys.argv[0])
	try:
		doc = xml.dom.minidom.parse(sys.argv[2])
	except Exception as inst:
		print >> sys.stderr
		print >> sys.stderr, inst
		print >> sys.stderr
		sys.exit(1)
	elem = doc.documentElement
	if (sys.argv[1] == "-doc"):
		print "/* WARNING: This file is generated!"
		print " * Do not edit this file, but edit config.xml instead and run"
		print " * python configgen.py -doc config.xml to regenerate this file!"
		print " */"
		# process header
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "header"):
					parseHeaderDoc(n)
		# generate list with all commands
		commandsList = ()
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "group"):
					commandsList = parseGroupsList(n, commandsList)
		print "\\secreflist"
		for x in sorted(commandsList):
			print "\\refitem cfg_%s %s" % (x.lower(), x)
		print "\\endsecreflist"
		# process groups and options
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "group"):
					parseGroupsDoc(n)
		# process footers
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "footer"):
					parseFooterDoc(n)
	elif (sys.argv[1] == "-cpp"):
		print "/* WARNING: This file is generated!"
		print " * Do not edit this file, but edit config.xml instead and run"
		print " * python configgen.py -cpp config.xml 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 ""
		# process header
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "header"):
					parseHeader(n,'cfg')
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "group"):
					parseGroups(n)
		print "}"
	elif (sys.argv[1] == "-wiz"):
		print "/* WARNING: This file is generated!"
		print " * Do not edit this file, but edit config.xml instead and run"
		print " * python configgen.py -wiz config.xml to regenerate this file!"
		print " */"
		print "#include \"configdoc.h\""
		print "#include \"docintf.h\""
		print ""
		print "void addConfigDocs(DocIntf *doc)"
		print "{"
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "header"):
					parseHeader(n,'doc')
		for n in elem.childNodes:
			if n.nodeType == Node.ELEMENT_NODE:
				if (n.nodeName == "group"):
					parseGroupCDocs(n)
		print "}"

if __name__ == '__main__':
	main()