| Total Complexity | 8 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
| 1 | """binary representation modes used for a StarStruct object.""" |
||
| 7 | 1 | @enum.unique |
|
| 8 | 1 | class Mode(enum.Enum): |
|
| 9 | """The StarStruct modes match the modes supported by struct.pack/unpack.""" |
||
| 10 | 1 | Native = '=' |
|
| 11 | 1 | Little = '<' |
|
| 12 | 1 | Big = '>' |
|
| 13 | 1 | Network = '!' |
|
| 14 | |||
| 15 | 1 | def to_byteorder(self): |
|
| 16 | """ |
||
| 17 | Convert a Mode to a byteorder string such as required by the |
||
| 18 | to_bytes() or from_bytes() functions. |
||
| 19 | """ |
||
| 20 | 1 | if self == self.Native: |
|
| 21 | 1 | return sys.byteorder |
|
| 22 | 1 | elif self == self.Little: |
|
| 23 | 1 | return 'little' |
|
| 24 | else: # Big or Network |
||
| 25 | 1 | return 'big' |
|
| 26 | |||
| 27 | 1 | @staticmethod |
|
| 28 | def from_byteorder(val): |
||
| 29 | """ |
||
| 30 | Convert a byteorder string such as used by the to_bytes() or |
||
| 31 | from_bytes() functions into a Mode value. |
||
| 32 | """ |
||
| 33 | 1 | if val == 'little': |
|
| 34 | 1 | return Mode.Little |
|
| 35 | 1 | elif val == 'big': |
|
| 36 | 1 | return Mode.Big |
|
| 37 | 1 | elif val == 'native': # custom |
|
| 38 | 1 | return Mode.Native |
|
| 39 | 1 | elif val == 'network': # custom |
|
| 40 | 1 | return Mode.Network |
|
| 41 | raise TypeError('{} not a valid byteorder'.format(val)) |
||
| 42 |