Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from classSkill import *
- import time
- import json
- listOfSkills = []
- listOfTalents = []
- curLocation = os.path.dirname(os.path.abspath(__file__))
- settingsFile = os.path.dirname(os.path.abspath(__file__))+"\\settings.json"
- defaultSettings = {
- "dir":os.path.dirname(os.path.abspath(__file__))+"\\dataFiles\\output\\",
- "xml":False,
- "csv":True,
- "read format":".csv",
- "other format":".xml",
- "wait": .35
- }
- title = " Torroes Prime's Text Adventure Engine Library builder "
- outputForm = "Current Output format: "
- outDirectory = curLocation+"\\dataFiles\\output\\"
- outFormat = ".csv"
- otherFormat = ".xml"
- readFormat = ".csv"
- waitValue = .45
- lastSkillSourceFile = "list_skills.csv"
- lastTalentSourceFile = "list_talents_data.csv"
- skillLibraryFile = "library_Skill"+outFormat
- talentLibraryFile = "library_Talents"+outFormat
- lastSourceDirectory = os.path.dirname(os.path.abspath(__file__))+"\\"
- def main():
- """method to display main screen and take user selection at top level"""
- global settingsFile
- readSettings(settingsFile)
- options = [f"Create a new {outFormat} library from a {readFormat} file","Change options","Quit program"]
- print(title)
- print("███╗ ███╗ █████╗ ██╗███╗ ██╗ ███╗ ███╗███████╗███╗ ██╗██╗ ██╗")
- print("████╗ ████║██╔══██╗██║████╗ ██║ ████╗ ████║██╔════╝████╗ ██║██║ ██║")
- print("██╔████╔██║███████║██║██╔██╗ ██║ ██╔████╔██║█████╗ ██╔██╗ ██║██║ ██║")
- print("██║╚██╔╝██║██╔══██║██║██║╚██╗██║ ██║╚██╔╝██║██╔══╝ ██║╚██╗██║██║ ██║")
- print("██║ ╚═╝ ██║██║ ██║██║██║ ╚████║ ██║ ╚═╝ ██║███████╗██║ ╚████║╚██████╔╝")
- print("╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝╚═╝ ╚═══╝ ╚═════╝ ")
- count = 1
- for key in options:
- print(count," -- ",key)
- count += 1
- selection = int(input("Please type the number of your seleciton: "))
- if selection == 1:
- #call methods to view the screen to create a new skill or talent library entry
- createNewFile()
- elif selection == 2:
- #call methods to view the screen to change serttings
- settings()
- elif selection == 3:
- saveSettings(settingsFile)
- quit()
- else:
- print("That is not a valid selection, dumb ass.")
- main()
- def settings():
- """method to display settings screen and take user selection to make changes to output format or output directory"""
- print(title)
- print("\n")
- print("\n")
- print("███████╗███████╗████████╗████████╗██╗███╗ ██╗ ██████╗ ███████╗")
- print("██╔════╝██╔════╝╚══██╔══╝╚══██╔══╝██║████╗ ██║██╔════╝ ██╔════╝")
- print("███████╗█████╗ ██║ ██║ ██║██╔██╗ ██║██║ ███╗███████╗")
- print("╚════██║██╔══╝ ██║ ██║ ██║██║╚██╗██║██║ ██║╚════██║")
- print("███████║███████╗ ██║ ██║ ██║██║ ╚████║╚██████╔╝███████║")
- print("╚══════╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ ╚══════╝")
- print(" The current settings are: ")
- print("Current Output directory:\n",outDirectory)
- print(f"Current program location is {curLocation}")
- if outFormat == ".xml":
- print(outputForm,".xml")
- else:
- print(outputForm,".csv")
- print("What do you want to do?")
- optionNames = ["Change output directory","Change output format","change input format","Restore Default Settings","Change nothing and return to main menu"]
- optionCount = 0
- for entry in optionNames:
- print(f" {optionCount+1} -- {entry}")
- optionCount += 1
- selection = "q"
- while selection != len(optionNames):
- selection = int(input("What would you like to do? "))
- if selection == 1:
- #call method to change the output directory
- changeOutputDirectory()
- selection = len(optionNames)
- elif selection == 2:
- #call method to change the output format
- changeFormat()
- selection = len(optionNames)
- elif selection == 3:
- #call method to change input format
- selection = len(optionNames)
- pass
- elif selection == 4:
- #call method to restore default settings
- resetToDefaults()
- time.sleep(waitValue)
- settings()
- elif selection == len(optionNames):
- #call method to display main screen
- main()
- else:
- print("That is not a valid selection, dumb ass.")
- time.sleep(waitValue)
- settings()
- def changeReadFormat():
- """method to change input format from csv to xml or vice versa"""
- global outFormat
- global otherFormat
- print(title)
- print("╔═╗┬ ┬┌─┐┌┐┌┌─┐┌─┐ ╦┌┐┌┌─┐┬ ┬┌┬┐ ╔═╗┌─┐┬─┐┌┬┐┌─┐┌┬┐")
- print("║ ├─┤├─┤││││ ┬├┤ ║│││├─┘│ │ │ ╠╣ │ │├┬┘│││├─┤ │ ")
- print("╚═╝┴ ┴┴ ┴┘└┘└─┘└─┘ ╩┘└┘┴ └─┘ ┴ ╚ └─┘┴└─┴ ┴┴ ┴ ┴ ")
- print(f"Currently the output format is set to {outFormat} format. Do you with to change it to {otherFormat}? Type 'Y' to confirm or 'N' to abort.")
- userConfirmation = input()
- if userConfirmation.lower() == "y":
- outFormat, otherFormat = otherFormat,outFormat
- print(f" The outformat has been changed to {outFormat}")
- time.sleep(settings["wait"])
- main()
- def changeFormat():
- """method to change output format from csv to xml or vice versa"""
- global outFormat
- global otherFormat
- print(title)
- print("╔═╗┬ ┬┌─┐┌┐┌┌─┐┌─┐ ╔═╗┬ ┬┌┬┐┌─┐┬ ┬┌┬┐ ╔═╗┌─┐┬─┐┌┬┐┌─┐┌┬┐")
- print("║ ├─┤├─┤││││ ┬├┤ ║ ║│ │ │ ├─┘│ │ │ ╠╣ │ │├┬┘│││├─┤ │ ")
- print("╚═╝┴ ┴┴ ┴┘└┘└─┘└─┘ ╚═╝└─┘ ┴ ┴ └─┘ ┴ ╚ └─┘┴└─┴ ┴┴ ┴ ┴ ")
- print(f"Currently the output format is set to {outFormat} format. Do you with to change it to {otherFormat}? Type 'Y' to confirm or 'N' to abort.")
- userConfirmation = input()
- if userConfirmation.lower() == "y":
- outFormat, otherFormat = otherFormat,outFormat
- print(f" The outformat has been changed to {outFormat}")
- time.sleep(waitValue)
- else:
- settings()
- settings()
- def changeOutputDirectory():
- global outDirectory
- """method to change the output directory to the supplied directory."""
- print("\n\n\n\n")
- print(title)
- print("╔═╗┬ ┬┌─┐┌┐┌┌─┐┌─┐ ╔═╗┬ ┬┌┬┐┌─┐┬ ┬┌┬┐ ╔╦╗┬┬─┐┌─┐┌─┐┌┬┐┌─┐┬─┐┬ ┬")
- print("║ ├─┤├─┤││││ ┬├┤ ║ ║│ │ │ ├─┘│ │ │ ║║│├┬┘├┤ │ │ │ │├┬┘└┬┘")
- print("╚═╝┴ ┴┴ ┴┘└┘└─┘└─┘ ╚═╝└─┘ ┴ ┴ └─┘ ┴ ═╩╝┴┴└─└─┘└─┘ ┴ └─┘┴└─ ┴ ")
- print("Current Output directory:",outDirectory)
- newDirectory = input("Please type in the directory you would like to change the output directory to or simply press enter to return to the main menu:")
- if newDirectory != "":
- try:
- os.chdir(newDirectory)
- print("Current working directory: {0}".format(os.getcwd()))
- outDirectory = newDirectory
- time.sleep(waitValue)
- except FileNotFoundError:
- print("Directory: {0} does not exist".format(newDirectory))
- time.sleep(settings["wait"])
- except NotADirectoryError:
- print("{0} is not a directory".format(newDirectory))
- time.sleep(settings["wait"])
- except PermissionError:
- print("You do not have permissions to change to {0}".format(newDirectory))
- time.sleep(settings["wait"])
- main()
- def checkFileFormat(fileName):
- """method to call indicated read method based on the extenstion of the supplied filename """
- pass
- def resetToDefaults():
- """method to put program settings back to defaults"""
- global outDirectory
- global outFormat
- global otherFormat
- global readFormat
- global waitValue
- global settingsFile
- outDirectory = defaultSettings["dir"]
- outFormat = ".csv"
- otherFormat = defaultSettings["other format"]
- readFormat = defaultSettings["read format"]
- waitValue = defaultSettings["wait"]
- saveSettings(settingsFile)
- def readXmlFile(fileName):
- """method to open supplied xml file name and read the contents into a list of lines, then returns to that list to calling entity. Displays an error if a csv file is supplied."""
- pass
- def readcsvFile(fileName):
- """method to open supplied csv file name and read the contents into a list of lines, then returns to that list to calling entity. Displays an error if an xml file is supplied."""
- dataList = []
- with open(fileName,'r') as src:
- dataList = src.readlines()
- return dataList
- def checkDir(directory):
- """method to check if a supplied directory exists and if not, calls the getNewDirectory method to prompt the user to provide a new directory."""
- if directory[-1] != "\\":
- directory+= "\\"
- while os.path.isdir(directory) == False:
- print(f"directory: \"{directory}\" not found.")
- directory = getNewDirectory()
- if directory[-1] != "\\":
- directory+= "\\"
- return directory
- def checkFile(fileName,directory):
- """method to check if supplied filenamme exists in supplied directory. Returns boolean"""
- if fileName in os.listdir(directory):
- return True
- else:
- return False
- def createNewFile():
- print("\n\n\n\n\n")
- print(title)
- print(" o-o o o o o--o o ")
- print(" / | |\ | | o | ")
- print("O o-o o-o oo -o- o-o | \ | o-o o o o O-o | o-o ")
- print(" \ | |-' | | | |-' | \| |-' \ / \ / | | | |-' ")
- print(" o-o o o-o o-o- o o-o o o o-o o o o | o o-o ")
- options = ["Create a new skills library file","Create a new talents library file","Change settings","Do nothing and return to the main menu"]
- print("Current Output directory:\n",outDirectory)
- print(f"Current Output format is {outFormat}")
- count = 1
- for option in options:
- print( count," -- ",option)
- count += 1
- userSelection = "q"
- while str(type(userSelection)) != "<class 'int'>":
- userSelection = int(input("Ente the number of your desired operation:"))
- if userSelection == 1:
- #call method to create a new skills library file
- makeNewFile(False)
- elif userSelection == 2:
- #call method to create a new talents library file
- makeNewFile(True)
- elif userSelection == 3:
- #call main method.
- settings()
- userSelection = 4
- elif userSelection == 4:
- #call main method.
- main()
- else:
- print("That is an invalid selecton. Try reading next time, okay?")
- createNewFile()
- def makeNewSkills(listOfLines):
- """method to take a list of lines and compile a new list of skill objects based on the contents of the supplied file. returns list to calling entity"""
- for entry in listOfLines:
- data = entry.split(",")
- newEntry = ""
- if "[]" in data[0]:
- newEntry = newSkillGroup(data)
- else:
- newEntry = newSkill(data)
- if str(type(newEntry)) == "<class 'classSkill.skill'>":
- listOfSkills.append(skill(newEntry))
- elif str(type(newEntry)) == "<class 'list'>":
- for entry in newEntry:
- listOfSkills.append(skill(entry))
- def writecsvFile(listOfData,csvFileToWriteTo):
- data = ""
- with open(csvFileToWriteTo,'w') as out:
- for item in listOfData:
- entryListing = item.getData():
- for datum in entryListing:
- data += datum+","
- out.write(data)
- def newSkill(lineOfData):
- """method to take a list of lines, and create a single new skill object."""
- n = lineOfData[0]
- a = lineOfData[1]
- c =lineOfData[2]
- t =lineOfData[3]
- d = lineOfData[4]
- newSkillItem = skill(name=n,advanced=a,characteristic=c,testType=t,description=d)
- return newSkillItem
- def newSkillGroup(listOfData):
- """method to take a list of lines, and create a list containing the data for a group of skills. returns a list of lines"""
- skillItems = []
- name = listOfData[0][:-2]
- advanced = listOfData[1]
- characteristic =listOfData[2]
- testType =listOfData[3]
- description = listOfData[4]
- for key in groups["skills"][name.lower()]:
- skillGroupName = name+"("+key+")"
- skillItems.append(skill(skillGroupName,advanced,characteristic,testType,description))
- return skillItems
- def makeNewTalents(listOfLines):
- pass
- def saveSettings(settingsFile):
- """method to write the programs settings to an external files so they are saved between uses"""
- global outDirectory
- global outFormat
- global otherFormat
- global readFormat
- global waitValue
- global lastSkillSourceFile
- global lastTalentSourceFile
- settings = {
- "dir":outDirectory,
- "outFormat":outFormat,
- "otherFormat":otherFormat,
- "read format":readFormat,
- "wait": waitValue,
- "skills source file":lastSkillSourceFile,
- "talents source file":lastTalentSourceFile,
- "last source directory":lastSourceDirectory
- }
- with open(settingsFile,'w') as s:
- s.write(json.dumps(settings))
- def readSettings(settingsFile):
- """method to read the settings file and apply those settings to the current values"""
- with open(settingsFile,'r') as src:
- for line in src.readlines():
- readData = json.loads(line)
- global outDirectory
- global outFormat
- global otherFormat
- global readFormat
- global waitValue
- global lastSkillSourceFile
- global lastTalentSourceFile
- global lastSourceDirectory
- #outDirectory = readData["dir"]
- outFormat = readData["outFormat"]
- otherFormat = readData["otherFormat"]
- readFormat = readData["read format"]
- waitValue = readData["wait"]
- lastSkillSourceFile = readData["skills source file"]
- lastTalentSourceFile =readData["talents source file"]
- lastSourceDirectory = readData["last source directory"]
- def newFile():
- print(title)
- print("███╗ ██╗███████╗██╗ ██╗ ███████╗██╗██╗ ███████╗")
- print("████╗ ██║██╔════╝██║ ██║ ██╔════╝██║██║ ██╔════╝")
- print("██╔██╗ ██║█████╗ ██║ █╗ ██║ █████╗ ██║██║ █████╗ ")
- print("██║╚██╗██║██╔══╝ ██║███╗██║ ██╔══╝ ██║██║ ██╔══╝ ")
- print("██║ ╚████║███████╗╚███╔███╔╝ ██║ ██║███████╗███████╗")
- print("╚═╝ ╚═══╝╚══════╝ ╚══╝╚══╝ ╚═╝ ╚═╝╚══════╝╚══════╝")
- print(" ")
- options = ["Create a new skills library file","Create a new Talents library file","Do nothing and return to the main menu"]
- optionNum = 1
- for entry in options:
- print(f" {optionNum} -- {entry}")
- optionNum += 1
- selection = "q'"
- while selection != 'x':
- selection = int(input("What would you like to do? "))
- if selection == 1:
- makeNewFile(False)
- main()
- # selection = 3
- elif selection == 2:
- makeNewFile(True)
- selection = 3
- elif selection == 3:
- main()
- selection = 3
- else:
- print("That is not a valid selection, dumb ass.")
- def readFile(fileName):
- content = ""
- if readFormat == ".csv":
- content = readcsvFile(fileName)
- else:
- content = readXmlFile(fileName)
- return content
- def makeNewFile(talent=True):
- global lastSourceDirectory
- global lastSkillSourceFile
- global lastTalentSourceFile
- fullSource=lastSourceDirectory
- if talent == False:
- readFileType = "skills"
- source = lastSkillSourceFile
- else:
- readFileType = "talents"
- source = lastTalentSourceFile
- print(f"Last time you built a {readFileType} library, you read a file named {source} located in {lastSourceDirectory}. Do you want to use that same file? Type 'Y' to confirm. Type 'N' to supply a different file name.")
- userSelection = input()
- if userSelection.lower() == "n":
- source = input("Please provide the name of the file you wish to read in from: ")
- dirLocation = checkDir(lastSourceDirectory)
- goodCheck = checkFile(source,dirLocation)
- print("goodCheck: ",goodCheck)
- while goodCheck == False:
- print(f"A file named \"{source}\" was not found in {dirLocation}. Is it somewhere else? Type 'Y' to supply an alternate directory.")
- userResponse = input()
- if userResponse.lower() != "y":
- print("I'm sorry. You should double check the file name and the location. Returning to the main menu.")
- time.sleep(waitValue)
- main()
- else:
- newDirectory = getNewDirectory()
- goodCheck = checkFile(source,newDirectory)
- dirLocation = newDirectory
- fullSource = newDirectory+source
- fullSource =dirLocation+source
- if talent == True:
- lastTalentSourceFile = source
- makeNewTalents(readFile(fullSource))
- elif talent == False:
- lastSkillSourceFile = source
- makeNewSkills(readFile(fullSource))
- writecsvFile(listOfSkills,outDirectory+"dataSkills.csv")
- for entry in listOfSkills:
- print(entry)
- saveSettings(settingsFile)
- newFile()
- def getNewDirectory():
- """askes the user for a new directory and returns the string to the calling entity"""
- newDirectory = input("Please type the full directory: ")
- return newDirectory
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement