mirror of
https://github.com/KoboldAI/KoboldAI-Client.git
synced 2025-03-04 03:17:44 +01:00
This PR does three things when loading a story from within the browser: 1. Prevents an error if a story file is not valid JSON. 2. Catches an error is a file is JSON, but lacks an actions property. 3. Replaces getcwd() and instead uses the path of the script file itself in case someone does not start the app from the current working directory.
85 lines
2.8 KiB
Python
85 lines
2.8 KiB
Python
import tkinter as tk
|
|
from tkinter import filedialog
|
|
from os import getcwd, listdir, path
|
|
import json
|
|
|
|
#==================================================================#
|
|
# Generic Method for prompting for file path
|
|
#==================================================================#
|
|
def getsavepath(dir, title, types):
|
|
root = tk.Tk()
|
|
root.attributes("-topmost", True)
|
|
path = tk.filedialog.asksaveasfile(
|
|
initialdir=dir,
|
|
title=title,
|
|
filetypes = types,
|
|
defaultextension="*.*"
|
|
)
|
|
root.destroy()
|
|
if(path != "" and path != None):
|
|
return path.name
|
|
else:
|
|
return None
|
|
|
|
#==================================================================#
|
|
# Generic Method for prompting for file path
|
|
#==================================================================#
|
|
def getloadpath(dir, title, types):
|
|
root = tk.Tk()
|
|
root.attributes("-topmost", True)
|
|
path = tk.filedialog.askopenfilename(
|
|
initialdir=dir,
|
|
title=title,
|
|
filetypes = types
|
|
)
|
|
root.destroy()
|
|
if(path != "" and path != None):
|
|
return path
|
|
else:
|
|
return None
|
|
|
|
#==================================================================#
|
|
# Generic Method for prompting for directory path
|
|
#==================================================================#
|
|
def getdirpath(dir, title):
|
|
root = tk.Tk()
|
|
root.attributes("-topmost", True)
|
|
path = filedialog.askdirectory(
|
|
initialdir=dir,
|
|
title=title
|
|
)
|
|
root.destroy()
|
|
if(path != "" and path != None):
|
|
return path
|
|
else:
|
|
return None
|
|
|
|
#==================================================================#
|
|
# Returns an array of dicts containing story files in /stories
|
|
#==================================================================#
|
|
def getstoryfiles():
|
|
list = []
|
|
for file in listdir(path.dirname(path.realpath(__file__))+"/stories"):
|
|
if file.endswith(".json"):
|
|
ob = {}
|
|
ob["name"] = file.replace(".json", "")
|
|
f = open(path.dirname(path.realpath(__file__))+"/stories/"+file, "r")
|
|
try:
|
|
js = json.load(f)
|
|
except:
|
|
print("Browser loading error: Story file is malformed or not a JSON file.")
|
|
f.close()
|
|
continue
|
|
f.close()
|
|
try:
|
|
ob["actions"] = len(js["actions"])
|
|
except TypeError:
|
|
print("Browser loading error: Story file has incorrect format.")
|
|
list.append(ob)
|
|
return list
|
|
|
|
#==================================================================#
|
|
# Returns True if json file exists with requested save name
|
|
#==================================================================#
|
|
def saveexists(name):
|
|
return path.exists(path.dirname(os.path.realpath(__file__))+"/stories/"+name+".json") |