-
Notifications
You must be signed in to change notification settings - Fork 0
/
info_logger.py
49 lines (43 loc) · 1.66 KB
/
info_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
import csv
import conf
import os
class info_logger:
def __init__(self):
self.dictkeys = conf.loaded['logger']['dictkeys']
self.dictlist = []
self.logfile = conf.loaded['logger']['logfile']
writelater = self.check_exists()
self.file = open(self.logfile,"w")
self.csvwriter = csv.DictWriter(self.file,self.dictkeys)
self.csvwriter.writeheader()
self.workingdict = dict(zip(self.dictkeys,[None]*10))
self.id = 0
if writelater:
for row in self.dictlist:
self.workingdict = row
self.writeout()
try:
self.id = self.dictlist[-1]['id']
except IndexError:
print("CSV not written properly in previous run.")
def check_exists(self):
if os.path.exists(self.logfile):
with open(self.logfile) as f:
csvreader = csv.DictReader(f)
for row in csvreader:
self.dictlist.append(row)
return True
def get_id(self):
return self.id
def add_value(self,id,key,value):
if key not in self.dictkeys or id == None:
return
if self.workingdict['id'] != id and self.workingdict['id'] != None:
self.writeout()
self.workingdict[key] = value
def writeout(self):
if self.workingdict['id'] != None:
self.csvwriter.writerow(self.workingdict)
self.workingdict = dict(zip(self.dictkeys,[None]*10))
self.file.flush()
os.fsync(self.file.fileno())