feat(wallpaper_changer): add search term for monitor and general search

monitor search term: one tem per monitor
gen search term_ multiple terms all filtering the same image
This commit is contained in:
Lucas Vivo committed 2026-11-15 19:45:15 +01:00
1 parent 8f5631d8e4
commit 790267bbb5
1 file changed
+39 -17
@@ -20,7 +20,8 @@ class WallpaperBuilder:
layout: ScreenLayout,
private_key: str | Path,
wallpaper_dir: Path,
search_terms: Sequence[str] | None = None,
search_terms_gen: Sequence[str] | None = None,
search_terms_monitor: Sequence[str] | None = None,
exclude: Sequence[str] = (),
output_path: str | Path | None = None,
decode_and_leave: bool = False,
@@ -28,21 +29,24 @@ class WallpaperBuilder:
) -> tuple[Path,list]:
if not images:
raise ValueError("No images are available to build a wallpaper")
if not layout.monitors:
if not layout.monitors or len(layout.monitors) == 0:
raise ValueError("Cannot build a wallpaper without monitor dimensions")
selected_images: list[Mapping[str, Path | str]] = []
for index, monitor in enumerate(layout.monitors):
term = None
if search_terms:
term = (
search_terms[0]
if len(search_terms) == 1
else search_terms[index % len(search_terms)]
search_term_cur_monitor = None
if monitor.height == 0 or monitor.width == 0:
raise ValueError(f"Monitor {index} has no dimensions")
if search_terms_monitor:
search_term_cur_monitor = (
search_terms_monitor[0]
if len(search_terms_monitor) == 1
else search_terms_monitor[index % len(search_terms_monitor)]
)
candidates = self.find_candidates(images, term, exclude)
candidates = self.find_candidates(images, search_term_cur_monitor, search_terms_gen, exclude)
if not candidates:
raise ValueError(f"No images match search term {term!r}")
raise ValueError(f"No images left after excluding {exclude}")
unused_candidates = [
candidate
@@ -52,8 +56,8 @@ class WallpaperBuilder:
logger.trace(f"Selected candidates for monitor {index}: {len(candidates)}")
selected_images.append(random.choice(unused_candidates or candidates))
left = min(monitor.x for monitor in layout.monitors)
right = max(monitor.x + monitor.width for monitor in layout.monitors)
left = min(monitor.x_coord for monitor in layout.monitors)
right = max(monitor.x_coord + monitor.width for monitor in layout.monitors)
height = max(monitor.height for monitor in layout.monitors)
canvas = Image.new("RGB", (right - left, height), "black")
draw = ImageDraw.Draw(canvas)
@@ -86,7 +90,7 @@ class WallpaperBuilder:
centering = centering
)
monitor_left = monitor.x - left
monitor_left = monitor.x_coord - left
canvas.paste(fitted, (monitor_left, 0))
font_size = max(12, monitor.height // 120)
margin_top = 55
@@ -101,7 +105,7 @@ class WallpaperBuilder:
stroke_fill="black",
)
images_used.append(image_record['name'])
logger.info(f"Pasted image {image_record['name']} on monitor at x={monitor.x}, width={monitor.width}, height={monitor.height}")
logger.info(f"Pasted image {image_record['name']} on monitor at x={monitor.x_coord}, width={monitor.width}, height={monitor.height}")
destination = (
Path(output_path)
@@ -117,7 +121,8 @@ class WallpaperBuilder:
@staticmethod
def find_candidates(
images: Sequence[Mapping[str, Path | str]],
search_term: str | None,
search_term_mon: str | None,
search_terms_gen: Sequence[str] | None,
exclude: Sequence[str],
) -> list[Mapping[str, Path | str]]:
excluded_terms = [term for term in (*exclude, "_decrypted") if term]
@@ -130,15 +135,32 @@ class WallpaperBuilder:
for term in excluded_terms
)
]
if search_term:
pattern = WallpaperBuilder._search_pattern(search_term)
if search_term_mon:
pattern = WallpaperBuilder._search_pattern(search_term_mon)
matches = [
image
for image in candidates
if re.search(pattern, str(image["name"]), re.IGNORECASE)
]
if matches:
logger.info(f"Found {len(matches)} images matching search term {search_term_mon}")
candidates = matches
else:
logger.warning(f"No matches found for term {search_term_mon}")
elif search_terms_gen:
matches = []
for image in candidates:
image_name = str(image["name"])
if all(
re.search(WallpaperBuilder._search_pattern(term), image_name, re.IGNORECASE)
for term in search_terms_gen
):
matches.append(image)
if matches:
logger.info(f"Found {len(matches)} images matching search term {search_terms_gen}")
candidates = matches
else:
logger.warning(f"No matches found for term {search_terms_gen}")
return candidates
@staticmethod