-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
173 lines (146 loc) · 5.05 KB
/
main.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
import alert
import configparser
from datetime import datetime, timedelta
import getopt
import sys
import time
def wait(minutes):
seconds = minutes * 60
time.sleep(seconds)
# Produces from string the longest substring
# that starts at index and is an integer
def get_contiguous_number(string, index):
length = len(string)
end = length
while end > index:
substring = string[index:end]
if substring.isdigit():
return substring
end -= 1
return ''
# Parses a timing pattern and returns a list of (char, int) tuples
# where the char is either 'w' for work or 'b' for break
def parse_pattern(raw_pattern):
pattern = raw_pattern.replace(' ', '')
length = len(pattern)
index = 0
output = []
while index < length:
next_char = pattern[index]
if next_char == 'w' or next_char == 'b':
# work period
num_str = get_contiguous_number(pattern, index + 1)
if len(num_str) == 0:
raise ValueError(
'No number found after char {} at index {} in pattern {}'.format(
next_char,
index,
raw_pattern
)
)
time = int(num_str)
output.append((next_char, time))
index += len(num_str) + 1
else:
raise ValueError(
'Unexpected char {} at index {} in pattern {}'.format(
next_char,
index,
raw_pattern
)
)
return output
# Cleans up a parsed timing pattern in the [('w'|'b', time)*] format
# Return type is the same
def clean_pattern(pattern):
length = len(pattern)
if length == 0 or length == 1:
return pattern
output = []
last_type, last_time = pattern[0]
for i in range(1, length):
current_type, current_time = pattern[i]
if current_type == last_type:
last_time += current_time
else:
output.append((last_type, last_time))
last_type = current_type
last_time = current_time
output.append((last_type, last_time))
return output
def main():
args = sys.argv[1:]
options = 'ne:p:' # empty string would only accept long options
long_options = [
'email-conf=',
'pattern=',
'no-notifcation'
]
config_vals = {
'email_conf': None,
'pattern': 'w25b5w25b5w25b5',
'no-notification': False
}
try:
opt_vals, args = getopt.getopt(args, options, long_options)
except getopt.GetoptError as err:
print('Encountered option error!')
print(str(err))
sys.exit()
#print(opt_vals)
for opt, arg in opt_vals:
if opt == '--no-notifcation' or opt == '-n':
config_vals['no-notification'] = True
if opt == '--email-conf' or opt == '-e':
config_vals['email_conf'] = arg
if opt == '--pattern' or opt == '-p':
config_vals['pattern'] = arg
if config_vals['pattern'] is None:
print('Pymodoro requires a pattern!')
sys.exit()
try:
parsed_pattern = parse_pattern(config_vals['pattern'])
cleaned_pattern = clean_pattern(parsed_pattern)
except Exception as err:
print("Could not parse pattern!")
print(str(err))
sys.exit()
if config_vals['email_conf'] is not None:
conf_parser = configparser.ConfigParser()
conf_path = r'email.conf'
conf_parser.read(conf_path)
from_address = conf_parser.get('email', 'fromAddress')
to_address = conf_parser.get('email', 'toAddress')
login = conf_parser.get('email', 'login')
password = conf_parser.get('email', 'password')
server = conf_parser.get('email', 'smtpserver')
subject = 'Pymodoro Alert!'
for task, duration in cleaned_pattern:
task_string = ''
if task == 'b':
task_string = 'break period'
elif task == 'w':
task_string = 'work period'
time = datetime.now()
time_str = time.strftime('%H:%M')
endtime = time + timedelta(minutes=duration)
endtime_str = endtime.strftime('%H:%M')
print('[{}] Beginning {} minute {} until {}!'.format(time_str, duration, task_string, endtime_str))
wait(duration)
time_str = datetime.now().strftime('%H:%M')
print('[{}] Completed {}!'.format(time_str, task_string))
notification_string = 'Your {} is complete!'.format(task_string)
if not config_vals['no-notification']:
alert.notify_desktop('Pymodoro', notification_string)
if config_vals['email_conf'] is not None:
alert.send_email(
from_addr = from_address,
to_addr_list = [to_address],
subject = subject,
message = notification_string,
login = login,
password = password,
smtpserver = server
)
if __name__ == '__main__':
main()