Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 1 | :: superclass_pyname = ofclass.superclass.pyname if ofclass.superclass else "loxi.OFObject" |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 2 | :: from loxi_ir import * |
Rich Lane | da11f8b | 2013-06-19 15:53:25 -0700 | [diff] [blame] | 3 | :: import py_gen.oftype |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame] | 4 | :: type_members = [m for m in ofclass.members if type(m) == OFTypeMember] |
Rich Lane | 002e70c | 2013-05-09 17:08:16 -0700 | [diff] [blame] | 5 | :: normal_members = [m for m in ofclass.members if type(m) == OFDataMember] |
Rich Lane | 9d98adf | 2013-11-29 18:37:24 -0800 | [diff] [blame] | 6 | class ${ofclass.pyname}(${superclass_pyname}): |
Rich Lane | a2de219 | 2013-11-29 17:58:32 -0800 | [diff] [blame] | 7 | :: for m in type_members: |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 8 | ${m.name} = ${m.value} |
| 9 | :: #endfor |
| 10 | |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 11 | def __init__(${', '.join(['self'] + ["%s=None" % m.name for m in normal_members])}): |
| 12 | :: for m in normal_members: |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 13 | if ${m.name} != None: |
| 14 | self.${m.name} = ${m.name} |
| 15 | else: |
Rich Lane | 0f7f9d5 | 2013-11-29 17:00:49 -0800 | [diff] [blame] | 16 | :: if m.name == 'xid': |
| 17 | :: # HACK for message xid |
| 18 | self.${m.name} = None |
| 19 | :: else: |
Andreas Wundsam | 69ecfdc | 2013-09-17 13:50:12 -0700 | [diff] [blame] | 20 | self.${m.name} = ${py_gen.oftype.gen_init_expr(m.oftype, version=version)} |
Rich Lane | 0f7f9d5 | 2013-11-29 17:00:49 -0800 | [diff] [blame] | 21 | :: #endif |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 22 | :: #endfor |
| 23 | return |
| 24 | |
| 25 | def pack(self): |
| 26 | packed = [] |
| 27 | :: include("_pack.py", ofclass=ofclass) |
| 28 | return ''.join(packed) |
| 29 | |
| 30 | @staticmethod |
Rich Lane | 5b2745c | 2013-11-30 00:36:24 -0800 | [diff] [blame^] | 31 | def unpack(reader): |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 32 | obj = ${ofclass.pyname}() |
| 33 | :: include("_unpack.py", ofclass=ofclass) |
| 34 | return obj |
| 35 | |
| 36 | def __eq__(self, other): |
| 37 | if type(self) != type(other): return False |
Rich Lane | 8ca3b77 | 2013-04-30 13:36:55 -0700 | [diff] [blame] | 38 | :: for m in normal_members: |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 39 | if self.${m.name} != other.${m.name}: return False |
| 40 | :: #endfor |
| 41 | return True |
| 42 | |
Rich Lane | d1dd9e6 | 2013-02-20 18:28:01 -0800 | [diff] [blame] | 43 | def pretty_print(self, q): |
| 44 | :: include('_pretty_print.py', ofclass=ofclass) |
Rich Lane | 78a3bc4 | 2013-11-29 19:22:08 -0800 | [diff] [blame] | 45 | |
| 46 | :: # Register with our superclass |
| 47 | :: if ofclass.superclass: |
| 48 | :: type_field_name = ofclass.superclass.discriminator.name |
| 49 | :: type_value = ofclass.member_by_name(type_field_name).value |
| 50 | ${superclass_pyname}.subtypes[${type_value}] = ${ofclass.pyname} |
| 51 | :: #endif |