-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
179 lines (136 loc) · 5.69 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import logging
import configparser
import datetime
import pathlib
import os
# The path to the IndicoWp main folder, that contains all the modules
PATH = os.path.dirname(os.path.realpath(__file__))
VERSION = '0.1.0.14'
PROJECT_PATH = ''
class Config:
"""
The static class, which acts as access point to the config parser object of this project.
The config file read is the 'config.ini' in the main project folder.
:cvar _instance: The static field containing the actual instance of the config parser
:cvar _path: The pathlib.Path object pointing to the path of the config file
"""
_instance = None
_path = None
@staticmethod
def get_instance():
"""
Returns the instance of the config file, that is saved in the singleton.
:return: The configparser.ConfigParser object for this projects config file
"""
if Config._instance is None:
Config._create_instance()
return Config._instance
@staticmethod
def _create_instance():
"""
Actually creates the config parser upon first call of the get_instance method by reading the 'config.ini' file
in the project folder.
:return: void
"""
Config._path = pathlib.Path(PROJECT_PATH) / 'config.ini'
Config._instance = configparser.ConfigParser()
Config._instance.read(str(Config._path))
class LoggingController:
"""
Handles the log file management.
Log files are being created for each day, with the date being part of the name of the log file.
This class creates a new log file if there is none and init's the logging Logger for the file.
Writes additional information into the log files at the beginning and end of a session.
:cvar DATE_FORMAT: The datetime format string for dates only, used for the file names
:cvar DATETIME_FORMAT: The datetime format string for date & time, used inside the log files
:ivar logging_folder: The string name of the sub folder in which the logs are stored
:ivar log_name: The name of the log file for the current day
:ivar path: The pathlib.Path object to the folder in which the logs are located
"""
DATE_FORMAT = '%Y_%m_%d'
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S'
BASE_NAME = 'indico'
def __init__(self):
# The name of the folder which will contain the log files
self.config = Config.get_instance()
self.logging_folder = self.config['LOGGING']['folder']
self.log_name = self._create_log_name()
# The path object pointing to the folder with the logs
self.path = pathlib.Path(PROJECT_PATH) / self.logging_folder / self.log_name
@property
def path_string(self):
"""
Returns the string path to the currently handled log file
:return: string
"""
return str(self.path)
def init(self):
"""
To be called before there is any attempt at writing log files.
This creates a log file if there is none and also fixes the logging.logger on that file.
:return: void
"""
self._prepare_log_file()
def close(self):
"""
Writes a closing sentence into the log file.
:return: void
"""
self._post_process_log_file()
def _prepare_log_file(self):
"""
If there is no log file yet creates new one and appends a line, that states at which point a new session was
started. Also inits the logging.BasicLogger on the newly created file.
:return: void
"""
datetime_string = self._current_datetime_string()
line_string = '\n\nSTARTING NEW LOGGING SESSION @ "{}"\n\n'.format(datetime_string)
file_mode = 'a'
with self.path.open(mode=file_mode) as file:
file.write(line_string)
file_path = str(self.path)
logging.basicConfig(
level=logging.INFO,
filename=file_path,
filemode='a',
format='%(asctime)s %(name)-40s %(levelname)-8s %(message)s'
)
def _post_process_log_file(self):
"""
Writes a new line into the log file, that signals at which point the session ended
:return: void
"""
datetime_string = self._current_datetime_string()
line_string = '\n\nCLOSING SESSION @ "{}"\n\n'.format(datetime_string)
with self.path.open(mode='a') as file:
file.write(line_string)
def _create_log_name(self):
"""
Creates the log name based on the BASE_NAME class variable and the current date.
:return: The string name for the log file
"""
# Creating the date string from the current date time object
date_string = self._current_date_string()
log_name = '{}_{}'.format(
self.BASE_NAME,
date_string
)
return log_name
def _current_datetime_string(self):
"""
Creates a datetime object for the current time of the method execution and formats the string based on the
format, that is given as the DATETIME_FORMAT class variable
:return: The string of the current date & time
"""
datetime_object = datetime.datetime.now()
datetime_string = datetime_object.strftime(self.DATETIME_FORMAT)
return datetime_string
def _current_date_string(self):
"""
Creates a datetime object for the current moment of the method being executed and formats the string based on
the format, that is given as the DATE_FORMAT class variable
:return: A string for the current date
"""
datetime_object = datetime.datetime.now()
datetime_string = datetime_object.strftime(self.DATE_FORMAT)
return datetime_string