Completed
Pull Request — master (#42)
by TJ
06:11
created

ElementNone.unpack()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 2
rs 10
1
"""
2
Element None
3
4
A non-packable, non-unpackable item that can help facilitate with additional information about an object,
5
pass extra references to a variable and potentially more
6
7
.. code-block:: python
8
9
    ExampleMessage = Message('VarTest', [('x', 'B'), ('y', 'B')])
10
11
    CRCedMessage = Message('CRCedMessage', [
12
       ('data', ExampleMessage),                  # A data field that has the example message in it
13
       ('extra_param', None),                     # The None Element
14
       ('crc', 'I', my_crc32, ['data', 'extra_param']),          # Crc the data, and give an error if we have something unexpected
15
   ])
16
17
"""
18
19
from typing import Optional
0 ignored issues
show
Configuration introduced by
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...
20
21
from starstruct.element import register, Element
22
from starstruct.modes import Mode
23
24
@register
25
class ElementNone(Element):
26
    """
27
    Initialize a StarStruct element object.
28
29
    :param field: The fields passed into the constructor of the element
30
    :param mode: The mode in which to pack the bytes
31
    :param alignment: Number of bytes to align to
32
    """
33
    def __init__(self, field: list, mode: Optional[Mode]=Mode.Native, alignment: Optional[int]=1):
34
        self.name = field[0]
35
36
        self.update(mode=mode, alignment=alignment)
37
38
    @staticmethod
39
    def valid(field: tuple) -> bool:
40
        return len(field) == 2 \
41
            and isinstance(field[0], str) \
42
            and field[1] is None
43
44
    def validate(self, msg):
45
        return True
46
47
    def update(self, mode=None, alignment=None):
48
        if mode:
49
            self._mode = mode
50
51
        if alignment:
52
            self._alignment = alignment
53
        return
54
55
    def pack(self, msg):
56
        return b''
57
58
    def unpack(self, msg, buf):
59
        return (msg, buf)
60
61
    def make(self, msg):
62
        return msg[self.name]
63