-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimesheetAssistant.py
221 lines (178 loc) · 9.69 KB
/
TimesheetAssistant.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import os
from subprocess import run, PIPE
from msvcrt import getch
import threading
from datetime import datetime, timedelta
from time import sleep
import re
APP_NAME_ASCII_ARTWORK = r"""
_______ _ _ _ _ _ _ __ __
|__ __(_) | | | | /\ (_) | | | | /_ |/_ |
| | _ _ __ ___ ___ ___| |__ ___ ___| |_ / \ ___ ___ _ ___| |_ __ _ _ __ | |_ __ _| | | |
| | | | '_ ` _ \ / _ \/ __| '_ \ / _ \/ _ \ __| / /\ \ / __/ __| / __| __/ _` | '_ \| __| \ \ / / | | |
| | | | | | | | | __/\__ \ | | | __/ __/ |_ / ____ \\__ \__ \ \__ \ || (_| | | | | |_ \ V /| |_| |
|_| |_|_| |_| |_|\___||___/_| |_|\___|\___|\__/_/ \_\___/___/_|___/\__\__,_|_| |_|\__| \_/ |_(_)_|"""
session_active_artwork = """
╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╗╔ ╔═╗╔═╗╔╦╗╦╦ ╦╔═╗
╚═╗║╣ ╚═╗╚═╗║║ ║║║║ ╠═╣║ ║ ║╚╗╔╝║╣
╚═╝╚═╝╚═╝╚═╝╩╚═╝╝╚╝ ╩ ╩╚═╝ ╩ ╩ ╚╝ ╚═╝"""
def cmd(command):
cmd_out = run(command.split(" "), shell=True, stdout=PIPE, stderr=PIPE, timeout=2)
return {"return_code": cmd_out.returncode,
"stdout": cmd_out.stdout.decode('utf-8'),
"stderr": cmd_out.stderr.decode('utf-8')}
def terminate():
print("\n" + "=" * 115)
print("\nThank You for using TimesheetAssistant v1.1 !")
print("If you fou this code useful, please share the GitHub Repository :D")
print("\n" + "=" * 115)
input("\nPress ENTER to Exit...")
exit(0)
def key_wait():
global session_active
while ord(getch()) != 145:
pass
session_active = False
print(APP_NAME_ASCII_ARTWORK, end='\n\n')
print("{:^115s}".format("By Sagar Dev Achar (https://www.github.com/SagarDevAchar/)"))
print("{:^115s}".format("Artworks generated by https://www.textkool.com/"))
print("\n" + "=" * 115, end='\n\n')
TIMESHEET_HEAD = "\nDate,Session Start,Session End,Work Duration,Summary"
folder_name = os.path.realpath(__file__).split("\\")[-2]
if 'timesheet_meta.bin' not in os.listdir():
input("No Timesheet maintained! Press ENTER to Initialize...")
project_name = folder_name
if input("\nDo you want to use the Folder Name as the Project Name [Y / N] : ").strip().upper()[0] == 'N':
project_name = input("Enter the Project Name : ").strip()
print("\nInitializing TimesheetAssistant for Project \"%s\"..." % project_name)
git_support = input("Do you want Git Support [Y / N] : ").strip().upper()[0] == 'Y'
if git_support:
git_check = cmd("git --version")
if git_check['return_code'] == 0:
print(git_check['stdout'].replace("\n", " found"))
git_support = True
TIMESHEET_HEAD += ",Git Commit SHA\n"
git_init = cmd("git init")
if git_init['return_code'] == 0:
print(git_init['stdout'].replace("\n", ""))
open(".gitignore", 'a').close()
print("Please consider setting up Git for this project now...")
else:
print("git was not found on this PC! Please make sure git is installed and setup properly (with PATH)")
terminate()
else:
TIMESHEET_HEAD += "\n"
with open('timesheet_meta.bin', 'w') as TIMESHEET_META:
TIMESHEET_META.write("folder_name={:s}|project_name={:s}|git_support={:s}|work_time={:d}d {:d}h {:d}m"
.format(folder_name,
project_name,
"Enabled" if git_support else "Disabled",
0, 0, 0))
with open('timesheet.csv', 'a') as TIMESHEET:
TIMESHEET.write(TIMESHEET_HEAD)
print("\n" + "=" * 115, end='\n\n')
TIMESHEET_META_DATA = {}
with open('timesheet_meta.bin', 'r') as TIMESHEET_META:
for data in TIMESHEET_META.read().split("|"):
prop = data.split("=")
TIMESHEET_META_DATA[prop[0]] = prop[1]
print("Project : " + TIMESHEET_META_DATA['project_name'])
print("Git Support : " + TIMESHEET_META_DATA['git_support'])
print("Current Work Time : " + TIMESHEET_META_DATA['work_time'])
if 'timesheet.csv' not in os.listdir():
print("\nLooks like the Timesheet has been deleted!")
print("Creating a new Timesheet...")
with open('timesheet.csv', 'a') as TIMESHEET:
TIMESHEET.write(TIMESHEET_HEAD)
if TIMESHEET_META_DATA['git_support'] == "Enabled":
TIMESHEET.write(",Git Commit SHA")
TIMESHEET.write("\n")
while True:
print("\n" + "=" * 115)
while True:
command = input("\nEnter \'Start\' to start timing your work session or \'Quit\' to exit : ").strip()
if command == "Start":
break
elif command == "Quit":
terminate()
else:
print("Wrong Confirmation Command! Input is CASE SENSITIVE")
START_TIME = datetime.now()
print(session_active_artwork)
print("{:^115s}".format("[ Press <Ctrl> + <Alt> + <Q> to stop the session ]"), end='\n\n')
session_active = True
key_thread = threading.Thread(target=key_wait, daemon=True)
key_thread.start()
while session_active:
delta = str(datetime.now() - START_TIME).split(".")[0].split(":")
print("\r{:^115s}".format("{:s} hours {:s} minutes {:s} seconds".format(delta[0], delta[1], delta[2])), end="")
sleep(1)
END_TIME = datetime.now()
print("\n\nSession Completed!\n")
current_session_info = {}
if TIMESHEET_META_DATA['git_support'] == "Enabled":
if input("Do you want to link a Git Commit to your session [Y / N] : ").strip().upper()[0] == 'Y':
sha_input = input("Please enter the SHA1 Hash of the commit [use -r for recent] : ").strip().lower().replace("-r", "")
if sha_input != "":
sha_input = " " + sha_input
commit_info = cmd(r'git log --pretty=format:"%H::%B" -n 1' + sha_input)['stdout'].replace("\"", "").replace("\n", "").split("::")
if len(commit_info) == 2:
current_session_info["sha1"] = commit_info[0]
current_session_info["message"] = commit_info[1]
print("Linked COMMIT %s to current session!" % current_session_info["sha1"].upper(), end='\n\n')
else:
current_session_info["sha1"] = None
current_session_info["message"] = ""
print("Failed to get commit info!", end='\n\n')
else:
current_session_info["sha1"] = None
current_session_info["message"] = ""
else:
current_session_info["sha1"] = None
current_session_info["message"] = ""
if current_session_info['message'] == "":
if input("Do you want to add a message to your session [Y / N] : ").strip().upper()[0] == 'Y':
current_session_info["message"] = input("MESSAGE : ").strip()
print("Added message to current session!")
print("")
current_work_time_seconds = (END_TIME - START_TIME).total_seconds()
previous_time_data = list(map(int, re.findall('[0-9]+', TIMESHEET_META_DATA['work_time'])))
total_work_time_minutes = int(previous_time_data[0] * 1440 + previous_time_data[1] * 60 + previous_time_data[2] +
current_work_time_seconds // 60)
current_work_time = [0, 0]
current_work_time[0] = current_work_time_seconds // 3600
current_work_time_seconds -= current_work_time[0] * 3600
current_work_time[1] = current_work_time_seconds // 60
total_work_time = [0, 0, 0]
total_work_time[0] = total_work_time_minutes // 1440
total_work_time_minutes -= total_work_time[0] * 1440
total_work_time[1] = total_work_time_minutes // 60
total_work_time_minutes -= total_work_time[1] * 60
total_work_time[2] = total_work_time_minutes
while True:
try:
with open('timesheet.csv', 'a') as TIMESHEET:
TIMESHEET.write(START_TIME.strftime("%d/%m/%Y,%H:%M:%S,"))
TIMESHEET.write(END_TIME.strftime("%H:%M:%S,"))
TIMESHEET.write("%dh %dm," % (current_work_time[0], current_work_time[1]))
TIMESHEET.write("\"%s\"" % current_session_info['message'])
if current_session_info['sha1']:
TIMESHEET.write("," + current_session_info['sha1'])
TIMESHEET.write("\n")
print("Current Session Logged in Timesheet!")
break
except Exception:
input("Looks like the Timesheet is open! Please close it and press ENTER to proceed...")
while True:
try:
with open('timesheet_meta.bin', 'w') as TIMESHEET_META:
TIMESHEET_META.write("folder_name={:s}|project_name={:s}|git_support={:s}|work_time={:d}d {:d}h {:d}m"
.format(TIMESHEET_META_DATA['folder_name'],
TIMESHEET_META_DATA['project_name'],
TIMESHEET_META_DATA['git_support'],
total_work_time[0], total_work_time[1], total_work_time[2]))
print("Refreshed the Metadata BIN!")
break
except Exception:
input("Looks like the Metadata File is open! Please close it and press ENTER to proceed...")
terminate()