- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer
More file actions
Latest commit
executable file
·116 lines (95 loc) · 3.3 KB
/
Copy pathtimer
File metadata and controls
executable file
·116 lines (95 loc) · 3.3 KB
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.
importsys
importargparse
importtime
importdatetime
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.
defget_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.")
returnparser.parse_args()
# Returns the file object for timer_log in the given mode as defined by the open
# function.
defopen_time_log(mode):
try:
f=open(".timer.log", mode, encoding="utf-8")
returnf
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.
defwrite(cmd, task):
f=open_time_log("a")
msg=cmd+":"+time.strftime("%Y-%m-%dT%H:%M:%S")
iftask!=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.
defprint_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
iftask!=None:
task=task[0]
else:
all_tasks=True
s1=""
s2=""
forlineinf:
line=line.rstrip()
# Figures out if you have built up a match
ifs1!=""ands2!="":
sum+=get_time(s2) -get_time(s1)
s1=""
s2=""
# Need to immediately go into slotting into s1 / s2
ifline.find("start") ==0and (task==get_task(line) orall_tasks):
s1=line
elifline.find("stop") ==0and (task==get_task(line) orall_tasks):
s2=line
else:
pass
# Clean up the very last set that isn't caught by the for loop.
ifs1!=""ands2!="":
sum+=get_time(s2) -get_time(s1)
print(sum)
# Gets the task associated with a line. If not task, returns None
defget_task(line):
if"\t"inline:
returnline[line.find("\t") +1:]
returnNone
# Pulls out a datetime object from a line in the timer_log
defget_time(line):
if"\t"inline:
line=line[0:line.find("\t")]
line=line[line.find(":") +1:].rstrip()
returndatetime.datetime.strptime(line, "%Y-%m-%dT%H:%M:%S")
defmain():
args=get_args()
cmd=args.CMD[0]
ifcmd=="start":
write(cmd, args.task)
elifcmd=="stop":
write(cmd, args.task)
elifcmd=="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()