Skip to content

Commit 33fada9

Browse files
Merge pull request #6 from MurdoMaclachlan/dev
Reduce privatisation of Logger attributes
2 parents ef10d9a + 851a034 commit 33fada9

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

smooth_logger/Logger.py

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,20 @@ def __init__(self,
3030
info: int = Categories.ENABLED,
3131
warning: int = Categories.MAXIMUM) -> None:
3232
self.bar: ProgressBar = ProgressBar()
33-
self.__is_empty: bool = True
34-
self.__log: list[LogEntry] = []
35-
self.__notifier: Notification = notification
36-
self.__program_name: str = program_name
37-
self.__scopes: dict[str, int] = {
33+
self.is_empty: bool = True
34+
self.program_name: str = program_name
35+
self._log: list[LogEntry] = []
36+
self._scopes: dict[str, int] = {
3837
"DEBUG": debug, # information for debugging the program
3938
"ERROR": error, # errors the program can recover from
4039
"FATAL": fatal, # errors that mean the program cannot continue
4140
"INFO": info, # general information for the user
4241
"WARNING": warning # things that could cause errors later on
4342
}
43+
self.__notifier: Notification = notification
4444
self.__write_logs = False
45-
self.__output_path: str = (
45+
46+
self._output_path: str = (
4647
self.__define_output_path()
4748
if config_path is None else
4849
f"{config_path}/logs"
@@ -59,17 +60,17 @@ def __create_log_entry(self, message: str, output: bool, scope: str) -> LogEntry
5960
6061
:returns: the created log entry
6162
"""
62-
entry: LogEntry = LogEntry(message, output, scope, self.__get_time())
63-
self.__log.append(entry)
63+
entry: LogEntry = LogEntry(message, output, scope, self._get_time())
64+
self._log.append(entry)
6465
return entry
6566

6667
def __create_log_folder(self) -> None:
6768
"""
6869
Creates the folder that will contain the log files.
6970
"""
70-
if not isdir(self.__output_path):
71-
print(f"Making path: {self.__output_path}")
72-
makedirs(self.__output_path, exist_ok=True)
71+
if not isdir(self._output_path):
72+
print(f"Making path: {self._output_path}")
73+
makedirs(self._output_path, exist_ok=True)
7374

7475
def __define_output_path(self) -> str:
7576
"""
@@ -91,12 +92,12 @@ def __define_output_path(self) -> str:
9192
os: str = "".join(list(platform)[:3])
9293
if os in ["dar", "lin", "win"]:
9394
path: str = (
94-
environ["APPDATA"] + f"\\{self.__program_name}\logs"
95+
environ["APPDATA"] + f"\\{self.program_name}\logs"
9596
if os == "win" else
96-
f"{expanduser('~')}/.config/{self.__program_name}/logs"
97+
f"{expanduser('~')}/.config/{self.program_name}/logs"
9798
)
9899
if not isdir(path):
99-
print(f"INFO: Making path: {path}")
100+
print(f"Making path: {path}")
100101
makedirs(path, exist_ok=True)
101102
return path
102103
else:
@@ -121,14 +122,14 @@ def __display_log_entry(self,
121122
:param is_bar: whether the progress bar is active
122123
:param console: whether the message should be printed to the console
123124
"""
124-
if scope == "NOSCOPE" or (self.__scopes[scope] != Categories.DISABLED and print_to_console):
125+
if scope == "NOSCOPE" or (self._scopes[scope] != Categories.DISABLED and print_to_console):
125126
print(entry.rendered)
126127
if is_bar:
127128
print(self.bar.state, end="\r", flush=True)
128129
if notify:
129130
self.notify(entry.message)
130131

131-
def __get_time(self, method: str = "time") -> str:
132+
def _get_time(self, method: str = "time") -> str:
132133
"""
133134
Gets the current time and parses it to a human-readable format; either
134135
'YYYY-MM-DD HH:MM:SS' or 'YYYY-MM-DD'.
@@ -159,15 +160,15 @@ def add_scope(self, name: str, category: int) -> bool:
159160
160161
:return: a boolean sucess status
161162
"""
162-
if name in self.__scopes.keys():
163+
if name in self._scopes.keys():
163164
self.new(
164165
f"Attempt was made to add new scope with name {name}, but scope with this name "
165166
+ "already exists.",
166167
"WARNING"
167168
)
168169
else:
169170
if category in set(item for item in Categories):
170-
self.__scopes[name] = category
171+
self._scopes[name] = category
171172
return True
172173
else:
173174
self.new(
@@ -181,8 +182,8 @@ def clean(self) -> None:
181182
"""
182183
Empties log array. Any log entries not saved to the output file will be lost.
183184
"""
184-
del self.__log[:]
185-
self.__is_empty = True
185+
del self._log[:]
186+
self.is_empty = True
186187
self.__write_logs = False
187188

188189
def edit_scope(self, name: str, category: int) -> bool:
@@ -195,9 +196,9 @@ def edit_scope(self, name: str, category: int) -> bool:
195196
196197
:returns: a boolean success status
197198
"""
198-
if name in self.__scopes.keys():
199+
if name in self._scopes.keys():
199200
if category in set(item for item in Categories):
200-
self.__scopes[name] = category
201+
self._scopes[name] = category
201202
return True
202203
else:
203204
self.new(
@@ -225,22 +226,22 @@ def get(self, mode: str = "all", scope: str = None) -> Union[list[LogEntry], Log
225226
226227
:returns: a single log entry or list of log entries, or nothing
227228
"""
228-
if self.__is_empty:
229+
if self.is_empty:
229230
pass
230231
elif scope is None:
231-
return (self.__log, self.__log[-1])[mode == "recent"]
232+
return (self._log, self._log[-1])[mode == "recent"]
232233
else:
233234
# return all log entries matching the query
234235
if mode == "all":
235236
data: list[LogEntry] = []
236-
for entry in self.__log:
237+
for entry in self._log:
237238
if scope is None or entry.scope == scope:
238239
data.append(entry)
239240
if data:
240241
return data
241242
# iterate through the log in reverse to find the most recent entry matching the query
242243
elif mode == "recent":
243-
for entry in reversed(self.__log):
244+
for entry in reversed(self._log):
244245
if scope is None or entry.scope == scope:
245246
return entry
246247
else:
@@ -274,9 +275,9 @@ def new(self,
274275
275276
:returns: a boolean success status
276277
"""
277-
if scope in self.__scopes or scope == "NOSCOPE":
278+
if scope in self._scopes or scope == "NOSCOPE":
278279
output: bool = (
279-
(self.__scopes[scope] == Categories.MAXIMUM)
280+
(self._scopes[scope] == Categories.MAXIMUM)
280281
if scope != "NOSCOPE" else
281282
False
282283
)
@@ -291,7 +292,7 @@ def new(self,
291292
self.__display_log_entry(entry, scope, notify, is_bar, print_to_console)
292293

293294
self.__write_logs = self.__write_logs or output
294-
self.__is_empty = False
295+
self.is_empty = False
295296

296297
return True
297298
else:
@@ -304,7 +305,7 @@ def notify(self, message: str) -> None:
304305
305306
:param message: the message to display
306307
"""
307-
self.__notifier.notify(title=self.__program_name, message=message)
308+
self._notifier.notify(title=self.program_name, message=message)
308309

309310
def output(self) -> None:
310311
"""
@@ -314,9 +315,9 @@ def output(self) -> None:
314315
Log files are marked with the date, so each new day, a new file will be created.
315316
"""
316317
if self.__write_logs:
317-
with open(f"{self.__output_path}/log-{self.__get_time(method='date')}.txt",
318+
with open(f"{self._output_path}/log-{self._get_time(method='date')}.txt",
318319
"at+") as log_file:
319-
for line in self.__log:
320+
for line in self._log:
320321
if line.output:
321322
log_file.write(line.rendered + "\n")
322323
self.clean()
@@ -329,8 +330,8 @@ def remove_scope(self, name: str) -> bool:
329330
330331
:returns: a boolean success status
331332
"""
332-
if name in self.__scopes.keys():
333-
del self.__scopes[name]
333+
if name in self._scopes.keys():
334+
del self._scopes[name]
334335
return True
335336
else:
336337
self.new(

0 commit comments

Comments
 (0)