SpecLog 0.0.1__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,190 @@
1
+ """
2
+ This is configuration file for logger
3
+
4
+ The class has name: 'loggerConfig'. It has one following functions:
5
+
6
+ Read a configuration file to create settings dictionary, devices dictionary and command dictionary
7
+
8
+ Logger will use the three dictionaries.
9
+
10
+ Author: Yen-Chun Huang
11
+
12
+ Company: Bridge 12 Technologies. Inc
13
+ """
14
+
15
+ import time
16
+ import os
17
+ from .config.config import (
18
+ CONFIG,
19
+ ) # this will automatically duplicate the config file to default folder
20
+ from configparser import ConfigParser
21
+
22
+ device_default = {
23
+ "device_status": False,
24
+ "address": None,
25
+ "id_command": "*IDN?",
26
+ "baud_rate": 9600,
27
+ "termination": "\n",
28
+ "data_bits": 8,
29
+ "flow_control": None,
30
+ "parity": None,
31
+ "stop_bits": None,
32
+ "delimiter": "",
33
+ "index": 0,
34
+ }
35
+
36
+ # command format in config file: variable: command, alias, min, max, static
37
+ command_default = {
38
+ "command": None,
39
+ "alias": None,
40
+ "min": None,
41
+ "max": None,
42
+ "static": None,
43
+ "bits": None,
44
+ "bit_static": None,
45
+ "indicators": None,
46
+ "indicators_reverse": None
47
+ }
48
+
49
+
50
+ class loggerConfig:
51
+ def __init__(self, config_file: str = None):
52
+ """
53
+ config_dir (str): the directory to the config file
54
+
55
+ """
56
+ self.config, self.config_file = self._getConfig(config_file)
57
+
58
+ self.settings = self._getSettings()
59
+ self.devices = self._getDevices()
60
+ self.commands = self._getCommands()
61
+
62
+ def _getConfig(self, config_file):
63
+ if config_file:
64
+ config_dir = os.path.dirname(config_file) # get directory
65
+ config_name = os.path.basename(config_file) # get file name
66
+
67
+ else:
68
+ # default directory and file name
69
+ config_dir = CONFIG["SETTINGS"]["log_folder_location"] + "/LOG_Config/"
70
+ config_name = "config.cfg"
71
+ config_file = config_dir + config_name
72
+
73
+ config = ConfigParser()
74
+ config.read(config_file)
75
+ return config, config_file
76
+
77
+ def _getSettings(self):
78
+ """ """
79
+ settings = {}
80
+ for key, value in self.config["SETTINGS"].items():
81
+ if value.isdigit() or value in ["True", "False", "None"]:
82
+ settings[key] = eval(value)
83
+ else:
84
+ settings[key] = value
85
+ return settings
86
+
87
+ def _getDevices(self):
88
+ devices = {}
89
+ for section in self.config.keys():
90
+ if section not in ["DEFAULT", "SETTINGS"]:
91
+ temp_dict = {} # new dictionary for devices
92
+ for key in device_default.keys():
93
+ if key in self.config[section].keys():
94
+ if key in [
95
+ "device_status",
96
+ "baud_rate",
97
+ "flow_control",
98
+ "parity",
99
+ "stop_bits",
100
+ "data_bits",
101
+ "index",
102
+ ]:
103
+ temp_dict[key] = eval(self.config[section][key])
104
+ elif key == "termination":
105
+ termination = self._getTermination(
106
+ self.config[section][key]
107
+ )
108
+ temp_dict[key] = termination
109
+ else:
110
+ temp_dict[key] = self.config[section][key]
111
+
112
+ temp_dict = {**device_default, **temp_dict}
113
+ devices[section] = temp_dict
114
+
115
+ return devices
116
+
117
+ def _getCommands(self):
118
+ commands = {}
119
+ for section in self.config.keys():
120
+ if section not in ["DEFAULT", "SETTINGS"]:
121
+ temp_dict = {} # new dictionary for commands
122
+ for key in self.config[section].keys():
123
+ if (
124
+ key not in device_default.keys()
125
+ ): # in key in config section but not for device settings
126
+ command_info = self._command_analysis(self.config[section][key])
127
+ temp_dict[key] = command_info # use variable as key
128
+
129
+ commands[section] = temp_dict
130
+ return commands
131
+
132
+ def _command_analysis(self, command_string: str):
133
+ """
134
+ Arg:
135
+ command_string (str): a command string with the format in config file: variable: command, alias, min, max, static
136
+
137
+ Return:
138
+ command_info (dict): the prepared dict with the same format as command_default
139
+
140
+ """
141
+ command_info = command_default.copy()
142
+ command_list = command_string.split(", ")
143
+ for index, item in enumerate(command_list):
144
+ item = item.strip() # remove leading and tailing white space
145
+ if index == 0:
146
+ command_info["command"] = item
147
+
148
+ else:
149
+ key, value = item.split("=") # get key and value from item
150
+ key = key.strip() # remove leading and tailing white space
151
+ value = value.strip() # remove leading and tailing white space
152
+
153
+ if key == "alias":
154
+ command_info["alias"] = value
155
+
156
+ elif key == 'bit_static':
157
+ command_info['bit_static'] = str(value)
158
+ else:
159
+ command_info[key] = eval(value)
160
+
161
+ if command_info['indicators']:
162
+ command_info['indicators_reverse'] = [False] * len(command_info['indicators'])
163
+ for i in range(len(command_info['indicators'])):
164
+ if '*' in command_info['indicators'][i]:
165
+ command_info['indicators_reverse'][i] = True
166
+ command_info['indicators'][i] = command_info['indicators'][i].replace('*', '')
167
+
168
+ return command_info
169
+
170
+ def _getTermination(self, termination: str):
171
+ """
172
+ Return CR or Lf in string
173
+
174
+ Args:
175
+ termination (str): CR, LF, CRLF or LFCR
176
+
177
+ Return:
178
+ termination (str): termination in string with backslash
179
+ """
180
+
181
+ if termination.replace("LF", "").replace("CR", ""):
182
+ self.debugLog.error("Termination is not valid")
183
+ raise ValueError("Termination is not valid")
184
+ eval_termination = termination.replace("LF", "\n").replace("CR", "\r")
185
+
186
+ return eval_termination
187
+
188
+
189
+ if __name__ == "__main__":
190
+ config = loggerConfig()