Coverage for src/settings.py: 82%

49 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-25 10:23 +0000

1"""Settings for the application. 

2This module contains the configuration settings for the application, 

3including database connection details, time intervals, and other parameters. 

4It is designed to be easily modifiable to suit different environments 

5and requirements. 

6""" 

7 

8import datetime 

9from pprint import pformat 

10import logging 

11import traceback 

12import time 

13 

14class Settings: 

15 

16 time_of_compare = 4 # time of compare in hours betwen the last and current state of the database 

17 data_base = 'sqlite:////home/user/.database_lista/web_lista.db' # path to the database 

18 

19 min_interval = 7 # minimum interval for the departure 

20 amount_of_zaprawa = 6.0 # amount of zaprawa m3 

21 names_dry_concret = r'\b(OW|Ow|ow|oW)\b' # regex for dry concret 

22 time_of_end_upload_zaprawa = datetime.time(8, 15) # time of end upload zaprawa 

23 travel_to_the_construction = 30 # time of travel to the construction and back 

24 unloading_time_for_pomp = 2 # time of unloading for pomp 

25 unloading_time_for_crane = 5.2 # 1 time of unloading for crane 

26 wenzels = ( 

27 ("zawod",("zawodzie 2 ", "zawodzie 1 "), 24), 

28 ("odola",("if will be 2 wenz", "odolany 519"), 17), 

29 ("zeran",("if will be 2 wenz", "żerań 502"), 12), 

30 ("gora",("if will be 2 wenz", "góra kalwaria 502"), 6), 

31 ) 

32 

33 def __init__(self): 

34 pass 

35 

36 

37# region logging 

38class PrettyFormatter(logging.Formatter): 

39 def format(self, record): 

40 # if message is a structured data, format it 

41 if isinstance(record.msg, (dict, list, tuple)): 

42 record.msg = pformat(record.msg) 

43 return super().format(record) 

44 

45logger = logging.getLogger(__name__) 

46logger.setLevel(logging.DEBUG) 

47 

48handler = logging.StreamHandler() 

49handler.setFormatter(PrettyFormatter('%(asctime)s - %(levelname)s - %(message)s')) 

50logger.addHandler(handler) 

51 

52lg = logger.debug 

53cr = logger.critical 

54inf = logger.info 

55exp = logger.exception 

56logging.disable(logging.DEBUG) 

57# logging.disable(logging.INFO) 

58# logging.disable(logging.CRITICAL) 

59# logging.disable(logging.EXCEPTION) 

60# endregion 

61 

62def formating_error_message(error, name): 

63 """Format the error message for logging 

64 

65 Args: 

66 error (Exception): error message 

67 

68 Returns: 

69 str: formatted error message 

70 """ 

71 err_type = type(error).__name__ 

72 tb_str = traceback.format_exc() 

73 timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 

74 current_frame = traceback.extract_tb(error.__traceback__)[-1] 

75 function_name = current_frame.name 

76 

77 explain_mistake = f""" 

78 Mistake mame: {name} 

79 Time: {timestamp} 

80 Type of error: {err_type} 

81 Message: {error} 

82 Function name: {function_name} 

83 Traceback: 

84 {tb_str} 

85 """ 

86 

87 return explain_mistake 

88 

89def timer(func): 

90 """ 

91 A decorator that measures and logs the execution time of the wrapped function. 

92 

93 Args: 

94 func (callable): The function to be wrapped and timed. 

95 

96 Returns: 

97 callable: The wrapped function with execution time logging. 

98 

99 Logs: 

100 Logs the execution time of the function in seconds using the `inf` logger 

101 """ 

102 def wrapper(*args, **kwargs): 

103 """ 

104 A decorator function that measures the execution time of the wrapped function. 

105 

106 Args: 

107 *args: Positional arguments to be passed to the wrapped function. 

108 **kwargs: Keyword arguments to be passed to the wrapped function. 

109 

110 Returns: 

111 The result of the wrapped function. 

112 

113 Logs: 

114 Logs the execution time of the wrapped function in seconds. 

115 """ 

116 start_time = time.time() 

117 result = func(*args, **kwargs) 

118 end_time = time.time() 

119 inf(f"Function {func.__name__} executed for {end_time - start_time} seconds") 

120 return result 

121 return wrapper