BonPrinter v1.2.0
Thermal Printer tool
Loading...
Searching...
No Matches
Source.Model.config Namespace Reference

Classes

class  Config
 Class Configuration: Read/Write configuration data (user or articles) More...

Functions

float get_dict_float (dict[str, dict[str, str]] d_dict, str s_section, str s_key)
 Get floating value in dictionary.
int get_dict_int (dict[str, dict[str, str]] d_dict, str s_section, str s_key)
 Get integer value in dictionary.
str get_dict_value (dict[str, dict[str, str]] d_dict, str s_section, str s_key)
 Get value from dictionary.
bool is_float (Any value)
 Check if string can convert to float.
bool is_int (Any value)
 Check if string can convert to integer.
dict[str, dict[str, str|list[str]]] read_data_from_config_file (str s_file_name)
 Read data from configuration file.
dict[str, Any] sort_dict (dict[str, Any] d_dict_to_sort, dict[str, Any] d_order_reference)
 Sort dictionary.
None write_config_to_file (str s_file_name, dict[str, Any] d_dict)
 Write configuration to file.

Variables

 log = logging.getLogger(__title__)
str S_ITEM_FILE = "articles.ini"
str S_ITEM_TEMP_FILE = "_temp_articles.ini"
str S_USER_FILE = "user.ini"
str S_USER_TEMP_FILE = "_temp_user.ini"

Detailed Description


Function Documentation

◆ get_dict_float()

float get_dict_float ( dict[str, dict[str, str]] d_dict,
str s_section,
str s_key )

Get floating value in dictionary.

Parameters
d_dict: dictionary
s_section: section
s_key: key
Returns
get floating value from dictionary. 0 if not present

Definition at line 62 of file config.py.

62def get_dict_float(d_dict: dict[str, dict[str, str]], s_section: str, s_key: str) -> float:
63 """!
64 @brief Get floating value in dictionary.
65 @param d_dict : dictionary
66 @param s_section : section
67 @param s_key : key
68 @return get floating value from dictionary. 0 if not present
69 """
70 f_float = 0.0
71 if s_section in d_dict:
72 value = d_dict[s_section].get(s_key)
73 if value is not None:
74 if is_float(value):
75 f_float = float(value)
76 return f_float
77
78

◆ get_dict_int()

int get_dict_int ( dict[str, dict[str, str]] d_dict,
str s_section,
str s_key )

Get integer value in dictionary.

Parameters
d_dict: dictionary
s_section: section
s_key: key
Returns
get integer value from dictionary. 0 if not present

Definition at line 79 of file config.py.

79def get_dict_int(d_dict: dict[str, dict[str, str]], s_section: str, s_key: str) -> int:
80 """!
81 @brief Get integer value in dictionary.
82 @param d_dict : dictionary
83 @param s_section : section
84 @param s_key : key
85 @return get integer value from dictionary. 0 if not present
86 """
87 i_int = 0
88 if s_section in d_dict:
89 value = d_dict[s_section].get(s_key)
90 if value is not None:
91 if is_int(value):
92 i_int = int(value)
93 return i_int
94
95

◆ get_dict_value()

str get_dict_value ( dict[str, dict[str, str]] d_dict,
str s_section,
str s_key )

Get value from dictionary.

Parameters
d_dict: dictionary
s_section: section
s_key: key
Returns
get integer value from dictionary. Nothing if not present

Definition at line 96 of file config.py.

96def get_dict_value(d_dict: dict[str, dict[str, str]], s_section: str, s_key: str) -> str:
97 """!
98 @brief Get value from dictionary.
99 @param d_dict : dictionary
100 @param s_section : section
101 @param s_key : key
102 @return get integer value from dictionary. Nothing if not present
103 """
104 if s_section in d_dict and s_key in d_dict[s_section]:
105 value = str(d_dict[s_section][s_key])
106 else:
107 value = ""
108 return value
109
110

◆ is_float()

bool is_float ( Any value)

Check if string can convert to float.

Parameters
value: value to check
Returns
status if string can convert to float

Definition at line 34 of file config.py.

34def is_float(value: Any) -> bool:
35 """!
36 @brief Check if string can convert to float.
37 @param value : value to check
38 @return status if string can convert to float
39 """
40 try:
41 float(value)
42 b_float = True
43 except (TypeError, ValueError):
44 b_float = False
45 return b_float
46
47

◆ is_int()

bool is_int ( Any value)

Check if string can convert to integer.

Parameters
value: value to check
Returns
status if string can convert to integer

Definition at line 48 of file config.py.

48def is_int(value: Any) -> bool:
49 """!
50 @brief Check if string can convert to integer.
51 @param value : value to check
52 @return status if string can convert to integer
53 """
54 try:
55 int(value)
56 b_int = True
57 except (TypeError, ValueError):
58 b_int = False
59 return b_int
60
61

◆ read_data_from_config_file()

dict[str, dict[str, str | list[str]]] read_data_from_config_file ( str s_file_name)

Read data from configuration file.

Parameters
s_file_name: file to read
Returns
return configuration of the file

Definition at line 484 of file config.py.

484def read_data_from_config_file(s_file_name: str) -> dict[str, dict[str, str | list[str]]]:
485 """!
486 @brief Read data from configuration file.
487 @param s_file_name : file to read
488 @return return configuration of the file
489 """
490 d_data: dict[str, dict[str, str | list[str]]] = {}
491 parser = ConfigParser()
492 with codecs.open(s_file_name, mode="r", encoding="utf-8") as file:
493 parser.read_file(file)
494 for s_section in parser.sections():
495 d_data[s_section] = {}
496 for key, value in parser.items(s_section):
497 b_list_key = False
498 if b_list_key and value.startswith('[') and value.endswith(']'): # read UID as list if possible
499 s_entries = value[1:-1].split(", ")
500 l_entries = [s_char.strip('\'\"') for s_char in s_entries]
501 d_data[s_section][key] = l_entries
502 else:
503 d_data[s_section][key] = value
504 return d_data
505
506

◆ sort_dict()

dict[str, Any] sort_dict ( dict[str, Any] d_dict_to_sort,
dict[str, Any] d_order_reference )

Sort dictionary.

Parameters
d_dict_to_sort: dictionary to sort
d_order_reference: reference dictionary to sort in this order
Returns
return sorted dictionary

Definition at line 453 of file config.py.

453def sort_dict(d_dict_to_sort: dict[str, Any], d_order_reference: dict[str, Any]) -> dict[str, Any]:
454 """!
455 @brief Sort dictionary.
456 @param d_dict_to_sort : dictionary to sort
457 @param d_order_reference : reference dictionary to sort in this order
458 @return return sorted dictionary
459 """
460 sorted_dict = OrderedDict()
461 unsorted_dict = OrderedDict()
462
463 for key, _order in d_order_reference.items():
464 if key in d_dict_to_sort:
465 value = d_dict_to_sort[key]
466
467 if isinstance(value, dict):
468 sorted_dict[key] = sort_dict(value, d_order_reference[key])
469 else:
470 sorted_dict[key] = value
471
472 # insert unsorted values at the end
473 for key, value in d_dict_to_sort.items():
474 if key not in d_order_reference:
475 if isinstance(value, dict):
476 unsorted_dict[key] = sort_dict(value, {})
477 else:
478 unsorted_dict[key] = value
479
480 sorted_dict.update(unsorted_dict)
481 return sorted_dict
482
483

◆ write_config_to_file()

None write_config_to_file ( str s_file_name,
dict[str, Any] d_dict )

Write configuration to file.

Parameters
s_file_name: write to this file
d_dict: dictionary to write

Definition at line 507 of file config.py.

507def write_config_to_file(s_file_name: str, d_dict: dict[str, Any]) -> None:
508 """!
509 @brief Write configuration to file.
510 @param s_file_name : write to this file
511 @param d_dict : dictionary to write
512 """
513 parser = cfg.ConfigParser()
514 for s_ini_section in d_dict:
515 parser.add_section(s_ini_section)
516 for s_ini_key in d_dict[s_ini_section]:
517 s_ini_value = d_dict[s_ini_section][s_ini_key]
518 if isinstance(s_ini_value, list):
519 s_ini_value = ', '.join(f'"{item}"' for item in s_ini_value)
520 s_ini_value = f"[{s_ini_value}]"
521 parser.set(s_ini_section, s_ini_key, s_ini_value)
522 with open(s_file_name, mode="w", encoding="utf-8") as o_cfg_file:
523 parser.write(o_cfg_file)

Variable Documentation

◆ log

log = logging.getLogger(__title__)

Definition at line 31 of file config.py.

◆ S_ITEM_FILE

str S_ITEM_FILE = "articles.ini"

Definition at line 26 of file config.py.

◆ S_ITEM_TEMP_FILE

str S_ITEM_TEMP_FILE = "_temp_articles.ini"

Definition at line 27 of file config.py.

◆ S_USER_FILE

str S_USER_FILE = "user.ini"

Definition at line 28 of file config.py.

◆ S_USER_TEMP_FILE

str S_USER_TEMP_FILE = "_temp_user.ini"

Definition at line 29 of file config.py.