You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
4.0KB

  1. #!/usr/bin/python
  2. import re
  3. import sys
  4. from xml.dom.minidom import parse, Node
  5. def findChildren(node, path):
  6. result = []
  7. if len(path)==1:
  8. for i in node.childNodes:
  9. if i.nodeType==Node.ELEMENT_NODE:
  10. if i.tagName==path[0]:
  11. result.append(i)
  12. else:
  13. for i in node.childNodes:
  14. if i.nodeType==Node.ELEMENT_NODE:
  15. if i.tagName==path[0]:
  16. result.extend(findChildren(i, path[1:]))
  17. return result
  18. def findData(node, path):
  19. return [ i.firstChild.data for i in findChildren(node, path) ]
  20. def findParams(node):
  21. n = findData(node, ['name'])[0]
  22. t = ''
  23. for i in node.childNodes:
  24. if i.nodeType==Node.TEXT_NODE:
  25. t += i.data
  26. if i.nodeType==Node.ELEMENT_NODE and i.tagName=='ptype':
  27. t += i.firstChild.data
  28. return ( t, n)
  29. def findEnums(dom):
  30. ret = {}
  31. for i in findChildren(dom, [ 'registry', 'enums', 'enum' ]):
  32. n = i.getAttribute('name')
  33. v = i.getAttribute('value')
  34. ret[n] = v
  35. return ret
  36. def findCommands(dom):
  37. ret = {}
  38. for i in findChildren(dom, [ 'registry', 'commands', 'command' ]):
  39. r,n = findParams(findChildren(i, ['proto'])[0])
  40. p = [ findParams(j) for j in findChildren(i, ['param'])]
  41. ret[n] = (r, p)
  42. return ret
  43. def findFeatures(dom):
  44. ret = {}
  45. for i in findChildren(dom, [ 'registry', 'feature' ]):
  46. n = i.getAttribute('name')
  47. e = []
  48. c = []
  49. for j in findChildren(i, [ 'require', 'enum' ]):
  50. e.append(j.getAttribute("name"))
  51. for j in findChildren(i, [ 'require', 'command' ]):
  52. c.append(j.getAttribute("name"))
  53. ret[n] = (e,c)
  54. return ret
  55. def findExtensions(dom):
  56. ret = {}
  57. for i in findChildren(dom, [ 'registry', 'extensions', 'extension' ]):
  58. n = i.getAttribute('name')
  59. e = []
  60. c = []
  61. for j in findChildren(i, [ 'require', 'enum' ]):
  62. e.append(j.getAttribute("name"))
  63. for j in findChildren(i, [ 'require', 'command' ]):
  64. c.append(j.getAttribute("name"))
  65. ret[n] = (e,c)
  66. return ret
  67. def findApi(dom, name):
  68. enums = findEnums(dom)
  69. commands = findCommands(dom)
  70. features = findFeatures(dom)
  71. extensions = findExtensions(dom)
  72. return (enums, commands, features, extensions)
  73. def writeExtension(f, name, extension, enums, commands):
  74. f.write('%s\n'%name)
  75. f.write('%s\n'%'https://www.khronos.org/registry/egl/specs/eglspec.1.5.pdf')
  76. if name.find('_VERSION_')==-1:
  77. f.write('%s\n'%name)
  78. else:
  79. f.write('\n')
  80. f.write('\n')
  81. enums = [ (j, enums[j]) for j in extension[0] ]
  82. for e in sorted(enums, key=lambda i: i[1]):
  83. f.write('\t%s %s\n'%(e[0], e[1]))
  84. commands = [ (j, commands[j]) for j in extension[1] ]
  85. for c in sorted(commands):
  86. params = ', '.join( [ '%s %s'%(j[0], j[1]) for j in c[1][1] ] )
  87. if len(params)==0:
  88. params = ' void '
  89. f.write('\t%s %s (%s)\n'%(c[1][0], c[0], params))
  90. if __name__ == '__main__':
  91. from optparse import OptionParser
  92. import os
  93. parser = OptionParser('usage: %prog [options] [XML specs...]')
  94. parser.add_option("--core", dest="core", help="location for core outputs", default='')
  95. parser.add_option("--extensions", dest="extensions", help="location for extensions outputs", default='')
  96. (options, args) = parser.parse_args()
  97. for i in args:
  98. dom = parse(i)
  99. api = findApi(dom, 'egl')
  100. print('Found {} enums, {} commands, {} features and {} extensions.'.format(
  101. len(api[0]), len(api[1]), len(api[2]), len(api[3])))
  102. if len(options.core):
  103. for i in api[2].keys():
  104. f = open('%s/%s'%(options.core, i), 'w')
  105. writeExtension(f, i, api[2][i], api[0], api[1])
  106. f.close()
  107. if len(options.extensions):
  108. for i in api[3].keys():
  109. f = open('%s/%s'%(options.extensions, i), 'w')
  110. writeExtension(f, i, api[3][i], api[0], api[1])
  111. f.close()