Skip to content

Commit a56cc72

Browse files
committed
added handling for remembering file paths, added gui option to disable zenity in GUI
1 parent f6b7fea commit a56cc72

File tree

1 file changed

+47
-46
lines changed

1 file changed

+47
-46
lines changed

koboldcpp.py

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@
110110
last_req_time = time.time()
111111
last_non_horde_req_time = time.time()
112112
currfinishreason = None
113-
113+
zenity_recent_dir = os.getcwd()
114+
zenity_permitted = True
114115

115116
saved_stdout = None
116117
saved_stderr = None
@@ -3456,37 +3457,28 @@ def stop(self):
34563457
sys.exit(0)
34573458

34583459
# Based on https://github.com/mathgeniuszach/xdialog/blob/main/xdialog/zenity_dialogs.py - MIT license | - Expanded version by Henk717
3460+
def zenity(filetypes=None, initialdir="", initialfile="", **kwargs) -> Tuple[int, str]:
3461+
import shutil
3462+
import subprocess
3463+
global zenity_recent_dir, zenity_permitted
34593464

3460-
def zenity_clean(txt: str):
3461-
return txt\
3462-
.replace("\\", "\\\\")\
3463-
.replace("$", "\\$")\
3464-
.replace("!", "\\!")\
3465-
.replace("*", "\\*")\
3466-
.replace("?", "\\?")\
3467-
.replace("&", "&")\
3468-
.replace("|", "|")\
3469-
.replace("<", "&lt;")\
3470-
.replace(">", "&gt;")\
3471-
.replace("(", "\\(")\
3472-
.replace(")", "\\)")\
3473-
.replace("[", "\\[")\
3474-
.replace("]", "\\]")\
3475-
.replace("{", "\\{")\
3476-
.replace("}", "\\}")\
3477-
3478-
def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tuple[int, str]:
3479-
import shutil, subprocess, os, platform
3480-
if not platform.system() == "Linux":
3481-
raise Exception("This feature should only be used on Linux, if you see this error there is no TK fallback implemented in the code.")
3465+
if not zenity_permitted:
3466+
raise Exception("Zenity disabled, attempting to use TK GUI.")
3467+
if sys.platform != "linux":
3468+
raise Exception("Zenity GUI is only usable on Linux, attempting to use TK GUI.")
34823469
zenity_bin = shutil.which("zenity")
34833470
if not zenity_bin:
3484-
zenity_bin = shutil.which("yad")
3471+
zenity_bin = shutil.which("yad")
34853472
if not zenity_bin:
3486-
raise Exception("Zenity not present")
3473+
raise Exception("Zenity not present, falling back to TK GUI.")
3474+
3475+
def zenity_clean(txt: str):
3476+
return txt.replace("\\", "\\\\").replace("$", "\\$").replace("!", "\\!").replace("*", "\\*")\
3477+
.replace("?", "\\?").replace("&", "&amp;").replace("|", "&#124;").replace("<", "&lt;").replace(">", "&gt;")\
3478+
.replace("(", "\\(").replace(")", "\\)").replace("[", "\\[").replace("]", "\\]").replace("{", "\\{").replace("}", "\\}")
34873479

34883480
# Build args based on keywords
3489-
args = ['/usr/bin/env', zenity_bin, '--'+typ]
3481+
args = ['/usr/bin/env', zenity_bin, '--file-selection']
34903482
for k, v in kwargs.items():
34913483
if v is True:
34923484
args.append(f'--{k.replace("_", "-").strip("-")}')
@@ -3503,7 +3495,7 @@ def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tupl
35033495

35043496
# Default filename and folder
35053497
if initialdir is None:
3506-
initialdir=os.getcwd()
3498+
initialdir=zenity_recent_dir
35073499
if initialfile is None:
35083500
initialfile=""
35093501
initialpath = os.path.join(initialdir, initialfile)
@@ -3513,53 +3505,54 @@ def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tupl
35133505
clean_env.pop("LD_LIBRARY_PATH", None)
35143506
clean_env["PATH"] = "/usr/bin:/bin"
35153507

3516-
proc = subprocess.Popen(
3508+
procres = subprocess.run(
35173509
args,
35183510
stdout=subprocess.PIPE,
35193511
stderr=subprocess.DEVNULL,
3520-
shell=False,
3521-
env=clean_env
3512+
env=clean_env,
3513+
check=False
35223514
)
3523-
stdout, _ = proc.communicate()
3524-
3525-
return (proc.returncode, stdout.decode('utf-8').strip())
3515+
result = procres.stdout.decode('utf-8').strip()
3516+
if procres.returncode==0 and result:
3517+
directory = result
3518+
if not os.path.isdir(result):
3519+
directory = os.path.dirname(result)
3520+
zenity_recent_dir = directory
3521+
return (procres.returncode, result)
35263522

35273523
# note: In this section we wrap around file dialogues to allow for zenity
3528-
35293524
def zentk_askopenfilename(**options):
35303525
try:
3531-
from os.path import isfile
3532-
result = zenity('file-selection', filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"))[1]
3533-
if result and not isfile(result):
3526+
result = zenity(filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"))[1]
3527+
if result and not os.path.isfile(result):
35343528
print("A folder was selected while we need a file, ignoring selection.")
35353529
return ''
3536-
except:
3530+
except Exception:
35373531
from tkinter.filedialog import askopenfilename
35383532
result = askopenfilename(**options)
35393533
return result
35403534

35413535
def zentk_askopenmultiplefilenames(**options):
35423536
try:
3543-
from os.path import isfile
3544-
files = zenity('file-selection', filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"), multiple=True, separator="\n")[1].splitlines()
3545-
result = tuple(filter(isfile, files))
3546-
except:
3537+
files = zenity(filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), title=options.get("title"), multiple=True, separator="\n")[1].splitlines()
3538+
result = tuple(filter(os.path.isfile, files))
3539+
except Exception:
35473540
from tkinter.filedialog import askopenfilenames
35483541
result = askopenfilenames(**options)
35493542
return result
35503543

35513544
def zentk_askdirectory(**options):
35523545
try:
3553-
result = zenity('file-selection', initialdir=options.get("initialdir"), title=options.get("title"), directory=True)[1]
3554-
except:
3546+
result = zenity(initialdir=options.get("initialdir"), title=options.get("title"), directory=True)[1]
3547+
except Exception:
35553548
from tkinter.filedialog import askdirectory
35563549
result = askdirectory(**options)
35573550
return result
35583551

35593552
def zentk_asksaveasfilename(**options):
35603553
try:
3561-
result = zenity('file-selection', filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), initialfile=options.get("initialfile"), title=options.get("title"), save=True)[1]
3562-
except:
3554+
result = zenity(filetypes=options.get("filetypes"), initialdir=options.get("initialdir"), initialfile=options.get("initialfile"), title=options.get("title"), save=True)[1]
3555+
except Exception:
35633556
from tkinter.filedialog import asksaveasfilename
35643557
result = asksaveasfilename(**options)
35653558
return result
@@ -3802,6 +3795,8 @@ def hide_tooltip(event):
38023795
admin_dir_var = ctk.StringVar()
38033796
admin_password_var = ctk.StringVar()
38043797

3798+
nozenity_var = ctk.IntVar(value=0)
3799+
38053800
curr_tab_idx = 0
38063801

38073802
def tabbuttonaction(name):
@@ -4439,6 +4434,12 @@ def kcpp_export_template():
44394434
ctk.CTkButton(extra_tab , text = "Generate LaunchTemplate", command = kcpp_export_template ).grid(row=5,column=0, stick="w", padx= 8, pady=2)
44404435
makelabel(extra_tab, "Analyze GGUF Metadata", 6, 0,tooltiptxt="Reads the metadata, weight types and tensor names in any GGUF file.")
44414436
ctk.CTkButton(extra_tab , text = "Analyze GGUF", command = analyze_gguf_model_wrapper ).grid(row=7,column=0, stick="w", padx= 8, pady=2)
4437+
if sys.platform == "linux":
4438+
def togglezenity(a,b,c):
4439+
global zenity_permitted
4440+
zenity_permitted = (nozenity_var.get()==0)
4441+
makecheckbox(extra_tab, "Use Classic FilePicker", nozenity_var, 20, tooltiptxt="Use the classic TKinter file picker instead.")
4442+
nozenity_var.trace("w", togglezenity)
44424443

44434444
# launch
44444445
def guilaunch():

0 commit comments

Comments
 (0)