2017-08-04 03:00:19 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2017-09-02 18:02:09 +00:00
|
|
|
import re
|
2017-08-04 03:00:19 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import random
|
|
|
|
|
2018-08-07 18:22:39 +00:00
|
|
|
the_list = "/home/m455/code/projects/nicethings/list.txt"
|
2018-08-04 14:11:03 +00:00
|
|
|
program_name = "nicethings"
|
|
|
|
top_line = "-----------------------------------------\n"
|
|
|
|
bottom_line = "\n-----------------------------------------"
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-07 18:03:40 +00:00
|
|
|
messages = {'list_created': "{} created\n{} now readable and writeable by other users".format(the_list, the_list),
|
2018-08-04 14:11:03 +00:00
|
|
|
'list_empty': "sorry, there's nothing in {}\nTry adding something to the list by typing:\n{} \"your message here\"".format(the_list, program_name),
|
2018-08-07 18:03:40 +00:00
|
|
|
'args_too_many': "sorry, i only accept one argument.\ntry using:\n{} \"your message here\"".format(program_name),
|
2018-08-07 14:19:30 +00:00
|
|
|
'input_null': "sorry, i couldn't find any input.\n\niry using:\n{} \"your message here\"".format(program_name),
|
|
|
|
'item_added': "you have added the following to the list:\n",
|
|
|
|
'try_again': "sorry, i didn't get that, try typing y or n:",
|
|
|
|
'bye': "bye bye",
|
|
|
|
'too_many_args': "sorry, i only accept one argument.\ntry using:\n{} \"your message here\"".format(program_name),
|
2018-08-04 14:11:03 +00:00
|
|
|
'file_not_found': "{} doesn't exist\ndo you want me to make a {} file right now? (y/n)".format(the_list, the_list)}
|
|
|
|
|
2018-08-07 17:40:41 +00:00
|
|
|
def write_to_file(a_file, content):
|
|
|
|
temp = open(a_file, 'a')
|
2018-08-04 14:11:03 +00:00
|
|
|
temp.write(content)
|
|
|
|
temp.close()
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-07 17:40:41 +00:00
|
|
|
def create_file(a_file):
|
|
|
|
open(a_file, 'a').close()
|
|
|
|
os.chmod(the_list, 0o666)
|
2018-08-04 14:11:03 +00:00
|
|
|
print(messages['list_created'])
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-07 17:40:41 +00:00
|
|
|
def show_random_line(a_file):
|
|
|
|
thefile = open(a_file, 'r').read(1)
|
|
|
|
temp = open(a_file, 'r')
|
2018-08-04 14:11:03 +00:00
|
|
|
contents = temp.read(1)
|
|
|
|
if not contents:
|
|
|
|
print(messages['list_empty'])
|
|
|
|
else:
|
|
|
|
print(random.choice(list(open(the_list, 'r'))).rstrip('\n'))
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-07 14:19:30 +00:00
|
|
|
def add_to_list(args):
|
|
|
|
write_to_file(the_list, args + "\n")
|
2018-08-07 17:05:19 +00:00
|
|
|
print(top_line + messages['item_added'], args, bottom_line)
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-07 14:19:30 +00:00
|
|
|
def hangle_args(args):
|
|
|
|
if len(args) <= 1:
|
2018-08-04 14:11:03 +00:00
|
|
|
show_random_line(the_list)
|
2018-08-07 14:19:30 +00:00
|
|
|
elif len(args) > 2:
|
|
|
|
print(messages['too_many_args'])
|
|
|
|
elif args[1].isspace() == True or args[1] == "":
|
2018-08-04 14:11:03 +00:00
|
|
|
print(messages['input_null'])
|
2017-08-04 03:00:19 +00:00
|
|
|
else:
|
2018-08-07 14:19:30 +00:00
|
|
|
add_to_list(args[1])
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-04 14:11:03 +00:00
|
|
|
def prompt_file_creation():
|
2017-08-04 03:00:19 +00:00
|
|
|
userchoice = input()
|
|
|
|
if userchoice == "y":
|
2018-08-04 14:11:03 +00:00
|
|
|
create_file(the_list)
|
2017-08-04 03:00:19 +00:00
|
|
|
elif userchoice == "n":
|
2018-08-07 14:19:30 +00:00
|
|
|
print(messages['bye'])
|
2017-08-04 03:00:19 +00:00
|
|
|
else:
|
2018-08-04 14:11:03 +00:00
|
|
|
print(messages['try_again'])
|
|
|
|
prompt_file_creation()
|
2017-08-04 03:00:19 +00:00
|
|
|
|
2018-08-07 14:19:30 +00:00
|
|
|
def main(args):
|
2018-08-04 14:11:03 +00:00
|
|
|
if os.path.exists(the_list) == True:
|
2018-08-07 14:19:30 +00:00
|
|
|
hangle_args(args)
|
2017-08-14 13:29:10 +00:00
|
|
|
else:
|
2018-08-04 14:11:03 +00:00
|
|
|
print(messages['file_not_found'])
|
|
|
|
prompt_file_creation()
|
2017-08-14 13:29:10 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2018-08-07 14:19:30 +00:00
|
|
|
main(sys.argv)
|