summaryrefslogtreecommitdiff
path: root/aslclass.py
blob: 31fda2ad603cd22640fd960dbb7fecc749ae3f34 (plain)
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
#!/git/aslclass.py
# Copyright 2013 Aditya Naik
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0

# ---Version 2.3.0---

# imports
import sys
import os
import msvcrt
from timeit import timeit as time
from time import clock as clock
from paths import listpaths
import re

class ASLreport:
	
	def __init__(self):
		self.shotcount=0
		self.pauseinterval=[]
		self.shotlist=[]
		self.totaltime=0
	
	# begin
	def metadata(self):
		load=input("Do you want to start a new state? (y/n +enter) (n=load state)\n")
		# load a file or start a new state?
		if load=='y': pass
		elif load=='n': self.loader()
		
		self.name=input("\nEnter movie name:\n")
		
		print("press enter to start")
		
		keystroke=str(msvcrt.getch())[-2]
		if keystroke=="r":
			print("counter begins")
			self.key()
		else: print("Unknown keystroke ")

	def loader(self):
		pathlist=os.listdir(".")
		print(pathlist)
		
		serial=input("Enter the serial number of the savefile (starts from 0)\n")
		# if the filetype does not have the required extension
		if listpaths('.')[int(serial)][-9:len(listpaths('.')[int(serial)])]!="savestate": 
			sys.exit("unknown filetype")
		loader=open(listpaths('.')[int(serial)], mode="r").read()
		
		# loading algorithm: search for required values with regex
		data=re.search(r"(?P<MovieName>.+)\n(?P<TotalTime>.+)\n(?P<ShotCount>\d+)",loader)
		
		# assign values
		try:
			self.name=data.group('MovieName')
			self.totaltime=float(data.group('TotalTime'))
			self.shotcount=int(data.group('ShotCount'))
		# .savestate file with incorrect or corrupt data
		except AttributeError: 
			sys.exit("unknown filetype")
		print("press enter to start loaded state")
		
		if str(msvcrt.getch())[-2]=="r":
			print("loaded state")
			self.key()
		else: print("Unknown keystroke ")
		
	
	def key(self):
		clock()
		tottime=0
		# wait for keystroke
		keystroke=str(msvcrt.getch())[-2]
		if keystroke=="s":
			# shot detected
			timestamp=clock()
			self.shotcount=self.shotcount+1
			
			if self.pauseinterval!=[]:
			# if 'pause' has never been used before
				for pausetime in self.pauseinterval: tottime=tottime+pausetime
				actualtime=float(timestamp)-float(tottime)
				self.shotlist.append((self.shotcount,actualtime ))
				print("Shot #%d at %fs "%(self.shotcount,actualtime))
			else:
				self.shotlist.append((self.shotcount,timestamp ))
				print("Shot #%d at %fs "%(self.shotcount,timestamp))
			self.key() 
		
		elif keystroke=="p": self.pause()
		elif keystroke=="e" : self.reset()
		else: print("Unkown keystroke")
	
	def pause(self):
		print("paused")
		self.startpause=clock()
		startagain=str(msvcrt.getch())[-2]
		
		if startagain=="p": 
			self.endpause=clock()
			print("restarted")
			# the next line will help to calculate the total time by keeping track of time lost in pauses
			self.pauseinterval.append(self.endpause-self.startpause)
			self.key()
		elif startagain=="e": self.reset()
		else: print("Unknown keystroke")
	
	def reset(self):
		print('\n==ASLreport for "%s"=='%self.name)
		self.totaltime=clock()
		
		for pausetime in self.pauseinterval: 
			# subtract pausetime from the totaltime of the session
			self.totaltime=self.totaltime-pausetime
		print(self.totaltime)
		
		try:  self.totaltime/self.shotcount
		# if shotcount is 0
		except ZeroDivisionError: sys.exit("no shots recorded")
		self.ASL=self.totaltime/self.shotcount
		
		print("number of shots=%d"%self.shotcount)	
		print("ASL =%fseconds"%self.ASL)
		self.report=input("\nDo you want to generate report file?(y/n)\n")
		if self.report=="y": self.reportgenerator()
		elif self.report=="n": sys.exit(0)
		
	def reportgenerator(self):
		fileopen=open(r"%s.ASLreport"%self.name,mode="w")
		fileopen.write(
		"ASL report for %s \n\n Record Time=%f\n Recorded Shots=%d\n ASL=%f\nShot Timestamps:\n%s"%(self.name,self.totaltime,self.shotcount,self.ASL,str(self.shotlist)))
	
	def savestate(self):
		# open and write a '.savestate' file with the required data and in a specific format
		saver=open("%s.savestate"%self.name, mode="w")
		saver.write("%s\n%f\n%f"%(self.name,self.totaltime,self.shotcount))
	
	
# --main boilerplate--
if __name__=="__main__":
	ASLreport().metadata()