/dlspec.py

http://gogl.googlecode.com/ · Python · 79 lines · 39 code · 13 blank · 27 comment · 10 complexity · 7f8d991c932c3f5f0d456bd83a981c27 MD5 · raw file

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