blob: 1e5dcc2751360ddbf000a3e06998c0f1b2edafc6 [file] [log] [blame]
Rich Lane78a3bc42013-11-29 19:22:08 -08001:: import py_gen.util as util
Rich Lane9d98adf2013-11-29 18:37:24 -08002:: superclass_pyname = ofclass.superclass.pyname if ofclass.superclass else "loxi.OFObject"
Rich Lane78a3bc42013-11-29 19:22:08 -08003:: fmts = { 1: "B", 2: "!H", 4: "!L" }
4:: fmt = fmts[ofclass.discriminator.length]
5:: trail = ' '.join([x.pyname for x in util.ancestors(ofclass)])
Rich Lane9d98adf2013-11-29 18:37:24 -08006class ${ofclass.pyname}(${superclass_pyname}):
Rich Lane78a3bc42013-11-29 19:22:08 -08007 subtypes = {}
8
9 @staticmethod
10 def unpack(reader):
11 subtype, = reader.peek(${repr(fmt)}, ${ofclass.discriminator.offset})
12 try:
13 subclass = ${ofclass.pyname}.subtypes[subtype]
14 except KeyError:
15 raise loxi.ProtocolError("unknown ${trail} subtype %#x" % subtype)
16 return subclass.unpack(reader)
17
18:: # Register with our superclass
19:: if ofclass.superclass:
20:: type_field_name = ofclass.superclass.discriminator.name
21:: type_value = ofclass.member_by_name(type_field_name).value
22${superclass_pyname}.subtypes[${type_value}] = ${ofclass.pyname}
23:: #endif