-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpomobar.py
220 lines (185 loc) · 6.12 KB
/
pomobar.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
#!/usr/bin/python3
##############################################################################
# AUTHOR: [Loopzen](https://github.com/loopzen/)
# DATE: 2018-10-09
# DESCRIPTION: Pomodoro timer to polybar
# - Counter of all your completed pomodoros
# - Desktop notifications
# REQUERIMENTS:
# - polybar
# - dunst
# - fontAwesome
##############################################################################
from os.path import expanduser
import os
import time
import sqlite3
import argparse
import configparser
# GLOBAL CONSTANTS
HOME_FOLDER = expanduser("~/")
MINUTE = 60
# DEFAULT CONFIGS
ICON_TOTAL = " "
DATABASE_PATH = HOME_FOLDER + "pomobar.db"
# CUSTOM CONFIGS
config = configparser.ConfigParser()
config.read(HOME_FOLDER + '.config/pomobar.conf')
if config.has_option('DEFAULT', 'ICON_TOTAL'):
ICON_TOTAL = config['DEFAULT']['ICON_TOTAL'] + " "
if config.has_option('PATH', 'DATABASE_PATH'):
DATABASE_PATH = config['PATH']['DATABASE_PATH']
class Pomodoro:
"""Normal pomodoro"""
def __init__(self, duration=25):
self.duration = int(duration)
self.ICON = " "
def start(self):
while self.duration >= 0:
writeOutput(self.ICON + str(self.duration))
recharge_polybar()
time.sleep(MINUTE)
self.duration -= 1
if self.duration <= 0:
GlobalPomodoroCounter.increment_global_pomodoro_counter()
writeOutput(ICON_TOTAL + GlobalPomodoroCounter.get_pomodoros_done())
recharge_polybar()
os.system("notify-send --urgency=normal Pomodoro finished")
class Break:
"""Break time class"""
def __init__(self, duration=5):
super().__init__()
self.duration = int(duration)
self.ICON = " "
def start(self):
while self.duration >= 0:
writeOutput(self.ICON + str(self.duration))
recharge_polybar()
time.sleep(MINUTE)
self.duration -= 1
if self.duration <= 0:
writeOutput(ICON_TOTAL + GlobalPomodoroCounter.get_pomodoros_done())
recharge_polybar()
os.system("notify-send --urgency=low Break finished")
class GlobalPomodoroCounter:
"""Leer y registrar el contador total de pomodoros realizados"""
@staticmethod
def create_table():
try:
connection = sqlite3.connect(DATABASE_PATH)
cursor = connection.cursor()
cursor.execute(
"""
CREATE TABLE POMODOROS (
COMPLETADOS TEXT
)
"""
)
cursor.execute("INSERT INTO POMODOROS VALUES ('0')")
connection.commit()
connection.close()
except Exception as e:
if "exists" in str(e):
pass
else:
print(e)
@staticmethod
def increment_global_pomodoro_counter():
pomodoros_completados = GlobalPomodoroCounter.get_pomodoros_done()
pomodoros_actualizados = str(int(pomodoros_completados) + 1)
try:
connection = sqlite3.connect(DATABASE_PATH)
cursor = connection.cursor()
cursor.execute(
"UPDATE POMODOROS SET COMPLETADOS = ("
"" + pomodoros_actualizados + ""
") WHERE COMPLETADOS = '" + pomodoros_completados + "'"
)
connection.commit()
connection.close()
except Exception as e:
print(e)
return pomodoros_completados
@staticmethod
def get_pomodoros_done():
pomodoros_completados = "0"
try:
connection = sqlite3.connect(DATABASE_PATH)
cursor = connection.cursor()
cursor.execute("SELECT COMPLETADOS FROM POMODOROS")
pomodoros_completados = cursor.fetchone()[0]
connection.close()
except Exception as e:
print(e)
return pomodoros_completados
##############################
# AUXILIAR FUNCTIONS
##############################
def kill_another_instances():
"""Kill another instances to avoid collisions"""
os.system("pgrep pomobar > pomobar.pid")
filePids = open("pomobar.pid", "r")
for pid in filePids:
pid = int(pid)
if pid != int(os.getpid()):
os.system("kill -9 " + str(pid))
filePids.close()
os.system("rm pomobar.pid")
def getArguments():
"""Handle input parameters"""
parser = argparse.ArgumentParser(
prog="POMOBAR",
description="Pomodoro implementation to polybar and terminal",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument(
"-a",
"--action",
type=str,
default="new",
help="What action? (new, break, kill)",
)
parser.add_argument("-d", "--duration", type=str, help="Pomodoro duration")
args = parser.parse_args()
return args
def dispatcher(args):
"""Dispatcher to serveral actions"""
if args.action == "new":
if args.duration is None:
pomodoro = Pomodoro()
else:
pomodoro = Pomodoro(args.duration)
GlobalPomodoroCounter.create_table()
pomodoro.start()
elif args.action == "break":
if args.duration is None:
break_time = Break()
else:
break_time = Break(args.duration)
break_time.start()
elif args.action == "kill":
stop()
def writeOutput(message):
"""Write message to output file and console"""
try:
print(message)
fileOutput = open(HOME_FOLDER + ".pomobaroutput", "w")
fileOutput.write(message)
fileOutput.close()
except Exception as e:
print(e)
def stop():
"""Stop stopwatch"""
writeOutput(ICON_TOTAL + GlobalPomodoroCounter.get_pomodoros_done())
recharge_polybar()
os.system("notify-send --urgency=low Pomodoro cancelado")
def recharge_polybar():
"""Command to restart polybar"""
os.system("polybar-msg hook pomobar 1 &> /dev/null")
def main():
"""Start programm"""
kill_another_instances()
args = getArguments()
dispatcher(args)
if __name__ == "__main__":
main()