@@ 79-129 (lines=51) @@ | ||
76 | # Classes |
|
77 | ||
78 | ||
79 | class Property(GenericStruct): |
|
80 | """Table Property class. |
|
81 | ||
82 | This class represents a Table Property generic structure. |
|
83 | """ |
|
84 | ||
85 | property_type = UBInt16(enum_ref=TableFeaturePropType) |
|
86 | length = UBInt16(4) |
|
87 | ||
88 | def __init__(self, property_type=None): |
|
89 | """Constructor of Generic Instruction receives the parameters bellow. |
|
90 | ||
91 | Args: |
|
92 | type(|TableFeaturePropType_v0x04|): |
|
93 | Property Type value of this instance. |
|
94 | """ |
|
95 | super().__init__() |
|
96 | self.property_type = property_type |
|
97 | ||
98 | def pack(self, value=None): |
|
99 | """Pack method used to update the length of instance and packing. |
|
100 | ||
101 | Args: |
|
102 | value: Structure to be packed. |
|
103 | """ |
|
104 | self.update_length() |
|
105 | return super().pack(value) |
|
106 | ||
107 | def unpack(self, buff=None, offset=0): |
|
108 | """Unpack *buff* into this object. |
|
109 | ||
110 | This method will convert a binary data into a readable value according |
|
111 | to the attribute format. |
|
112 | ||
113 | Args: |
|
114 | buff (bytes): Binary buffer. |
|
115 | offset (int): Where to begin unpacking. |
|
116 | ||
117 | Raises: |
|
118 | :exc:`~.exceptions.UnpackException`: If unpack fails. |
|
119 | """ |
|
120 | property_type = UBInt16(enum_ref=TableFeaturePropType) |
|
121 | property_type.unpack(buff, offset) |
|
122 | self.__class__ = TableFeaturePropType(property_type.value).find_class() |
|
123 | ||
124 | length = UBInt16() |
|
125 | length.unpack(buff, offset=offset+2) |
|
126 | super().unpack(buff[:offset+length.value], offset=offset) |
|
127 | ||
128 | def update_length(self): |
|
129 | """Update the length of current instance.""" |
|
130 | self.length = self.get_size() |
|
131 | ||
132 |
@@ 54-98 (lines=45) @@ | ||
51 | ||
52 | ||
53 | # Classes |
|
54 | ||
55 | class Instruction(GenericStruct): |
|
56 | """Generic Instruction class. |
|
57 | ||
58 | This class represents a Generic Instruction that can be instanciated as |
|
59 | 'InstructionApplyAction', 'InstructionClearAction', 'InstructionGotoTable', |
|
60 | 'InstructionMeter', 'InstructionWriteAction', 'InstructionWriteMetadata'. |
|
61 | """ |
|
62 | ||
63 | instruction_type = UBInt16(enum_ref=InstructionType) |
|
64 | length = UBInt16() |
|
65 | ||
66 | def __init__(self, instruction_type=None): |
|
67 | """Constructor of Generic Instruction receives the parameters bellow. |
|
68 | ||
69 | Args: |
|
70 | instruction_type(InstructionType): Type of instruction. |
|
71 | """ |
|
72 | super().__init__() |
|
73 | self.instruction_type = instruction_type |
|
74 | ||
75 | def update_length(self): |
|
76 | """Method used to update length attribute.""" |
|
77 | self.length = self.get_size() |
|
78 | ||
79 | def unpack(self, buff=None, offset=0): |
|
80 | """Unpack *buff* into this object. |
|
81 | ||
82 | This method will convert a binary data into a readable value according |
|
83 | to the attribute format. |
|
84 | ||
85 | Args: |
|
86 | buff (bytes): Binary buffer. |
|
87 | offset (int): Where to begin unpacking. |
|
88 | ||
89 | Raises: |
|
90 | :exc:`~.exceptions.UnpackException`: If unpack fails. |
|
91 | """ |
|
92 | instruction_type = UBInt16(enum_ref=InstructionType) |
|
93 | instruction_type.unpack(buff, offset) |
|
94 | self.__class__ = InstructionType(instruction_type.value).find_class() |
|
95 | ||
96 | length = UBInt16() |
|
97 | length.unpack(buff, offset=offset+2) |
|
98 | ||
99 | super().unpack(buff[:offset+length.value], offset) |
|
100 | ||
101 |