Issues (43)

starstruct/elementconstant.py (1 issue)

1
"""
2
The constant element class for StarStruct
3
4
To be used in the following way:
5
6
.. code-block:: python
7
8
    ExampleMessage = Message('constant_message', [
9
        ('regular', 'B'),                           # Two regular messages
10
        ('fill_in_later', 'H'),
11
        ('ending_sequence', 'II', (0xAA, 0xBB)),    # An ending sequence to a message
12
                                                    # that's always the same
13
    ])
14
15
:todo: Not sure if alignment is working correctly here, or if it needs to do anything
16
17
"""
18
19 1
import struct
20 1
from typing import Optional, Tuple
0 ignored issues
show
The import typing could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
21
22 1
from starstruct.element import register, Element
23 1
from starstruct.modes import Mode
24
25
26 1
@register
27 1
class ElementConstant(Element):
28 1
    def __init__(self, field, mode=Mode.Native, alignment=1):
29 1
        self.name = field[0]
30 1
        self.format = field[1]
31 1
        self.values = field[2]
32
33 1
        self._mode = mode
34 1
        self._alignment = alignment
35
36 1
    @property
37
    def _struct(self):
38 1
        return struct.Struct(self._mode.value + self.format)
39
40 1
    @property
41
    def _packed(self):
42 1
        return self._struct.pack(*self.values)
43
44 1
    @staticmethod
45 1
    def valid(field: list) -> bool:
46
        """
47
        Validate whether this field could supply this element with the corect values
48
        """
49 1
        return len(field) == 3 \
50
            and isinstance(field[0], str) \
51
            and isinstance(field[1], str) \
52
            and struct.calcsize(field[1]) \
53
            and isinstance(field[2], tuple)
54
55 1
    def validate(self, msg):
56
        """
57
        Ensure that the supplied message contains the required information for
58
        this element object to operate.
59
60
        Constants will alawys be valid
61
        """
62 1
        return True
63
64 1
    def update(self, mode: Optional[Tuple]=None, alignment: Optional[int]=1) -> None:
65
        """change the mode of the struct format"""
66
        if mode:
67
            self._mode = mode
68
69
        if alignment:
70
            self._alignment = alignment
71
72 1
    def pack(self, msg: dict) -> bytes:
73
        """
74
        Pack the provided values into the supplied buffer.
75
76
        :param msg: The message specifying the values to pack
77
        """
78 1
        return self._packed
79
80 1
    def unpack(self, msg: dict, buf: bytes) -> Tuple[bytes, bytes]:
81
        """Unpack data from the supplied buffer using the initialized format."""
82 1
        return (self._struct.unpack_from(buf), buf[self._struct.size:])
83
84 1
    def make(self, msg: dict):
85
        """
86
        Return the expected "made" value
87
88
        :param msg: The values to make
89
        """
90
        return self.values
91