76 lines
2.6 KiB
Python
Executable File
76 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import re
|
|
import os
|
|
import sys
|
|
import random
|
|
|
|
# edit the path below accordingly
|
|
the_list = "/home/m455/code/nicethings/list.txt"
|
|
program_name = "nicethings"
|
|
top_line = "-----------------------------------------\n"
|
|
bottom_line = "\n-----------------------------------------"
|
|
|
|
messages = {'list_created': "{} created\n{} now readable and writeable by other users".format(the_list, the_list),
|
|
'list_empty': "sorry, there's nothing in {}\nTry adding something to the list by typing:\n{} \"your message here\"".format(the_list, program_name),
|
|
'args_too_many': "sorry, i only accept one argument.\ntry using:\n{} \"your message here\"".format(program_name),
|
|
'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),
|
|
'file_not_found': "{} doesn't exist\ndo you want me to make a {} file right now? (y/n)".format(the_list, the_list)}
|
|
|
|
def write_to_file(a_file, content):
|
|
temp = open(a_file, 'a')
|
|
temp.write(content)
|
|
temp.close()
|
|
|
|
def create_file(a_file):
|
|
open(a_file, 'a').close()
|
|
os.chmod(the_list, 0o666)
|
|
print(messages['list_created'])
|
|
|
|
def show_random_line(a_file):
|
|
thefile = open(a_file, 'r').read(1)
|
|
temp = open(a_file, 'r')
|
|
contents = temp.read(1)
|
|
if not contents:
|
|
print(messages['list_empty'])
|
|
else:
|
|
print(random.choice(list(open(the_list, 'r'))).rstrip('\n'))
|
|
|
|
def add_to_list(args):
|
|
write_to_file(the_list, args + "\n")
|
|
print(top_line + messages['item_added'] + args + bottom_line)
|
|
|
|
def hangle_args(args):
|
|
if len(args) <= 1:
|
|
show_random_line(the_list)
|
|
elif len(args) > 2:
|
|
print(messages['too_many_args'])
|
|
elif args[1].isspace() == True or args[1] == "":
|
|
print(messages['input_null'])
|
|
else:
|
|
add_to_list(args[1])
|
|
|
|
def prompt_file_creation():
|
|
userchoice = input()
|
|
if userchoice == "y":
|
|
create_file(the_list)
|
|
elif userchoice == "n":
|
|
print(messages['bye'])
|
|
else:
|
|
print(messages['try_again'])
|
|
prompt_file_creation()
|
|
|
|
def main(args):
|
|
if os.path.exists(the_list) == True:
|
|
hangle_args(args)
|
|
else:
|
|
print(messages['file_not_found'])
|
|
prompt_file_creation()
|
|
|
|
if __name__ == '__main__':
|
|
main(sys.argv)
|