bear-tools 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ Copyright 2024 JugglingBears
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,16 @@
1
+ Metadata-Version: 2.1
2
+ Name: bear-tools
3
+ Version: 0.1.0
4
+ Summary: An assortment of QA/Automation related tools
5
+ License: MIT
6
+ Author: Sean Foley
7
+ Author-email: sean.foley.engr@gmail.com
8
+ Requires-Python: >=3.10,<4.0
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Description-Content-Type: text/markdown
15
+
16
+
File without changes
@@ -0,0 +1,19 @@
1
+ [tool.poetry]
2
+ name = "bear-tools"
3
+ version = "0.1.0"
4
+ description = "An assortment of QA/Automation related tools"
5
+ authors = ["Sean Foley <sean.foley.engr@gmail.com>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+
9
+ packages = [
10
+ { include = "bear-tools", from = "src" },
11
+ ]
12
+
13
+ [tool.poetry.dependencies]
14
+ python = "^3.10"
15
+
16
+
17
+ [build-system]
18
+ requires = ["poetry-core"]
19
+ build-backend = "poetry.core.masonry.api"
File without changes
@@ -0,0 +1,174 @@
1
+ """
2
+ Design concept for Transport Protocol generalization that can be used with TCP/IP client/server systems
3
+
4
+ Types: 1, 2, 3
5
+
6
+ Type 1: Set the value of x (bool)
7
+ Example:
8
+ Request: 01:01:(00|01)
9
+ Response: 01:01:(00|01) (00:fail, 01:success)
10
+
11
+ Type 2: Set the value of y (int32) and z (str)
12
+ Example:
13
+ Request: 02:04:xx:xx:xx:xx:{variable}:xx:...
14
+ Response: 02:01:(00|01) (00:fail, 01:success)
15
+
16
+ Type 3: Get the current value of x, y, and z (in that order)
17
+ Example:
18
+ Request: 03
19
+ Response:
20
+ 03: Type ID
21
+ 01:(00|01): Status of command (00:no errors, 01:errors)
22
+ 01:(00|01): Current value of x (length: 1 byte)
23
+ 04:xx:xx:xx:xx: Current value of y (length: 4 bytes)
24
+ {variable}:xx:... Current value of z (variable lengt)
25
+ """
26
+
27
+ import abc
28
+ from dataclasses import dataclass
29
+ from enum import Enum, IntEnum
30
+ from typing import Iterator, Literal, Type, TypedDict
31
+
32
+ class ExampleTypes(IntEnum):
33
+ TYPE_1 = 1
34
+ TYPE_2 = 2
35
+ TYPE_3 = 3
36
+
37
+
38
+ @dataclass
39
+ class LV:
40
+ description: str
41
+ length: int | Literal['variable']
42
+ value_type: Type[int] | Type[str] | Type[bool] | None
43
+ value: int | str | bool | None = None
44
+
45
+
46
+ @dataclass
47
+ class TLV:
48
+ type_value: Enum
49
+ parameters: list[LV]
50
+
51
+
52
+ @dataclass
53
+ class TransportProtocol:
54
+ type_enum: Type[Enum]
55
+ schema: list[TLV]
56
+
57
+
58
+ def __get_next_length_value_object(self, tlv: TLV, data: bytes) -> Iterator[LV | None]:
59
+ """
60
+ (Generator Method)
61
+ Get the next LV (Length-Value) object
62
+
63
+ :param tlv: The Type-Length-Value object associated with the given data
64
+ :param data: The raw Length-Value data to parse
65
+ :return: The LV object corresponding to the next chunk of data or None if there are any errors
66
+ """
67
+
68
+ def debug():
69
+ print(f'length: {length}, value: {value_raw}, parameter.value_type: {parameter.value_type}')
70
+
71
+ if len(data) < 1:
72
+ print('Error: There is no data')
73
+ yield None
74
+
75
+ else:
76
+ i: int = 1
77
+ for parameter in tlv.parameters:
78
+ length: int = data[i]
79
+ value_raw: bytes = bytes(data[i+1:i+1+length])
80
+ i += 1 + length
81
+ if parameter.value_type == bool:
82
+ value: int = int.from_bytes(value_raw, byteorder='big')
83
+ if value in (0, 1):
84
+ yield LV(parameter.description, length, bool, value=bool(value))
85
+ else:
86
+ print(f'Error: Expected bool value. Actual value: {value_raw}')
87
+ debug()
88
+ yield None
89
+ elif parameter.value_type == int:
90
+ # TODO
91
+ print(f'Parse {value_raw} into an int')
92
+ elif parameter.value_type == str:
93
+ # TODO
94
+ print(f'Parse {value_raw} into a str')
95
+ elif parameter.value_type is None:
96
+ # TODO
97
+ print(f'Parse {value_raw} into None')
98
+ else:
99
+ print(f'Error: Unexpected Length-Value value type: {parameter.value_type}')
100
+
101
+ if i != len(data):
102
+ print(f'Error: Data leftover after parsing all Length-Value data: {bytes(data[i:]).hex(":")}')
103
+
104
+
105
+
106
+ def parse(self, data: bytes) -> TLV | None:
107
+ """
108
+ Parse raw bytestream data into a TLV object
109
+
110
+ :param data: The raw data to parse
111
+ :return: The data deserialized into a TLV object if possible; None otherwise
112
+ """
113
+
114
+ if len(data) < 1:
115
+ return None
116
+
117
+ # Extract Type data
118
+ type_id: int = data[0]
119
+ tlv: TLV | None = None
120
+ for _tlv in self.schema:
121
+ if _tlv.type_value == type_id:
122
+ tlv = _tlv
123
+ break
124
+ if tlv is None:
125
+ print(f'Error: TLV not found for type id: {type_id}')
126
+ return None
127
+
128
+ print(f'tlv: {tlv}')
129
+
130
+ # Extract Length-Value data
131
+ length_value_data: list[LV] = []
132
+ for _lv in self.__get_next_length_value_object(tlv, data):
133
+ if _lv is None:
134
+ print(f'Error: Failed to parse data into TLV: {tlv}')
135
+ return None
136
+ else:
137
+ length_value_data.append(_lv)
138
+
139
+ print(f'Length-Value Data:')
140
+ for _lv in length_value_data:
141
+ print(f' {_lv}')
142
+
143
+
144
+ def main():
145
+ protocol = TransportProtocol(
146
+ ExampleTypes, [
147
+ TLV(
148
+ ExampleTypes.TYPE_1,
149
+ [
150
+ LV('Set the value of x', 1, bool)
151
+ ]
152
+ ),
153
+ TLV(
154
+ ExampleTypes.TYPE_2,
155
+ [
156
+ LV('Set the value of y', 4, int),
157
+ LV('Set the value of z', 'variable', str)
158
+ ]
159
+ ),
160
+ TLV(
161
+ ExampleTypes.TYPE_3,
162
+ [
163
+ LV('Get the current value of x, y, and z', 0, None)
164
+ ]
165
+ )
166
+ ])
167
+
168
+ tlv: TLV | None = protocol.parse(bytes.fromhex('01:01:01'.replace(':', '')))
169
+
170
+
171
+
172
+ if __name__ == '__main__':
173
+ main()
174
+