Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4c994a28f0 | ||
|
|
55177b1444 | ||
|
|
199d622c24 | ||
|
|
79d9fbf09a |
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.py[cod]
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
[Paths]
|
||||||
|
SEARCH_ROOT = C:\CNCPrograms\Programs
|
||||||
|
TRASH_DIR = C:\CNCPrograms\Trash
|
||||||
|
INDEX_FILE = C:\CNCPrograms\search_index.pkl
|
||||||
|
|
||||||
|
[Features]
|
||||||
|
TRASH_ENABLED = True
|
||||||
|
EDITOR_ENABLED = False
|
||||||
|
AUTO_CLOSE_SECONDS = 300
|
||||||
|
WORKER_COUNT = 16
|
||||||
|
|
||||||
|
[Copy]
|
||||||
|
Extension =
|
||||||
|
|
||||||
|
[Buttons]
|
||||||
|
BUTTON_SIZE_SEARCH=15
|
||||||
|
BUTTON_SIZE_OPEN=15
|
||||||
|
BUTTON_SIZE_SELECT_ALL=15
|
||||||
|
BUTTON_SIZE_SAVE=15
|
||||||
|
BUTTON_SIZE_COPY=15
|
||||||
|
BUTTON_SIZE_TRASH=15
|
||||||
|
BUTTON_SIZE_CLOSE=15
|
||||||
|
BUTTON_SIZE_MESSAGE=15
|
||||||
|
BUTTON_SIZE_YES_NO=15
|
||||||
|
|
||||||
|
[UI]
|
||||||
|
TEXT_FONT = Consolas
|
||||||
|
|
||||||
|
SEARCH_WIDTH=400
|
||||||
|
SEARCH_HEIGHT=150
|
||||||
|
SEARCH_FONT_SIZE=12
|
||||||
|
|
||||||
|
RESULTS_WIDTH=1000
|
||||||
|
RESULTS_HEIGHT=700
|
||||||
|
RESULTS_FONT_SIZE=11
|
||||||
|
|
||||||
|
PREVIEW_WIDTH=1200
|
||||||
|
PREVIEW_HEIGHT=900
|
||||||
|
PREVIEW_FONT_SIZE=24
|
||||||
|
|
||||||
|
ERROR_WIDTH=400
|
||||||
|
ERROR_HEIGHT=100
|
||||||
|
ERROR_FONT_SIZE=20
|
||||||
+380
@@ -0,0 +1,380 @@
|
|||||||
|
import os
|
||||||
|
import re
|
||||||
|
import csv
|
||||||
|
import hashlib
|
||||||
|
import tkinter as tk
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# GUI
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def select_folder():
|
||||||
|
return input("Enter folder path: ").strip().strip("'\"")
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Helper Functions
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def normalize_nc_content(text):
|
||||||
|
|
||||||
|
output = []
|
||||||
|
|
||||||
|
for line in text.splitlines():
|
||||||
|
|
||||||
|
line = line.upper()
|
||||||
|
|
||||||
|
# Remove comments
|
||||||
|
line = re.sub(r"\(.*?\)", "", line)
|
||||||
|
line = re.sub(r";.*", "", line)
|
||||||
|
|
||||||
|
# Remove tool numbers
|
||||||
|
line = re.sub(r"\bT\d+\b", "", line)
|
||||||
|
|
||||||
|
# Remove spindle speeds
|
||||||
|
line = re.sub(r"\bS[-+]?\d*\.?\d+\b", "", line)
|
||||||
|
|
||||||
|
line = line.strip()
|
||||||
|
|
||||||
|
if line:
|
||||||
|
output.append(line)
|
||||||
|
|
||||||
|
return "\n".join(output)
|
||||||
|
|
||||||
|
|
||||||
|
def calculate_file_hash(file_path):
|
||||||
|
|
||||||
|
sha = hashlib.sha256()
|
||||||
|
|
||||||
|
with open(file_path, "rb") as f:
|
||||||
|
|
||||||
|
while True:
|
||||||
|
|
||||||
|
chunk = f.read(8192)
|
||||||
|
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
|
||||||
|
sha.update(chunk)
|
||||||
|
|
||||||
|
return sha.hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Geometry Comparison
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
AXIS_PATTERN = re.compile(r"([XYZIJKR])([-+]?\d*\.?\d+)")
|
||||||
|
COMMAND_PATTERN = re.compile(r"\b(G\d+|M\d+)\b")
|
||||||
|
|
||||||
|
|
||||||
|
def extract_toolpath(file_path):
|
||||||
|
|
||||||
|
motions = []
|
||||||
|
|
||||||
|
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
|
||||||
|
|
||||||
|
for raw_line in f:
|
||||||
|
|
||||||
|
line = normalize_nc_content(raw_line)
|
||||||
|
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
commands = COMMAND_PATTERN.findall(line)
|
||||||
|
|
||||||
|
axes = {
|
||||||
|
axis: float(value)
|
||||||
|
for axis, value in AXIS_PATTERN.findall(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
if commands or axes:
|
||||||
|
|
||||||
|
motions.append({
|
||||||
|
"commands": tuple(commands),
|
||||||
|
"axes": axes
|
||||||
|
})
|
||||||
|
|
||||||
|
return motions
|
||||||
|
|
||||||
|
|
||||||
|
def compare_program_structure(file1, file2, tolerance=0.005):
|
||||||
|
|
||||||
|
path1 = extract_toolpath(file1)
|
||||||
|
path2 = extract_toolpath(file2)
|
||||||
|
|
||||||
|
if len(path1) != len(path2):
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
max_diff = 0
|
||||||
|
|
||||||
|
for move1, move2 in zip(path1, path2):
|
||||||
|
|
||||||
|
if move1["commands"] != move2["commands"]:
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
axes = set(move1["axes"]) | set(move2["axes"])
|
||||||
|
|
||||||
|
for axis in axes:
|
||||||
|
|
||||||
|
if axis not in move1["axes"]:
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
if axis not in move2["axes"]:
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
diff = abs(
|
||||||
|
move1["axes"][axis]
|
||||||
|
- move2["axes"][axis]
|
||||||
|
)
|
||||||
|
|
||||||
|
max_diff = max(max_diff, diff)
|
||||||
|
|
||||||
|
if diff > tolerance:
|
||||||
|
return False, max_diff
|
||||||
|
|
||||||
|
return True, max_diff
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Extension Normalization
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def normalize_extensions(root_folder):
|
||||||
|
|
||||||
|
root_path = Path(root_folder)
|
||||||
|
|
||||||
|
if not root_path.exists():
|
||||||
|
raise FileNotFoundError(root_folder)
|
||||||
|
|
||||||
|
for file in root_path.rglob("*"):
|
||||||
|
|
||||||
|
if not file.is_file():
|
||||||
|
continue
|
||||||
|
|
||||||
|
suffix = file.suffix.lower()
|
||||||
|
|
||||||
|
if suffix == "":
|
||||||
|
|
||||||
|
new_file = file.with_name(
|
||||||
|
file.name + ".txt"
|
||||||
|
)
|
||||||
|
|
||||||
|
elif suffix == ".nc":
|
||||||
|
|
||||||
|
new_file = file.with_suffix(".txt")
|
||||||
|
|
||||||
|
elif suffix == ".txt":
|
||||||
|
|
||||||
|
if file.suffix == ".txt":
|
||||||
|
continue
|
||||||
|
|
||||||
|
new_file = file.with_suffix(".txt")
|
||||||
|
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
file.rename(new_file)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Failed: {file}")
|
||||||
|
print(e)
|
||||||
|
|
||||||
|
print("Extension normalization complete.")
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Program Splitting
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def split_file_by_key(input_file, output_dir):
|
||||||
|
|
||||||
|
os.makedirs(output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
current_output = None
|
||||||
|
|
||||||
|
with open(input_file, "r", encoding="utf-8", errors="ignore") as infile:
|
||||||
|
|
||||||
|
for line in infile:
|
||||||
|
|
||||||
|
if line.startswith("O"):
|
||||||
|
|
||||||
|
if current_output:
|
||||||
|
|
||||||
|
current_output.write("%\n")
|
||||||
|
current_output.close()
|
||||||
|
|
||||||
|
filename = line[:6].strip()
|
||||||
|
|
||||||
|
output_path = os.path.join(output_dir, f"{filename}.txt")
|
||||||
|
|
||||||
|
current_output = open(output_path, "w", encoding="utf-8")
|
||||||
|
|
||||||
|
current_output.write("%\n")
|
||||||
|
|
||||||
|
if current_output:
|
||||||
|
current_output.write(line)
|
||||||
|
|
||||||
|
if current_output:
|
||||||
|
|
||||||
|
current_output.write("%\n")
|
||||||
|
current_output.close()
|
||||||
|
|
||||||
|
print(f"Finished: {input_file}")
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Exact Duplicate Removal
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def remove_exact_duplicates(root_dir):
|
||||||
|
|
||||||
|
known_hashes = {}
|
||||||
|
deleted = []
|
||||||
|
|
||||||
|
for file in Path(root_dir).rglob("*.txt"):
|
||||||
|
|
||||||
|
file_hash = calculate_file_hash(file)
|
||||||
|
|
||||||
|
if file_hash in known_hashes:
|
||||||
|
|
||||||
|
print(f"Removing exact duplicate: {file}")
|
||||||
|
|
||||||
|
file.unlink()
|
||||||
|
|
||||||
|
deleted.append(file)
|
||||||
|
|
||||||
|
else:
|
||||||
|
known_hashes[file_hash] = file
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"Removed {len(deleted)} exact duplicates."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Geometry Duplicate Removal
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def remove_geometry_duplicates(root_dir, tolerance=0.005):
|
||||||
|
|
||||||
|
files = list(Path(root_dir).rglob("*.txt"))
|
||||||
|
|
||||||
|
removed = set()
|
||||||
|
|
||||||
|
for i in range(len(files)):
|
||||||
|
|
||||||
|
if files[i] in removed:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for j in range(i + 1, len(files)):
|
||||||
|
|
||||||
|
if files[j] in removed:
|
||||||
|
continue
|
||||||
|
|
||||||
|
duplicate, max_diff = compare_program_structure(files[i], files[j], tolerance)
|
||||||
|
|
||||||
|
if duplicate:
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"Removing geometric duplicate:\n"
|
||||||
|
f" Keep: {files[i].name}\n"
|
||||||
|
f" Delete: {files[j].name}\n"
|
||||||
|
f" Max Delta: {max_diff:.5f}"
|
||||||
|
)
|
||||||
|
|
||||||
|
files[j].unlink()
|
||||||
|
|
||||||
|
removed.add(files[j])
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"Removed {len(removed)} geometry duplicates."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Similarity Report
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
def generate_similarity_report(
|
||||||
|
root_dir,
|
||||||
|
tolerance=0.005,
|
||||||
|
output_csv="similarity_report.csv"
|
||||||
|
):
|
||||||
|
|
||||||
|
files = list(Path(root_dir).rglob("*.txt"))
|
||||||
|
|
||||||
|
with open(output_csv, "w", newline="", encoding="utf-8") as csvfile:
|
||||||
|
|
||||||
|
writer = csv.writer(csvfile)
|
||||||
|
|
||||||
|
writer.writerow([
|
||||||
|
"File1",
|
||||||
|
"File2",
|
||||||
|
"Duplicate",
|
||||||
|
"MaxCoordinateDelta"
|
||||||
|
])
|
||||||
|
|
||||||
|
for i in range(len(files)):
|
||||||
|
|
||||||
|
for j in range(i + 1, len(files)):
|
||||||
|
|
||||||
|
duplicate, delta = compare_program_structure(files[i], files[j], tolerance)
|
||||||
|
|
||||||
|
writer.writerow([
|
||||||
|
files[i].name,
|
||||||
|
files[j].name,
|
||||||
|
duplicate,
|
||||||
|
delta
|
||||||
|
])
|
||||||
|
|
||||||
|
print(
|
||||||
|
f"Similarity report written to {output_csv}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# Main
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
input_dir = select_folder()
|
||||||
|
|
||||||
|
if not os.path.isdir(input_dir):
|
||||||
|
|
||||||
|
print("Invalid folder.")
|
||||||
|
|
||||||
|
raise SystemExit(1)
|
||||||
|
|
||||||
|
normalize_extensions(input_dir)
|
||||||
|
|
||||||
|
master_output_dir = os.path.join(input_dir, "Split_Output")
|
||||||
|
|
||||||
|
os.makedirs(master_output_dir, exist_ok=True)
|
||||||
|
|
||||||
|
for root, dirs, files in os.walk(input_dir):
|
||||||
|
|
||||||
|
if os.path.abspath(root).startswith(os.path.abspath(master_output_dir)):
|
||||||
|
continue
|
||||||
|
|
||||||
|
for filename in files:
|
||||||
|
|
||||||
|
if not filename.lower().endswith(".pgm"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
input_file = os.path.join(root, filename)
|
||||||
|
|
||||||
|
program_output_dir = os.path.join(master_output_dir, os.path.splitext(filename)[0])
|
||||||
|
|
||||||
|
split_file_by_key(input_file, program_output_dir)
|
||||||
|
|
||||||
|
remove_exact_duplicates(master_output_dir)
|
||||||
|
|
||||||
|
remove_geometry_duplicates(master_output_dir, tolerance=0.005)
|
||||||
|
|
||||||
|
generate_similarity_report(master_output_dir, tolerance=0.005)
|
||||||
|
|
||||||
|
print("All files processed.")
|
||||||
+146
@@ -0,0 +1,146 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import pickle
|
||||||
|
import configparser
|
||||||
|
from concurrent.futures import ThreadPoolExecutor
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
|
# CONFIG
|
||||||
|
config = configparser.ConfigParser()
|
||||||
|
config.read(r"C:\ProgramData\Program-Search\config.ini")
|
||||||
|
# PATHS
|
||||||
|
SEARCH_ROOT = config["Paths"]["SEARCH_ROOT"]
|
||||||
|
INDEX_FILE = config["Paths"]["INDEX_FILE"]
|
||||||
|
# FEATURES
|
||||||
|
WORKER_COUNT = config.getint("Features", "WORKER_COUNT", fallback=8)
|
||||||
|
# ============================================================
|
||||||
|
|
||||||
|
|
||||||
|
def read_file(path, mtime, size):
|
||||||
|
try:
|
||||||
|
with open(path, "r", errors="ignore") as f:
|
||||||
|
return (
|
||||||
|
path,
|
||||||
|
{
|
||||||
|
"filename": os.path.basename(path).lower(),
|
||||||
|
"content": f.read().lower(),
|
||||||
|
"mtime": mtime,
|
||||||
|
"size": size,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def scan_files(root):
|
||||||
|
"""Return {path: (mtime, size)} for every file under root."""
|
||||||
|
found = {}
|
||||||
|
stack = [root]
|
||||||
|
|
||||||
|
while stack:
|
||||||
|
current = stack.pop()
|
||||||
|
try:
|
||||||
|
with os.scandir(current) as it:
|
||||||
|
for entry in it:
|
||||||
|
try:
|
||||||
|
if entry.is_dir(follow_symlinks=False):
|
||||||
|
stack.append(entry.path)
|
||||||
|
elif entry.is_file():
|
||||||
|
st = entry.stat()
|
||||||
|
found[entry.path] = (st.st_mtime, st.st_size)
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
return found
|
||||||
|
|
||||||
|
|
||||||
|
def load_index():
|
||||||
|
try:
|
||||||
|
with open(INDEX_FILE, "rb") as f:
|
||||||
|
return pickle.load(f)
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
def save_index(index):
|
||||||
|
os.makedirs(os.path.dirname(INDEX_FILE), exist_ok=True)
|
||||||
|
|
||||||
|
# Write to a temp file first so a crash never leaves a corrupt index.
|
||||||
|
tmp_file = INDEX_FILE + ".tmp"
|
||||||
|
with open(tmp_file, "wb") as f:
|
||||||
|
pickle.dump(index, f, protocol=pickle.HIGHEST_PROTOCOL)
|
||||||
|
os.replace(tmp_file, INDEX_FILE)
|
||||||
|
|
||||||
|
|
||||||
|
def update_index(index, verbose=False):
|
||||||
|
"""
|
||||||
|
Bring index up to date with SEARCH_ROOT in place.
|
||||||
|
Only new or changed files are re-read; deleted files are removed.
|
||||||
|
Returns True if anything changed.
|
||||||
|
"""
|
||||||
|
if verbose:
|
||||||
|
print("Scanning files...")
|
||||||
|
|
||||||
|
on_disk = scan_files(SEARCH_ROOT)
|
||||||
|
|
||||||
|
removed = [p for p in index if p not in on_disk]
|
||||||
|
for p in removed:
|
||||||
|
del index[p]
|
||||||
|
|
||||||
|
to_read = []
|
||||||
|
for path, (mtime, size) in on_disk.items():
|
||||||
|
data = index.get(path)
|
||||||
|
if (
|
||||||
|
data is None
|
||||||
|
or data.get("mtime") != mtime
|
||||||
|
or data.get("size") != size
|
||||||
|
):
|
||||||
|
to_read.append((path, mtime, size))
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(
|
||||||
|
f"Found {len(on_disk)} files: "
|
||||||
|
f"{len(to_read)} new/changed, {len(removed)} removed"
|
||||||
|
)
|
||||||
|
|
||||||
|
completed = 0
|
||||||
|
with ThreadPoolExecutor(max_workers=WORKER_COUNT) as executor:
|
||||||
|
for result in executor.map(lambda a: read_file(*a), to_read):
|
||||||
|
completed += 1
|
||||||
|
if verbose and completed % 100 == 0:
|
||||||
|
print(f"{completed}/{len(to_read)}")
|
||||||
|
if result:
|
||||||
|
path, data = result
|
||||||
|
index[path] = data
|
||||||
|
|
||||||
|
return bool(removed or to_read)
|
||||||
|
|
||||||
|
|
||||||
|
def build_index(full=False):
|
||||||
|
index = {} if full else load_index()
|
||||||
|
|
||||||
|
changed = update_index(index, verbose=True)
|
||||||
|
|
||||||
|
if not changed and os.path.exists(INDEX_FILE):
|
||||||
|
print("\nIndex already up to date.")
|
||||||
|
return
|
||||||
|
|
||||||
|
print("\nSaving index...")
|
||||||
|
save_index(index)
|
||||||
|
|
||||||
|
size_mb = os.path.getsize(INDEX_FILE) / 1024 / 1024
|
||||||
|
print(
|
||||||
|
f"Index saved:\n"
|
||||||
|
f"{INDEX_FILE}\n"
|
||||||
|
f"{len(index)} files\n"
|
||||||
|
f"{size_mb:.2f} MB"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
# Run with --full to rebuild the index from scratch.
|
||||||
|
build_index(full="--full" in sys.argv)
|
||||||
+192
-174
@@ -2,288 +2,307 @@
|
|||||||
import os
|
import os
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import scrolledtext
|
from tkinter import scrolledtext
|
||||||
import tkinter.font as tkfont
|
|
||||||
import shutil
|
import shutil
|
||||||
|
import ctypes
|
||||||
|
import string
|
||||||
import configparser
|
import configparser
|
||||||
|
|
||||||
|
|
||||||
# ============================================================
|
# ============================================================
|
||||||
# CONFIG:
|
# CONFIG:
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
config.read(r"C:\ProgramData\Program-Search\config.ini")
|
config.read(r"C:\ProgramData\Program-Search\config.ini")
|
||||||
|
# PATHS
|
||||||
SEARCH_ROOT = config["Paths"]["SEARCH_ROOT"]
|
SEARCH_ROOT = config["Paths"]["SEARCH_ROOT"]
|
||||||
TRASH_DIR = config["Paths"]["TRASH_DIR"]
|
TRASH_DIR = config["Paths"]["TRASH_DIR"]
|
||||||
EDITOR_ENABLED = config.getboolean("Features", "EDITOR_ENABLED")
|
# FEATURES
|
||||||
AUTO_CLOSE_SECONDS = config.getint("Features", "AUTO_CLOSE_SECONDS")
|
EDITOR_ENABLED = config.getboolean("Features", "EDITOR_ENABLED", fallback=True)
|
||||||
CLOSE_ALL_ENABLED = config.getboolean("Features", "CLOSE_ALL_ENABLED")
|
AUTO_CLOSE_SECONDS = config.getint("Features", "AUTO_CLOSE_SECONDS", fallback=30)
|
||||||
# ============================================================
|
# UI
|
||||||
|
TEXT_FONT = config.get("UI", "TEXT-font", fallback="Consolas")
|
||||||
|
SEARCH_WIDTH = config.getint("UI", "SEARCH_WIDTH", fallback=400)
|
||||||
|
SEARCH_HEIGHT = config.getint("UI", "SEARCH_HEIGHT", fallback=150)
|
||||||
|
SEARCH_FONT_SIZE = config.getint("UI", "SEARCH_FONT_SIZE", fallback=12)
|
||||||
|
RESULTS_WIDTH = config.getint("UI", "RESULTS_WIDTH", fallback=1000)
|
||||||
|
RESULTS_HEIGHT = config.getint("UI", "RESULTS_HEIGHT", fallback=700)
|
||||||
|
RESULTS_FONT_SIZE = config.getint("UI", "RESULTS_FONT_SIZE", fallback=11)
|
||||||
|
PREVIEW_WIDTH = config.getint("UI", "PREVIEW_WIDTH", fallback=1200)
|
||||||
|
PREVIEW_HEIGHT = config.getint("UI", "PREVIEW_HEIGHT", fallback=900)
|
||||||
|
PREVIEW_FONT_SIZE = config.getint("UI", "PREVIEW_FONT_SIZE", fallback=24)
|
||||||
|
ERROR_WIDTH = config.getint("UI", "ERROR_WIDTH", fallback=400)
|
||||||
|
ERROR_HEIGHT = config.getint("UI", "ERROR_HEIGHT", fallback=100)
|
||||||
|
ERROR_FONT_SIZE = config.getint("UI", "ERROR_FONT_SIZE", fallback=11)
|
||||||
|
# BUTTONS
|
||||||
|
BUTTON_SIZE_SEARCH = config.getint("Buttons", "BUTTON_SIZE_SEARCH", fallback=15)
|
||||||
|
BUTTON_SIZE_OPEN = config.getint("Buttons", "BUTTON_SIZE_OPEN", fallback=15)
|
||||||
|
BUTTON_SIZE_SELECT_ALL = config.getint("Buttons", "BUTTON_SIZE_SELECT_ALL", fallback=15)
|
||||||
|
BUTTON_SIZE_SAVE = config.getint("Buttons", "BUTTON_SIZE_SAVE", fallback=15)
|
||||||
|
BUTTON_SIZE_COPY = config.getint("Buttons", "BUTTON_SIZE_COPY", fallback=15)
|
||||||
|
BUTTON_SIZE_TRASH = config.getint("Buttons", "BUTTON_SIZE_TRASH", fallback=15)
|
||||||
|
BUTTON_SIZE_CLOSE = config.getint("Buttons", "BUTTON_SIZE_CLOSE", fallback=15)
|
||||||
|
BUTTON_SIZE_MESSAGE = config.getint("Buttons", "BUTTON_SIZE_MESSAGE", fallback=15)
|
||||||
|
BUTTON_SIZE_YES_NO = config.getint("Buttons", "BUTTON_SIZE_YES_NO", fallback=15)
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
os.makedirs(TRASH_DIR, exist_ok=True)
|
os.makedirs(TRASH_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
def center_window(win, width, height):
|
||||||
|
screen_width = win.winfo_screenwidth()
|
||||||
|
screen_height = win.winfo_screenheight()
|
||||||
|
x = int((screen_width / 2) - (width / 2))
|
||||||
|
y = int((screen_height / 2) - (height / 2))
|
||||||
|
win.geometry(f"{width}x{height}+{x}+{y}")
|
||||||
|
|
||||||
def custom_messagebox(title, message, kind="info"):
|
def custom_message(title, message):
|
||||||
win = tk.Toplevel()
|
win = tk.Toplevel()
|
||||||
win.title(title)
|
win.title(title)
|
||||||
win.geometry("600x300")
|
win.protocol("WM_DELETE_WINDOW", win.destroy)
|
||||||
win.grab_set()
|
|
||||||
|
tk.Label(
|
||||||
|
win,
|
||||||
|
text=message,
|
||||||
|
font=(TEXT_FONT, ERROR_FONT_SIZE),
|
||||||
|
wraplength=ERROR_WIDTH - 30,
|
||||||
|
justify="center"
|
||||||
|
).pack(padx=15, pady=15)
|
||||||
|
|
||||||
|
tk.Button(win, text="OK", font=(TEXT_FONT, BUTTON_SIZE_MESSAGE), command=win.destroy).pack(pady=10)
|
||||||
|
|
||||||
|
win.bind("<Return>", lambda e: win.destroy())
|
||||||
|
center_window(win, ERROR_WIDTH, ERROR_HEIGHT)
|
||||||
|
win.lift()
|
||||||
win.focus_force()
|
win.focus_force()
|
||||||
center_window(win)
|
win.transient()
|
||||||
|
win.grab_set()
|
||||||
frame = tk.Frame(win, padx=20, pady=20)
|
win.update()
|
||||||
frame.pack(expand=True, fill="both")
|
win.wait_window()
|
||||||
|
|
||||||
tk.Label(frame, text=message, font=("Consolas", 24), wraplength=550).pack(pady=20)
|
|
||||||
|
|
||||||
if kind == "yesno":
|
|
||||||
result = {"value": False}
|
|
||||||
|
|
||||||
def yes():
|
|
||||||
result["value"] = True
|
|
||||||
win.destroy()
|
|
||||||
|
|
||||||
def no():
|
|
||||||
result["value"] = False
|
|
||||||
win.destroy()
|
|
||||||
|
|
||||||
tk.Button(frame, text="Yes", font=("Consolas", 24), padx=10, pady=10, command=yes).pack(side="left", padx=16)
|
|
||||||
tk.Button(frame, text="No", font=("Consolas", 24), padx=10, pady=10, command=no).pack(side="right", padx=16)
|
|
||||||
|
|
||||||
win.wait_window()
|
|
||||||
return result["value"]
|
|
||||||
|
|
||||||
else:
|
|
||||||
def close():
|
|
||||||
win.destroy()
|
|
||||||
|
|
||||||
tk.Button(frame, text="OK", font=("Consolas", 24), padx=10, pady=10, command=close).pack(pady=20)
|
|
||||||
win.bind("<Return>", lambda event: close())
|
|
||||||
win.wait_window()
|
|
||||||
|
|
||||||
|
|
||||||
def center_window(win):
|
|
||||||
win.update_idletasks()
|
|
||||||
w = win.winfo_width()
|
|
||||||
h = win.winfo_height()
|
|
||||||
sw = win.winfo_screenwidth()
|
|
||||||
sh = win.winfo_screenheight()
|
|
||||||
x = (sw // 2) - (w // 2)
|
|
||||||
y = (sh // 2) - (h // 2)
|
|
||||||
win.geometry(f"{w}x{h}+{x}+{y}")
|
|
||||||
|
|
||||||
|
def custom_yesno(title, message):
|
||||||
|
win = tk.Toplevel()
|
||||||
|
win.title(title)
|
||||||
|
|
||||||
|
result = {"value": False}
|
||||||
|
|
||||||
|
tk.Label(
|
||||||
|
win,
|
||||||
|
text=message,
|
||||||
|
font=(TEXT_FONT, ERROR_FONT_SIZE),
|
||||||
|
wraplength=ERROR_WIDTH - 30,
|
||||||
|
justify="center"
|
||||||
|
).pack(padx=15, pady=15)
|
||||||
|
|
||||||
|
button_frame = tk.Frame(win)
|
||||||
|
button_frame.pack(pady=10)
|
||||||
|
|
||||||
|
def yes():
|
||||||
|
result["value"] = True
|
||||||
|
win.destroy()
|
||||||
|
|
||||||
|
def no():
|
||||||
|
result["value"] = False
|
||||||
|
win.destroy()
|
||||||
|
|
||||||
|
tk.Button(button_frame, text="Yes", width=BUTTON_SIZE_YES_NO, font=(TEXT_FONT, BUTTON_SIZE_YES_NO), command=yes).pack(side="left", padx=10)
|
||||||
|
tk.Button(button_frame, text="No", width=BUTTON_SIZE_YES_NO, font=(TEXT_FONT, BUTTON_SIZE_YES_NO), command=no).pack(side="left", padx=10)
|
||||||
|
|
||||||
|
win.protocol("WM_DELETE_WINDOW", no)
|
||||||
|
center_window(win, ERROR_WIDTH, ERROR_HEIGHT)
|
||||||
|
win.lift()
|
||||||
|
win.focus_force()
|
||||||
|
win.grab_set()
|
||||||
|
win.wait_window()
|
||||||
|
|
||||||
|
return result["value"]
|
||||||
|
|
||||||
def popup_search_input():
|
def popup_search_input():
|
||||||
win = tk.Toplevel()
|
win = tk.Toplevel()
|
||||||
win.title("Search Trash")
|
win.title("Search")
|
||||||
win.geometry("600x300")
|
win.geometry(f"{SEARCH_WIDTH}x{SEARCH_HEIGHT}")
|
||||||
center_window(win)
|
|
||||||
|
tk.Label(win, text="Enter part number or name:", font=(TEXT_FONT, SEARCH_FONT_SIZE)).pack(pady=10)
|
||||||
tk.Label(win, text="Enter part number or name:", font=("Consolas", 18)).pack(pady=20)
|
entry = tk.Entry(win, font=(TEXT_FONT, SEARCH_FONT_SIZE))
|
||||||
|
entry.pack(pady=5)
|
||||||
entry = tk.Entry(win, font=("Consolas", 18))
|
win.lift()
|
||||||
entry.pack(pady=10)
|
win.attributes("-topmost", True)
|
||||||
entry.focus_set()
|
|
||||||
|
result = {"value": None, "force_close": False}
|
||||||
result = {"value": None}
|
|
||||||
|
|
||||||
def submit():
|
def submit():
|
||||||
result["value"] = entry.get().strip().lower()
|
result["value"] = entry.get().strip().lower()
|
||||||
win.destroy()
|
win.destroy()
|
||||||
|
|
||||||
entry.bind("<Return>", lambda event: submit())
|
def force_close():
|
||||||
win.bind("<Return>", lambda event: submit())
|
result["force_close"] = True
|
||||||
|
win.destroy()
|
||||||
tk.Button(win, text="Search", font=("Consolas", 18), padx=20, pady=10, command=submit).pack(pady=40)
|
|
||||||
|
entry.bind("<Return>", lambda e: submit())
|
||||||
|
tk.Button(win, text="Search", font=(TEXT_FONT, BUTTON_SIZE_SEARCH), command=submit).pack(pady=10)
|
||||||
|
|
||||||
|
center_window(win, SEARCH_WIDTH, SEARCH_HEIGHT)
|
||||||
|
win.protocol("WM_DELETE_WINDOW", force_close)
|
||||||
|
win.after(50, lambda: (entry.focus_set(), entry.icursor(tk.END)))
|
||||||
|
win.after(100, lambda: win.attributes("-topmost", False))
|
||||||
win.wait_window()
|
win.wait_window()
|
||||||
return result["value"]
|
return result["value"]
|
||||||
|
|
||||||
|
|
||||||
def search_files(term):
|
def search_files(term):
|
||||||
matches = []
|
matches = []
|
||||||
|
term_lower = term.lower()
|
||||||
|
|
||||||
for root, dirs, files in os.walk(TRASH_DIR):
|
for root, dirs, files in os.walk(TRASH_DIR):
|
||||||
for fname in files:
|
for fname in files:
|
||||||
path = os.path.join(root, fname)
|
path = os.path.join(root, fname)
|
||||||
fname_lower = fname.lower()
|
|
||||||
|
filename_match = term_lower in fname.lower()
|
||||||
filename_match = term in fname_lower
|
|
||||||
contents_match = False
|
contents_match = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(path, "r", errors="ignore") as f:
|
with open(path, "r", errors="ignore") as f:
|
||||||
if term in f.read().lower():
|
if term_lower in f.read().lower():
|
||||||
contents_match = True
|
contents_match = True
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if filename_match or contents_match:
|
if filename_match or contents_match:
|
||||||
matches.append(path)
|
matches.append(path)
|
||||||
|
|
||||||
return matches
|
return matches
|
||||||
|
|
||||||
|
|
||||||
def popup_results_list(matches):
|
def popup_results_list(matches):
|
||||||
win = tk.Toplevel()
|
win = tk.Toplevel()
|
||||||
win.title("Matches Found")
|
win.title("Matches Found")
|
||||||
win.geometry("1200x1000")
|
win.geometry(f"{RESULTS_WIDTH}x{RESULTS_HEIGHT}")
|
||||||
center_window(win)
|
|
||||||
|
tk.Label(win, text="Select files to preview:", font=(TEXT_FONT, RESULTS_FONT_SIZE)).pack(pady=10)
|
||||||
tk.Label(win, text="Select files to preview:", font=("Consolas", 18)).pack(pady=20)
|
|
||||||
|
lb = tk.Listbox(win, selectmode=tk.MULTIPLE, font=(TEXT_FONT, RESULTS_FONT_SIZE))
|
||||||
lb = tk.Listbox(win, selectmode=tk.MULTIPLE, font=("Consolas", 18))
|
|
||||||
lb.pack(expand=True, fill="both")
|
lb.pack(expand=True, fill="both")
|
||||||
|
lb.bind("<Return>", lambda e: submit())
|
||||||
|
|
||||||
for m in matches:
|
for m in matches:
|
||||||
lb.insert(tk.END, m)
|
parent = os.path.basename(os.path.dirname(m))
|
||||||
|
filename = os.path.basename(m)
|
||||||
|
display = f"{parent}\\{filename}"
|
||||||
|
lb.insert(tk.END, display)
|
||||||
|
|
||||||
result = {"selected": []}
|
result = {"selected": []}
|
||||||
|
|
||||||
def select_all():
|
def select_all():
|
||||||
lb.select_set(0, tk.END)
|
lb.select_set(0, tk.END)
|
||||||
|
|
||||||
def submit():
|
def submit():
|
||||||
sel = lb.curselection()
|
sel = lb.curselection()
|
||||||
|
if not sel:
|
||||||
|
custom_message("No Selection", "Please select at least 1 file.")
|
||||||
|
return
|
||||||
result["selected"] = [matches[i] for i in sel]
|
result["selected"] = [matches[i] for i in sel]
|
||||||
win.destroy()
|
win.destroy()
|
||||||
|
|
||||||
lb.bind("<Return>", lambda event: submit())
|
tk.Button(win, text="Open Selected", font=(TEXT_FONT, BUTTON_SIZE_OPEN), command=submit).pack(padx=10)
|
||||||
win.bind("<Return>", lambda event: submit())
|
tk.Button(win, text="Select All", font=(TEXT_FONT, BUTTON_SIZE_SELECT_ALL), command=select_all).pack(padx=10)
|
||||||
|
|
||||||
tk.Button(win, text="Open Selected", font=("Consolas", 18), padx=20, pady=10, command=submit).pack(side="right", padx=20, pady=20)
|
center_window(win, RESULTS_WIDTH, RESULTS_HEIGHT)
|
||||||
tk.Button(win, text="Select All", font=("Consolas", 18), padx=20, pady=10, command=select_all).pack(side="left", padx=20, pady=20)
|
win.lift()
|
||||||
|
win.focus_force()
|
||||||
win.wait_window()
|
win.wait_window()
|
||||||
return result["selected"]
|
return result["selected"]
|
||||||
|
|
||||||
|
|
||||||
def popup_file_preview(path, on_close_callback):
|
def popup_file_preview(path, on_close_callback):
|
||||||
win = tk.Toplevel()
|
win = tk.Toplevel()
|
||||||
win.title(path)
|
height = min(PREVIEW_HEIGHT, win.winfo_screenheight() - 80)
|
||||||
win.geometry("900x1000")
|
win.geometry(f"{PREVIEW_WIDTH}x{height}")
|
||||||
center_window(win)
|
|
||||||
|
frame = tk.Frame(win)
|
||||||
box = scrolledtext.ScrolledText(win, wrap=tk.WORD, font=("Consolas", 18))
|
frame.pack(side="bottom", fill="x")
|
||||||
box.pack(expand=True, fill="both")
|
|
||||||
|
box = scrolledtext.ScrolledText(win, wrap=tk.WORD, font=(TEXT_FONT, PREVIEW_FONT_SIZE))
|
||||||
|
box.pack(expand=True, fill="both", padx=5, pady=10)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(path, "r", errors="ignore") as f:
|
with open(path, "r", errors="ignore") as f:
|
||||||
original_text = f.read()
|
original_text = f.read()
|
||||||
box.insert(tk.END, original_text)
|
box.insert(tk.END, original_text)
|
||||||
box.edit_modified(False)
|
box.edit_modified(False)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
original_text = ""
|
||||||
box.insert(tk.END, f"Error reading file:\n{e}")
|
box.insert(tk.END, f"Error reading file:\n{e}")
|
||||||
|
|
||||||
modified = {"changed": False}
|
modified = {"changed": False}
|
||||||
|
|
||||||
def mark_modified(event=None):
|
def mark_modified(event=None):
|
||||||
if box.edit_modified():
|
if box.edit_modified():
|
||||||
modified["changed"] = True
|
modified["changed"] = True
|
||||||
box.edit_modified(False)
|
box.edit_modified(False)
|
||||||
|
|
||||||
if EDITOR_ENABLED:
|
if EDITOR_ENABLED:
|
||||||
box.bind("<<Modified>>", mark_modified)
|
box.bind("<<Modified>>", mark_modified)
|
||||||
else:
|
else:
|
||||||
box.configure(state="disabled")
|
box.configure(state="disabled")
|
||||||
|
|
||||||
frame = tk.Frame(win)
|
|
||||||
frame.pack(fill="x", pady=10)
|
|
||||||
|
|
||||||
def save_file():
|
def save_file():
|
||||||
try:
|
try:
|
||||||
with open(path, "w", errors="ignore") as f:
|
with open(path, "w", errors="ignore") as f:
|
||||||
f.write(box.get("1.0", tk.END))
|
f.write(box.get("1.0", tk.END))
|
||||||
modified["changed"] = False
|
modified["changed"] = False
|
||||||
custom_messagebox("Saved", f"File saved:\n{path}")
|
custom_message("Saved", f"File saved:\n{path}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
custom_messagebox("Save Error", str(e))
|
custom_message("Save Error", str(e))
|
||||||
|
|
||||||
if EDITOR_ENABLED:
|
if EDITOR_ENABLED:
|
||||||
tk.Button(frame, text="Save", fg="green", font=("Consolas", 18), padx=15, pady=8, command=save_file).pack(side="left", padx=10, pady=10)
|
tk.Button(frame, text="Save", fg="green", font=(TEXT_FONT, BUTTON_SIZE_SAVE), command=save_file).pack(side="left", padx=10)
|
||||||
|
|
||||||
def restore_file():
|
def restore_file():
|
||||||
if EDITOR_ENABLED and modified["changed"]:
|
if EDITOR_ENABLED and modified["changed"]:
|
||||||
ask = custom_messagebox("Unsaved Changes", "You have unsaved changes.\nSave before restoring?", kind="yesno")
|
ask = custom_yesno("Unsaved Changes", "You have unsaved changes.\nSave before restoring?")
|
||||||
if ask:
|
if ask:
|
||||||
save_file()
|
save_file()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rel_path = os.path.relpath(path, TRASH_DIR)
|
rel_path = os.path.relpath(path, TRASH_DIR)
|
||||||
dest_path = os.path.join(SEARCH_ROOT, rel_path)
|
dest_path = os.path.join(SEARCH_ROOT, rel_path)
|
||||||
|
|
||||||
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
||||||
shutil.move(path, dest_path)
|
shutil.move(path, dest_path)
|
||||||
|
custom_message("Restored", f"File restored to:\n{dest_path}")
|
||||||
custom_messagebox("Restored", f"File restored to:\n{dest_path}")
|
|
||||||
win.destroy()
|
|
||||||
on_close_callback()
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
custom_messagebox("Restore Error", str(e))
|
custom_message("Restore Error", str(e))
|
||||||
|
|
||||||
tk.Button(frame, text="Restore File", fg="blue", font=("Consolas", 18), padx=15, pady=8, command=restore_file).pack(side="left", padx=10, pady=10)
|
|
||||||
|
|
||||||
def close():
|
def close():
|
||||||
if EDITOR_ENABLED and modified["changed"]:
|
if EDITOR_ENABLED and modified["changed"]:
|
||||||
ask = custom_messagebox("Unsaved Changes", "You have unsaved changes.\nSave before closing?", kind="yesno")
|
ask = custom_yesno("Unsaved Changes", "You have unsaved changes.\nSave before closing?")
|
||||||
if ask:
|
if ask:
|
||||||
save_file()
|
save_file()
|
||||||
|
|
||||||
win.destroy()
|
win.destroy()
|
||||||
on_close_callback()
|
on_close_callback()
|
||||||
|
|
||||||
tk.Button(frame, text="Close", font=("Consolas", 18), padx=15, pady=8, command=close).pack(side="right", padx=10, pady=10)
|
|
||||||
|
|
||||||
def close_all_button():
|
|
||||||
ask = custom_messagebox("WARNING!", "Are you sure you want to close all windows?", kind="yesno")
|
|
||||||
if ask:
|
|
||||||
close_all(win.master)
|
|
||||||
|
|
||||||
if CLOSE_ALL_ENABLED:
|
|
||||||
tk.Button(frame, text="Close All", font=("Consolas", 18), padx=15, pady=8, command=close_all_button).pack(side="right", padx=10, pady=10)
|
|
||||||
|
|
||||||
def auto_close():
|
def auto_close():
|
||||||
if win.winfo_exists():
|
if win.winfo_exists():
|
||||||
win.destroy()
|
win.destroy()
|
||||||
on_close_callback()
|
on_close_callback()
|
||||||
|
|
||||||
win.protocol("WM_DELETE_WINDOW", close)
|
win.protocol("WM_DELETE_WINDOW", close)
|
||||||
win.after(AUTO_CLOSE_SECONDS * 1000, auto_close)
|
win.after(AUTO_CLOSE_SECONDS * 1000, auto_close)
|
||||||
|
|
||||||
|
tk.Button(frame, text="Restore File", fg="blue", font=(TEXT_FONT, BUTTON_SIZE_CLOSE), command=restore_file).pack(side="left", padx=10)
|
||||||
def close_all(root):
|
tk.Button(frame, text="Close", font=(TEXT_FONT, BUTTON_SIZE_CLOSE), command=close).pack(side="right", padx=10)
|
||||||
for w in root.winfo_children():
|
|
||||||
try:
|
|
||||||
w.destroy()
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
root.destroy()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
root = tk.Tk()
|
root = tk.Tk()
|
||||||
root.withdraw()
|
root.withdraw()
|
||||||
|
|
||||||
default_font = tkfont.nametofont("TkDefaultFont")
|
|
||||||
default_font.configure(family="Consolas", size=18)
|
|
||||||
|
|
||||||
text_font = tkfont.nametofont("TkTextFont")
|
|
||||||
text_font.configure(family="Consolas", size=18)
|
|
||||||
|
|
||||||
fixed_font = tkfont.nametofont("TkFixedFont")
|
|
||||||
fixed_font.configure(family="Consolas", size=18)
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
search_term = popup_search_input()
|
result = popup_search_input()
|
||||||
if not search_term:
|
search_term = result["value"]
|
||||||
|
|
||||||
|
if result["force_close"]:
|
||||||
root.destroy()
|
root.destroy()
|
||||||
return
|
return
|
||||||
|
if not search_term:
|
||||||
|
custom_message("Invalid Search", "Please enter a search term.")
|
||||||
|
continue
|
||||||
matches = search_files(search_term)
|
matches = search_files(search_term)
|
||||||
|
if not matches:
|
||||||
if matches:
|
custom_message("No Results", "No matching files found.")
|
||||||
break
|
continue
|
||||||
|
break
|
||||||
custom_messagebox("No Results", "No matching files found.")
|
|
||||||
|
|
||||||
selected = popup_results_list(matches)
|
selected = popup_results_list(matches)
|
||||||
if not selected:
|
if not selected:
|
||||||
@@ -291,17 +310,16 @@ def main():
|
|||||||
return
|
return
|
||||||
|
|
||||||
open_windows = {"count": len(selected)}
|
open_windows = {"count": len(selected)}
|
||||||
|
|
||||||
def window_closed():
|
def window_closed():
|
||||||
open_windows["count"] -= 1
|
open_windows["count"] -= 1
|
||||||
if open_windows["count"] == 0:
|
if open_windows["count"] == 0:
|
||||||
root.destroy()
|
root.destroy()
|
||||||
|
|
||||||
for path in selected:
|
for path in selected:
|
||||||
popup_file_preview(path, window_closed)
|
popup_file_preview(path, window_closed)
|
||||||
|
|
||||||
root.mainloop()
|
root.mainloop()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
@@ -1,380 +1,410 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import filedialog, messagebox, scrolledtext
|
from tkinter import scrolledtext
|
||||||
import tkinter.font as tkfont
|
import shutil
|
||||||
import shutil
|
import ctypes
|
||||||
import ctypes
|
import string
|
||||||
import string
|
import configparser
|
||||||
import configparser
|
import Indexer
|
||||||
|
|
||||||
|
# ============================================================
|
||||||
# ============================================================
|
# CONFIG:
|
||||||
# CONFIG:
|
config = configparser.ConfigParser()
|
||||||
config = configparser.ConfigParser()
|
config.read(r"C:\ProgramData\Program-Search\config.ini")
|
||||||
config.read(r"C:\ProgramData\Program-Search\config.ini")
|
# PATHS
|
||||||
SEARCH_WINDOW_SIZE = config["General"]["SEARCH_WINDOW_SIZE"]
|
SEARCH_ROOT = config["Paths"]["SEARCH_ROOT"]
|
||||||
RESULTS_WINDOW_SIZE = config["General"]["RESULTS_WINDOW_SIZE"]
|
TRASH_DIR = config["Paths"]["TRASH_DIR"]
|
||||||
PREVIEW_WINDOW_SIZE = config["General"]["PREVIEW_WINDOW_SIZE"]
|
INDEX_FILE = config["Paths"]["INDEX_FILE"]
|
||||||
SEARCH_ROOT = config["Paths"]["SEARCH_ROOT"]
|
# FEATURES
|
||||||
TRASH_DIR = config["Paths"]["TRASH_DIR"]
|
TRASH_ENABLED = config.getboolean("Features", "TRASH_ENABLED", fallback=False)
|
||||||
TRASH_ENABLED = config.getboolean("Features", "TRASH_ENABLED")
|
EDITOR_ENABLED = config.getboolean("Features", "EDITOR_ENABLED", fallback=False)
|
||||||
EDITOR_ENABLED = config.getboolean("Features", "EDITOR_ENABLED")
|
AUTO_CLOSE_SECONDS = config.getint("Features", "AUTO_CLOSE_SECONDS", fallback=3600)
|
||||||
MANUEAL_REPLACE_ENABLED = config.getboolean("Features", "MANUEAL_REPLACE_ENABLED")
|
# COPY
|
||||||
CLOSE_ALL_ENABLED = config.getboolean("Features", "CLOSE_ALL_ENABLED")
|
EXTENSION = config.get("Copy", "Extension", fallback="")
|
||||||
AUTO_CLOSE_SECONDS = config.getint("Features", "AUTO_CLOSE_SECONDS")
|
# UI
|
||||||
Extension = config["Copy"]["Extension"]
|
TEXT_FONT = config.get("UI","TEXT_FONT", fallback="Consolas")
|
||||||
# ============================================================
|
SEARCH_WIDTH = config.getint("UI", "SEARCH_WIDTH", fallback=400)
|
||||||
|
SEARCH_HEIGHT = config.getint("UI", "SEARCH_HEIGHT", fallback=150)
|
||||||
|
SEARCH_FONT_SIZE = config.getint("UI", "SEARCH_FONT_SIZE", fallback=12)
|
||||||
os.makedirs(TRASH_DIR, exist_ok=True)
|
RESULTS_WIDTH = config.getint("UI", "RESULTS_WIDTH", fallback=1000)
|
||||||
|
RESULTS_HEIGHT = config.getint("UI", "RESULTS_HEIGHT", fallback=700)
|
||||||
|
RESULTS_FONT_SIZE = config.getint("UI", "RESULTS_FONT_SIZE", fallback=11)
|
||||||
def custom_messagebox(title, message, kind="info"):
|
PREVIEW_WIDTH = config.getint("UI", "PREVIEW_WIDTH", fallback=1200)
|
||||||
win = tk.Toplevel()
|
PREVIEW_HEIGHT = config.getint("UI", "PREVIEW_HEIGHT", fallback=900)
|
||||||
win.title(title)
|
PREVIEW_FONT_SIZE = config.getint("UI", "PREVIEW_FONT_SIZE", fallback=24)
|
||||||
win.geometry(SEARCH_WINDOW_SIZE)
|
ERROR_WIDTH = config.getint("UI", "ERROR_WIDTH", fallback=400)
|
||||||
win.grab_set()
|
ERROR_HEIGHT = config.getint("UI", "ERROR_HEIGHT", fallback=100)
|
||||||
win.focus_force()
|
ERROR_FONT_SIZE = config.getint("UI", "ERROR_FONT_SIZE", fallback=11)
|
||||||
frame = tk.Frame(win, padx=20, pady=20)
|
# BUTTONS
|
||||||
frame.pack(expand=True, fill="both")
|
BUTTON_SIZE_SEARCH = config.getint("Buttons", "BUTTON_SIZE_SEARCH", fallback=15)
|
||||||
center_window(win)
|
BUTTON_SIZE_OPEN = config.getint("Buttons", "BUTTON_SIZE_OPEN", fallback=15)
|
||||||
|
BUTTON_SIZE_SELECT_ALL = config.getint("Buttons", "BUTTON_SIZE_SELECT_ALL", fallback=15)
|
||||||
tk.Label(frame, text=message, font=("Consolas", 24), wraplength=550).pack(pady=20)
|
BUTTON_SIZE_SAVE = config.getint("Buttons", "BUTTON_SIZE_SAVE", fallback=15)
|
||||||
def close():
|
BUTTON_SIZE_COPY = config.getint("Buttons", "BUTTON_SIZE_COPY", fallback=15)
|
||||||
win.destroy()
|
BUTTON_SIZE_TRASH = config.getint("Buttons", "BUTTON_SIZE_TRASH", fallback=15)
|
||||||
|
BUTTON_SIZE_CLOSE = config.getint("Buttons", "BUTTON_SIZE_CLOSE", fallback=15)
|
||||||
if kind == "yesno":
|
BUTTON_SIZE_MESSAGE = config.getint("Buttons", "BUTTON_SIZE_MESSAGE", fallback=15)
|
||||||
result = {"value": False}
|
BUTTON_SIZE_YES_NO = config.getint("Buttons", "BUTTON_SIZE_YES_NO", fallback=15)
|
||||||
IsInfo = False
|
# ============================================================
|
||||||
def yes():
|
|
||||||
result["value"] = True
|
os.makedirs(TRASH_DIR, exist_ok=True)
|
||||||
win.destroy()
|
|
||||||
|
SEARCH_INDEX = Indexer.load_index()
|
||||||
def no():
|
if Indexer.update_index(SEARCH_INDEX):
|
||||||
result["value"] = False
|
try:
|
||||||
win.destroy()
|
Indexer.save_index(SEARCH_INDEX)
|
||||||
|
except Exception:
|
||||||
tk.Button(frame, text="Yes", font=("Consolas", 24), padx=10, pady=10, command=yes).pack(side="left", padx=16)
|
pass
|
||||||
tk.Button(frame, text="No", font=("Consolas", 24), padx=10, pady=10, command=no).pack(side="right", padx=16)
|
|
||||||
|
def find_external_drives():
|
||||||
win.wait_window()
|
drives = []
|
||||||
return result["value"]
|
DRIVE_REMOVABLE = 2
|
||||||
|
|
||||||
else:
|
for letter in string.ascii_uppercase:
|
||||||
tk.Button(frame, text="OK", font=("Consolas", 24), padx=10, pady=10, command=close).pack(pady=20)
|
drive = f"{letter}:\\"
|
||||||
win.bind("<Return>", lambda event: close())
|
if os.path.exists(drive):
|
||||||
win.wait_window()
|
dtype = ctypes.windll.kernel32.GetDriveTypeW(drive)
|
||||||
|
if dtype == DRIVE_REMOVABLE:
|
||||||
|
drives.append(drive)
|
||||||
def center_window(win):
|
|
||||||
win.update_idletasks()
|
return drives
|
||||||
w = win.winfo_width()
|
|
||||||
h = win.winfo_height()
|
|
||||||
sw = win.winfo_screenwidth()
|
def popup_search_input():
|
||||||
sh = win.winfo_screenheight()
|
win = tk.Toplevel()
|
||||||
x = (sw // 2) - (w // 2)
|
win.title("Search")
|
||||||
y = (sh // 2) - (h // 2)
|
win.geometry(f"{SEARCH_WIDTH}x{SEARCH_HEIGHT}")
|
||||||
win.geometry(f"{w}x{h}+{x}+{y}")
|
|
||||||
|
tk.Label(win, text="Enter part number or name:", font=(TEXT_FONT, SEARCH_FONT_SIZE)).pack(pady=10)
|
||||||
|
entry = tk.Entry(win, font=(TEXT_FONT, SEARCH_FONT_SIZE))
|
||||||
def find_external_drives():
|
entry.pack(pady=5)
|
||||||
drives = []
|
win.lift()
|
||||||
DRIVE_REMOVABLE = 2
|
win.attributes("-topmost", True)
|
||||||
|
win.after(50, lambda: (entry.focus_set(), entry.icursor(tk.END)))
|
||||||
for letter in string.ascii_uppercase:
|
win.after(100, lambda: (win.attributes("-topmost"), False))
|
||||||
drive = f"{letter}:\\"
|
|
||||||
if os.path.exists(drive):
|
result = {"value": None, "force_close": False}
|
||||||
dtype = ctypes.windll.kernel32.GetDriveTypeW(drive)
|
|
||||||
if dtype == DRIVE_REMOVABLE:
|
def submit():
|
||||||
drives.append(drive)
|
result["value"] = entry.get().strip().lower()
|
||||||
|
win.destroy()
|
||||||
return drives
|
|
||||||
|
def force_close():
|
||||||
|
result["force_close"] = True
|
||||||
def close_all(root):
|
win.destroy()
|
||||||
for w in root.winfo_children():
|
|
||||||
try:
|
entry.bind("<Return>", lambda e: submit())
|
||||||
w.destroy()
|
tk.Button(win, text="Search", font=(TEXT_FONT, BUTTON_SIZE_SEARCH), command=submit).pack(pady=10)
|
||||||
except:
|
|
||||||
pass
|
center_window(win,SEARCH_WIDTH, SEARCH_HEIGHT)
|
||||||
root.destroy()
|
win.protocol("WM_DELETE_WINDOW", force_close)
|
||||||
|
win.wait_window()
|
||||||
|
return result
|
||||||
def popup_search_input():
|
|
||||||
win = tk.Toplevel()
|
|
||||||
win.title("Search")
|
def center_window(win, width, height):
|
||||||
win.geometry(SEARCH_WINDOW_SIZE)
|
screen_width = win.winfo_screenwidth()
|
||||||
center_window(win)
|
screen_height = win.winfo_screenheight()
|
||||||
|
|
||||||
tk.Label(win, text="Enter program number, part number, or name:").pack(pady=10)
|
x = int((screen_width / 2) - (width / 2))
|
||||||
entry = tk.Entry(win)
|
y = int((screen_height / 2) - (height / 2))
|
||||||
entry.pack(pady=5)
|
|
||||||
entry.focus_set()
|
win.geometry(f"{width}x{height}+{x}+{y}")
|
||||||
entry.bind("<Return>", lambda event: submit())
|
|
||||||
win.bind("<Return>", lambda event: submit())
|
|
||||||
|
def custom_message(title, message):
|
||||||
result = {"value": None}
|
win = tk.Toplevel()
|
||||||
|
win.title(title)
|
||||||
def submit():
|
win.protocol("WM_DELETE_WINDOW", win.destroy)
|
||||||
result["value"] = entry.get().strip().lower()
|
|
||||||
win.destroy()
|
|
||||||
|
tk.Label(win, text=message, font=(TEXT_FONT, ERROR_FONT_SIZE), wraplength=ERROR_WIDTH - 30, justify="center").pack(padx=15, pady=15)
|
||||||
tk.Button(win, text="Search", command=submit, padx=20, pady=20).pack(pady=60)
|
|
||||||
|
tk.Button(win, text="OK", font=(TEXT_FONT, BUTTON_SIZE_MESSAGE), command=win.destroy).pack(pady=10)
|
||||||
win.wait_window(); return result["value"]
|
|
||||||
|
win.bind("<Return>", lambda e: win.destroy())
|
||||||
|
|
||||||
def search_files(term):
|
center_window(win,ERROR_WIDTH, ERROR_HEIGHT)
|
||||||
matches = []
|
win.lift()
|
||||||
|
win.focus_force()
|
||||||
for root, dirs, files in os.walk(SEARCH_ROOT):
|
win.transient()
|
||||||
for fname in files:
|
win.grab_set()
|
||||||
path = os.path.join(root, fname)
|
win.update()
|
||||||
fname_lower = fname.lower()
|
win.wait_window()
|
||||||
|
|
||||||
filename_match = term in fname_lower
|
|
||||||
contents_match = False
|
def custom_yesno(title, message):
|
||||||
|
win = tk.Toplevel()
|
||||||
try:
|
win.title(title)
|
||||||
with open(path, "r", errors="ignore") as f:
|
|
||||||
if term in f.read().lower():
|
result = {"value": False}
|
||||||
contents_match = True
|
|
||||||
except:
|
tk.Label(
|
||||||
pass
|
win,
|
||||||
|
text=message,
|
||||||
if filename_match or contents_match:
|
font=(TEXT_FONT, ERROR_FONT_SIZE),
|
||||||
matches.append(path)
|
wraplength=ERROR_WIDTH - 30,
|
||||||
|
justify="center"
|
||||||
return matches
|
).pack(padx=15, pady=15)
|
||||||
|
|
||||||
|
button_frame = tk.Frame(win)
|
||||||
def popup_results_list(matches):
|
button_frame.pack(pady=10)
|
||||||
win = tk.Toplevel()
|
|
||||||
win.title("Matches Found")
|
def yes():
|
||||||
win.geometry(RESULTS_WINDOW_SIZE)
|
result["value"] = True
|
||||||
center_window(win)
|
win.destroy()
|
||||||
|
|
||||||
tk.Label(win, text="Select files to preview:").pack(pady=10)
|
def no():
|
||||||
|
result["value"] = False
|
||||||
lb = tk.Listbox(win, selectmode=tk.MULTIPLE)
|
win.destroy()
|
||||||
lb.pack(expand=True, fill="both")
|
|
||||||
|
tk.Button(button_frame, text="Yes", width=10, font=(TEXT_FONT, BUTTON_SIZE_YES_NO), command=yes).pack(side="left", padx=10)
|
||||||
for m in matches:
|
tk.Button(button_frame, text="No", width=10, font=(TEXT_FONT, BUTTON_SIZE_YES_NO), command=no).pack(side="left", padx=10)
|
||||||
lb.insert(tk.END, m)
|
|
||||||
|
win.protocol("WM_DELETE_WINDOW", no)
|
||||||
result = {"selected": []}
|
center_window(win,ERROR_WIDTH, ERROR_HEIGHT)
|
||||||
|
win.lift()
|
||||||
def select_all():
|
win.focus_force()
|
||||||
lb.select_set(0, tk.END)
|
win.grab_set()
|
||||||
def submit():
|
win.wait_window()
|
||||||
sel = lb.curselection()
|
|
||||||
result["selected"] = [matches[i] for i in sel]
|
return result["value"]
|
||||||
win.destroy()
|
|
||||||
|
|
||||||
tk.Button(win, text="Open Selected", command=submit, padx=20, pady=10).pack(side ="right", padx=20, pady=10)
|
def search_files(term):
|
||||||
tk.Button(win, text="Select All", command=select_all, padx=20, pady=10).pack(side ="left", padx=20, pady=10)
|
|
||||||
|
term = term.lower()
|
||||||
lb.bind("<Return>", lambda event: submit())
|
|
||||||
win.bind("<Return>", lambda event: submit())
|
matches = []
|
||||||
|
|
||||||
win.wait_window(); return result["selected"]
|
for path, data in SEARCH_INDEX.items():
|
||||||
|
|
||||||
|
if term in data.get("filename"):
|
||||||
def popup_file_preview(path, on_close_callback):
|
matches.append(path)
|
||||||
win = tk.Toplevel()
|
|
||||||
win.title(path)
|
elif term in data.get("content", ""):
|
||||||
win.geometry(PREVIEW_WINDOW_SIZE)
|
matches.append(path)
|
||||||
|
|
||||||
box = scrolledtext.ScrolledText(win, wrap=tk.WORD)
|
return matches
|
||||||
box.pack(expand=True, fill="both")
|
|
||||||
|
|
||||||
try:
|
def popup_results_list(matches):
|
||||||
with open(path, "r", errors="ignore") as f:
|
win = tk.Toplevel()
|
||||||
original_text = f.read()
|
win.title("Matches Found")
|
||||||
box.insert(tk.END, original_text)
|
win.geometry(f"{RESULTS_WIDTH}x{RESULTS_HEIGHT}")
|
||||||
box.edit_modified(False)
|
|
||||||
except Exception as e:
|
tk.Label(win, text="Select files to preview:", font=(TEXT_FONT, RESULTS_FONT_SIZE)).pack(pady=10)
|
||||||
original_text = ""
|
|
||||||
box.insert(tk.END, f"Error reading file:\n{e}")
|
lb = tk.Listbox(win, selectmode=tk.MULTIPLE, font=(TEXT_FONT, RESULTS_FONT_SIZE))
|
||||||
|
lb.pack(expand=True, fill="both")
|
||||||
modified = {"changed": False}
|
lb.bind("<Return>", lambda e: submit())
|
||||||
|
|
||||||
def mark_modified(event=None):
|
for m in matches:
|
||||||
if box.edit_modified():
|
parent = os.path.basename(os.path.dirname(m))
|
||||||
modified["changed"] = True
|
filename = os.path.basename(m)
|
||||||
box.edit_modified(False)
|
display = f"{parent}\\{filename}"
|
||||||
|
lb.insert(tk.END, display)
|
||||||
if EDITOR_ENABLED:
|
|
||||||
box.bind("<<Modified>>", mark_modified)
|
result = {"selected": []}
|
||||||
else:
|
|
||||||
box.configure(state="disabled")
|
def select_all():
|
||||||
|
lb.select_set(0, tk.END)
|
||||||
frame = tk.Frame(win)
|
def submit():
|
||||||
frame.pack(fill="x", pady=10)
|
sel = lb.curselection()
|
||||||
|
if not sel:
|
||||||
|
custom_message("No Selection", "Please select at least 1 file.")
|
||||||
def save_file():
|
return
|
||||||
try:
|
result["selected"] = [matches[i] for i in sel]
|
||||||
with open(path, "w", errors="ignore") as f:
|
win.destroy()
|
||||||
f.write(box.get("1.0", tk.END))
|
|
||||||
modified["changed"] = False
|
tk.Button(win, text="Open Selected", font=(TEXT_FONT, BUTTON_SIZE_OPEN), command=submit).pack(padx=10)
|
||||||
custom_messagebox("Saved", f"File saved:\n{path}")
|
tk.Button(win, text="Select All", font=(TEXT_FONT, BUTTON_SIZE_SELECT_ALL), command=select_all).pack(padx=10)
|
||||||
except Exception as e:
|
|
||||||
custom_messagebox("Save Error", str(e))
|
center_window(win,RESULTS_WIDTH, RESULTS_HEIGHT)
|
||||||
|
win.lift()
|
||||||
def copy_to_external():
|
win.focus_force()
|
||||||
if EDITOR_ENABLED and modified["changed"]:
|
win.wait_window()
|
||||||
ask = custom_messagebox("Unsaved Changes", "You have unsaved changes.\nSave before copying?", kind = "yesno")
|
return result["selected"]
|
||||||
if ask:
|
|
||||||
save_file()
|
|
||||||
|
def popup_file_preview(path, on_close_callback, preview_windows):
|
||||||
drives = find_external_drives()
|
win = tk.Toplevel()
|
||||||
if not drives:
|
preview_windows.append(win)
|
||||||
custom_messagebox("No External Drive Found", "No external drive detected."); return
|
win.title(path)
|
||||||
|
height = min(PREVIEW_HEIGHT, win.winfo_screenheight() - 80)
|
||||||
base_name = os.path.splitext(os.path.basename(path))[0]
|
win.geometry(f"{PREVIEW_WIDTH}x{height}")
|
||||||
new_name = base_name + Extension
|
win.bind("<Escape>", lambda e: close())
|
||||||
|
|
||||||
copied = []
|
frame = tk.Frame(win)
|
||||||
for d in drives:
|
frame.pack(side="bottom", fill="x")
|
||||||
try:
|
|
||||||
dest = os.path.join(d, new_name)
|
box = scrolledtext.ScrolledText(win, wrap=tk.WORD, font=(TEXT_FONT, PREVIEW_FONT_SIZE))
|
||||||
shutil.copy2(path, dest)
|
box.pack(expand=True, fill="both", padx=5, pady=10)
|
||||||
copied.append(dest)
|
|
||||||
except Exception as e:
|
try:
|
||||||
custom_messagebox("Copy Error", f"Could not copy to {d}:\n{e}")
|
with open(path, "r", errors="ignore") as f:
|
||||||
|
original_text = f.read()
|
||||||
if copied:
|
box.insert(tk.END, original_text)
|
||||||
custom_messagebox("Copied Successfully", "Copied to:\n\n" + "\n".join(copied))
|
box.edit_modified(False)
|
||||||
win.destroy()
|
except Exception as e:
|
||||||
on_close_callback()
|
original_text = ""
|
||||||
close_all(win.master)
|
box.insert(tk.END, f"Error reading file:\n{e}")
|
||||||
|
|
||||||
def move_to_trash():
|
modified = {"changed": False}
|
||||||
confirm = custom_messagebox("Move to Trash",f"Are you sure you want to move this file to trash?\n\n{path}", kind = "yesno")
|
|
||||||
if confirm:
|
def mark_modified(event=None):
|
||||||
try:
|
if box.edit_modified():
|
||||||
rel_path = os.path.relpath(path, SEARCH_ROOT)
|
modified["changed"] = True
|
||||||
dest_path = os.path.join(TRASH_DIR, rel_path)
|
box.edit_modified(False)
|
||||||
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
|
||||||
shutil.move(path, dest_path)
|
if EDITOR_ENABLED:
|
||||||
custom_messagebox("Trashed", f"Moved to trash:\n{dest_path}")
|
box.bind("<<Modified>>", mark_modified)
|
||||||
win.destroy()
|
else:
|
||||||
on_close_callback()
|
box.configure(state="disabled")
|
||||||
|
|
||||||
except Exception as e:
|
|
||||||
custom_messagebox("Trash Error", str(e))
|
|
||||||
|
def save_file():
|
||||||
def manual_replace():
|
try:
|
||||||
drives = find_external_drives()
|
with open(path, "w", errors="ignore") as f:
|
||||||
if not drives: custom_messagebox("No External Drive Found","No external drive detected."); return
|
f.write(box.get("1.0", tk.END))
|
||||||
base_name = os.path.splitext(os.path.basename(path))[0]
|
modified["changed"] = False
|
||||||
new_name = base_name + Extension
|
custom_message("Saved", f"File saved:\n{path}")
|
||||||
replacement_path = None
|
except Exception as e:
|
||||||
for d in drives:
|
custom_message("Save Error", str(e))
|
||||||
candidate = os.path.join(d, new_name)
|
|
||||||
if os.path.exists(candidate): replacement_path = candidate;
|
if EDITOR_ENABLED:
|
||||||
break
|
tk.Button(frame, text="Save", fg="green", font=(TEXT_FONT, BUTTON_SIZE_SAVE), command=save_file).pack(side="left", padx=10)
|
||||||
if not replacement_path: custom_messagebox("File Not Found",f"No replacement file named:\n\n{new_name}\n\nwas found on any external drive."); return
|
|
||||||
try:
|
def copy_to_external():
|
||||||
with open(replacement_path,"r",errors="ignore") as f: new_contents = f.read()
|
if EDITOR_ENABLED and modified["changed"]:
|
||||||
except Exception as e: custom_messagebox("Read Error",f"Could not read replacement file:\n{e}"); return
|
ask = custom_yesno(
|
||||||
try:
|
"Unsaved Changes",
|
||||||
with open(path,"w",errors="ignore") as f: f.write(new_contents)
|
"You have unsaved changes.\nSave before copying?"
|
||||||
except Exception as e: custom_messagebox("Write Error",f"Could not overwrite original file:\n{e}"); return
|
)
|
||||||
box.delete("1.0",tk.END); box.insert(tk.END,new_contents); box.edit_modified(False); modified["changed"] = False
|
if ask:
|
||||||
custom_messagebox("Replaced Successfully",f"File replaced using:\n{replacement_path}")
|
save_file()
|
||||||
|
|
||||||
def close():
|
drives = find_external_drives()
|
||||||
if EDITOR_ENABLED and modified["changed"]:
|
if not drives:
|
||||||
ask = custom_messagebox("Unsaved Changes", "You have unsaved changes.\nSave before closing?", kind = "yesno")
|
custom_message("No External Drive Found", "No external drive detected.")
|
||||||
if ask:
|
return
|
||||||
save_file()
|
|
||||||
|
base_name = os.path.splitext(os.path.basename(path))[0]
|
||||||
win.destroy()
|
new_name = base_name + EXTENSION
|
||||||
on_close_callback()
|
|
||||||
|
copied = []
|
||||||
def auto_close():
|
for d in drives:
|
||||||
if win.winfo_exists():
|
try:
|
||||||
win.destroy()
|
dest = os.path.join(d, new_name)
|
||||||
on_close_callback()
|
shutil.copy2(path, dest)
|
||||||
|
copied.append(dest)
|
||||||
def close_all_button():
|
except Exception as e:
|
||||||
ask = custom_messagebox("WARNING!", "Are you sure you want to cloose all windows?", kind = "yesno")
|
custom_message("Copy Error", f"Could not copy to {d}:\n{e}")
|
||||||
if ask:
|
|
||||||
close_all(win.master)
|
if copied:
|
||||||
|
custom_message("Copied Successfully", "Copied to:\n\n" + "\n".join(copied))
|
||||||
win.protocol("WM_DELETE_WINDOW", close)
|
win.destroy()
|
||||||
win.after(AUTO_CLOSE_SECONDS * 1000, auto_close)
|
on_close_callback()
|
||||||
|
|
||||||
if EDITOR_ENABLED:
|
def move_to_trash():
|
||||||
tk.Button(win, text="Save", fg="green", command=save_file, padx=15, pady=8).pack(side ="left",padx=10, pady=10)
|
confirm = custom_yesno(
|
||||||
|
"Move to Trash",
|
||||||
if TRASH_ENABLED == True:
|
f"Are you sure you want to move this file to trash?\n\n{path}"
|
||||||
tk.Button(win, text="Move to Trash", fg="red", command=move_to_trash, padx=15, pady=8).pack(side ="left",padx=10, pady=10)
|
)
|
||||||
|
if confirm:
|
||||||
tk.Button(win, text="Copy to External Drive", command=copy_to_external, padx=15, pady=8).pack(side ="left",padx=10, pady=10)
|
try:
|
||||||
|
rel_path = os.path.relpath(path, SEARCH_ROOT)
|
||||||
tk.Button(win, text="Close", command=close, padx=15, pady=8).pack(side ="right",padx=10, pady=10)
|
dest_path = os.path.join(TRASH_DIR, rel_path)
|
||||||
|
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
|
||||||
if CLOSE_ALL_ENABLED:
|
shutil.move(path, dest_path)
|
||||||
tk.Button(win, text="Close All", command=lambda: close_all_button(), padx=15, pady=8).pack(side ="right",padx=10, pady=10)
|
custom_message("Trashed", f"Moved to trash:\n{dest_path}")
|
||||||
|
win.destroy()
|
||||||
if MANUEAL_REPLACE_ENABLED == True:
|
on_close_callback()
|
||||||
tk.Button(win, text="Replace from External Drive", command=manual_replace, padx=15, pady=8).pack(side ="right",padx=10, pady=10)
|
except Exception as e:
|
||||||
|
custom_message("Trash Error", str(e))
|
||||||
|
|
||||||
|
def close():
|
||||||
def main():
|
if EDITOR_ENABLED and modified["changed"]:
|
||||||
root = tk.Tk()
|
ask = custom_yesno("Unsaved Changes","You have unsaved changes.\nSave before closing?")
|
||||||
root.withdraw()
|
if ask:
|
||||||
default_font = tkfont.nametofont("TkDefaultFont")
|
save_file()
|
||||||
default_font.configure(family="Consolas", size=18)
|
if win in preview_windows:
|
||||||
|
preview_windows.remove(win)
|
||||||
text_font = tkfont.nametofont("TkTextFont")
|
win.destroy()
|
||||||
text_font.configure(family="Consolas", size=18)
|
on_close_callback()
|
||||||
|
|
||||||
fixed_font = tkfont.nametofont("TkFixedFont")
|
def auto_close():
|
||||||
fixed_font.configure(family="Consolas", size=18)
|
if win.winfo_exists():
|
||||||
|
win.destroy()
|
||||||
root.option_add("*Dialog.msg.font", "Consolas 18")
|
on_close_callback()
|
||||||
root.option_add("*Dialog.msg.width", 60)
|
|
||||||
root.option_add("*Dialog.msg.height", 10)
|
def close_all():
|
||||||
root.option_add("*Dialog.msg.wrapLength", 600)
|
confirm = custom_yesno("Close All", "Close all preview windows?")
|
||||||
|
if not confirm:
|
||||||
while True:
|
return
|
||||||
search_term = popup_search_input()
|
for preview in preview_windows[:]:
|
||||||
if not search_term:
|
try:
|
||||||
root.destroy()
|
if preview.winfo_exists():
|
||||||
return
|
preview.destroy()
|
||||||
|
except tk.TclError:
|
||||||
matches = search_files(search_term)
|
pass
|
||||||
|
preview_windows.clear()
|
||||||
if matches:
|
|
||||||
break
|
win.protocol("WM_DELETE_WINDOW", close)
|
||||||
|
win.after(AUTO_CLOSE_SECONDS * 1000, auto_close)
|
||||||
custom_messagebox("No Results", "No matching files found.")
|
|
||||||
|
if TRASH_ENABLED:
|
||||||
selected = popup_results_list(matches)
|
tk.Button(frame, text="Move to Trash", fg="red", font=(TEXT_FONT, BUTTON_SIZE_TRASH), command=move_to_trash).pack(side="left", padx=10)
|
||||||
if not selected:
|
|
||||||
root.destroy()
|
tk.Button(frame, text="Copy to External Drive", fg="blue", font=(TEXT_FONT, BUTTON_SIZE_COPY), command=copy_to_external).pack(side="left", padx=10)
|
||||||
return
|
|
||||||
|
tk.Button(frame, text="Close", font=(TEXT_FONT, BUTTON_SIZE_CLOSE), command=close).pack(side="right", padx=10)
|
||||||
|
|
||||||
open_windows = {"count": len(selected)}
|
tk.Button(frame, text="Close all", font=(TEXT_FONT, BUTTON_SIZE_CLOSE), command=close_all).pack(side="right", padx=10)
|
||||||
|
|
||||||
def window_closed():
|
def main():
|
||||||
open_windows["count"] -= 1
|
root = tk.Tk()
|
||||||
if open_windows["count"] == 0:
|
root.withdraw()
|
||||||
root.destroy()
|
|
||||||
|
while True:
|
||||||
|
result = popup_search_input()
|
||||||
for path in selected:
|
search_term = result["value"]
|
||||||
popup_file_preview(path, window_closed)
|
|
||||||
|
if result["force_close"]:
|
||||||
root.mainloop()
|
root.destroy()
|
||||||
|
return
|
||||||
|
if not search_term:
|
||||||
if __name__ == "__main__":
|
custom_message("Invalid Search", "Please enter a search term.")
|
||||||
|
continue
|
||||||
|
matches = search_files(search_term)
|
||||||
|
if not matches:
|
||||||
|
custom_message("No Results", "No matching files found.")
|
||||||
|
continue
|
||||||
|
break
|
||||||
|
|
||||||
|
selected = popup_results_list(matches)
|
||||||
|
if not selected:
|
||||||
|
root.destroy()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
open_windows = {"count": len(selected)}
|
||||||
|
|
||||||
|
preview_windows = []
|
||||||
|
|
||||||
|
def window_closed():
|
||||||
|
open_windows["count"] -= 1
|
||||||
|
if open_windows["count"] == 0:
|
||||||
|
root.destroy()
|
||||||
|
|
||||||
|
|
||||||
|
for path in selected:
|
||||||
|
popup_file_preview(path, window_closed, preview_windows)
|
||||||
|
|
||||||
|
root.mainloop()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
-18
@@ -1,18 +0,0 @@
|
|||||||
[General]
|
|
||||||
SEARCH_WINDOW_SIZE = 600x300
|
|
||||||
RESULTS_WINDOW_SIZE = 1200x1000
|
|
||||||
PREVIEW_WINDOW_SIZE = 900x1000
|
|
||||||
|
|
||||||
[Paths]
|
|
||||||
SEARCH_ROOT = C:\CNCPrograms\Programs
|
|
||||||
TRASH_DIR = C:\CNCPrograms\Trash
|
|
||||||
|
|
||||||
[Features]
|
|
||||||
TRASH_ENABLED = False
|
|
||||||
EDITOR_ENABLED = False
|
|
||||||
MANUEAL_REPLACE_ENABLED = False
|
|
||||||
CLOSE_ALL_ENABLED = True
|
|
||||||
AUTO_CLOSE_SECONDS = 300
|
|
||||||
|
|
||||||
[Copy]
|
|
||||||
Extension =
|
|
||||||
Reference in new issue
Block a user