Improve photo organization controls and reference completion

This commit is contained in:
ajp_anton committed 2026-10-06 19:27:32 +00:00
1 parent ad2b15994f
commit e3633b8e1a
6 files changed
+316 -41

No files matched your search

+69 -24
View File
@@ -12,6 +12,7 @@ from pathlib import Path
from tools.console import dark_field, dim, draw_screen, inverse, light_red, raw_key_input
from tools.exiftool import run_exiftool_json
from tools.filenames import naive_wall_time
from tools.filesystem import complete_path, entered_path, resolve_output_directory
from tools.media_metadata import DATETIME_RE
from tools.photo_planning import UserChoices
from tools.photo_records import IMAGE_EXTS, MediaRecord, build_records
@@ -137,6 +138,7 @@ class TimezoneAnalysis:
@dataclass
class SettingsState:
working_dir: Path
time_offset: timedelta | None
infer_missing_timestamps: bool
fixed_timezone: timezone
@@ -146,6 +148,7 @@ class SettingsState:
artist_enabled: bool = False
artist_value: str = ""
organize_files: bool = False
group_files: bool = True
group_min_size: int = 2
process_pto_files: bool = True
@@ -218,7 +221,7 @@ def parse_clock_time(value: str, reference: datetime) -> datetime:
def reference_record(executable: str, records: list[MediaRecord], value: str) -> MediaRecord:
text = value.strip().strip('"')
text = str(entered_path(value))
absolute = os.path.isabs(text) or bool(re.match(r"^[A-Za-z]:[\\/]", text) or text.startswith("\\\\"))
candidates = records if absolute else [record for record in records if record.path.name.lower() == text.lower()]
if not candidates and not absolute:
@@ -243,6 +246,16 @@ def reference_record(executable: str, records: list[MediaRecord], value: str) ->
return photos[0]
def reference_completions(text: str, records: list[MediaRecord]) -> list[str]:
quote = text[0] if text[:1] in {"\"", "'"} else ""
prefix = text[1:] if quote else text
if quote and prefix.endswith(quote):
prefix = prefix[:-1]
names = {record.path.name for record in records if record.is_image}
return [quote + name + quote for name in sorted(names, key=str.casefold)
if name.casefold().startswith(prefix.casefold())]
def option_text(text: str, selected: bool, active: bool = True) -> str:
value = f"{text}"
if not active:
@@ -271,7 +284,6 @@ def default_artist(records: list[MediaRecord]) -> str:
def settings_lines(
state: SettingsState,
analysis: TimezoneAnalysis,
working_dir: Path,
rows: list[str],
row: int,
option: int,
@@ -287,7 +299,8 @@ def settings_lines(
option_text("none", option == 0),
option_text(time_value if state.time_offset else "shift", option == 1),
]
rename_names = ["Adjust/replace", "Adjust/add", "Replace", "Add"]
rename_modes = ("none", "adjust_replace", "adjust_add", "replace", "add")
rename_names = ["No", "Adjust/replace", "Adjust/add", "Replace", "Add"]
rename_options = [option_text(name, option == index) for index, name in enumerate(rename_names)]
fixed = timezone_to_string(state.fixed_timezone)
fixed_option = f"Set {fixed} ({'on' if state.fixed_timezone_enabled else 'off'})"
@@ -295,18 +308,19 @@ def settings_lines(
option_text(fixed_option, option == 0),
option_text("Fill gaps", option == 1, analysis.gap_count > 0),
]
group_options = [option_text("Yes" if state.organize_files else "No", option == 0)]
if selected == "organize" or state.organize_files:
organize_mode = "Grouping" if state.organize_files and state.group_files else "Flat" if state.organize_files else "No moving"
group_options = [option_text(organize_mode, selected == "organize" and option == 0)]
if selected == "organize" or organize_mode == "Grouping":
group_options.extend(
(
option_text(f"{state.group_min_size}+ files", option == 1, state.organize_files),
option_text(".pto", option == 2, state.organize_files and state.process_pto_files),
option_text(f"{state.group_min_size}+ files", selected == "organize" and option == 1, organize_mode == "Grouping"),
option_text(f".pto {'on' if state.process_pto_files else 'off'}", selected == "organize" and option == 2, organize_mode == "Grouping"),
)
)
if selected != "time":
time_options = [time_value]
if selected != "rename":
rename_options = [rename_names[("adjust_replace", "adjust_add", "replace", "add").index(state.rename_mode)]]
rename_options = [rename_names[rename_modes.index(state.rename_mode)]]
if selected != "timezone":
enabled = []
if state.fixed_timezone_enabled:
@@ -319,11 +333,10 @@ def settings_lines(
artist = light_red("(remove)")
if selected == "artist":
artist = option_text("no change", True) if not state.artist_enabled else option_text(artist, True)
if selected != "organize" and not state.organize_files:
group_options = ["No"]
if selected != "organize" and organize_mode != "Grouping":
group_options = [organize_mode]
lines = [
f"Working directory: {working_dir}",
"",
f"Output directory: {option_text(str(state.working_dir), selected == 'working')}",
f"Time correction: {' '.join(time_options)}",
]
if "infer" in rows:
@@ -372,6 +385,7 @@ def prompt_choices(
analysis = analyze_timezones(records)
first_zone = analysis.ordered_zones[0] if analysis.ordered_zones else local_timezone()
state = copy.deepcopy(initial_state) if initial_state else SettingsState(
working_dir=working_dir,
time_offset=None,
infer_missing_timestamps=True,
fixed_timezone=first_zone,
@@ -380,7 +394,7 @@ def prompt_choices(
process_pto_files=has_pto_files,
artist_value=default_artist(records),
)
rows = ["time"]
rows = ["working", "time"]
if any(record.original_time is None for record in records):
rows.append("infer")
rows.extend(["timezone", "rename", "artist", "organize", "confirm"])
@@ -389,12 +403,13 @@ def prompt_choices(
editor = None
text = message = ""
reference = None
completions = []
def enter_row(index: int) -> None:
nonlocal row, option
row = index % len(rows)
if rows[row] == "rename":
option = ("adjust_replace", "adjust_add", "replace", "add").index(state.rename_mode)
option = ("none", "adjust_replace", "adjust_add", "replace", "add").index(state.rename_mode)
elif rows[row] == "time":
option = 1 if state.time_offset is not None else 0
else:
@@ -408,13 +423,26 @@ def prompt_choices(
with raw_key_input() as next_key:
while True:
draw_screen(settings_lines(state, analysis, working_dir, rows, row, option, editor=editor, text=text, message=message, reference=reference))
lines = settings_lines(state, analysis, rows, row, option, editor=editor, text=text, message=message, reference=reference)
if completions:
lines.insert(-2, dim(" ".join(completions[:8]) + (" ..." if len(completions) > 8 else "")))
draw_screen(lines)
key = next_key()
completions = []
if editor:
if key == "esc":
finish_editor()
elif key == "backspace":
text = text[:-1]
elif key == "tab" and editor == "working":
text = complete_path(text, working_dir)
elif key == "tab" and editor == "time-source":
matches = reference_completions(text, records)
if matches:
common = os.path.commonprefix([name.casefold() for name in matches])
text = matches[0][:len(common)]
completions = matches if len(matches) > 1 else []
message = ""
elif key == "enter":
if editor == "timezone":
try:
@@ -424,6 +452,13 @@ def prompt_choices(
else:
state.fixed_timezone_enabled = True
finish_editor()
elif editor == "working":
try:
state.working_dir = resolve_output_directory(text, working_dir)
except ValueError as exc:
message = str(exc)
else:
finish_editor()
elif editor == "artist":
state.artist_value = text
state.artist_enabled = True
@@ -473,7 +508,7 @@ def prompt_choices(
apply_timezone_settings(records, state)
artist_action = "leave" if not state.artist_enabled else ("set" if state.artist_value else "clear")
return UserChoices(
working_dir=working_dir,
working_dir=state.working_dir,
time_offset=state.time_offset,
fixed_timezone=state.fixed_timezone if state.fixed_timezone_enabled else None,
fill_timezone_gaps=state.fill_timezone_gaps,
@@ -482,14 +517,20 @@ def prompt_choices(
rename_mode=state.rename_mode,
organize_files=state.organize_files,
group_min_size=state.group_min_size,
process_pto_files=state.organize_files and state.process_pto_files,
process_pto_files=state.organize_files and state.group_files and state.process_pto_files,
infer_missing_timestamps=state.infer_missing_timestamps,
group_files=state.group_files,
), state
if key == "down" or (key == "enter" and current != "time"):
if key == "down" or (key == "enter" and current not in {"working", "time"}):
enter_row(row + 1)
continue
if current == "time":
if current == "working":
if key == "enter":
editor, text = "working", ""
elif len(key) == 1:
editor, text = "working", key
elif current == "time":
if key == "left":
option = 0
state.time_offset = None
@@ -519,8 +560,8 @@ def prompt_choices(
if key == "left":
option = max(0, option - 1)
elif key == "right":
option = min(3, option + 1)
state.rename_mode = ("adjust_replace", "adjust_add", "replace", "add")[option]
option = min(4, option + 1)
state.rename_mode = ("none", "adjust_replace", "adjust_add", "replace", "add")[option]
elif current == "artist":
if key == "space":
state.artist_enabled = not state.artist_enabled
@@ -529,15 +570,19 @@ def prompt_choices(
elif current == "organize":
if key == "left":
option = max(0, option - 1)
elif key == "right" and state.organize_files:
elif key == "right" and state.organize_files and state.group_files:
option = min(2, option + 1)
elif key == "space":
if option == 0:
state.organize_files = not state.organize_files
if not state.organize_files:
state.organize_files, state.group_files = True, False
elif not state.group_files:
state.group_files = True
else:
state.organize_files = False
elif option == 2:
state.process_pto_files = not state.process_pto_files
elif option == 1 and key.isdigit():
editor, text = "group", key
elif current == "confirm" and key in {"left", "right"}:
option = 1 - option