/dlspec.py
http://gogl.googlecode.com/ · Python · 79 lines · 39 code · 13 blank · 27 comment · 10 complexity · 7f8d991c932c3f5f0d456bd83a981c27 MD5 · raw file
- #/usr/bin/env python
- #Copyright (c) 2009 Richard Ward
- #
- #Permission is hereby granted, free of charge, to any person obtaining a copy
- #of this software and associated documentation files (the "Software"), to deal
- #in the Software without restriction, including without limitation the rights
- #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- #copies of the Software, and to permit persons to whom the Software is
- #furnished to do so, subject to the following conditions:
- #
- #The above copyright notice and this permission notice shall be included in
- #all copies or substantial portions of the Software.
- #
- #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- #THE SOFTWARE.
- #This script downloads the latest opengl spec files from kronos, processes
- #them and generates Go files.
- import urllib,re,sys
- def usage():
- sys.stderr.write("Usage: dlspec.py packagename [/path/to/desired/directory]\n")
- sys.exit(1)
- if len(sys.argv)<2 or len(sys.argv)>3:
- usage()
- PACKAGENAME=sys.argv[1]
- PATHNAME="."
- if len(sys.argv)==3:
- PATHNAME=sys.argv[2]
- if not os.path.exists(PATHNAME) or not os.path.isdir(PATHNAME):
- usage()
- PATHNAME=PATHNAME+"/"
- def getSpecFile(name,ext="spec"):
- url=urllib.urlopen("http://www.opengl.org/registry/api/"+name+"."+ext)
- if url.getcode() is not 200:
- raise IOError("Could not download specifications")
- return url.readlines()
- #how the regex works:
- #you must read it in parralell with the spec.
- #The enums are on lines composed as follows:
- #whitespace IDENTIFIERwhich_may_have_underscores_lowercase_and_digits whitespace = whitespace NUMBER
- #the number may be a hex number. It may even be a previous constant in some cases.
- parserRegex=re.compile("[ \t]+([A-Z0-9_]+)+[\t ]*=[\t ]*([0-9A-Z_a-gx]+)")
- def parseEnumFile(name):
- lines=getSpecFile(name)
- f=file(PATHNAME+name+".go","w")
- f.write('package '+PACKAGENAME+'\n\n')
- f.write('//THIS FILE IS AUTOMATICALLY GENERATED BY dlspec.py.\n')
- f.write('//DO NOT ALTER THIS FILE MANUALLY!\n\n')
- f.write('const (\n')
- enums={}
- lineno=0;
- for line in lines:
- lineno=lineno+1
- match=parserRegex.match(line)
- if match is not None:
- enum=match.expand("\\1")
- if not enums.has_key(enum):
- f.write(match.expand("\tGL_\\1 = \\2;\n"))
- enums[enum]=None
- f.write(')\n')
- f.close();
- parseEnumFile("enum")