feat(wallpaper_changer):add image hash, move to src
decode and leave: decrypt file instead of saving loaded image add conf_dir instead of ~/Wallpaper add decode_image function instead of decrypting directly
This commit is contained in:
1 parent
bd6ed049f7
commit
8aa679b405
1 file changed
+11
-10
+11
-10
@@ -7,6 +7,7 @@ from pathlib import Path
|
|||||||
from PIL import Image, ImageDraw, ImageFont, ImageOps
|
from PIL import Image, ImageDraw, ImageFont, ImageOps
|
||||||
|
|
||||||
from wallpaper_changer.encryption_manager import EncryptionManager
|
from wallpaper_changer.encryption_manager import EncryptionManager
|
||||||
|
from wallpaper_changer.image_repository import ImageRepository
|
||||||
from wallpaper_changer.logging_utils import get_logger
|
from wallpaper_changer.logging_utils import get_logger
|
||||||
from wallpaper_changer.monitor_manager import ScreenLayout
|
from wallpaper_changer.monitor_manager import ScreenLayout
|
||||||
|
|
||||||
@@ -16,9 +17,10 @@ class WallpaperBuilder:
|
|||||||
|
|
||||||
def build(
|
def build(
|
||||||
self,
|
self,
|
||||||
images: Sequence[Mapping[str, Path | str]],
|
images: Sequence[Mapping[str, Path | str,str]],
|
||||||
layout: ScreenLayout,
|
layout: ScreenLayout,
|
||||||
private_key: str | Path,
|
private_key: str | Path,
|
||||||
|
conf_dir: str | Path,
|
||||||
search_terms: Sequence[str] | None = None,
|
search_terms: Sequence[str] | None = None,
|
||||||
exclude: Sequence[str] = (),
|
exclude: Sequence[str] = (),
|
||||||
output_path: str | Path | None = None,
|
output_path: str | Path | None = None,
|
||||||
@@ -30,7 +32,7 @@ class WallpaperBuilder:
|
|||||||
if not layout.monitors:
|
if not layout.monitors:
|
||||||
raise ValueError("Cannot build a wallpaper without monitor dimensions")
|
raise ValueError("Cannot build a wallpaper without monitor dimensions")
|
||||||
|
|
||||||
selected_images: list[Mapping[str, Path | str]] = []
|
selected_images: list[Mapping[str, Path | str,str]] = []
|
||||||
for index, monitor in enumerate(layout.monitors):
|
for index, monitor in enumerate(layout.monitors):
|
||||||
term = None
|
term = None
|
||||||
if search_terms:
|
if search_terms:
|
||||||
@@ -64,11 +66,11 @@ class WallpaperBuilder:
|
|||||||
source = self._open_image(
|
source = self._open_image(
|
||||||
image_record,
|
image_record,
|
||||||
private_key,
|
private_key,
|
||||||
encryption_manager,
|
|
||||||
disable_encryption=disable_encryption,
|
disable_encryption=disable_encryption,
|
||||||
)
|
)
|
||||||
if decode_and_leave:
|
if decode_and_leave:
|
||||||
source.save(Path(image_record["file"]).with_name(image_record["name"]))
|
dest = Path(image_record["file"]).with_name(image_record["name"])
|
||||||
|
encryption_manager.decrypt(image_record["file"], dest, private_key)
|
||||||
logger.debug(f"Leaving decrypted image on disk: {image_record['file']}")
|
logger.debug(f"Leaving decrypted image on disk: {image_record['file']}")
|
||||||
|
|
||||||
centering_panorama = [(0,0),(1,0)]
|
centering_panorama = [(0,0),(1,0)]
|
||||||
@@ -93,14 +95,16 @@ class WallpaperBuilder:
|
|||||||
stroke_width=1,
|
stroke_width=1,
|
||||||
stroke_fill="black",
|
stroke_fill="black",
|
||||||
)
|
)
|
||||||
|
logger.info(f"Pasted image {image_record['name']} on monitor at x={monitor.x}, width={monitor.width}, height={monitor.height}")
|
||||||
|
|
||||||
destination = (
|
destination = (
|
||||||
Path(output_path)
|
Path(output_path)
|
||||||
if output_path
|
if output_path
|
||||||
else (Path.home() / "Wallpaper" / "current" / "wallpaper.jpg")
|
else (Path(conf_dir).expanduser().resolve() / "current" / "wallpaper.jpg")
|
||||||
)
|
)
|
||||||
destination.parent.mkdir(parents=True, exist_ok=True)
|
destination.parent.mkdir(parents=True, exist_ok=True)
|
||||||
canvas.save(destination, format="JPEG", quality=95)
|
canvas.save(destination, format="JPEG", quality=95)
|
||||||
|
logger.info(f"Wallpaper built and saved to {destination}")
|
||||||
return destination
|
return destination
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -140,9 +144,8 @@ class WallpaperBuilder:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _open_image(
|
def _open_image(
|
||||||
image_record: Mapping[str, Path | str],
|
image_record: Mapping[str, Path | str, str],
|
||||||
private_key: str | Path,
|
private_key: str | Path,
|
||||||
encryption_manager: EncryptionManager | None,
|
|
||||||
disable_encryption: bool = False,
|
disable_encryption: bool = False,
|
||||||
) -> Image.Image:
|
) -> Image.Image:
|
||||||
image_path = Path(image_record["file"])
|
image_path = Path(image_record["file"])
|
||||||
@@ -153,9 +156,7 @@ class WallpaperBuilder:
|
|||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Cannot open encrypted image while encryption is disabled: {image_path}"
|
f"Cannot open encrypted image while encryption is disabled: {image_path}"
|
||||||
)
|
)
|
||||||
if encryption_manager is None:
|
decrypted = ImageRepository(image_path.parent).decode_image(image_path, None, private_key, image_record.get("hash"))
|
||||||
raise RuntimeError("Encryption manager is required to decrypt images")
|
|
||||||
decrypted = encryption_manager.decrypt(image_path, None, private_key)
|
|
||||||
if decrypted is None:
|
if decrypted is None:
|
||||||
raise ValueError(f"Could not decrypt image: {image_path}")
|
raise ValueError(f"Could not decrypt image: {image_path}")
|
||||||
image_data = decrypted
|
image_data = decrypted
|
||||||
Reference in new issue
Block a user