1+ import util .colors as color
2+ import PySimpleGUI as sg
3+ from dataclasses import dataclass
4+ from util .file_hash import get_file_hash
5+ import os
6+ import re
7+
8+ FILE_ICON = "📑"
9+ DIRECTORY_ICON = "📂"
10+ UP_DIRECTORY_TXT = "_____UP_____"
11+ SYSTEM_DRIVE_ICON = "💻"
12+ FILE_EXPLORER_LSTBOX_KEY = "-file_explorer_lstbox-"
13+ FOLDER_INP_KEY = '-folder_inp_browse-'
14+ FONT = 'Arial 12'
15+
16+
17+ # "-browser_lb-dclick-"
18+ _file_extensions = {".ckpt" ,".safetensors" }
19+
20+ @dataclass
21+ class CurrentDirectory :
22+ path : str
23+
24+ @dataclass
25+ class SelectedFileSystem :
26+ path : str = None
27+
28+ @dataclass
29+ class SelectedFolder :
30+ path : str = None
31+
32+ def setup (window ):
33+ folder_browse_inp_elem :sg .Input = window [FOLDER_INP_KEY ]
34+ file_explorer_lstbox_key_elem :sg .Listbox = window [FILE_EXPLORER_LSTBOX_KEY ]
35+ file_explorer_lstbox_key_elem .bind ('<Double-Button-1>' ,"dclick-" )
36+ folder_browse_inp_elem .update (CurrentDirectory .path )
37+ return folder_browse_inp_elem ,file_explorer_lstbox_key_elem
38+
39+ def get_system_files (folder_path , sort = "ASC" ):
40+ CurrentDirectory .path = folder_path
41+
42+ if not os .path .isdir (folder_path ):
43+ print ("Error: Invalid folder path." )
44+ return
45+
46+ items = os .listdir (folder_path )
47+
48+ filtered_items = []
49+
50+ for item in items :
51+
52+ full_path = os .path .join (folder_path , item )
53+
54+ if os .path .isfile (full_path ):
55+ if any (item .endswith (ext ) for ext in _file_extensions ):
56+ new_item = f'[{ FILE_ICON } ] [{ item } ] [{ get_file_hash (full_path )} ]'
57+ filtered_items .append (new_item )
58+ else :
59+ new_item = f'[{ DIRECTORY_ICON } ] [{ item } ]'
60+ filtered_items .append (new_item )
61+
62+ if sort == "ASC" :
63+ filtered_items = sorted (filtered_items , key = lambda x : x .lower ())
64+ elif sort == "DESC" :
65+ filtered_items = sorted (filtered_items , key = lambda x : x .lower (), reverse = True )
66+ else :
67+ print ("Error: Invalid sort order. Must be 'ASC' or 'DESC'." )
68+ return
69+
70+ filtered_items .insert (0 ,UP_DIRECTORY_TXT )
71+ return filtered_items
72+
73+ def get_system_files_list ():
74+ """
75+ Returns list of filenames or directories to display in the listbox
76+ :return: List of filenames or directories
77+ :rtype: List[str]
78+ """
79+ return get_system_files (os .path .join (os .getenv ("SystemDrive" ), "\\ " ))
80+
81+ def layout ():
82+
83+ layout = sg .Frame ('' ,[
84+ [
85+ sg .Listbox (key = FILE_EXPLORER_LSTBOX_KEY ,values = get_system_files_list (),text_color = color .TERMINAL_BLUE ,background_color = color .GRAY_1111 ,
86+ select_mode = sg .SELECT_MODE_BROWSE ,expand_x = True ,expand_y = True , size = (70 ,25 ),bind_return_key = True ,
87+ enable_events = True ,sbar_relief = sg .RELIEF_FLAT ,sbar_trough_color = 0 ,sbar_width = 20 ,font = "Ariel 11 " ,),
88+ ],
89+ ],expand_x = True ,expand_y = True ,border_width = 0 ,relief = sg .RELIEF_FLAT ,element_justification = "c" ,background_color = color .GRAY_9900 )
90+
91+
92+ return layout
93+
94+ def get_system_drives ():
95+ return [f"[{ SYSTEM_DRIVE_ICON } ] [{ chr (drive )} :/]" for drive in range (ord ("C" ), ord ("Z" ) + 1 ) if os .path .exists (f"{ chr (drive )} :/" )]
96+
97+ def mouse_clicks_events (event , values ,input_files_directory_widget ,file_explorer_lstbox_key_elem ):
98+ if event == FILE_EXPLORER_LSTBOX_KEY :
99+ # get the selected item from the widget
100+ system_file_values_list = values [FILE_EXPLORER_LSTBOX_KEY ]
101+ # dedect if is root drive like C:\ or D:\ or E:\ etc
102+ if system_file_values_list [0 ][1 :] == ":/" :
103+ # print("root drive")
104+ pass
105+ elif system_file_values_list :
106+ system_file_values = system_file_values_list [0 ]
107+ if system_file_values == UP_DIRECTORY_TXT :
108+ SelectedFolder .path = None
109+ SelectedFileSystem .path = None
110+
111+ else :
112+ system_file_list = re .findall ("\[([^\]]+)\]" , system_file_values )
113+ icon = system_file_list [0 ]
114+ system_file = system_file_list [1 ]
115+ system_file_fullpath = os .path .normpath (os .path .join (CurrentDirectory .path , system_file ))
116+
117+ if icon == DIRECTORY_ICON :
118+ SelectedFolder .path = system_file_fullpath
119+ SelectedFileSystem .path = None
120+ elif icon == FILE_ICON :
121+ SelectedFileSystem .path = system_file_fullpath
122+ SelectedFolder .path = None
123+
124+ if event == f"{ FILE_EXPLORER_LSTBOX_KEY } dclick-" :
125+ system_file_values_list = values [FILE_EXPLORER_LSTBOX_KEY ]
126+ if system_file_values_list :
127+ system_file_values = system_file_values_list [0 ]
128+ back_path = input_files_directory_widget .get ()
129+ if system_file_values == UP_DIRECTORY_TXT :
130+ # go back to the previous directory
131+ if back_path [1 :] == ":\\ " or back_path [1 :] == ":/" : # if is root drive
132+ # print("root drive")
133+ input_files_directory_widget .update ("" )
134+ file_explorer_lstbox_key_elem .update (get_system_drives ())
135+ else :
136+ # print("not root drive")
137+ try :
138+ back_path = os .path .split (input_files_directory_widget .get ())[0 ]
139+ file_explorer_lstbox_key_elem .update (get_system_files (f"{ back_path } " , sort = "ASC" ))
140+ input_files_directory_widget .update (f"{ back_path } " )
141+ except Exception as e :
142+ # print(e)
143+ pass
144+ else :
145+ system_file_list = re .findall ("\[([^\]]+)\]" , system_file_values )
146+ icon = system_file_list [0 ]
147+ system_file = system_file_list [1 ]
148+ system_file_fullpath = os .path .normpath (os .path .join (CurrentDirectory .path , system_file ))
149+
150+ if icon == SYSTEM_DRIVE_ICON :
151+ input_files_directory_widget .update (system_file_fullpath )
152+ file_explorer_lstbox_key_elem .update (get_system_files (system_file_fullpath , sort = "ASC" ))
153+ if icon == DIRECTORY_ICON :
154+ try :
155+ listing_ = get_system_files (system_file_fullpath , sort = "ASC" )
156+ file_explorer_lstbox_key_elem .update (listing_ )
157+ input_files_directory_widget .update (system_file_fullpath )
158+ except :
159+ pass
160+
161+ def folder_inp_browse_layout (visible_ ,disabled = False ):
162+ layout = sg .Frame ('' ,[
163+ [
164+ sg .Input (key = FOLDER_INP_KEY ,readonly = True ,disabled_readonly_background_color = color .DARK_GRAY ,enable_events = True ,
165+ expand_x = True ,expand_y = True ,font = FONT ,background_color = color .DARK_GRAY ),
166+ sg .FolderBrowse (disabled = disabled ,size = (10 ,2 ))
167+ ],
168+ ],expand_x = True ,visible = visible_ ,relief = sg .RELIEF_SOLID ,border_width = 1 ,background_color = color .GRAY_9900 )
169+ return layout
170+
171+ def folder_inp_browse_events (event ,values ,folder_browse_inp_elem ,file_explorer_lstbox_key_elem ):
172+ if event == FOLDER_INP_KEY :
173+ folder_browse_inp_elem .update (values [FOLDER_INP_KEY ])
174+ file_explorer_lstbox_key_elem .update (get_system_files (values [FOLDER_INP_KEY ], sort = "ASC" ))
0 commit comments