-
Notifications
You must be signed in to change notification settings - Fork 0
/
pomodoro.py
275 lines (210 loc) · 6.12 KB
/
pomodoro.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python
from time import sleep
import threading
from libs.KThread import *
import pygame
"""I don't use this library any more, I use pygame.mixer.music, who let's set the volume"""
#from libs.play_sound import *
"""If you want to play the music with winsound, uncomment this imports and check play_sound() method"""
#import sys #, osm inspect
#if sys.platform[:3] == "win":
# import winsound
"""Old class and method to make the thread"""
#################################################
# #
# class Temporizador(threading.Thread): #
# #
# def __init__ (self, time2): #
# self.time = time2 #
# threading.Thread.__init__(self) #
# global contador #
# self.contador = 0 #
# #
# def run (self): #
# while self.contador < self.time: #
# sleep(1) #
# self.contador += 1 #
# #
# def seconds(self): #
# return self.time - self.contador #
# #
#################################################
# #
# #
#global f #
#f = Temporizador(time) #
#f.start() #Parte de otro metod0 #
# #
# Esta metodo ha sido eliminado para dar paso #
# al proporcionado por KThread, que permite #
# matar el thread. #
# #
#################################################
__author__ = "Rock Neurotiko"
__copyright__= "Copyright 2012, Rock Neurotiko"
__credits__ = ["Rock Neurotiko",]
__license__ = "GNU/GPL v3"
__version__ = "0.3"
__maintainer__ = "Rock Neurotiko"
__email__ = "miguelglafuente@gmail.com"
class Pomodoro(object):
"""
Main backend class. Contains all the back end methods to runs.
"""
def __init__(self):
"""
Constructor.
Initialize control variables.
"""
self.ESTADO = "STOP" #STATES: STOP, START, PAUSE, END
self.TIEMPO = 25*60
self.INTERR = 0
self.estado = self.ESTADO
self.tiempo = self.TIEMPO
self.interr = self.INTERR
self.on = False
self.segundos_trans = 0
self.volume = 1.0
self.filemp3 = "alarma.mp3"
def crear_pom(self, time = 0):
"""
Method to create a pomodoro.
If it's already started or paused, it will be interrupted and started again.
Just makes the adjust to run, but the pomodoro doesn't start
@param time The time selected to creates the pomodoro.
@type time Integer
"""
if (self.estado=="START" or self.estado=="PAUSE"):
self.interrupt_pom()
self.segundos_trans = 0 #reboot the seconds
self.estado="STOP"
if time==0:
time = 25*60
self.tiempo = time
def iniciar_pom(self):
"""
Method to start a pomodoro.
If it's already started it will be interrupted and started again.
"""
#print("llama a iniciar") #debug control
if(self.estado =="START"):
self.interrupt_pom()
self.segundos_trans = 0 #reboot the seconds
self.estado = "START"
self.arrancar(self.tiempo)
global e
e = KThread(target = self.contador)
e.start()
self.interr = 0
def resume_pom(self):
"""
Method to resume a pomodoro.
"""
#print("llama a resumir") #debug control
self.estado = "START"
self.arrancar(self.tiempo)
global e
e = KThread(target = self.contador)
e.start()
self.interr = 0
def interrupt_pom(self):
"""
Method to STOP and pomodoro.
Just works if it's already started.
"""
#print "Llama a parar" #debug control
if (self.estado=="STOP" or self.estado=="PAUSE"):
print "No puedes parar algo no empezado"
else:
self.interr += 1
print "Pomodoro parado quedando: " + self.get_tiempo()
t.cancel() #kill the timer
self.on = False #turn off the state of t
e.kill() #kill the counter
self.estado = "PAUSE" #put the state into PAUSE
def arrancar(self, time):
"""
Method to control the finish of the pomodoro.
Creates a timer threading.
@param time The time selected of the pomodoro.
@type time Integer
"""
global t
if self.on == True:
t.cancel()
self.on == False
t = threading.Timer(time, self.terminar)
t.start()
self.on = True
def terminar(self):
"""
Method who is executed when the pomodoro is finished.
This method is called from the Timer threading of arrancar method
"""
self.estado = "END"
e.kill()
self.on = False
self.segundos_trans = 0
print "Fin del pomodoro, puedes descansar.\n"
self.play_sound()
def contador(self):
"""
Method who controls the time.
This method is called from the KThread instance.
"""
sleep(1)
while (self.segundos_trans < self.tiempo and self.estado == "START"):
self.segundos_trans += 1
sleep(1)
def get_interruptions(self):
return self.interr
def get_tiempo(self):
"""
Getter.
@return The time parsed in a String.
"""
tiempo_local = self.seconds()
if tiempo_local <= 60:
return str(self.seconds()) + " " + "segundos."
elif tiempo_local <= 3600:
segundos_local = tiempo_local - (tiempo_local/60)*60
return str(tiempo_local/60) + " " + "minutos, y " + str(segundos_local) + " " + "segundos."
else:
minutos_local = (tiempo_local - (tiempo_local - ((tiempo_local/3600)*3600)/60))/60
segundos_local = tiempo_local - minutos_local - minutos_local*3600
return (str(tiempo_local/3600) + " " + "hora/s, y " + str(minutos_local) + " " +
"minuto/s, y " + str(segundos_local) + " " + "segundo/s.")
def seconds(self):
"""
Turns the time into seconds
"""
return self.tiempo - self.segundos_trans
def play_sound(self):
"""
Play a song.
Method called at the end of the pomodoro.
"""
#so_system = sys.platform
pygame.mixer.init()
#pygame.mixer.music.load('alarma.mp3')
try:
pygame.mixer.music.load(self.filemp3)
except:
pygame.mixer.music.load("alarma.mp3")
pygame.mixer.music.set_volume(self.volume)
pygame.mixer.music.play()
# if(so_system[:3] == "win"):
# winsound.PlaySound('alarma.wav', winsound.SND_FILENAME)
# elif(so_system[:3] == "lin"):
# play_sound.play(os.path.dirname(inspect.getfile(inspect.currentframe())) + "/alarma.wav")
"""
Getters and setters.
"""
def set_Filemp3(self, path):
"""
Set the filemp3 control variable of the Pomodoro instance.
@param path The path of the song. (Not empty)
@type path String
"""
if path!="":
self.filemp3 = path