Improve photo organization controls and reference completion
This commit is contained in:
1 parent
ad2b15994f
commit
e3633b8e1a
6 files changed
+316
-41
No files matched your search
@@ -51,8 +51,9 @@ def print_preview(records: list[MediaRecord], choices: UserChoices, pto_plans: l
|
||||
elif choices.fill_timezone_gaps:
|
||||
print(" Timezone offsets: fill missing gaps")
|
||||
print(f" Filename mode: {choices.rename_mode.replace('_', '/')}")
|
||||
print(f" Organize/group: {'yes' if choices.organize_files else 'no'}")
|
||||
if choices.organize_files:
|
||||
organization = "grouping" if choices.organize_files and choices.group_files else "flat" if choices.organize_files else "no moving"
|
||||
print(f" Organize/group: {organization}")
|
||||
if choices.group_photos:
|
||||
print(f" Burst/HDR minimum: {choices.group_min_size}+ files")
|
||||
print(f" Panorama project files: {'yes' if choices.process_pto_files else 'no'}")
|
||||
if choices.artist_action != "leave":
|
||||
|
||||
@@ -123,7 +123,7 @@ def infer_group_subseconds(groups: list[list[MediaRecord]]) -> None:
|
||||
count = len(records)
|
||||
if count <= 1:
|
||||
continue
|
||||
if len(seconds) == 2:
|
||||
if len(seconds) <= 2:
|
||||
fps = max(counts.values())
|
||||
elif index == 0:
|
||||
fps = max(count, counts[seconds[1]])
|
||||
|
||||
+32
-5
@@ -42,10 +42,11 @@ class UserChoices:
|
||||
group_min_size: int
|
||||
process_pto_files: bool
|
||||
infer_missing_timestamps: bool
|
||||
group_files: bool = True
|
||||
|
||||
@property
|
||||
def group_photos(self) -> bool:
|
||||
return self.organize_files and self.group_min_size >= 2
|
||||
return self.organize_files and self.group_files and self.group_min_size >= 2
|
||||
|
||||
@property
|
||||
def move_to_working_dir(self) -> bool:
|
||||
@@ -114,7 +115,9 @@ def filename_timestamp_stem(stem: str, timestamp: datetime, mode: str) -> str:
|
||||
def plan_names(records: list[MediaRecord], choices: UserChoices) -> None:
|
||||
by_stem = _unique_records_by_stem(records)
|
||||
for record in records:
|
||||
record.new_stem = record.rename_skip_reason = None
|
||||
record.new_stem = record.final_name = record.rename_skip_reason = None
|
||||
if choices.rename_mode == "none":
|
||||
continue
|
||||
if record.adjusted_time is None:
|
||||
record.new_stem = _camera_range_stem(record, by_stem)
|
||||
record.rename_skip_reason = None if record.new_stem else "missing timestamp"
|
||||
@@ -227,6 +230,31 @@ def plan_targets(records: list[MediaRecord], choices: UserChoices) -> None:
|
||||
record.target_path = directory / name
|
||||
if record.sidecar:
|
||||
record.target_sidecar = directory / f"{Path(name).stem}M01.XML"
|
||||
_make_targets_unique(records)
|
||||
|
||||
|
||||
def _make_targets_unique(records: list[MediaRecord]) -> None:
|
||||
used: set[tuple[Path, str]] = set()
|
||||
ordered = sorted(
|
||||
records,
|
||||
key=lambda record: (
|
||||
record.target_path != record.path,
|
||||
group_sort_key(record),
|
||||
),
|
||||
)
|
||||
for record in ordered:
|
||||
assert record.target_path is not None
|
||||
target = record.target_path
|
||||
index = 0
|
||||
while (target.parent.resolve(), target.name.lower()) in used:
|
||||
index += 1
|
||||
target = record.target_path.with_name(
|
||||
f"{record.target_path.stem}-{index}{record.target_path.suffix}"
|
||||
)
|
||||
used.add((target.parent.resolve(), target.name.lower()))
|
||||
record.target_path = target
|
||||
if record.sidecar:
|
||||
record.target_sidecar = target.with_name(f"{target.stem}M01.XML")
|
||||
|
||||
|
||||
def normalize_reference(value: str) -> str:
|
||||
@@ -370,9 +398,8 @@ def prepare_plan(records: list[MediaRecord], choices: UserChoices, references: l
|
||||
apply_time_offset(records, choices.time_offset)
|
||||
burst_groups = detect_photo_groups(records, choices.group_min_size) if choices.group_photos else []
|
||||
groups = merge_record_groups(records, [*burst_groups, *(reference.records for reference in references if choices.process_pto_files)])
|
||||
if groups:
|
||||
assign_groups(records, groups)
|
||||
if choices.organize_files:
|
||||
assign_groups(records, groups)
|
||||
if choices.group_photos:
|
||||
infer_group_subseconds(burst_groups)
|
||||
plan_names(records, choices)
|
||||
if groups:
|
||||
|
||||
+69
-24
@@ -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
|
||||
|
||||
Reference in new issue
Block a user