-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime-log.py
223 lines (203 loc) · 8.51 KB
/
time-log.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
222
223
from datetime import date
import datetime, time
import os, sys, subprocess
class time_log:
def __init__(self):
self.FILENAME = "time-log.txt"
self.FILE = os.path.join(os.getcwd(), self.FILENAME)
ct = datetime.datetime.now()
self.CUR_TIME = ct.strftime("%H:%M:%S")
today = date.today()
self.CUR_DATE = today.strftime("%m/%d/%Y")
def capture_time(self):
print("""
___________.___ _____ ___________ _________ .____ ________ _________ ____ __.
\__ ___/| | / \ \_ _____/ \_ ___ \| | \_____ \ \_ ___ \| |/ _|
| | | |/ \ / \ | __)_ ______ / \ \/| | / | \/ \ \/| <
| | | / Y \| \ /_____/ \ \___| |___/ | \ \___| | \
|____| |___\____|__ /_______ / \______ /_______ \_______ /\______ /____|__ \
\/ \/ \/ \/ \/ \/ \/
______________.___. _______________________________ _____
/ _____/\__ | |/ _____/\__ ___/\_ _____/ / \
\_____ \ / | |\_____ \ | | | __)_ / \ / \
/ \ \____ |/ \ | | | \/ Y \
/_______ / / ______/_______ / |____| /_______ /\____|__ /
\/ \/ \/ \/ \/
""")
if sys.platform == 'linux' or sys.platform == 'linux2':
#os.system('clear')
print("\n"*3,"-----------------------------------------------------",
"\n[X] - OS ::",sys.platform)
elif sys.platform == 'win32':
#os.system("cls")
print("\n"*3,"-----------------------------------------------------",
"\n[X] - OS ::",sys.platform)
print("\n[X] - default folder path ::", self.FILE)
print("[X] - default text editor :: Sublime-Text3")
try:
with open(self.FILE) as file:
self.lastline = file.readlines()[-1]
print("\n[X] - LAST EVENT ::",self.lastline)
except IndexError:
print("\n[X] - NEW FILE NO ENTRIES")
except FileNotFoundError:
print("\n[X] - NO FILE FOUND")
# captures current time and input action
print(f"-----------------------------------------------------",
f'\nDATE = {self.CUR_DATE}\nTIME = {self.CUR_TIME}\n',
f"-----------------------------------------------------")
self.OPTION = input("CHOOSE AN OPTION [IN/OUT/SHOW/OPEN/EXIT]: ")
self.OPTION = self.OPTION.upper()
def clockr(self):
# clock in status: YOUR CLOCKED IN [OR] YOUR CLOCKED OUT
# add comments option
try:
if self.OPTION == "OUT":
ct = datetime.datetime.now()
self.CUR_TIME = ct.strftime("%H:%M:%S")
today = date.today()
self.CUR_DATE = today.strftime("%m/%d/%Y")
# get clocked in data
last_clocked = self.lastline.split("@")[0]
last_date = self.lastline.split("-:-")[1]
last_time = self.lastline.split("-:-")[2]
last_time_iso = last_time.split(":")
# gets last time. hour and minute into individual variables
last_hour = int(last_time_iso[0])
last_minute = int(last_time_iso[1])
# converts last time into decimal notation
last_perc_time = last_minute*(1/60)+last_hour
#second = time_calc[2]
# gets clocked out current time. hour and minute into individual variables
cur_time_iso = self.CUR_TIME.split(":")
cur_hour = int(cur_time_iso[0])
cur_minute = int(cur_time_iso[1])
# converts current time into decimal notation
cur_perc_time = cur_minute*(1/60)+cur_hour
print(f"-LAST CLOCKED ({last_clocked}) AT:\n{last_date}\n{last_time}\n",
f"-----------------------------------------------------")
if last_perc_time > cur_perc_time:
night_hour = 24 - last_perc_time
tot_hourmin = night_hour + cur_perc_time
elif last_perc_time < cur_perc_time:
tot_hourmin = cur_perc_time - last_perc_time
elif last_perc_time == cur_perc_time:
tot_hourmin = cur_perc_time - last_perc_time
with open(self.FILE, "a") as file:
file.write(f'{self.OPTION}@-:-{self.CUR_DATE}-:-{self.CUR_TIME}-:-{tot_hourmin}\n')
print(f"TOTAL HOURS = {tot_hourmin}\nYOU HAVE BEEN CLOCKED **{self.OPTION}**\n",
f"-----------------------------------------------------")
input("RETURN TO MENU - [ENTER]")
time_log()
except (IndexError, AttributeError):
print("you have no clockins or the log file has been incorrectly altered")
input("RETURN TO MENU - [ENTER]")
time_log()
try:
# check if last line is "IN"
if self.OPTION == 'IN':
ct = datetime.datetime.now()
self.CUR_TIME = ct.strftime("%H:%M:%S")
today = date.today()
self.CUR_DATE = today.strftime("%m/%d/%Y")
#print(self.OPTION)
last_clocked = self.lastline.split("@")[0]
last_date = self.lastline.split("-:-")[1]
last_time = self.lastline.split("-:-")[2]
print(f"-LAST CLOCKED ({last_clocked}) AT:\n{last_date}\n{last_time}\n")
with open(self.FILE, "a") as file:
file.write(f'{self.OPTION}@-:-{self.CUR_DATE}-:-{self.CUR_TIME}\n')
print(f"-----------------------------------------------------",
f"\nYOU HAVE BEEN CLOCKED **{self.OPTION}**\n",
f"-----------------------------------------------------")
input("RETURN - [ENTER]")
time_log()
except (IndexError, AttributeError):
print(f"-----------------------------------------------------",
f"\n[X] - FIRST CLOCK IN EVENT")
with open(self.FILE, "a") as file:
file.write(f'{self.OPTION}@-:-{self.CUR_DATE}-:-{self.CUR_TIME}\n')
print(f"YOU HAVE BEEN CLOCKED **{self.OPTION}**",
f"\n-----------------------------------------------------")
input("RETURN TO MENU - [ENTER]")
time_log()
def redun_chk(self):
# redundancy checks for no file, input is correct, if already clocked in
if self.OPTION == "EXIT":
print("EXITING PROGRAM")
sys.exit()
try:
# check log file if clocked in/out
with open(self.FILE) as file:
self.lastline = file.readlines()[-1]
if self.OPTION == "SHOW":
with open(self.FILE, 'r') as file:
read_file = file.read(1)
if not read_file:
print("NO RECORDS FOUND")
pass
print(file.read())
input("RETURN TO MENU - [ENTER]")
time_log()
# NO EXCEPTION BUG creates clock in record with show
elif self.OPTION == "OPEN":
if sys.platform == 'linux' or sys.platform == 'linux2':
os.system("subl time-log.txt")
try:
if sys.platform == 'win32':
if os.system("sublime time-log.txt") != 0:
raise Exception("sublime is not installed")
except:
os.system("notepad time-log.txt")
input("RETURN TO MENU - [ENTER]")
time_log()
elif self.OPTION != "IN" and self.OPTION != "OUT":
print("invalid choice")
input("RETURN TO MENU - [ENTER]")
time_log()
# if try to clock in again, give error message: ALREADY CLOCKED IN
elif self.OPTION in self.lastline:
print(f"-----------------------------------------------------",
f"\nALREADY CLOCKED {self.OPTION}")
last_clocked = self.lastline.split("@")[0]
last_date = self.lastline.split("-:-")[1]
last_time = self.lastline.split("-:-")[2]
print(f"-LAST CLOCKED ({last_clocked}) AT:\n{last_date}\n{last_time}\n",
f"-----------------------------------------------------",)
input("RETURN TO MENU - [ENTER]")
time_log()
# if clock request mismatched with log then do successfully clock requested action
elif self.OPTION not in self.lastline:
ct = datetime.datetime.now()
self.CUR_TIME = ct.strftime("%H:%M:%S")
today = date.today()
self.CUR_DATE = today.strftime("%m/%d/%Y")
print(f"-----------------------------------------------------",
f"\nCLOCKING ({self.OPTION}) AT: \nDATE = {self.CUR_DATE}\nTIME = {self.CUR_TIME}\n")
tl.clockr()
# if no clock in send meessage: FIRST CLOCK IN MESSAGE RECORDED
else:
print("UNKNOW ERROR")
input("RETURN TO MENU - [ENTER]")
time_log()
except FileNotFoundError:
if self.FILENAME not in os.listdir():
print(f"-----------------------------------------------------",
f"\n[X] - NO FILE FOUND [{self.FILENAME}]")
fopen = open(self.FILE, 'w')
fopen.write(f'')
fopen.close()
print(f"[X] - CREATING NEW FILE",
f"\n-----------------------------------------------------\n")
input("RETURN TO MENU - [ENTER]")
except IndexError:
tl.clockr()
if __name__ == "__main__":
while True:
tl = time_log()
tl.capture_time()
try:
tl.redun_chk()
except IndexError:
# check if empty log file control flow ends here
tl.clockr()