plantstar-shared 2.0.3.0.2.8__py3-none-any.whl → 2.0.3.0.2.10__py3-none-any.whl

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.
@@ -2,9 +2,9 @@ import msgpack
2
2
 
3
3
  from plantstar_shared.errors import SocketConnectionError
4
4
 
5
+
5
6
  # Converts bytes into an object using msgpack, with a parameter for strict_map_key
6
7
  def convert_bytes_to_object(object_as_bytes, strict_map_key=False):
7
-
8
8
  if not object_as_bytes:
9
9
  raise SocketConnectionError
10
10
 
@@ -2,37 +2,75 @@ import struct
2
2
 
3
3
  from plantstar_shared.convert_bytes_to_object import convert_bytes_to_object
4
4
  from plantstar_shared.convert_object_to_bytes import convert_object_to_bytes
5
- from plantstar_shared.errors import SocketConnectionError
5
+ from plantstar_shared.errors import SocketConnectionError, SysconProgrammingError
6
6
 
7
- SIZE_OF_UNSIGNED_INT_STRUCT = 4
7
+ SIZE_OF_UNSIGNED_INT_STRUCT = 4 # size of integer value that precedes data coming from DCM
8
8
 
9
9
 
10
- def read_size_value_from_socket(*, remote_socket):
11
- size_struct = remote_socket.recv(SIZE_OF_UNSIGNED_INT_STRUCT)
10
+ def read_size_value_from_socket(*, remote_socket, is_big_endian, number_of_bytes_for_size_prefix):
11
+ size_bytes = remote_socket.recv(number_of_bytes_for_size_prefix)
12
12
 
13
- if not size_struct:
13
+ if not size_bytes:
14
14
  return None
15
15
 
16
- try:
17
- message_length = struct.unpack('>I', size_struct)[0]
18
- except struct.error as error:
19
- raise SocketConnectionError
16
+ if number_of_bytes_for_size_prefix == 2:
17
+ message_length = int.from_bytes(size_bytes, 'big') if is_big_endian else int.from_bytes(size_bytes, 'little')
18
+ elif number_of_bytes_for_size_prefix == SIZE_OF_UNSIGNED_INT_STRUCT:
19
+ number_format = ">I" if is_big_endian else "<I"
20
+
21
+ try:
22
+ message_length = struct.unpack(number_format, size_bytes)[0]
23
+ except struct.error as error:
24
+ raise SocketConnectionError
25
+ else:
26
+ raise SysconProgrammingError("A size prefix was set to something other than 2 or 4")
20
27
 
21
28
  return message_length
22
29
 
23
30
 
24
- def get_message_from_socket(*, remote_socket):
25
- message_length = read_size_value_from_socket(remote_socket=remote_socket)
31
+ def get_bytes_from_socket(*, remote_socket, number_of_bytes_to_read=None, is_big_endian=True, number_of_bytes_for_size_prefix=0, should_remove_prefix_size_from_read=False):
32
+ """Obtains a number of bytes from a provided socket, with options for different configurations and situations
26
33
 
27
- if not message_length:
28
- return None
34
+ Keywords / Example Configurations:
35
+ remote_socket -- the connection that bytes will be read from
36
+
37
+ number_of_bytes_to_read -- the number of bytes to read on the socket, if this is known, this is the only optional parameter needed (default: 0)
38
+
39
+ is_big_endian -- bool that determines what format string should be used for unpacking the size value (default: True)
40
+
41
+ number_of_bytes_for_size_prefix -- the number of bytes to read to determine the total size of the message to pass (default: 0)
42
+
43
+ should_remove_prefix_size_from_read -- bool that determines if the size should be removed from the calculated number of bytes to read (default: False)
44
+
45
+ -----
46
+
47
+ get_bytes_from_socket(remote_socket=conn, number_of_bytes_to_read=100) - likely would not be called directly, in most cases
48
+
49
+ get_bytes_from_socket(remote_socket=conn, is_big_endian=True, number_of_bytes_for_size_prefix=4) - reads 4 bytes to get the size, then reads that amount (DCM socket, etc)
50
+
51
+ get_bytes_from_socket(remote_socket=conn, is_big_endian=False, number_of_bytes_for_size_prefix=2, should_remove_prefix_size_from_read=True) - reads 2 bytes to get the size,
52
+ then reads that calculated amount minus 2 (Husky socket, etc)
53
+
54
+ """
55
+
56
+ if not number_of_bytes_to_read:
57
+ if number_of_bytes_for_size_prefix:
58
+ number_of_bytes_to_read = read_size_value_from_socket(
59
+ remote_socket=remote_socket, is_big_endian=is_big_endian, number_of_bytes_for_size_prefix=number_of_bytes_for_size_prefix
60
+ )
61
+
62
+ if not number_of_bytes_to_read:
63
+ raise SysconProgrammingError("get_bytes_from_socket was called with no parameters for number_of_bytes_to_read or number_of_bytes_for_size_prefix")
64
+
65
+ if should_remove_prefix_size_from_read:
66
+ number_of_bytes_to_read = number_of_bytes_to_read - number_of_bytes_for_size_prefix
29
67
 
30
68
  packets = []
31
69
  bytes_received = 0
32
70
 
33
71
  # While loop that will stream in data if the full request is not available yet
34
- while bytes_received < message_length:
35
- buffer_size = message_length - bytes_received
72
+ while bytes_received < number_of_bytes_to_read:
73
+ buffer_size = number_of_bytes_to_read - bytes_received
36
74
  packet = remote_socket.recv(buffer_size)
37
75
 
38
76
  if not packet:
@@ -62,7 +100,13 @@ def send_encoded_message_on_socket(*, remote_socket, encoded_message):
62
100
  bytes_sent += new_bytes_sent
63
101
 
64
102
 
65
- def get_object_from_socket(*, remote_socket):
66
- object_from_interface_as_bytes = get_message_from_socket(remote_socket=remote_socket)
103
+ def get_object_from_socket(
104
+ *, remote_socket, number_of_bytes_to_read=None, is_big_endian=True, number_of_bytes_for_size_prefix=SIZE_OF_UNSIGNED_INT_STRUCT, should_remove_prefix_size_from_read=False
105
+ ):
106
+ """Function that is used between the APU and DCM to send dictionaries """
107
+ object_from_interface_as_bytes = get_bytes_from_socket(
108
+ remote_socket=remote_socket, number_of_bytes_to_read=number_of_bytes_to_read, is_big_endian=is_big_endian, number_of_bytes_for_size_prefix=number_of_bytes_for_size_prefix,
109
+ should_remove_prefix_size_from_read=should_remove_prefix_size_from_read
110
+ )
67
111
  object_from_interface = convert_bytes_to_object(object_from_interface_as_bytes)
68
112
  return object_from_interface
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: plantstar-shared
3
- Version: 2.0.3.0.2.8
3
+ Version: 2.0.3.0.2.10
4
4
  Summary: Shared code used in plantstar_apu and plantstar_dcm
5
5
  Home-page: https://github.com/SYSCON-International/plantstar_shared
6
6
  Author: SYSCON International
@@ -6,7 +6,7 @@ plantstar_shared/SysconType.py,sha256=6Ae9F0fBr0vpXAmP8fYqoWyFUGTEECtS55zAYMkeKl
6
6
  plantstar_shared/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  plantstar_shared/add_size_onto_string_and_return.py,sha256=j7XSs5ubKKo5LCMVljzZudK2nw8CZh-zMSLT_t30aGg,385
8
8
  plantstar_shared/api_types.py,sha256=gKN2LmUFBcpa7gJSNwCJ0eSv4IqkIP01pCLfy8-mT50,5001
9
- plantstar_shared/convert_bytes_to_object.py,sha256=MVOKqJJeUjvEKJL_ELNPkxZEgqwlk3zT8CwfVtpwOgU,409
9
+ plantstar_shared/convert_bytes_to_object.py,sha256=YKf410EWUpeyyeBhJ3kl6PYIAcHJKKkhp8JkE-CuSv0,405
10
10
  plantstar_shared/convert_object_to_bytes.py,sha256=vVS3dKkPynOJ2NV2LgLHdEOZflK_0hhdS-NuU_gcfHs,206
11
11
  plantstar_shared/errors.py,sha256=qEUHh2d7pqZHoag6YJjnVfRWzWAzjuyGZH7stT_-Nzk,341
12
12
  plantstar_shared/global_definitions.py,sha256=mj59nU21DYza4XgZQxLX2lTvJYooN_-ZFrh8SVXFBRs,1153
@@ -14,8 +14,8 @@ plantstar_shared/is_valid_signed_string.py,sha256=5ZiEsPRz30J8yxN4A1dHq-hlz7VI9r
14
14
  plantstar_shared/obtain_raw_data_processor_function_for_action_name.py,sha256=DYo307PraRRD8zGiSO6Fn03bZ4X9aDOYV9rwaRm8mXM,1134
15
15
  plantstar_shared/syscon_image_field.py,sha256=GlTTudqFT5EsWM5sPRO0p5SDascX7gI1BEOeF1OG7Tw,441
16
16
  plantstar_shared/syscon_json.py,sha256=8OG9_55d52V9e2fFNguDtQ_pnw1YwyYsg2pnUkhjW6g,4213
17
- plantstar_shared/tcp_socket_utils.py,sha256=sI5IJXMTsQuTPzm8V5T1nsY5aMF-78zGL69aIDLCD_A,2250
18
- plantstar_shared-2.0.3.0.2.8.dist-info/METADATA,sha256=1QDXF2nl6RkCT0t3JpHJezBnojrHM2ei6QfvrrBQUf0,658
19
- plantstar_shared-2.0.3.0.2.8.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
20
- plantstar_shared-2.0.3.0.2.8.dist-info/top_level.txt,sha256=9ZTCr_aP1GqYGt7EVmlSIUF72bdMZErapyhbik-MWVQ,17
21
- plantstar_shared-2.0.3.0.2.8.dist-info/RECORD,,
17
+ plantstar_shared/tcp_socket_utils.py,sha256=uQ_Jhw1JVGtFD44AN7qnQo9mHor_lvPu2vr0yLBpd3Q,5312
18
+ plantstar_shared-2.0.3.0.2.10.dist-info/METADATA,sha256=1Ny-4BV00AclXLq-V9Pf5f7dKCZyv1FMHR_bCNGrjHg,659
19
+ plantstar_shared-2.0.3.0.2.10.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
20
+ plantstar_shared-2.0.3.0.2.10.dist-info/top_level.txt,sha256=9ZTCr_aP1GqYGt7EVmlSIUF72bdMZErapyhbik-MWVQ,17
21
+ plantstar_shared-2.0.3.0.2.10.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.42.0)
2
+ Generator: bdist_wheel (0.43.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5