-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer
executable file
·116 lines (95 loc) · 3.3 KB
/
timer
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
#!/usr/bin/env python3
# Developed by Joshua Gutow. Copyright 2016 under the MIT License.
import sys
import argparse
import time
import datetime
desc = '''This program helps you track the time you spend on something.
The file .timer.log in the current directory is used to track the time spent.
The timer log stores the info in the following format: "[start/stop]:date\ttask"
Available commands:
start - log the current time as the start time.
stop - log the current time as the end time.
total - print out how much time has been spent.'''
# Sets up the arguments for the program.
def get_args():
parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("CMD", nargs=1, help="Command to run")
parser.add_argument("-t", "--task", nargs=1,
help="Specify the task that the time was spent on.")
return parser.parse_args()
# Returns the file object for timer_log in the given mode as defined by the open
# function.
def open_time_log(mode):
try:
f = open(".timer.log", mode, encoding="utf-8")
return f
except:
print("Couldn't open the timer log file")
sys.exit(1)
# Writes the cmd, task, and current time to the file as described in the desc.
def write(cmd, task):
f = open_time_log("a")
msg = cmd + ":" + time.strftime("%Y-%m-%dT%H:%M:%S")
if task != None:
msg += "\t" + task[0]
msg += "\n"
f.write(msg)
f.close
# Prints the total time spent on a task. A task of type None returns the total
# time spent on all tasks.
def print_total(task):
sum = datetime.timedelta()
f = open_time_log("r")
# Sets up the system that figures out how to sort by task.
all_tasks = False
if task != None:
task = task[0]
else:
all_tasks = True
s1 = ""
s2 = ""
for line in f:
line = line.rstrip()
# Figures out if you have built up a match
if s1 != "" and s2 != "":
sum += get_time(s2) - get_time(s1)
s1 = ""
s2 = ""
# Need to immediately go into slotting into s1 / s2
if line.find("start") == 0 and (task == get_task(line) or all_tasks):
s1 = line
elif line.find("stop") == 0 and (task == get_task(line) or all_tasks):
s2 = line
else:
pass
# Clean up the very last set that isn't caught by the for loop.
if s1 != "" and s2 != "":
sum += get_time(s2) - get_time(s1)
print(sum)
# Gets the task associated with a line. If not task, returns None
def get_task(line):
if "\t" in line:
return line[line.find("\t") + 1:]
return None
# Pulls out a datetime object from a line in the timer_log
def get_time(line):
if "\t" in line:
line = line[0:line.find("\t")]
line = line[line.find(":") + 1:].rstrip()
return datetime.datetime.strptime(line, "%Y-%m-%dT%H:%M:%S")
def main():
args = get_args()
cmd = args.CMD[0]
if cmd == "start":
write(cmd, args.task)
elif cmd == "stop":
write(cmd, args.task)
elif cmd == "total":
print_total(args.task)
else:
print("Must pass a valid command. Refer to the help if needed.")
sys.exit(1)
if __name__ == '__main__':
main()