2 XML parser. One function for each top-level element in the schema.
4 Most functions just declare a new object and add it to the module.
5 For typedefs, eventcopies, xidtypes, and other aliases though,
6 we do not create a new type object, we just record the existing one under a new name.
9 from os.path import join
10 from xml.etree.cElementTree import parse
12 from xcbgen.xtypes import *
14 def import_(node, module, namespace):
16 For imports, we load the file, create a new namespace object,
17 execute recursively, then record the import (for header files, etc.)
19 # To avoid circular import error
20 from xcbgen import state
21 new_file = join(namespace.dir, '%s.xml' % node.text)
22 new_root = parse(new_file).getroot()
23 new_namespace = state.Namespace(new_file)
24 execute(module, new_namespace)
25 if not module.has_import(node.text):
26 module.add_import(node.text, new_namespace)
28 def typedef(node, module, namespace):
29 id = node.get('newname')
30 name = namespace.prefix + (id,)
31 type = module.get_type(node.get('oldname'))
32 module.add_type(id, namespace.ns, name, type)
34 def xidtype(node, module, namespace):
36 name = namespace.prefix + (id,)
37 type = module.get_type('CARD32')
38 module.add_type(id, namespace.ns, name, type)
40 def xidunion(node, module, namespace):
42 name = namespace.prefix + (id,)
43 type = module.get_type('CARD32')
44 module.add_type(id, namespace.ns, name, type)
46 def enum(node, module, namespace):
48 name = namespace.prefix + (id,)
49 type = Enum(name, node)
50 module.add_type(id, namespace.ns, name, type)
52 def struct(node, module, namespace):
54 name = namespace.prefix + (id,)
55 type = Struct(name, node)
56 module.add_type(id, namespace.ns, name, type)
58 def union(node, module, namespace):
60 name = namespace.prefix + (id,)
61 type = Union(name, node)
62 module.add_type(id, namespace.ns, name, type)
64 def request(node, module, namespace):
66 name = namespace.prefix + (id,)
67 type = Request(name, node)
68 module.add_request(id, name, type)
70 def event(node, module, namespace):
72 name = namespace.prefix + (id,)
73 event = Event(name, node)
74 event.add_opcode(node.get('number'), name, True)
75 module.add_event(id, name, event)
77 def eventcopy(node, module, namespace):
79 name = namespace.prefix + (id,)
80 event = module.get_event(node.get('ref'))
81 event.add_opcode(node.get('number'), name, False)
82 module.add_event(id, name, event)
84 def error(node, module, namespace):
86 name = namespace.prefix + (id,)
87 error = Error(name, node)
88 error.add_opcode(node.get('number'), name, True)
89 module.add_error(id, name, error)
91 def errorcopy(node, module, namespace):
93 name = namespace.prefix + (id,)
94 error = module.get_error(node.get('ref'))
95 error.add_opcode(node.get('number'), name, False)
96 module.add_error(id, name, error)
98 funcs = {'import' : import_,
101 'xidunion' : xidunion,
107 'eventcopy' : eventcopy,
109 'errorcopy' : errorcopy}
111 def execute(module, namespace):
112 for elt in list(namespace.root):
113 funcs[elt.tag](elt, module, namespace)