Skip to content

Commit d4e56d9

Browse files
add competition season and update time calculation
1 parent cebaacd commit d4e56d9

File tree

2 files changed

+122
-134
lines changed

2 files changed

+122
-134
lines changed

guiType.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,13 @@ def refreshListboxes(n=None): # whenever someone signs in/out or theres a new us
132132
def __addtolistbox(nameIO,select):
133133
try:
134134
with open(opts['pathTime'] + nameIO.replace(' ', '') + '.txt', 'r') as f:
135-
if datetime.strptime(opts['buildStart'],opts['ioForm']) <= datetime.now() <= datetime.strptime(opts['buildLeave'],opts['ioForm']):
136-
timet = min( int(ioServ.calcSeasonTime(nameIO)[0]//3600), 999 )
135+
inSeason = False
136+
timet = 0
137+
for season in opts["seasons"]:
138+
v = ioServ.calcSeasonTime(nameIO, season)
139+
inSeason, timet = v[0],v[1]
140+
timet = min( int(timet//3600), 999 )
141+
if inSeason: break
137142
else:
138143
timet = min( int(ioServ.calcTotalTime(nameIO)//3600), 999 )
139144
timeIO = ' '*max(3-len(str(timet)),0) + str( timet )
@@ -188,22 +193,28 @@ def __addtolistbox(nameIO,select):
188193
nameL.see(select+1)
189194

190195
def hoursToColor(name):
191-
timet,days = ioServ.calcSeasonTime(name)
196+
inSeason = False
197+
timet = 0
198+
days = 0
199+
for season in opts["seasons"]:
200+
inSeason, timet, days = ioServ.calcSeasonTime(name, season)
201+
if inSeason: break
202+
else:
203+
timet, days = ioServ.calcWeekTime(name),0
204+
192205
timet /= 3600
193206

194207
if timet >= 52: # light gray, done with hours
195208
return '#e0e0e0'
196209

197-
if days > 0:
210+
if days > 7:
198211
days -= 7
199212
timet -= days*8/7 # in season
200213

201-
if timet >= 6:
202-
return '#00bf00' # green
203-
elif 2 <= timet < 6:
204-
return '#FFAF00' # yellow orange
205-
else:
206-
return '#FF4444' # red
214+
if timet >= 6.0: return '#00bf00' # green
215+
elif timet >= 2: return '#FFAF00' # yellow orange
216+
else: return '#FF4444' # red
217+
207218
return '#000000'
208219

209220

@@ -287,7 +298,14 @@ def main():
287298
ioF = Frame(root, bg=glblBGC)
288299
iIOB = Button(ioF, text='IN', font=f, bg='green', fg='white', command=lambda:ioSign('i'), width=12, height=2)
289300
oIOB = Button(ioF, text='OUT', font=f, bg='red', fg='white', command=lambda:ioSign('o'), width=12, height=2)
290-
infoT = Label(ioF, text='', font=f, height=5, wraplength=0, justify=CENTER, bg=glblBGC) # white space generator ftw
301+
302+
whatSeason = "Off"
303+
for season in opts["seasons"]:
304+
if datetime.strptime(opts[season+'Start'],opts['ioForm']) <= datetime.now() <= datetime.strptime(opts[season+'Leave'],opts['ioForm']):
305+
whatSeason = season
306+
break
307+
infoT = Label(ioF, text="Currently\nin\n"+whatSeason+"\nSeason", fg="white", font=f, height=5, wraplength=200, justify=CENTER, bg=glblBGC) # white space generator ftw
308+
291309
newB = Button(ioF, text='New User', font=f, bg= 'blue', fg='white', command=makeNewUserWindow, width=12, height=2)
292310

293311
iIOB.pack(pady=8)

ioServ.py

Lines changed: 93 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ def generateDefaultOpts():
3232
autoClockLim : 04:30:00
3333
usernameFile : usernameFile.txt
3434
adminPass : 1234
35-
buildStart : 10:30:00 06.01.2018
36-
buildLeave : 23:59:59 20.02.2018
35+
seasons : Build,Competition
36+
BuildStart : 10:30:00 06.01.2018
37+
BuildLeave : 23:59:59 20.02.2018
38+
CompetitionStart : 00:00:00 21.02.2018
39+
CompetitionLeave : 23:59:59 14.04.2018
3740
posTitle : Student,Mentor,Adult
3841
posJobs : Programmer,Mechanic,Media
3942
@@ -97,7 +100,7 @@ def signIO(n,c):
97100
else:
98101
## NORMAL SIGN IN ##
99102
hrs = 0
100-
if datetime.strptime(opts['buildStart'],opts['ioForm']) <= datetime.now() <= datetime.strptime(opts['buildLeave'],opts['ioForm']):
103+
if datetime.strptime(opts['BuildStart'],opts['ioForm']) <= datetime.now() <= datetime.strptime(opts['BuildLeave'],opts['ioForm']):
101104
hrs = calcSeasonTime(nameIO.replace(' ', ''))[0]
102105
#print('in season, '+nameIO, hrs)
103106
else:
@@ -156,149 +159,116 @@ def findCapitals(s): # for generating usernames
156159

157160

158161
def calcTotalTime(n):
159-
n = n.replace(' ', '')
160-
try:
161-
userFile = open(opts['pathTime'] + n + '.txt', 'r')
162-
linesFromFile = userFile.readlines()
163-
164-
dt = datetime.now()
165-
firstDayOfWeek = dt - timedelta(days=dt.weekday()) - timedelta(days=1)
166-
addCurrentTime = linesFromFile and linesFromFile[-1][0]=='i' and datetime.strptime(linesFromFile[-1][4:].strip(), opts['ioForm']) > firstDayOfWeek
167-
168-
totalTime = 0
169-
lastState = "n"
170-
lastTime = 0
171-
172-
for currentLine in linesFromFile:
173-
currentLine = currentLine.strip()
174-
if currentLine:
175-
time_str = currentLine[4:]
176-
state = currentLine[0]
177-
time = datetime.strptime(time_str, opts['ioForm'])
178-
if state == "i":
179-
lastTime = time
180-
lastState = state
181-
elif state == "o":
182-
if lastState=="i": totalTime += (time - lastTime).total_seconds()
183-
lastTime = time
184-
lastState = state
185-
else:
186-
state = "n"
187-
if lastState == 'i' and addCurrentTime: totalTime += (datetime.now() - lastTime).total_seconds()
188-
189-
userFile.close()
190-
return totalTime
191-
except FileNotFoundError:
192-
#print('User '+n+"'s time file not found.")
193-
return 0
162+
return calcUserTime(n)
194163

195164
def calcWeekTime(n):
196-
n = n.replace(' ','')
197-
try:
198-
userFile = open(opts['pathTime'] + n + '.txt', 'r')
199-
totalTime,lastTime,lastState = 0,0,'n'
200-
linesFromFile = userFile.readlines()
201-
202-
dt = datetime.now()
203-
firstDayOfWeek = dt - timedelta(days=dt.weekday()) - timedelta(days=1)
204-
addCurrentTime = linesFromFile and linesFromFile[-1][0]=='i' and datetime.strptime(linesFromFile[-1][4:].strip(), opts['ioForm']) > firstDayOfWeek
205-
206-
for currentLine in reversed(linesFromFile):
207-
currentLine = currentLine.strip()
208-
if currentLine:
209-
time_str = currentLine[4:]
210-
state = currentLine[0]
211-
time = datetime.strptime(time_str, opts['ioForm'])
212-
if state == "i":
213-
if lastState == "o":
214-
totalTime += (lastTime - time).total_seconds()
215-
lastTime = time
216-
lastState = state
217-
if state == "o":
218-
lastTime = time
219-
lastState = state
220-
else:
221-
state = "n"
222-
lastState = state
165+
dt = datetime.now()
166+
firstDayOfWeek = (dt - timedelta(days=dt.weekday()) - timedelta(days=1)).strftime(opts['ioForm']) # last sunday
167+
lastDayOfWeek = (dt - timedelta(days=dt.weekday()) + timedelta(days=7)).strftime(opts['ioForm']) # next sunday
223168

224-
if currentLine and datetime.strptime(time_str, opts['ioForm']) < firstDayOfWeek: break
169+
return calcUserTime(n, startIO=firstDayOfWeek,endIO=lastDayOfWeek)
225170

226-
if addCurrentTime:
227-
totalTime += (datetime.now() - datetime.strptime(linesFromFile[-1][4:].strip(), opts['ioForm'])).total_seconds()
228-
userFile.close()
229-
return totalTime
230-
except FileNotFoundError:
231-
#print('User '+n+"'s time file not found.")
232-
return 0
233171

234-
def calcSeasonTime(n):
235-
n = n.replace(' ','') # filenames have no spaces
172+
def calcSeasonTime(name,season):
173+
if not (datetime.strptime(opts[season+'Start'],opts['ioForm']) <= datetime.now() <= datetime.strptime(opts[season+'Leave'],opts['ioForm'])):
174+
return False,0,0
236175

237176
currentDate = datetime.now()
238-
buildStart = datetime.strptime(opts['buildStart'], opts['ioForm'])
239-
buildLeave = datetime.strptime(opts['buildLeave'], opts['ioForm'])
177+
buildStart = datetime.strptime(opts[season+'Start'], opts['ioForm'])
178+
buildLeave = datetime.strptime(opts[season+'Leave'], opts['ioForm'])
240179

241180
buildDelta = currentDate-buildStart
242181

243-
weeksSinceStart = max(buildDelta.days,0)
182+
daysSinceStart = max(buildDelta.days,0)
244183

245-
if not ( buildStart <= currentDate <= buildLeave ): return calcWeekTime(n),0 # if not in build season, just give the total hours for this week
184+
totalTime = calcUserTime(name, startIO=opts[season+'Start'],endIO=opts[season+'Leave'])
246185

247-
totalTime = 0
248-
lastTime = 0
249-
lastState = 'n'
250-
251-
try:
252-
with open(opts['pathTime'] + n + '.txt', 'r') as f: userIOAs = f.readlines()
253-
254-
dt = datetime.now()
255-
firstDayOfWeek = dt - timedelta(days=dt.weekday()) - timedelta(days=1)
256-
addCurrentTime = userIOAs and userIOAs[-1][0]=='i' and datetime.strptime(userIOAs[-1][4:].strip(), opts['ioForm']) > firstDayOfWeek
257-
258-
for line in reversed(userIOAs):
259-
line = line.strip()
260-
if line:
261-
time_str = line[4:]
262-
state = line[0]
263-
time = datetime.strptime(time_str, opts['ioForm'])
264-
if state == "i":
265-
if lastState == "o": totalTime += (lastTime - time).total_seconds()
266-
lastTime = time
267-
lastState = state
268-
elif state == "o":
269-
lastTime = time
270-
lastState = state
271-
else:
272-
state = "n"
273-
lastState = state
274-
if datetime.strptime(time_str, opts['ioForm']) < buildStart:
275-
#print(n+"'s done ", totalTime)
276-
break
186+
return True,totalTime,daysSinceStart
277187

278-
if addCurrentTime: totalTime += (currentDate - datetime.strptime(userIOAs[-1][4:].strip(), opts['ioForm'])).total_seconds()
188+
def calcUserTime(name, startIO=None,endIO=None):
189+
filename = opts['pathTime'] + name.strip().replace(" ","") + ".txt" # generate filename
279190

280-
#print(n, totalTime, weeksSinceStart)
281-
return totalTime, weeksSinceStart
191+
# ensure the file exists
192+
try:
193+
open(filename, 'r').close()
282194
except FileNotFoundError:
283-
return 0, weeksSinceStart
195+
print(name+"'s file was not found!")
196+
return 0
284197

285-
def calcTimeframeHours(n, datemax):
198+
# should the times be between specific dates?
199+
checkTimes = False
200+
if startIO!=None and endIO==None: raise ValueError("startIO defined, but endIO not defined")
201+
if startIO==None and endIO!=None: raise ValueError("endIO defined, but startIO not defined")
202+
if startIO!=None and endIO!=None:
203+
checkTimes = True
204+
startIO = datetime.strptime(startIO, opts["ioForm"])
205+
endIO = datetime.strptime(endIO, opts["ioForm"])
206+
207+
208+
currentDate = datetime.now()
209+
lastTime = datetime.now()
210+
lastState = "n"
286211
totalTime = 0
287-
lastTime = 0
288-
lastState = 'n'
289212

290-
try:
291-
pass
292-
except FileNotFoundError:
293-
pass
213+
for line in open(filename, 'r'):
214+
line = line.strip().split(" | ")
215+
if not line: continue # if nothing on line, skip line
216+
217+
state = line[0]
218+
linetime = line[1]
219+
datatime = datetime.strptime(linetime, opts["ioForm"])
220+
221+
if state in "!@": continue # ensure this isnt a double
222+
223+
if checkTimes and datatime < startIO: continue # skip until in timeframe
224+
225+
if state == "i":
226+
pass
227+
elif state == "o":
228+
if lastState=="i": totalTime += (datatime - lastTime).total_seconds()
229+
230+
lastTime = datatime
231+
lastState = state
232+
233+
if checkTimes and datatime > endIO: break # if left timeframe then finish
234+
235+
else: # if finished with no breaks
236+
# add current time if still signed in
237+
if lastState == "i": totalTime += (currentDate - lastTime).total_seconds()
294238

239+
return totalTime
295240

296-
def mkfile(t):
297-
open(t, 'a+').close() # make files if they dont exist
298241

299242

243+
def mkfile(t): open(t, 'a+').close() # make files if they dont exist
244+
300245
def loadUsers():
301246
allusers = {}
302247
for line in open(opts['usernameFile'], 'r+'):
303248
l = line.split(' | ') # name | username | title | jobs
304-
allusers[l[2]] = (allusers[l[2]] or []) + []
249+
allusers[l[2]] = (allusers[l[2]] or []) + []
250+
251+
252+
def calcSlackTimeString():
253+
names = []
254+
times = {}
255+
seasontimes = {}
256+
257+
longestname = 0
258+
259+
for line in open(opts['usernameFile'], "r+"):
260+
name = line.strip().split(" | ")[0]
261+
names += [name]
262+
#print(name)
263+
longestname = max(len(name),longestname)
264+
times[name] = calcTotalTime(name)//3600
265+
seasontimes[name] = calcSeasonTime(name)[0]//3600
266+
267+
topstr = "Name" + " "*(longestname-4) + " - Season - Total\n\n"
268+
269+
for name in names:
270+
totaltime = str(times[name])
271+
seasontime = str(seasontimes[name])
272+
topstr += name + " "*(longestname-len(name)) + " - " + " "*(5-len(seasontime)) + str(seasontimes[name]) + " - " + " "*(5-len(totaltime)) + str(times[name]) + "\n"
273+
274+
return topstr

0 commit comments

Comments
 (0)