-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.py
363 lines (294 loc) · 11.2 KB
/
list.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from pytlas import intent, training, translations, meta
from pytlas.settings import CONFIG
import json
import os
import unicodedata
import re
import smtplib
import sys
# This entity will be shared among training data since it's not language specific
@training('en')
def en_data(): return """
%[enumerate_list]
what are my lists
enumerate my lists
%[delete_list]
delete list @[list_name]
delete @[list_name]
delete my list @[list_name]
erase @[list_name]
destroy @[list_name]
put @[list_name] in the can
trash list @[list_name]
%[add_item]
add @[item_name] in list @[list_name]
add @[item_name] in my @[list_name]
add @[item_name] in the @[list_name]
I want add something in my @[list_name]
could you add something in the @[list_name]
%[remove_item]
remove @[item_name] from @[list_name]
%[display_list]
what is the @[list_name] content
show me the @[list_name] content
show me my @[list_name]
display my @[list_name]
what I have in my @[list_name]
%[send_list]
send to @[to_email] the list @[list_name]
send the list @[list_name] to @[to_email]
send @[list_name] to @[to_email]
%[help_list]
give me help on list skill
give me advises on list skill
how does the list skill work
@[list_name]
shoping list
todo list
wish list
christmas list
gift list
@[item_name]
garlic
salt
walk the dog
call mother
bread
eggs
trip
do homework
@[to_email]
me
"""
#@training('fr')
#def fr_data(): return """
#"""
#@translations('fr')
#def fr_translations(): return {
#}
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
slug_value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
slug_value = re.sub(r'[^\w\s-]', '', slug_value).strip().lower()
slug_value = re.sub(r'[-\s]+', '_', slug_value)
return slug_value
def default_pytlas_list_path():
return os.path.join(os.path.expanduser('~'),'.pytlas-list')
def build_list_file_path(list_name, list_dir_path):
list_filename = slugify(list_name)+'.json'
list_path = os.path.join(list_dir_path,list_filename)
return list_path
def create_blank_file(list_name, list_path):
list_content = { 'name' : list_name, 'items' : [] }
with open(list_path,'w') as json_file:
json.dump(list_content, json_file)
def append_in_file(item_name, list_path):
list_content = {}
with open(list_path,'r') as json_file:
list_content = json.load(json_file)
list_content['items'].append(item_name)
with open(list_path,'w') as json_file:
json.dump(list_content, json_file)
def remove_from_file(item_name, list_path):
list_content = {}
with open(list_path,'r') as json_file:
list_content = json.load(json_file)
list_content['items'] = [x for x in list_content['items'] if x != item_name]
with open(list_path,'w') as json_file:
json.dump(list_content, json_file)
@meta()
def help_meta(_): return {
'name': _('list'),
'description': _('The skill which helps you to manage your TODO lists.'),
'author': 'Jean-Michel LEKSTON',
'version': '1.0.0',
'homepage': 'https://github.com/atlassistant/pytlas-list',
}
@intent('enumerate_list')
def on_enumerate_list(req):
list_dir_path = CONFIG.get('path',section='pytlas_list')
if not list_dir_path:
list_dir_path = default_pytlas_list_path()
list_names = []
for f in os.listdir(list_dir_path):
file_path = os.path.join(list_dir_path, f)
if os.path.isfile(file_path) and os.path.splitext(file_path)[1] == '.json':
with open(file_path,'r') as json_file:
list_content = json.load(json_file)
list_names.append(list_content['name'])
if len(list_names) == 0:
result = req._("I found no list")
elif len(list_names) == 1:
result = req._("I found only one list : {0}").format(list_names[0])
else:
found_list = ', '.join(list_names)
result = req._("I found the following lists : {0}").format(found_list)
req.agent.answer(result)
return req.agent.done()
@intent('delete_list')
def on_delete_list(req):
list_name = req.intent.slot('list_name').first().value
if not list_name:
return req.agent.ask('list_name',req._('In which list?'))
list_dir_path = CONFIG.get('path',section='pytlas_list')
if not list_dir_path:
list_dir_path = default_pytlas_list_path()
list_path = build_list_file_path(list_name, list_dir_path)
if not os.path.exists(list_path):
req.agent.answer(req._('Hummm! The list "{0}" seems not exists').format(list_name))
return req.agent.done()
delete_confirmed = req.intent.slot('delete_confirmed').first().value
yes = req._('Yes')
no = req._('No')
if not delete_confirmed:
return req.agent.ask('delete_confirmed', req._('Would you like delete "{0}"?').format(list_name), [yes, no])
if delete_confirmed == no:
return req.agent.done()
try:
os.remove(list_path)
except:
req.agent.answer(req._('Oops! Something bad append. I can\'t delete file "{0}"').format(list_path))
return req.agent.done()
@intent('add_item')
def on_add_item(req):
list_name = req.intent.slot('list_name').first().value
if not list_name:
return req.agent.ask('list_name',req._('In which list?'))
list_dir_path = CONFIG.get('path',section='pytlas_list')
if not list_dir_path:
list_dir_path = default_pytlas_list_path()
list_path = build_list_file_path(list_name, list_dir_path)
if not os.path.exists(list_path):
create_confirmed = req.intent.slot('create_confirmed').first().value
yes = req._('Yes')
no = req._('No')
if not create_confirmed:
return req.agent.ask('create_confirmed', req._('Hummm! The list "{0}" seems not exists. Would you like create it?').format(list_name), [yes, no])
if create_confirmed == no:
return req.agent.done()
try:
create_blank_file(list_name, list_path)
except:
req.agent.answer(req._('Oops! Something bad append. I can\'t create file "{0}"').format(list_path))
return req.agent.done()
item_name = req.intent.slot('item_name').first().value
if not item_name:
return req.agent.ask('item_name', req._('What do you want to add in your list?'))
try:
append_in_file(item_name, list_path)
except:
req.agent.answer(req._('Oops! Something bad append. I can\'t add "{0}" in file "{1}"').format(item_name, list_path))
return req.agent.done()
req.agent.answer(req._('Ok, "{0}" has been added in your list "{1}"').format(item_name, list_name))
return req.agent.done()
@intent('remove_item')
def on_remove_item(req):
list_name = req.intent.slot('list_name').first().value
if not list_name:
return req.agent.ask('list_name',req._('From which list?'))
list_dir_path = CONFIG.get('path',section='pytlas_list')
if not list_dir_path:
list_dir_path = default_pytlas_list_path()
list_path = build_list_file_path(list_name, list_dir_path)
if not os.path.exists(list_path):
req.agent.answer(req._('Hummm! The list "{0}" seems not exists').format(list_name))
return req.agent.done()
item_name = req.intent.slot('item_name').first().value
if not item_name:
return req.agent.ask('item_name', req._('What do you want remove from your list?'))
try:
remove_from_file(item_name, list_path)
except:
req.agent.answer(req._('Oops! Something bad append. I can\'t remove "{0}" from file "{1}"').format(item_name, list_path))
return req.agent.done()
req.agent.answer(req._('Ok, "{0}" has been removed from your list "{1}"').format(item_name, list_name))
return req.agent.done()
@intent('display_list')
def on_display_list(req):
list_name = req.intent.slot('list_name').first().value
if not list_name:
return req.agent.ask('list_name',req._('Which list?'))
list_dir_path = CONFIG.get('path',section='pytlas_list')
if not list_dir_path:
list_dir_path = default_pytlas_list_path()
list_path = build_list_file_path(list_name, list_dir_path)
if not os.path.exists(list_path):
req.agent.answer(req._('Hummm! The list "{0}" seems not exists.').format(list_name))
return req.agent.done()
list_content = {}
try:
with open(list_path,'r') as json_file:
list_content = json.load(json_file)
except:
req.agent.answer(req._('Oops! Something bad append. I can\'t open the file "{0}"').format(list_path))
return req.agent.done()
req.agent.answer(req._('Your list "{0}" contains : {1}').format(list_content['name'], ', '.join(list_content['items'])))
return req.agent.done()
@intent('send_list')
def on_send_list(req):
list_name = req.intent.slot('list_name').first().value
if not list_name:
return req.agent.ask('list_name',req._('Which list?'))
list_dir_path = CONFIG.get('path',section='pytlas_list')
if not list_dir_path:
list_dir_path = default_pytlas_list_path()
list_path = build_list_file_path(list_name, list_dir_path)
if not os.path.exists(list_path):
req.agent.answer(req._('Hummm! The list "{0}" seems not exists.').format(list_name))
return req.agent.done()
from_email = req.intent.slot('from_email').first().value
if not from_email:
from_email = CONFIG.get('from_email',section='pytlas_list')
if not from_email:
return req.agent.ask('from_email',req._('Please tell me from which email address'))
to_email = req.intent.slot('to_email').first().value
if not to_email:
return req.agent.ask('to_email',req._('Please tell to which email address'))
if to_email == "me":
to_email = from_email
smtp_address = req.intent.slot('smtp_address').first().value
if not smtp_address:
smtp_address = CONFIG.get('smtp_address',section='pytlas_list')
if not smtp_address:
return req.agent.ask('smtp_address',req._('Please give me the smtp server address'))
smtp_login = req.intent.slot('smtp_login').first().value
if not smtp_login:
smtp_login = CONFIG.get('smtp_login',section='pytlas_list')
if not smtp_login:
return req.agent.ask('smtp_login',req._('Please give me the smtp server login'))
smtp_pwd = req.intent.slot('smtp_pwd').first().value
if not smtp_pwd:
smtp_pwd = CONFIG.get('smtp_pwd',section='pytlas_list')
if not smtp_pwd:
return req.agent.ask('smtp_pwd',req._('Please give me the smtp server pwd'))
list_content = {}
try:
with open(list_path,'r') as json_file:
list_content = json.load(json_file)
except:
req.agent.answer(req._('Oops! Something bad append. I can\'t open the file "{0}"').format(list_path))
return req.agent.done()
msg = "\n"
msg = msg + list_content['name'] + "\n"
msg = msg + "=" * len(list_content['name']) + "\n"
for item in list_content['items']:
msg = msg + "-[ ]" + item + "\n"
# print('sending to {0} from {1} on {2} using credential {3} {4} of \n{5}'.format(to_email, from_email, smtp_address, smtp_login, smtp_pwd, msg))
try:
server = smtplib.SMTP(smtp_address+":587")
server.ehlo()
server.starttls()
#Next, log in to the server
server.login(smtp_login, smtp_pwd)
#Send the mail
server.sendmail(from_email, to_email, msg)
server.quit()
except:
req.agent.answer(req._('Hummm! Email sending failed. Cause : {0}.').format(sys.exc_info()[0]))
return req.agent.done()
req.agent.answer(req._('Email successfully sent.'))
return req.agent.done()