XCBButton -> XCBButtonIndex. XCBHost -> XCBHostMode. XCBSetupReq -> XCBSetupRequest
[free-sw/xcb/proto] / src / import.py
1 #!/usr/bin/python
2
3 from xml.sax.saxutils import XMLFilterBase, XMLGenerator
4 from xml.sax import make_parser
5 import sys, os
6
7 path = [os.path.curdir, 'extensions']
8 def find_file_on_path(name):
9         for d in path:
10                 test = os.path.join(d, name)
11                 if os.path.isfile(test):
12                         return test
13         raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), name)
14
15 seen = {}
16
17 class ProcessImports(XMLFilterBase):
18         def setContentHandler(self, handler):
19                 self.handler = handler
20                 XMLFilterBase.setContentHandler(self, handler)
21
22         def ensure(self, name):
23                 if not seen.has_key(name):
24                         child = ProcessImports(make_parser())
25                         child.setContentHandler(self.handler)
26                         child.parse(find_file_on_path(name + '.xml'))
27
28         def startDocument(self):
29                 pass
30         def endDocument(self):
31                 pass
32
33         inimport = None
34
35         def startElement(self, name, attrs):
36                 assert self.inimport is None
37                 if name == 'import':
38                         self.inimport = ""
39                         return
40                 XMLFilterBase.startElement(self, name, attrs)
41                 if name == 'xcb':
42                         seen[attrs['header']] = True
43                         self.ensure('xproto')
44
45         def characters(self, content):
46                 if self.inimport is not None:
47                         self.inimport += content
48                 else:
49                         XMLFilterBase.characters(self, content)
50
51         def endElement(self, name):
52                 if name == 'import':
53                         self.ensure(self.inimport)
54                         self.inimport = None
55                         return
56                 XMLFilterBase.endElement(self, name)
57
58 out = XMLGenerator()
59 importer = ProcessImports(make_parser())
60 importer.setContentHandler(out)
61 out.startDocument()
62 if len(sys.argv) > 1:
63         importer.parse(sys.argv[1])
64 else:
65         importer.parse(sys.stdin)
66 out.endDocument()