More compact format for wiki output at
[coreboot.git] / util / optionlist / mkOptionList.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2005 Florian Zeitz <florian-zeitz@lycos.de>
4 # Copyright (C) 2005 Stefan Reinauer <stepan@coresystems.de>
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; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19
20
21 def xmlString(string):
22         for i in range(len(string)-1):
23                 if string[i] == "&":
24                         string = string[:i] + "&amp;" + string[i+1:]
25                 if string[i] == "<":
26                         string = string[:i] + "&lt;" + string[i+1:]
27                 if string[i] == ">":
28                         string = string[:i] + "&gt;" + string[i+1:]
29         return string
30
31 def openInfile(filename):
32         "getting the input from the inputfile (e.g. Options.lb)"
33         infile = open(filename, "r")
34         infile.seek(0)
35         input = infile.readlines()
36         infile.close()
37         return input
38
39 def prepInput(input):
40         "preparing the input for parsing (not really neccessary, but makes things simpler and doesnt take too long)"
41         i = -1
42         while True:
43                 i += 1
44                 if i >= len(input): break
45                 if input[i] == ("" or "\n"):
46                         input.pop(i)
47                 if input[i][0:1] == "\t":
48                         input[i] = input[i][1:]
49                         i = -1
50         return input
51
52 def parseInput(input):
53         "parse the output"
54         output = ""
55         for line in input:
56                 line = xmlString(line)
57                 if line[:6] == "define":
58                         output = output + '<option name="' + line[7:-1] + '">' + "\n"
59                 elif line[:3] == "end":
60                         output = output + '</option>' + "\n\n"
61                 elif line[:7] == "default":
62                         output = output + '<default>' + line[8:-1] + '</default>' + "\n"
63                 elif line[:6] == "format":
64                         output = output + '<format>' + line[7:-1] + '</format>' + "\n"
65                 elif line[:6] == "export":
66                         output = output + '<export>' + line[7:-1] + '</export>' + "\n"
67                 elif line[:7] == "comment":
68                         output = output + '<comment>' + line[8:-1] + '</comment>' + "\n"
69         
70         return output
71
72 def parseArgv():
73         "parse the given arguments"
74         import sys
75
76         In = Out = False
77
78         if len(sys.argv) >= 2:
79                 if sys.argv[1] == ("-h" or "--help"):
80                         print "Syntax: mkOptionList.py [infile] [outfile]"
81                 else:
82                         In = True
83                         inFilename = sys.argv[1]
84         if len(sys.argv) >= 3:
85                 if sys.argv[2] == ("-h" or "--help"):
86                         print "Syntax: mkOptionList.py [infile] [outfile]"
87                 else:
88                         Out = True
89                         outFilename = sys.argv[2]
90
91         if In and not Out:
92                 return inFilename
93         elif In and Out:
94                 return inFilename, outFilename
95         
96
97 def main():
98         import time
99         if not parseArgv():
100                 inFilename = "../../src/config/Options.lb"
101                 outFilename = "Options.xml"
102         else:
103                 inFilename, outFilename = parseArgv()
104         
105         input = openInfile(inFilename)
106         input = prepInput(input)
107         output = parseInput(input)
108         
109         print "mkOptionList.py: coreboot option list generator"
110         print " input file : ", inFilename
111         print " output file: ", outFilename
112         
113         #opening the output file
114         outfile = open(outFilename, "w", 0)
115
116         #write the beginning of the XML to the output file
117         outfile.write('<?xml version="1.0"?>')
118         outfile.write("\n")
119         outfile.write('<?xml-stylesheet type="text/xsl" href="Options.xsl"?>')
120         outfile.write("\n")
121         outfile.write('<options>')
122         outfile.write("\n")
123         outfile.write('<creationdate>')
124         outfile.write(time.strftime('%Y/%m/%d %H:%M:%S'))
125         outfile.write('</creationdate>')
126         outfile.write("\n")
127
128         
129         #write the parsed file to the output file
130         outfile.write(output)
131         
132         #write closing tags to the output file and close it
133         outfile.write('</options>')
134         outfile.write("\n")
135         outfile.flush()
136         outfile.close()
137
138         print "Done!"
139
140 if __name__ == "__main__":
141         main()