110
110
last_req_time = time .time ()
111
111
last_non_horde_req_time = time .time ()
112
112
currfinishreason = None
113
-
113
+ zenity_recent_dir = os .getcwd ()
114
+ zenity_permitted = True
114
115
115
116
saved_stdout = None
116
117
saved_stderr = None
@@ -3456,37 +3457,28 @@ def stop(self):
3456
3457
sys .exit (0 )
3457
3458
3458
3459
# 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
3459
3464
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 ("<" , "<" )\
3470
- .replace (">" , ">" )\
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." )
3482
3469
zenity_bin = shutil .which ("zenity" )
3483
3470
if not zenity_bin :
3484
- zenity_bin = shutil .which ("yad" )
3471
+ zenity_bin = shutil .which ("yad" )
3485
3472
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 ("&" , "&" ).replace ("|" , "|" ).replace ("<" , "<" ).replace (">" , ">" )\
3478
+ .replace ("(" , "\\ (" ).replace (")" , "\\ )" ).replace ("[" , "\\ [" ).replace ("]" , "\\ ]" ).replace ("{" , "\\ {" ).replace ("}" , "\\ }" )
3487
3479
3488
3480
# Build args based on keywords
3489
- args = ['/usr/bin/env' , zenity_bin , '--' + typ ]
3481
+ args = ['/usr/bin/env' , zenity_bin , '--file-selection' ]
3490
3482
for k , v in kwargs .items ():
3491
3483
if v is True :
3492
3484
args .append (f'--{ k .replace ("_" , "-" ).strip ("-" )} ' )
@@ -3503,7 +3495,7 @@ def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tupl
3503
3495
3504
3496
# Default filename and folder
3505
3497
if initialdir is None :
3506
- initialdir = os . getcwd ()
3498
+ initialdir = zenity_recent_dir
3507
3499
if initialfile is None :
3508
3500
initialfile = ""
3509
3501
initialpath = os .path .join (initialdir , initialfile )
@@ -3513,53 +3505,54 @@ def zenity(typ, filetypes=None, initialdir="", initialfile="", **kwargs) -> Tupl
3513
3505
clean_env .pop ("LD_LIBRARY_PATH" , None )
3514
3506
clean_env ["PATH" ] = "/usr/bin:/bin"
3515
3507
3516
- proc = subprocess .Popen (
3508
+ procres = subprocess .run (
3517
3509
args ,
3518
3510
stdout = subprocess .PIPE ,
3519
3511
stderr = subprocess .DEVNULL ,
3520
- shell = False ,
3521
- env = clean_env
3512
+ env = clean_env ,
3513
+ check = False
3522
3514
)
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 )
3526
3522
3527
3523
# note: In this section we wrap around file dialogues to allow for zenity
3528
-
3529
3524
def zentk_askopenfilename (** options ):
3530
3525
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 ):
3534
3528
print ("A folder was selected while we need a file, ignoring selection." )
3535
3529
return ''
3536
- except :
3530
+ except Exception :
3537
3531
from tkinter .filedialog import askopenfilename
3538
3532
result = askopenfilename (** options )
3539
3533
return result
3540
3534
3541
3535
def zentk_askopenmultiplefilenames (** options ):
3542
3536
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 :
3547
3540
from tkinter .filedialog import askopenfilenames
3548
3541
result = askopenfilenames (** options )
3549
3542
return result
3550
3543
3551
3544
def zentk_askdirectory (** options ):
3552
3545
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 :
3555
3548
from tkinter .filedialog import askdirectory
3556
3549
result = askdirectory (** options )
3557
3550
return result
3558
3551
3559
3552
def zentk_asksaveasfilename (** options ):
3560
3553
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 :
3563
3556
from tkinter .filedialog import asksaveasfilename
3564
3557
result = asksaveasfilename (** options )
3565
3558
return result
@@ -3802,6 +3795,8 @@ def hide_tooltip(event):
3802
3795
admin_dir_var = ctk .StringVar ()
3803
3796
admin_password_var = ctk .StringVar ()
3804
3797
3798
+ nozenity_var = ctk .IntVar (value = 0 )
3799
+
3805
3800
curr_tab_idx = 0
3806
3801
3807
3802
def tabbuttonaction (name ):
@@ -4439,6 +4434,12 @@ def kcpp_export_template():
4439
4434
ctk .CTkButton (extra_tab , text = "Generate LaunchTemplate" , command = kcpp_export_template ).grid (row = 5 ,column = 0 , stick = "w" , padx = 8 , pady = 2 )
4440
4435
makelabel (extra_tab , "Analyze GGUF Metadata" , 6 , 0 ,tooltiptxt = "Reads the metadata, weight types and tensor names in any GGUF file." )
4441
4436
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 )
4442
4443
4443
4444
# launch
4444
4445
def guilaunch ():
0 commit comments