2 File in charge of tracking the variables for the current action.
5from typing
import Dict, Any, Type, Union
7from display_tty
import Disp, TOML_CONF, FILE_DESCRIPTOR, SAVE_TO_FILE, FILE_NAME
11 """Custom exception to handle scope-related errors."""
13 def __init__(self, message="There is a problem with the scope."):
18 """Custom exception to handle variable-related errors."""
20 def __init__(self, message="The variable was not found."):
28 def __init__(self, success: int = 0, error: int = 84, debug: bool =
False):
30 Class in charge of storing and restoring variables.
33 success (int, optional): _description_. Defaults to 0.
34 error (int, optional): _description_. Defaults to 84.
35 debug (bool, optional): _description_. Defaults to False.
50 logger=self.__class__.__name__
55 Destructor for the class.
58 self.
disp.log_debug(
"Clearing content", title)
60 self.
disp.log_debug(
"Scopes cleared", title)
67 scope_name (Any): _description_: The name of the scope.
70 int: _description_: Returns self.success if it succeeds, self.error otherwise.
72 title =
"create_scope"
74 self.
disp.log_warning(
75 f
"Scope {scope_name} already present.", title
79 self.
disp.log_debug(f
"Scope {scope_name} created.", title)
82 def add_variable(self, name: str, variable_data: Any, variable_type: Type = str, scope: Any =
"default_scope") -> int:
84 Add a variable to the current action.
87 name (str): _description_: The name of the variable
88 variable_data (Any): _description_: The data of the given variable
89 variable_type (Type): _description_: The type of the data
90 scope (str, optional): _description_: The scope of the variable, defaults to "default_scope"
93 TypeError: _description_: If the type of the variable is incorrect.
96 int: _description_: Returns self.success if it succeeds, self.error otherwise.
98 title =
"add_variable"
100 self.
disp.log_warning(
101 f
"scope {scope} not present, creating", title
105 self.
disp.log_error(f
"Variable: {name} is already present.", title)
107 if isinstance(variable_data, variable_type)
is False:
108 msg = f
"Incorrect type for variable {name}."
109 self.
disp.log_error(msg, title)
112 "data": variable_data,
"type": variable_type
114 msg = f
"Variable: {name} of type {variable_type}"
115 msg += f
" containing {variable_data} successfully added"
116 msg += f
" to scope {scope}."
117 self.
disp.log_debug(msg, title)
120 def update_variable(self, name: str, variable_data: Any, variable_type: Type = str, scope: Any =
"default_scope") -> int:
122 Update the variable from the current action.
125 name (str): _description_: The name of the variable
126 variable_data (Any): _description_: The data of the given variable
127 variable_type (Type): _description_: The type of the data
128 scope (str, optional): _description_: The scope in which the data is stored. Defaults to "default_scope"
131 ScopeError: _description_: If the scope is not found.
132 VariableNotFoundError: _description_: If the variable is not found.
133 TypeError: _description_: If the type of the variable is incorrect
136 int: _description_: Returns self.success if it succeeds, self.error otherwise.
138 title =
"update_variable"
140 msg = f
"Scope {scope} not found."
141 self.
disp.log_error(msg, title)
144 msg = f
"Variable: {name} is not present."
145 self.
disp.log_error(msg, title)
147 if isinstance(variable_data, variable_type)
is False:
148 msg = f
"Incorrect type for variable {name}."
149 self.
disp.log_error(msg, title)
152 msg = f
"Variable: {name} of type {variable_type}"
153 msg += f
" containing {variable_data} successfully added"
154 msg += f
" to scope {scope}."
155 self.
disp.log_debug(msg, title)
158 def insert_or_update(self, name: str, variable_data: Any, variable_type: Type = str, scope: Any =
"default_scope") -> int:
160 Insert or update the variable from the current action.
163 name (str): _description_: The name of the variable
164 variable_data (Any): _description_: The data of the given variable
165 variable_type (Type): _description_: The type of the data
166 scope (str, optional): _description_: The scope in which the data is stored. Defaults to "default_scope"
169 ValueError: _description_: If the variable does not exist.
170 TypeError: _description_: If the type of the variable is incorrect
171 ScopeError: _description_: If the scope is not found.
172 VariableNotFoundError: _description_: If the variable is not found.
175 int: _description_: Returns self.success if it succeeds, self.error otherwise.
177 title =
"insert_or_update"
179 self.
disp.log_warning(
180 "Scope is not present in the list, adding.", title
182 return self.
add_variable(name, variable_data, variable_type, scope)
185 "Variable is already present in the list, updating.", title
189 "Variable is not present in the list, adding.", title
191 return self.
add_variable(name, variable_data, variable_type, scope)
193 def has_variable(self, name: str, scope: Any =
"default_scope") -> bool:
195 Check if the variable exists in the current action.
198 name (str): _description_: The name of the variable
199 scope (str, optional): _description_: The scope in which we wish to search for the variable. Enter '*' to search all the variable scopes. Defaults to "default_scope"
202 ScopeError: _description_: If the scope is not found.
205 bool: _description_: Returns True if the variable exists, False otherwise.
207 title =
"has_variable"
208 self.
disp.log_debug(f
"Checking if variable {name} exists.", title)
213 f
"Variable {name} exists in scope {key}.", title
217 f
"Variable {name} does not exist in any scopes.", title
221 msg = f
"Scope {scope} not found."
222 self.
disp.log_debug(msg, title)
225 self.
disp.log_debug(f
"Variable {name} does not exist.", title)
227 self.
disp.log_debug(f
"Variable {name} exists.", title)
230 def get_variable(self, name: str, scope: Any =
"default_scope") -> Any:
232 Get the variable from the current action.
235 name (str): _description_: The name of the variable
236 scope (str, optional): _description_: The scope in which to get the variable. Defaults to "default_scope".
239 ScopeError: _description_: If the scope is not found.
240 ValueError: _description_: If the variable does not exist.
243 Any: _description_: Returns the variable if it exists, self.error otherwise.
245 title =
"get_variable"
247 msg = f
"Scope {scope} not found."
248 self.
disp.log_error(msg, title)
251 msg = f
"Variable {name} not found."
252 self.
disp.log_error(msg, title)
253 raise ValueError(msg)
254 self.
disp.log_debug(f
"Variable {name} found.", title)
259 Get all the variables from the current action.
262 scope (str, optional): _description_: The scope in which to get the variables. User '*' to return all the vairables from all the scopes. Defaults to "default_scope".
265 ScopeError: _description_: If the scope is not found.
268 Dict[str, Any]: _description_: Returns all the variables.
270 title =
"get_variables"
272 self.
disp.log_debug(
"Returning all the variables.", title)
275 msg = f
"Scope {scope} not found."
276 self.
disp.log_error(msg, title)
280 def get_scope(self, scope: Any =
"default_scope") -> Dict[str, Any]:
282 The function in charge of returning the content of a scope.
283 This function is just there for program logic.
284 This is function behaves the exact same way as the get_variables function.
287 scope (str, optional): _description_: The scope in which to get the variables. User '*' to return all the vairables from all the scopes. Defaults to "default_scope".
290 ScopeError: _description_: If the scope is not found.
293 Dict[str, Any]: _description_: Returns all the variables.
299 Get the type of the variable from the current action.
302 name (str): _description_: The name of the variable
303 scope (str, optional): _description_: The scope in which to get the variable. Defaults to "default_scope".
306 ScopeError: _description_: If the scope is not
307 ValueError: _description_: If the variable does not exist.
310 Type: _description_: Returns the type of the variable if it exists, self.error otherwise.
312 title =
"get_variable_type"
314 msg = f
"Scope {scope} not found."
315 self.
disp.log_error(msg, title)
318 msg = f
"Variable {name} does not exist in scope {scope}."
319 self.
disp.log_error(msg, title)
320 raise ValueError(msg)
321 self.
disp.log_debug(f
"Retrieving type for {name}", title)
326 Remove the variable from the current action.
329 name (str): _description_: The name of the variable
330 scope (str, optional): _description_: The scope in which the variable is stored. Defaults to "default_scope".
333 ScopeError: _description_: If the scope is not found.
334 VariableNotFoundError: _description_: If the variable is not found.
337 int: _description_: Returns self.success if it succeeds, self.error otherwise.
339 title =
"remove_variable"
340 msg = f
"Removing variable {name} from the scope {scope}"
341 msg +=
" if present."
342 self.
disp.log_debug(msg, title)
344 msg = f
"Scope {scope} not found."
345 self.
disp.log_error(msg, title)
348 msg = f
"Variable {name} is not present"
349 msg += f
" in scope {scope}."
350 self.
disp.log_error(msg, title)
352 msg = f
"Removing variable {name} "
353 msg += f
"from the scope {scope}."
354 self.
disp.log_debug(msg, title)
360 Clear all the variables from the current action.
363 scope (str, optional): _description_: The scope in which to clear the variables. Enter '*' to clear the content of all the scopes. Defaults to "default_scope".
366 int: _description_: Returns self.success if it succeeds, self.error otherwise.
368 title =
"clear_variables"
370 f
"Clearing all the variables in scope {scope}.", title
374 self.
disp.log_debug(f
"Clearing content for scope {i}", title)
376 self.
disp.log_debug(
"All the variables have been cleared.", title)
379 msg = f
"Scope {scope} not found."
380 self.
disp.log_error(msg, title)
384 f
"All the variables have been cleared for scope {scope}.", title
390 Clear all the scopes from the current action.
395 title =
"clear_scopes"
396 self.
disp.log_debug(
"Clearing all the scopes.", title)
398 self.
disp.log_debug(
"All the scopes have been cleared.", title)
403 Clear all the scopes content from the current action.
408 title =
"clear_scope_contents"
409 self.
disp.log_debug(
"Clearing all the scopes.", title)
411 self.
disp.log_debug(f
"Clearing content for scope {i}", title)
413 self.
disp.log_debug(
"All the scopes have been cleared.", title)
418 Remove the scope from the current action.
421 scope (str): _description_: The scope to remove.
424 ScopeError: _description_: If the scope is not found.
427 int: _description_: Returns self.success if it succeeds, self.error otherwise.
429 title =
"remove_scope"
430 self.
disp.log_debug(f
"Removing scope {scope}.", title)
432 msg = f
"Scope {scope} not found."
433 self.
disp.log_error(msg, title)
436 self.
disp.log_debug(f
"Scope {scope} removed.", title)
441 Sanitize the data for json serialization.
444 data (Any): _description_: The data to sanitize.
445 use_scope (bool, optional): _description_: Specify if the data to process is to be queried in the variable scopes. Default: False
448 ScopeError: _description_: If the scope is not found.
451 Any: _description_: The sanitized data.
453 title =
"sanitize_for_json"
454 if use_scope
is True:
456 msg = f
"The provided scope {data_or_scope}"
457 msg +=
" was not found in the current dataset."
458 self.
disp.log_debug(msg, title)
460 self.
disp.log_debug(f
"Sanitising scope {data_or_scope}", title)
462 if isinstance(data_or_scope, dict):
463 for key, value
in data_or_scope.items():
465 elif isinstance(data_or_scope, list):
466 for index, item
in enumerate(data_or_scope):
468 elif isinstance(data_or_scope, set):
469 data_or_scope = list(data_or_scope)
470 for index, item
in enumerate(data_or_scope):
472 elif isinstance(data_or_scope, tuple):
473 data_or_scope = list(data_or_scope)
474 for index, item
in enumerate(data_or_scope):
476 data_or_scope = tuple(data_or_scope)
477 elif isinstance(data_or_scope, bytes):
478 data_or_scope = base64.b64encode(data_or_scope).decode(
'utf-8')
479 elif isinstance(data_or_scope, (int, float, str, bool))
is False:
480 data_or_scope = str(data_or_scope)
__init__(self, message="There is a problem with the scope.")
__init__(self, message="The variable was not found.")
Any sanitize_for_json(self, Any data_or_scope, bool use_scope=False)
int clear_variables(self, Any scope="default_scope")
int add_variable(self, str name, Any variable_data, Type variable_type=str, Any scope="default_scope")
int create_scope(self, Any scope_name)
int remove_variable(self, str name, Any scope="default_scope")
Any get_variable(self, str name, Any scope="default_scope")
Dict[str, Any] get_variables(self, Any scope="default_scope")
int update_variable(self, str name, Any variable_data, Type variable_type=str, Any scope="default_scope")
bool has_variable(self, str name, Any scope="default_scope")
int insert_or_update(self, str name, Any variable_data, Type variable_type=str, Any scope="default_scope")
int clear_scope_contents(self)
__init__(self, int success=0, int error=84, bool debug=False)
Dict[str, Any] get_scope(self, Any scope="default_scope")
Union[int, Type] get_variable_type(self, str name, Any scope="default_scope")
int remove_scope(self, Any scope)