-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbender.py
executable file
·310 lines (205 loc) · 6.42 KB
/
bender.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
#!/usr/bin/env python
import os
BOT_COMMAND = "bender"
BOT_OPTION_NEW = "new"
BOT_OPTION_SAVE = "save"
BOT_OPTION_DISCARD = "discard"
BOT_OPTION_LIST = "list"
BOT_OPTION_EXIT = "exit"
BOT_OPTION_HELP = "help"
BOT_ATTR_TAGS = "--tags"
BOT_UNICODE = u"\U0001F916"
file_list = []
user_contents = ""
is_content_mode = False
available_robots_message = "Hi there! Currently we only have 'Bender' available as your personal Robot.He is the lazy one. Just type 'Bender' or 'bender' to call him. \n"
def main():
configure()
init_bot()
def configure():
save_folder_name = "bot savefiles"
cur_dir = os.path.abspath(os.path.dirname(__file__))
save_file_path = os.path.join(cur_dir,save_folder_name)
# Creating folder for save files if not present
if not os.path.isdir(save_file_path):
os.makedirs(save_folder_name)
os.chdir(save_file_path)
else:
# Listing all the save files name present in save folder
os.chdir(save_file_path)
global file_list
file_list = [f for f in os.listdir(save_file_path) if os.path.isfile(os.path.join(save_file_path,f))]
def init_bot():
prompt = "> "
global user_contents
print_welcome_message()
while True:
user_input = input(prompt)
if is_content_mode:
# Incase the line starts with Save or Discard command
if user_input.lower().startswith(BOT_COMMAND):
user_input_arr = user_input.strip().lower().split()
parse_result = parse_commands(user_input_arr)
# If the command is invalid in content mode, it is regarded as content
if parse_result == -1:
user_contents += user_input + "\n"
elif parse_result == 0:
print_exit_message
break
# The user is still in content mode
else:
user_contents += user_input + "\n"
else:
# If this is not content_mode & first command it bot command
user_input_arr = user_input.strip().lower().split()
try:
if user_input_arr[0] == BOT_COMMAND:
parse_result = parse_commands(user_input_arr)
# Exit Condition
if parse_result == 0:
print_exit_message()
break
# Invalid command condition
elif parse_result == -1:
invalid_command_msg = "{} Ain't doing that. Bite my shiny metal a$$. \n".format(BOT_UNICODE)
print(invalid_command_msg)
else:
print(available_robots_message)
except:
print(available_robots_message)
def parse_commands(options_arr):
global is_content_mode
global user_contents
"""
-1: Invalid commands
0: Exit
1: Success commands
"""
try:
if options_arr[1] == BOT_OPTION_NEW:
message = "{} Start writing anything. After you have finished, execute save command in new line. \n".format(BOT_UNICODE)
print(message)
is_content_mode = True
user_contents = ""
return 1
elif options_arr[1] == BOT_OPTION_EXIT:
is_content_mode = False
user_contents = ""
return 0
elif options_arr[1] == BOT_OPTION_DISCARD:
message = "{} Discarding written content \n".format(BOT_UNICODE)
print(message)
is_content_mode = False
user_contents = ""
return 1
elif options_arr[1] == BOT_OPTION_SAVE:
try:
attr = options_arr[2]
is_valid = validate_tag(attr)
if is_valid:
actual_tag_name = attr[1:]
tag_exists = check_if_tags_exists(actual_tag_name)
file_name = actual_tag_name + ".txt"
if tag_exists:
f = open(file_name,"a+")
f.write("\n")
else:
f = open(file_name,"w+")
f.write(user_contents)
f.flush()
f.close()
if not tag_exists:
global file_list
file_list.append(file_name)
is_content_mode = False
user_contents = ""
print("{} Saved {}! Bender rocks! \n".format(BOT_UNICODE,attr))
else:
print("{} Give me hashtag baby!! \n".format(BOT_UNICODE))
is_content_mode = False
except:
print("{} Need hashtags too baby!!! \n".format(BOT_UNICODE))
elif options_arr[1] == BOT_OPTION_LIST:
try:
attr = options_arr[2]
if attr == BOT_ATTR_TAGS:
print_saved_tags("")
else:
attr = attr[1:]
tag_exists = check_if_tags_exists(attr)
if tag_exists:
file_name = attr + ".txt"
file = open(file_name,"r")
file_contents = file.read()
print(file_contents)
file.close()
else:
print("{} I did my best, but couldn't find anything for #{} \n".format(BOT_UNICODE,attr))
except:
print("{} List what? \n".format(BOT_UNICODE))
elif options_arr[1] == BOT_OPTION_HELP:
print_bot_help_message()
else:
return -1
except IndexError:
# If no command is provided
print("{} I'm here baby! What do you want? \n".format(BOT_UNICODE))
def validate_tag(tag):
""" Chceks if tag is valid. Tag should not be empty and should start with '#' character """
tag = tag.strip().lower()
return len(tag) > 1 and tag.startswith("#")
def print_bot_help_message():
print ("""
----------------------------------------------------------------------
{} Shut up and get to the point
[new]:
- Create new content
- Usage: bender new
[save] [Argument]:
- Saves or Appends what you write for given #hashtag as an argument
- Usage: bender save #mytodos
[list] [Argument]:
- List all the saved items
Argument:
1. --tags: List all the available tags which you have saved
2. [#hashtag]: List the contents for given hashtag
Usage:
bender list --tags, bender list #mytodos
[exit]:
- Exit out of script
Usage: bender exit
----------------------------------------------------------------------
""".format(BOT_UNICODE))
def check_if_tags_exists(tag):
for item in file_list:
filename = item[0:-4]
if tag == filename:
return True
return False
def print_saved_tags(tag_to_match):
counter = 0
if tag_to_match:
for item in file_list:
tag = item[0:-4]
if tag == tag_to_match:
counter += 1
print("#{}".format(tag))
else:
for item in file_list:
print("#{}".format(item[0:-4]))
counter += 1
print("\n{} tags found \n".format(counter))
# Welcome message to be displayed when the script starts
def print_welcome_message():
print ("""
### WELCOME TO PLANET EXPRESS ###
Hi! I am Professor Farnsworth.
Here at Planet Express, we are currently focused in saving whatever comes to your mind. Have some to-do's or some fancy ideas? Tell Robot to remember it for you.
We have 'Bender' as your personal Robot. Type 'Bender' or 'bender' to call him.
""")
def print_exit_message():
exit_message = "{} Bender needs beer. Bye ya'll. \n".format(BOT_UNICODE)
print(exit_message)
# Start point for the script
if __name__ == '__main__':
main()