Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import os #provides a portable way of using operating system functionalities
from users import checkUnique, newFriend, userDetails
global currentUser
'''Besides checkink the user credentials to access the chatbot, creates a new text file (if not exist) and storing two variables, nickname and city, which can be used further in different functions
Otherwise, check if data in created file exist. '''
def setup_time():
exists = os.path.isfile("settings.txt") # Returning boolean (True) if file "settings.thx" exist.
if not exists: # Otherwise, a new file will be created in write mode. Inputs name and city will be saved and stored.
f = open("settings.txt", "w+")
print("Hello! Who are you? ")
nick = input("")
nickConfirm = checkUnique(nick) #calls function to check if the user with the nickname inserted already has account
if nickConfirm == True:
firstName = str(userDetails(nick, "FIRST_NAME")) #calls function to get the first name of the user
lastName = str(userDetails(nick, "LAST_NAME")) #calls function to get the last name of the user
print("Hello " + firstName + " " + lastName)
else:
if nickConfirm != "off":
friendship = input("Since I don't know you, would you like to be my friend? ")
friendship = friendship.lower()
if friendship in ["yes", "y", "of course", "yap", "obviously", "indeed", "that is correct", "affirmative",
"ok"]:
nick = newFriend() #calls function to add new user
while nick == False:
nick = newFriend() #only adds user when agrees with all the conditions
else:
print("Ok I don't want to be your friend either. Bye!")
exit()
if nickConfirm != "off":
nick = str(userDetails(nick, "NICKNAME")) #calls function to get the nickname of the new user and saves it
city = str(userDetails(nick, "CITY")) #calls function to get the city of the new user and saves it
f.write("name:" + nick + "\n")
f.write("city:" + city + "\n")
f.write("state:complete")
f.close()
else: # This else statement will be run if a text file already exists and check its data.
settings_file = open("settings.txt")
for setting in settings_file:
if "state:complete" in setting: #While name and city already exist (state:complete) in "settings.txt"
settings_file.close() #Just close the file
break
'''The purpose of this function is to delete text file (data inside) after the main function will end,
so that a new data (name and city) can be created and stored next time while main code run.'''
def reset():
exists = os.path.isfile("settings.txt") #To avoid the error, checking if the file exists
if exists:
os.remove("settings.txt") #If True, then deleting the file
setup_time() #Calling function setup_time to create a new file and new data (name and city)
'''The "arg" is taken from the main code. It is either 0 or 1, depend on which argument from the file we need, either name or city.'''
def load_setting(arg):
try:
settings_file = open("settings.txt")
if arg == 0:
for setting in settings_file:
if "name:" in setting:
nick = setting.replace('name:','') #Taking just the user input name. Will be used while the program is running and printing username/calling the user.
settings_file.close()
return nick
elif arg == 1:
for setting in settings_file:
if "city:" in setting:
city = setting.replace('city:','') #Similar to above, but argument city which was defined by the user as an input on beginning can be used in function weather_ask.
settings_file.close()
return city
except:
pass