-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.py
73 lines (62 loc) · 2.4 KB
/
log.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
import datetime
import os
class Log():
def __init__(self):
self.user = os.getlogin()
self.directory_name = "logs/" + self.user
self.current_date = datetime.datetime.now().strftime("%Y%m%d")
self.current_time = datetime.datetime.now().strftime("%H%M%S")
self.is_open = False
self.file_name = None
self.file = self.open_log()
def create_directory(self):
"""
Creates a new directory to store logs. If a directory with the same name
already exists, the directory is not created.
:return: None
"""
if not os.path.exists(self.directory_name):
os.makedirs(self.directory_name)
def open_log(self):
"""
Open log. Checks if directory is created. If not, it is created. Returns
the log file.
Log is opened with the keyword letter 'w' to overwrite data and only
send small chunks of information.
More info on file opening options available on http://stackoverflow.com
/questions/1466000
/python-open-built-in-function-difference-between-modes-a-a-w-w-and-r
:return: csv file
"""
# Create directory named "logs"
self.create_directory()
# Get current date and user login name to create file
self.file_name = "{}/{}T{}.csv".format(self.directory_name,
self.current_date,
self.current_time)
# Open log and set self.is_open to true
self.file = open(self.file_name, 'w')
self.is_open = True
return self.file
def write_file(self, data):
for item in data:
try:
self.file.write(item + '\n')
except UnicodeEncodeError:
# item is a pyobjc_unicode type. For non-ascii
# characters it is necessary to convert encode into UTF-8 before
# saving them into a log file.
item = item.encode('utf-8')
self.file.write(item + '\n')
def reopen_log(self):
if not self.is_open:
self.file = open(self.file_name, 'w')
self.is_open = True
def close_log(self):
"""
Checks if log is open and then closes the file.
:return: None
"""
if self.is_open:
self.file.close()
self.is_open = False