-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
56 lines (40 loc) · 1.34 KB
/
logger.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
#!/usr/bin/env python3
import os
import datetime
from functions import get_datetime
from preferences import *
class Log:
def __init__ (self, dirpath: str):
self._content_add = list()
self.start_dt = get_datetime(datetime_format=LOG_DATETIME_FORMAT)
self.path = os.path.join (dirpath, LOG_FILENAME)
if not os.path.exists (self.path):
try:
os.makedirs (dirpath)
except:
pass
self._reset()
def _reset (self):
with open (self.path, 'w', encoding='utf-8') as log:
log.write('pytags log\n')
def add_line (self, line: str):
self._content_add.append (line)
def _make_header (self):
header = [
f'pytags version {VERSION}\n',
f'start datetime: {self.start_dt}\n',
f'stop datetime: {self.stop_dt}\n'
]
return header
def save (self):
"""
Write changes to log
"""
self.stop_dt = get_datetime(datetime_format=LOG_DATETIME_FORMAT)
with open (self.path, 'a', encoding='utf-8') as log:
log.write ('\n')
log.writelines( self._make_header() )
for line in self._content_add:
log.write (f'{line}\n')
# Reset content list
self._content_add = list()