-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathPlainTextDriver.py
64 lines (53 loc) · 2.26 KB
/
PlainTextDriver.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
#
# PlainTextDriver.py
# Class for reading MacTimeLog log
#
# Copyright 2009 Artem Yunusov. All rights reserved.
#
from __future__ import with_statement
import os
import datetime
class PlainTextDriver(object):
def __init__(self, logPath, dateFormat, dateLength, projectSeparator):
self._dateFormat = str(dateFormat)
self._dateLength = dateLength
self._projectSeparator = projectSeparator
self._logPath = logPath
if not os.path.exists(logPath):
with open(logPath, "w") as log:
pass
self._log = ""
def _readLog(self):
with open(self._logPath) as log:
self._log = log.read().decode("utf8")
def writeTask(self, date, task, projectName):
"""Write task"""
if projectName:
data = "%s %s %s %s" % (date.strftime(self._dateFormat), projectName, self._projectSeparator, task)
else:
data = "%s %s" % (date.strftime(self._dateFormat), task)
with open(self._logPath, "a") as log:
log.write(data.encode( "utf-8" ) + "\n")
def getByRange(self, startDate, endDate):
"""Return day log"""
self._readLog()
lines = self._log.strip().split("\n")
buff = []
for line in lines[::-1]:
if line.strip().startswith("#"):
continue
if line.strip() and len(line) >= self._dateLength:
date = datetime.datetime.strptime(line.strip()[0:self._dateLength], self._dateFormat)
if date >= startDate and date <= endDate:
date = line[0:self._dateLength]
right = line[self._dateLength + 1:].strip()
prjIndex = right.find(self._projectSeparator)
if prjIndex == -1:
projectName = ""
taskName = right.strip()
else:
projectName = right[:prjIndex].strip()
taskName = right[prjIndex + len(self._projectSeparator):].strip()
buff.append((datetime.datetime.strptime(date, self._dateFormat), taskName, projectName))
buff.reverse()
return buff