feat: Add cross-platform file path selection for password files

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 10:08:38 -08:00
parent b8698d41b8
commit ac5306ed30

View File

@ -33,6 +33,43 @@ def save_uploaded_file(uploaded_file):
tmp_file.write(uploaded_file.getvalue())
return tmp_file.name
def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
"""Get file path using native file dialog when possible"""
try:
if platform.system() == "Windows":
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw() # Hide the main window
root.attributes('-topmost', True) # Bring dialog to front
path = filedialog.askopenfilename(title=title, filetypes=file_types)
return path if path else None
elif platform.system() == "Linux":
# Use zenity for Linux systems
try:
result = subprocess.run(
['zenity', '--file-selection', '--title', title],
capture_output=True,
text=True
)
return result.stdout.strip() if result.returncode == 0 else None
except FileNotFoundError:
return None
elif platform.system() == "Darwin": # macOS
try:
result = subprocess.run(
['osascript', '-e', f'choose file with prompt "{title}"'],
capture_output=True,
text=True
)
return result.stdout.strip() if result.returncode == 0 else None
except FileNotFoundError:
return None
except Exception as e:
st.error(f"Error opening file dialog: {str(e)}")
return None
return None
def get_directory_path(title="Select Directory"):
"""Get directory path using native file dialog when possible"""
try:
@ -157,20 +194,10 @@ with col2:
value=st.session_state.get('password_path', ''))
password_browse = st.button("Browse Password File")
if password_browse:
try:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title="Select Password File",
filetypes=[("Text Files", "*.txt")]
)
if file_path:
st.session_state['password_path'] = file_path
st.experimental_rerun()
except Exception as e:
st.error(f"Error opening file dialog: {str(e)}")
path = get_file_path("Select Password File", [("Text Files", "*.txt")])
if path:
st.session_state['password_path'] = path
st.experimental_rerun()
if password_path and os.path.exists(password_path):
with open(password_path, 'r', encoding='utf-8') as pf: