nicethings/nicethings

74 lines
2.5 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env python3
2017-09-02 18:02:09 +00:00
import re
import os
import sys
import random
2018-08-04 14:11:03 +00:00
the_list = "list.txt"
program_name = "nicethings"
top_line = "-----------------------------------------\n"
bottom_line = "\n-----------------------------------------"
2018-08-04 14:11:03 +00:00
messages = {'list_created': "{} created, try your command again and it should work :)".format(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),
2018-08-07 14:19:30 +00:00
'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),
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)}
def write_to_file(list_file, content):
temp = open(list_file, 'a')
temp.write(content)
temp.close()
2018-08-04 14:11:03 +00:00
def create_file(list_file):
open(list_file, 'a').close()
print(messages['list_created'])
2018-08-04 14:11:03 +00:00
def show_random_line(list_file):
thefile = open(list_file, 'r').read(1)
temp = open(list_file, 'r')
contents = temp.read(1)
if not contents:
print(messages['list_empty'])
else:
print(random.choice(list(open(the_list, 'r'))).rstrip('\n'))
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)
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'])
else:
2018-08-07 14:19:30 +00:00
add_to_list(args[1])
2018-08-04 14:11:03 +00:00
def prompt_file_creation():
userchoice = input()
if userchoice == "y":
2018-08-04 14:11:03 +00:00
create_file(the_list)
elif userchoice == "n":
2018-08-07 14:19:30 +00:00
print(messages['bye'])
else:
2018-08-04 14:11:03 +00:00
print(messages['try_again'])
prompt_file_creation()
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)
else:
2018-08-04 14:11:03 +00:00
print(messages['file_not_found'])
prompt_file_creation()
if __name__ == '__main__':
2018-08-07 14:19:30 +00:00
main(sys.argv)