refactor: Improve directory processing UI with session state and rerun
This commit is contained in:
parent
79b2bb0835
commit
6b5c851a00
@ -207,118 +207,121 @@ if uploaded_files:
|
|||||||
mime="application/zip",
|
mime="application/zip",
|
||||||
)
|
)
|
||||||
|
|
||||||
else: # Directory Processing
|
else: # Directory Processing
|
||||||
st.header("Directory Processing")
|
st.header("Directory Processing")
|
||||||
|
|
||||||
|
col1, col2 = st.columns(2)
|
||||||
|
|
||||||
|
with col1:
|
||||||
|
source_dir = st.text_input("Source Directory Path",
|
||||||
|
help="Enter the full path to the directory containing files to process",
|
||||||
|
value=st.session_state.get('source_dir', ''))
|
||||||
|
source_browse = st.button("Browse Source Directory")
|
||||||
|
if source_browse:
|
||||||
|
path = get_directory_path("Select Source Directory")
|
||||||
|
if path:
|
||||||
|
st.session_state['source_dir'] = path
|
||||||
|
st.experimental_rerun()
|
||||||
|
|
||||||
col1, col2 = st.columns(2)
|
dest_dir = st.text_input("Destination Directory Path",
|
||||||
|
help="Enter the full path where processed files will be saved",
|
||||||
with col1:
|
value=st.session_state.get('dest_dir', ''))
|
||||||
source_dir = st.text_input("Source Directory Path",
|
dest_browse = st.button("Browse Destination Directory")
|
||||||
help="Enter the full path to the directory containing files to process")
|
if dest_browse:
|
||||||
source_browse = st.button("Browse Source Directory")
|
path = get_directory_path("Select Destination Directory")
|
||||||
if source_browse:
|
if path:
|
||||||
path = get_directory_path("Select Source Directory")
|
st.session_state['dest_dir'] = path
|
||||||
if path:
|
st.experimental_rerun()
|
||||||
source_dir = path
|
|
||||||
st.session_state['source_dir'] = path # Persist the selection
|
with col2:
|
||||||
|
if file_type == "Excel":
|
||||||
|
password_option = st.radio(
|
||||||
|
"Password Option:",
|
||||||
|
("No Password", "Password File")
|
||||||
|
)
|
||||||
|
|
||||||
dest_dir = st.text_input("Destination Directory Path",
|
passwords = []
|
||||||
help="Enter the full path where processed files will be saved")
|
if password_option == "Password File":
|
||||||
dest_browse = st.button("Browse Destination Directory")
|
password_path = st.text_input("Password File Path",
|
||||||
if dest_browse:
|
help="Enter the full path to the text file containing passwords",
|
||||||
path = get_directory_path("Select Destination Directory")
|
value=st.session_state.get('password_path', ''))
|
||||||
if path:
|
password_browse = st.button("Browse Password File")
|
||||||
dest_dir = path
|
if password_browse:
|
||||||
st.session_state['dest_dir'] = path # Persist the selection
|
try:
|
||||||
|
import tkinter as tk
|
||||||
with col2:
|
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)}")
|
||||||
|
|
||||||
|
if password_path and os.path.exists(password_path):
|
||||||
|
with open(password_path, 'r', encoding='utf-8') as pf:
|
||||||
|
passwords = [line.strip() for line in pf if line.strip()]
|
||||||
|
st.info(f"Loaded {len(passwords)} passwords from file")
|
||||||
|
|
||||||
|
if source_dir and dest_dir and st.button("Process Directory", type="primary"):
|
||||||
|
if not os.path.exists(source_dir):
|
||||||
|
st.error(f"Source directory does not exist: {source_dir}")
|
||||||
|
elif not os.path.exists(os.path.dirname(dest_dir)):
|
||||||
|
st.error(f"Parent of destination directory does not exist: {os.path.dirname(dest_dir)}")
|
||||||
|
else:
|
||||||
|
os.makedirs(dest_dir, exist_ok=True)
|
||||||
|
|
||||||
|
# Get all files recursively
|
||||||
if file_type == "Excel":
|
if file_type == "Excel":
|
||||||
password_option = st.radio(
|
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True)
|
||||||
"Password Option:",
|
files.extend(glob.glob(os.path.join(source_dir, "**/*.xlsm"), recursive=True))
|
||||||
("No Password", "Password File")
|
else: # Word
|
||||||
)
|
files = glob.glob(os.path.join(source_dir, "**/*.docx"), recursive=True)
|
||||||
|
files.extend(glob.glob(os.path.join(source_dir, "**/*.docm"), recursive=True))
|
||||||
passwords = []
|
|
||||||
if password_option == "Password File":
|
if not files:
|
||||||
password_path = st.text_input("Password File Path",
|
st.warning(f"No {file_type} files found in the source directory")
|
||||||
help="Enter the full path to the text file containing passwords")
|
|
||||||
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:
|
|
||||||
password_path = file_path
|
|
||||||
st.session_state['password_path'] = file_path
|
|
||||||
except Exception as e:
|
|
||||||
st.error(f"Error opening file dialog: {str(e)}")
|
|
||||||
|
|
||||||
if password_path and os.path.exists(password_path):
|
|
||||||
with open(password_path, 'r', encoding='utf-8') as pf:
|
|
||||||
passwords = [line.strip() for line in pf if line.strip()]
|
|
||||||
st.info(f"Loaded {len(passwords)} passwords from file")
|
|
||||||
|
|
||||||
if source_dir and dest_dir and st.button("Process Directory", type="primary"):
|
|
||||||
if not os.path.exists(source_dir):
|
|
||||||
st.error(f"Source directory does not exist: {source_dir}")
|
|
||||||
elif not os.path.exists(os.path.dirname(dest_dir)):
|
|
||||||
st.error(f"Parent of destination directory does not exist: {os.path.dirname(dest_dir)}")
|
|
||||||
else:
|
else:
|
||||||
os.makedirs(dest_dir, exist_ok=True)
|
progress_bar = st.progress(0)
|
||||||
|
status_text = st.empty()
|
||||||
|
|
||||||
# Get all files recursively
|
for idx, source_path in enumerate(files):
|
||||||
if file_type == "Excel":
|
try:
|
||||||
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True)
|
# Create a container for each file
|
||||||
files.extend(glob.glob(os.path.join(source_dir, "**/*.xlsm"), recursive=True))
|
relative_path = os.path.relpath(source_path, source_dir)
|
||||||
else: # Word
|
dest_path = os.path.join(dest_dir, relative_path)
|
||||||
files = glob.glob(os.path.join(source_dir, "**/*.docx"), recursive=True)
|
|
||||||
files.extend(glob.glob(os.path.join(source_dir, "**/*.docm"), recursive=True))
|
with st.expander(f"Processing {relative_path}", expanded=True):
|
||||||
|
st.write(f"📝 Processing {relative_path}...")
|
||||||
|
|
||||||
|
# Create destination directory if needed
|
||||||
|
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
||||||
|
|
||||||
|
# Process based on file type
|
||||||
|
if file_type == "Excel":
|
||||||
|
copy_excel_file(source_path, dest_path, passwords)
|
||||||
|
else: # Word
|
||||||
|
remove_all_protection_tags(source_path, dest_path)
|
||||||
|
|
||||||
|
st.success("✅ Processing complete!")
|
||||||
|
|
||||||
|
# Update progress
|
||||||
|
progress = (idx + 1) / len(files)
|
||||||
|
progress_bar.progress(progress)
|
||||||
|
status_text.text(f"Processed {idx + 1} of {len(files)} files")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
st.error(f"❌ Error processing {relative_path}: {str(e)}")
|
||||||
|
|
||||||
if not files:
|
progress_bar.empty()
|
||||||
st.warning(f"No {file_type} files found in the source directory")
|
status_text.text("✨ All processing complete!")
|
||||||
else:
|
|
||||||
progress_bar = st.progress(0)
|
# Show the output directory
|
||||||
status_text = st.empty()
|
st.success(f"Processed files are saved in: {dest_dir}")
|
||||||
|
|
||||||
for idx, source_path in enumerate(files):
|
|
||||||
try:
|
|
||||||
# Create a container for each file
|
|
||||||
relative_path = os.path.relpath(source_path, source_dir)
|
|
||||||
dest_path = os.path.join(dest_dir, relative_path)
|
|
||||||
|
|
||||||
with st.expander(f"Processing {relative_path}", expanded=True):
|
|
||||||
st.write(f"📝 Processing {relative_path}...")
|
|
||||||
|
|
||||||
# Create destination directory if needed
|
|
||||||
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
|
||||||
|
|
||||||
# Process based on file type
|
|
||||||
if file_type == "Excel":
|
|
||||||
copy_excel_file(source_path, dest_path, passwords)
|
|
||||||
else: # Word
|
|
||||||
remove_all_protection_tags(source_path, dest_path)
|
|
||||||
|
|
||||||
st.success("✅ Processing complete!")
|
|
||||||
|
|
||||||
# Update progress
|
|
||||||
progress = (idx + 1) / len(files)
|
|
||||||
progress_bar.progress(progress)
|
|
||||||
status_text.text(f"Processed {idx + 1} of {len(files)} files")
|
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
st.error(f"❌ Error processing {relative_path}: {str(e)}")
|
|
||||||
|
|
||||||
progress_bar.empty()
|
|
||||||
status_text.text("✨ All processing complete!")
|
|
||||||
|
|
||||||
# Show the output directory
|
|
||||||
st.success(f"Processed files are saved in: {dest_dir}")
|
|
||||||
|
|
||||||
# Footer
|
# Footer
|
||||||
st.sidebar.markdown("---")
|
st.sidebar.markdown("---")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user