-
Notifications
You must be signed in to change notification settings - Fork 0
/
tm
executable file
·179 lines (144 loc) · 4.88 KB
/
tm
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
import argparse
import sys
import os
import os.path
import datetime
gDataDir = os.path.expanduser('~') + "/.tm/"
gTasksFile = gDataDir + "tasks.txt"
gLogFile = gDataDir + "log.txt"
class TimeLogEntry():
def __init__( self ):
self.dt = None
self.task = None
class TimeLog():
def __init__( self, logFile ):
self.logFile = logFile
self.logList = []
self.changed = False
def start( self ):
try:
with open( self.logFile, 'r' ) as f:
for line in f:
obj = TimeLogEntry()
splitted = line.strip().split()
obj.dt = datetime.datetime.fromtimestamp( int(splitted[0]) )
obj.task = splitted[1]
self.logList.append( obj )
except KeyboardInterrupt:
pass
def end( self ):
if self.changed:
try:
with open( self.logFile, 'w' ) as f:
for entry in self.logList:
f.write( str( int( entry.dt.timestamp() ) ) + ' ' + entry.task + '\n' )
except KeyboardInterrupt:
pass
def __enter__( self ):
self.start()
return self
def __exit__( self, excType, excValue, traceback ):
self.end()
def AddLogEntry( self, task ):
now = datetime.datetime.now()
entry = TimeLogEntry()
entry.dt = now
entry.task = task.strip()
self.logList.append( entry )
self.changed = True
return now
def GetLastEntry( self ):
return self.logList[-1]
class AmbiguousTerm( Exception ):
pass
class TaskList():
def __init__( self, taskFile ):
self.taskFile = taskFile
self.taskList = []
self.changed = False
def start( self ):
try:
with open( self.taskFile, 'r' ) as f:
self.taskList = f.readlines()
except KeyboardInterrupt:
pass
def end( self ):
if self.changed:
try:
with open( self.taskFile, 'w' ) as f:
f.writelines( self.taskList )
except KeyboardInterrupt:
pass
def __enter__( self ):
self.start()
return self
def __exit__( self, excType, excValue, traceback ):
self.end()
def FindTask( self, term ):
full_task = ''
for task in self.taskList:
if task.lower().startswith( term.lower() ):
if full_task == '':
full_task = task.strip()
else:
raise AmbiguousTerm()
return full_task
########################################
def Init( dataDir ):
if not os.path.isdir( dataDir ):
os.mkdir( dataDir )
print( "Made data directory: " + dataDir )
def Exit( msg='', code=0 ):
if msg != '':
if code == 0:
print( msg )
else:
print( msg, file=sys.stderr )
sys.exit( code )
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument( "cmd", choices=["start", "stop", "status"] )
parser.add_argument( "task", nargs='?' )
args = parser.parse_args()
Init( gDataDir )
with TaskList( gTasksFile ) as task_list, TimeLog( gLogFile ) as time_log:
if args.cmd == "start":
try:
task = task_list.FindTask( args.task )
if task == '':
Exit( 'Task not found. Add new tasks with command "new".', 1 )
except AmbiguousTerm:
Exit( 'Ambiguous.', 2 )
try:
last_entry = time_log.GetLastEntry()
if last_entry.task == task:
Exit( 'Already running.', 3 )
except IndexError:
pass
time_log.AddLogEntry( task )
Exit( 'Started: "' + task + '" (' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ')' )
elif args.cmd == "stop":
last_task = args.cmd
try:
last_entry = time_log.GetLastEntry()
last_task = last_entry.task
except IndexError:
pass
if last_task == args.cmd:
Exit( 'Already stopped.', 4 )
time_log.AddLogEntry( args.cmd )
Exit( 'Stopped (' + datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + ')' )
elif args.cmd == "status":
last_task = "stop"
try:
last_entry = time_log.GetLastEntry()
last_task = last_entry.task
except IndexError:
pass
text = ""
if last_task == "stop":
text = "Stopped"
else:
text = 'Current project: "' + last_task + '"'
Exit( text + " (since " + last_entry.dt.strftime('%Y-%m-%d %H:%M:%S') + ")" )