PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 6ms RepoModel.GetById 35ms app.codeStats 0ms

/dlspec.py

http://gogl.googlecode.com/
Python | 79 lines | 39 code | 13 blank | 27 comment | 15 complexity | 7f8d991c932c3f5f0d456bd83a981c27 MD5 | raw file
 1#/usr/bin/env python
 2
 3#Copyright (c) 2009 Richard Ward
 4#
 5#Permission is hereby granted, free of charge, to any person obtaining a copy
 6#of this software and associated documentation files (the "Software"), to deal
 7#in the Software without restriction, including without limitation the rights
 8#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9#copies of the Software, and to permit persons to whom the Software is
10#furnished to do so, subject to the following conditions:
11#
12#The above copyright notice and this permission notice shall be included in
13#all copies or substantial portions of the Software.
14#
15#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21#THE SOFTWARE.
22
23#This script downloads the latest opengl spec files from kronos, processes
24#them and generates Go files.
25
26import urllib,re,sys
27
28def usage():
29	sys.stderr.write("Usage: dlspec.py packagename [/path/to/desired/directory]\n")
30	sys.exit(1)
31
32if len(sys.argv)<2 or len(sys.argv)>3:
33	usage()
34
35PACKAGENAME=sys.argv[1]
36
37PATHNAME="."
38if len(sys.argv)==3:
39	PATHNAME=sys.argv[2]
40	if not os.path.exists(PATHNAME) or not os.path.isdir(PATHNAME):
41		usage()
42PATHNAME=PATHNAME+"/"
43
44def getSpecFile(name,ext="spec"):
45	url=urllib.urlopen("http://www.opengl.org/registry/api/"+name+"."+ext)
46	if url.getcode() is not 200:
47		raise IOError("Could not download specifications")
48	return url.readlines()
49
50#how the regex works:
51#you must read it in parralell with the spec.
52#The enums are on lines composed as follows:
53#whitespace IDENTIFIERwhich_may_have_underscores_lowercase_and_digits whitespace = whitespace NUMBER
54#the number may be a hex number. It may even be a previous constant in some cases.
55parserRegex=re.compile("[ \t]+([A-Z0-9_]+)+[\t ]*=[\t ]*([0-9A-Z_a-gx]+)")
56def parseEnumFile(name):
57	lines=getSpecFile(name)
58	f=file(PATHNAME+name+".go","w")
59	f.write('package '+PACKAGENAME+'\n\n')
60	f.write('//THIS FILE IS AUTOMATICALLY GENERATED BY dlspec.py.\n')
61	f.write('//DO NOT ALTER THIS FILE MANUALLY!\n\n')
62	f.write('const (\n')
63	enums={}
64	lineno=0;
65	for line in lines:
66		lineno=lineno+1
67		match=parserRegex.match(line)
68		if match is not None:
69			enum=match.expand("\\1")
70			if not enums.has_key(enum):
71				f.write(match.expand("\tGL_\\1 = \\2;\n"))
72			enums[enum]=None
73	f.write(')\n')
74	f.close();
75
76
77
78parseEnumFile("enum")
79