diff --git a/wallpaper_changer/main.py b/wallpaper_changer/main.py index d7488bd..4cf6c52 100755 --- a/wallpaper_changer/main.py +++ b/wallpaper_changer/main.py @@ -1,15 +1,15 @@ #!/home/lucas/scripts/wallpaper_changer/.venv/bin/python3 -from pathlib import Path import sys +from pathlib import Path -from wallpaper_changer.logging_utils import get_logger from serverlibrary import setup_logging -from wallpaper_changer.config import Config from wallpaper_changer.argument_parser import parse_args +from wallpaper_changer.config import Config +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.monitor_manager import MonitorManager -from wallpaper_changer.encryption_manager import EncryptionManager, FileInfo from wallpaper_changer.wallpaper_builder import WallpaperBuilder from wallpaper_changer.wallpaper_setter import WallpaperSetter diff --git a/wallpaper_changer/src/wallpaper_changer/config.py b/wallpaper_changer/src/wallpaper_changer/config.py index 417ae38..1d1dc55 100644 --- a/wallpaper_changer/src/wallpaper_changer/config.py +++ b/wallpaper_changer/src/wallpaper_changer/config.py @@ -1,6 +1,6 @@ +import socket from dataclasses import dataclass from pathlib import Path -import socket from wallpaper_changer.logging_utils import get_logger @@ -22,7 +22,7 @@ class Config: if not config_file.exists(): config_file.parent.mkdir(parents=True, exist_ok=True) config_file.write_text( - f"# Wallpaper Changer Configuration File\n" + "# Wallpaper Changer Configuration File\n" ) return config_file diff --git a/wallpaper_changer/src/wallpaper_changer/encryption_manager.py b/wallpaper_changer/src/wallpaper_changer/encryption_manager.py index 552e368..519daaf 100644 --- a/wallpaper_changer/src/wallpaper_changer/encryption_manager.py +++ b/wallpaper_changer/src/wallpaper_changer/encryption_manager.py @@ -1,9 +1,8 @@ -from dataclasses import dataclass import hashlib import os -from pathlib import Path +from dataclasses import dataclass from os import PathLike -from typing import overload +from pathlib import Path from cryptography.hazmat.primitives import hashes, padding from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes diff --git a/wallpaper_changer/src/wallpaper_changer/image_repository.py b/wallpaper_changer/src/wallpaper_changer/image_repository.py index 360ed46..a82ada1 100644 --- a/wallpaper_changer/src/wallpaper_changer/image_repository.py +++ b/wallpaper_changer/src/wallpaper_changer/image_repository.py @@ -1,7 +1,6 @@ import hashlib import logging import re -import shutil from dataclasses import dataclass from pathlib import Path @@ -83,7 +82,7 @@ class ImageRepository: if not new_images_folder.is_dir(): logger.warning(f"New folder does not exist: {new_images_folder}") return [] - new_images = list(sorted(new_images_folder.glob("*"))) + new_images = sorted(new_images_folder.glob("*")) if not new_images: logger.trace(f"No new images found in {new_images_folder}") return [] @@ -325,8 +324,8 @@ class ImageRepository: return decoded_paths def decode_image( - self, encrypted_path: Path, decoded_path: Path, private_key: str | Path, hash_str: str - ) -> None: + self, encrypted_path: Path, decoded_path: Path | None, private_key: str | Path, hash_str: str + ) -> bytes|None: encryption_manager = EncryptionManager() decrypted, hash = encryption_manager.decrypt(encrypted_path, decoded_path, private_key) if hash != hash_str: diff --git a/wallpaper_changer/src/wallpaper_changer/monitor_manager.py b/wallpaper_changer/src/wallpaper_changer/monitor_manager.py index c75ac77..186b551 100644 --- a/wallpaper_changer/src/wallpaper_changer/monitor_manager.py +++ b/wallpaper_changer/src/wallpaper_changer/monitor_manager.py @@ -1,8 +1,10 @@ -from dataclasses import dataclass -from pathlib import Path import re import xml.etree.ElementTree as ET +from dataclasses import dataclass +from pathlib import Path + from wallpaper_changer.logging_utils import get_logger + logger = get_logger(__name__) @@ -44,9 +46,11 @@ class MonitorManager: image_dir: Path, monitor_config: int = 0, drm_root: Path = Path("/sys/class/drm"), - monitor_file: Path = Path.home() / ".config/monitors.xml", + monitor_file: Path | None = None, lid_state: Path = Path("/proc/acpi/button/lid/LID0/state"), ) -> ScreenLayout: + if monitor_file is None: + monitor_file = Path.home() / ".config/monitors.xml" if host == "serverhost": return self._read_server_layout(image_dir, monitor_config) if host not in {"quiethost", "vivohost"}: diff --git a/wallpaper_changer/src/wallpaper_changer/wallpaper_builder.py b/wallpaper_changer/src/wallpaper_changer/wallpaper_builder.py index 8ec4407..d5c44cb 100644 --- a/wallpaper_changer/src/wallpaper_changer/wallpaper_builder.py +++ b/wallpaper_changer/src/wallpaper_changer/wallpaper_builder.py @@ -14,10 +14,9 @@ from wallpaper_changer.monitor_manager import ScreenLayout logger = get_logger(__name__) class WallpaperBuilder: - def build( self, - images: Sequence[Mapping[str, Path | str,str]], + images: Sequence[Mapping[str, Path | str]], layout: ScreenLayout, private_key: str | Path, conf_dir: str | Path, @@ -32,7 +31,7 @@ class WallpaperBuilder: if not layout.monitors: raise ValueError("Cannot build a wallpaper without monitor dimensions") - selected_images: list[Mapping[str, Path | str,str]] = [] + selected_images: list[Mapping[str, Path | str]] = [] for index, monitor in enumerate(layout.monitors): term = None if search_terms: @@ -68,8 +67,8 @@ class WallpaperBuilder: private_key, disable_encryption=disable_encryption, ) - if decode_and_leave: - dest = Path(image_record["file"]).with_name(image_record["name"]) + if decode_and_leave and encryption_manager: + dest = Path(image_record["file"]).with_name(str(image_record["name"])) encryption_manager.decrypt(image_record["file"], dest, private_key) logger.debug(f"Leaving decrypted image on disk: {image_record['file']}") @@ -144,7 +143,7 @@ class WallpaperBuilder: @staticmethod def _open_image( - image_record: Mapping[str, Path | str, str], + image_record: Mapping[str, Path | str], private_key: str | Path, disable_encryption: bool = False, ) -> Image.Image: diff --git a/wallpaper_changer/tests/unit/test_config.py b/wallpaper_changer/tests/unit/test_config.py index 8065609..8c4013f 100644 --- a/wallpaper_changer/tests/unit/test_config.py +++ b/wallpaper_changer/tests/unit/test_config.py @@ -1,7 +1,7 @@ +import unittest from contextlib import redirect_stdout from importlib.metadata import version from io import StringIO -import unittest from pathlib import Path from tempfile import TemporaryDirectory from unittest.mock import patch @@ -68,30 +68,29 @@ class ConfigTests(unittest.TestCase): (("x", "/tmp/custom-wallpapers"), "/tmp/custom-wallpapers", Path("/tmp/custom-wallpapers")), ) - with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"): - with patch("pathlib.Path.home", return_value=home): - for (mode, folder), expected_folder, expected_img_dir in cases: - with self.subTest(mode=mode, folder=folder): - arguments = [ - "--mode", - mode, - "--temp-dir", - "/tmp/wallpaper-cache", - "--private-key", - "/tmp/private-key.pem", - ] - if folder is not None: - arguments.extend(("--folder", folder)) - args = self.parse_args(*arguments) + with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"), \ + patch("pathlib.Path.home", return_value=home): + for (mode, folder), expected_folder, expected_img_dir in cases: + with self.subTest(mode=mode, folder=folder): + arguments = [ + "--mode", + mode, + "--temp-dir", + "/tmp/wallpaper-cache", + "--private-key", + "/tmp/private-key.pem", + ] + if folder is not None: + arguments.extend(("--folder", folder)) + args = self.parse_args(*arguments) - config = Config.load(args) - - self.assertEqual(config.host, "testhost") - self.assertEqual(config.img_folder, expected_folder) - self.assertEqual(config.img_dir, expected_img_dir) - self.assertEqual(config.wallpaper_dir, Path("/tmp/wallpaper-cache/wallpaper")) - self.assertEqual(config.private_key, Path("/tmp/private-key.pem")) + config = Config.load(args) + self.assertEqual(config.host, "testhost") + self.assertEqual(config.img_folder, expected_folder) + self.assertEqual(config.img_dir, expected_img_dir) + self.assertEqual(config.wallpaper_dir, Path("/tmp/wallpaper-cache/wallpaper")) + self.assertEqual(config.private_key, Path("/tmp/private-key.pem")) def test_load_uses_config_default_private_key_when_not_set(self) -> None: home = Path("/home/test-user") @@ -112,8 +111,8 @@ class ConfigTests(unittest.TestCase): assets = Path(__file__).resolve().parents[1] / "assets" args.private_key = assets / "test_privateKey.pem" - with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"): - with patch("pathlib.Path.home", return_value=home): + with patch("wallpaper_changer.config.socket.gethostname", return_value="testhost"), \ + patch("pathlib.Path.home", return_value=home): config = Config.load(args) self.assertEqual(config.private_key, args.private_key) @@ -139,9 +138,8 @@ class VersionArgumentTests(unittest.TestCase): for flag in ("--version", "-v"): with self.subTest(flag=flag): output = StringIO() - with redirect_stdout(output): - with self.assertRaises(SystemExit) as raised: - parse_args([flag]) + with redirect_stdout(output), self.assertRaises(SystemExit) as raised: + parse_args([flag]) self.assertEqual(raised.exception.code, 0) self.assertEqual( diff --git a/wallpaper_changer/tests/unit/test_image_repository.py b/wallpaper_changer/tests/unit/test_image_repository.py index 247afdd..2f9bafe 100644 --- a/wallpaper_changer/tests/unit/test_image_repository.py +++ b/wallpaper_changer/tests/unit/test_image_repository.py @@ -1,12 +1,12 @@ +import hashlib import shutil import unittest -import hashlib from pathlib import Path from tempfile import TemporaryDirectory -from wallpaper_changer.image_repository import ImageRepository -from wallpaper_changer.config import Config from wallpaper_changer.argument_parser import parse_args +from wallpaper_changer.config import Config +from wallpaper_changer.image_repository import ImageRepository class ImageRepositoryTests(unittest.TestCase):