summaryrefslogtreecommitdiff
path: root/util/optionlist/mkOptionList.py
blob: c91f11aa22700f13cf88339a887691ab8eb4f9fc (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
#!/usr/bin/python

def xmlString(string):
	for i in range(len(string)-1):
		if string[i] == "&":
			string = string[:i] + "&" + string[i+1:]
		if string[i] == "<":
			string = string[:i] + "&lt;" + string[i+1:]
		if string[i] == ">":
			string = string[:i] + "&gt;" + string[i+1:]
	return string

def openInfile(filename):
	"getting the input from the inputfile (e.g. Options.lb)"
	infile = open(filename, "r")
	infile.seek(0)
	input = infile.readlines()
	infile.close()
	return input

def prepInput(input):
	"preparing the input for parsing (not really neccessary, but makes things simpler and doesnt take too long)"
	i = -1
	while True:
		i += 1
		if i >= len(input): break
		if input[i] == ("" or "\n"):
			input.pop(i)
		if input[i][0:1] == "\t":
			input[i] = input[i][1:]
			i = -1
	return input

def parseInput(input):
	"parse the output"
	output = ""
	for line in input:
		line = xmlString(line)
		if line[:6] == "define":
			output = output + '<option name="' + line[7:-1] + '">' + "\n"
		elif line[:3] == "end":
			output = output + '</option>' + "\n\n"
		elif line[:7] == "default":
			output = output + '<default>' + line[8:-1] + '</default>' + "\n"
		elif line[:6] == "format":
			output = output + '<format>' + line[7:-1] + '</format>' + "\n"
		elif line[:6] == "export":
			output = output + '<export>' + line[7:-1] + '</export>' + "\n"
		elif line[:7] == "comment":
			output = output + '<comment>' + line[8:-1] + '</comment>' + "\n"
	
	return output

def parseArgv():
	"parse the given arguments"
	import sys

	In = Out = False

	if len(sys.argv) >= 2:
		if sys.argv[1] == ("-h" or "--help"):
			print "Syntax: mkOptionList.py [infile] [outfile]"
		else:
			In = True
			inFilename = sys.argv[1]
	if len(sys.argv) >= 3:
		if sys.argv[2] == ("-h" or "--help"):
			print "Syntax: mkOptionList.py [infile] [outfile]"
		else:
			Out = True
			outFilename = sys.argv[2]

	if In and not Out:
		return inFilename
	elif In and Out:
		return inFilename, outFilename
	

def main():
	import time
	if not parseArgv():
		inFilename = "../../src/config/Options.lb"
		outFilename = "Options.xml"
	else:
		inFilename, outFilename = parseArgv()
	
	input = openInfile(inFilename)
	input = prepInput(input)
	output = parseInput(input)
	
	print "mkOptionList.py: LinuxBIOS option list generator"
	print " input file : ", inFilename
	print " output file: ", outFilename
	
	#opening the output file
	outfile = open(outFilename, "w", 0)

	#write the beginning of the XML to the output file
	outfile.write('<?xml version="1.0"?>')
	outfile.write("\n")
	outfile.write('<?xml-stylesheet type="text/xsl" href="Options.xsl"?>')
	outfile.write("\n")
	outfile.write('<options>')
	outfile.write("\n")
	outfile.write('<creationdate>')
	outfile.write(time.strftime('%Y/%m/%d %H:%M:%S'))
	outfile.write('</creationdate>')
	outfile.write("\n")

	
	#write the parsed file to the output file
	outfile.write(output)
	
	#write closing tags to the output file and close it
	outfile.write('</options>')
	outfile.write("\n")
	outfile.flush()
	outfile.close()

	print "Done!"

if __name__ == "__main__":
	main()