symetrie-hexapod 2023.1.0__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.
@@ -0,0 +1,83 @@
1
+ import logging
2
+ import re
3
+
4
+ logger = logging.getLogger(__name__)
5
+
6
+ regex_response = {
7
+ # Error message as ERRXXX
8
+ "ERROR": re.compile(r"\a(ERR\d{3})\r"),
9
+ # a floating point number [-]x[.[xxx]], possibly followed by spaces
10
+ "FLOAT": re.compile(r"(-?(\d*\.)?\d+)\s*\r\x06"),
11
+ # an integer number possible [-]iiiii, possibly followed by spaces
12
+ "INT": re.compile(r"(-?\d+)\s*\r\x06"),
13
+ # Anything else, stripping off '\x06'
14
+ "ANY": re.compile(r"(.*)\x06"),
15
+ # Just the null character
16
+ "NUL": re.compile(r"(\x00)"),
17
+ }
18
+
19
+
20
+ def match_regex_response(regex_prog, res):
21
+ """
22
+ This 'matches' from the start of the string 'res'. If you want to match anywhere in the
23
+ string, you will need to make another function search_regex_response() with 'search' instead
24
+ of 'match'.
25
+
26
+ Return None if no match and the match object otherwise.
27
+ """
28
+ logger.flash_flood(f"res = {res} with type {type(res)}")
29
+ if isinstance(res, bytes):
30
+ res = res.decode()
31
+ match_obj = regex_prog.match(res)
32
+ return match_obj
33
+
34
+
35
+ def patter_n_int(nr):
36
+ return re.compile(fr"(-?\d+)\r((-?\d+)\r){{{nr}}}\x06")
37
+
38
+
39
+ def patter_n_float(nr):
40
+ return re.compile(fr"(-?\d+)\r((-?(\d*\.)?\d+)\r){{{nr}}}\x06")
41
+
42
+
43
+ def match_int_response(res):
44
+ match_obj = match_regex_response(regex_response["INT"], res)
45
+ if match_obj is None:
46
+ logger.error(f"Could not parse INT response for {res}")
47
+ return None
48
+ return int(match_obj[1])
49
+
50
+
51
+ def match_float_response(res):
52
+ match_obj = match_regex_response(regex_response["FLOAT"], res)
53
+ if match_obj is None:
54
+ logger.error(f"Could not parse FLOAT response for {res}")
55
+ return None
56
+ return float(match_obj[1])
57
+
58
+
59
+ def match_error_response(res):
60
+ match_obj = match_regex_response(regex_response["ERROR"], res)
61
+ if match_obj is None:
62
+ logger.error(f"Could not parse ERROR response for {res}")
63
+ return None
64
+ return match_obj[1]
65
+
66
+
67
+ def match_nul_response(res):
68
+ match_obj = match_regex_response(regex_response["NUL"], res)
69
+ if match_obj is None:
70
+ logger.error(f"Could not parse NUL response for {res}")
71
+ return None
72
+ return match_obj[1]
73
+
74
+
75
+ def match_string_response(res):
76
+ match_obj = match_regex_response(regex_response["ANY"], res)
77
+ if match_obj is None:
78
+ logger.error(f"Could not parse STRING response for {res}")
79
+ return None
80
+ if len(match_obj[1]) > 0 and match_obj[1][-1] == "\r":
81
+ return match_obj[1][:-1]
82
+ else:
83
+ return match_obj[1]