diff --git a/wallpaper_changer/main.py b/wallpaper_changer/main.py index d8514aa..d7488bd 100755 --- a/wallpaper_changer/main.py +++ b/wallpaper_changer/main.py @@ -1,4 +1,4 @@ -#!/home/lucas/scripts/.venv/bin/python3 +#!/home/lucas/scripts/wallpaper_changer/.venv/bin/python3 from pathlib import Path import sys @@ -9,116 +9,133 @@ from wallpaper_changer.config import Config from wallpaper_changer.argument_parser import parse_args from wallpaper_changer.image_repository import ImageRepository from wallpaper_changer.monitor_manager import MonitorManager -from wallpaper_changer.encryption_manager import EncryptionManager +from wallpaper_changer.encryption_manager import EncryptionManager, FileInfo from wallpaper_changer.wallpaper_builder import WallpaperBuilder from wallpaper_changer.wallpaper_setter import WallpaperSetter logger = get_logger(__name__) -args = parse_args() -if args.disable_encryption and ( - args.check_encryption - or args.decode_all - or args.decode_argument - or args.printNT - or args.encryptNT - or args.decryptNT -): - raise ValueError( - "--disable-encryption cannot be used with encryption check, decode, or naming-table commands" - ) +def main() -> None: + args = parse_args() -if ( - args.check_encryption or args.decode_all or args.decode_argument or args.printNT -) and args.folder == "default": - args.folder = "x" -setup_logging(args.log_level, "wallpaper_changer") + if args.disable_encryption and ( + args.check_encryption + or args.decode_all + or args.decode_argument + or args.printNT + or args.encryptNT + or args.decryptNT + ): + raise ValueError( + "--disable-encryption cannot be used with encryption check, decode, or naming-table commands" + ) -if args.clear: - args.folder = "default" - args.search = None + setup_logging(args.log_level, "wallpaper_changer") -cfg = Config.load(args) + if args.clear: + args.mode = "default" + args.folder = None + args.search = None + WallpaperSetter().set_ubuntu("~/.wallpaper/Default/default_wallpaper.jpg") -if args.printNT: - naming_table = EncryptionManager().load_naming_table( - cfg.img_dir / "naming_table.enc", cfg.private_key - ) - for original_name, encrypted_name in naming_table: - if args.grep_nt and args.grep_nt not in original_name: - continue - print(f"{original_name} -> {encrypted_name}") - sys.exit(0) -image_repository = ImageRepository(cfg.img_dir) + cfg = Config.load(args) -if args.encryptNT: - EncryptionManager().encrypt_naming_table_file( - cfg.img_dir / "naming_table.dec", - cfg.img_dir / "naming_table.enc", + if args.printNT: + naming_table = EncryptionManager().load_naming_table( + cfg.img_dir / "naming_table.enc", cfg.private_key + ) + headers = ("Image file", "Encrypted file", "Image hash") + rows = [ + (item.image_name, item.encrypted_file, item.image_hash) + for item in naming_table + if not args.grep_nt or args.grep_nt in item.image_name + ] + widths = tuple( + max([len(header), *(len(row[column]) for row in rows)]) + for column, header in enumerate(headers) + ) + print(" ".join(header.ljust(width) for header, width in zip(headers, widths))) + for row in rows: + print(" ".join(value.ljust(width) for value, width in zip(row, widths))) + sys.exit(0) + + image_repository = ImageRepository(cfg.img_dir) + + if args.encryptNT: + EncryptionManager().encrypt_naming_table_file( + cfg.img_dir / "naming_table.dec", + cfg.img_dir / "naming_table.enc", + cfg.private_key, + ) + sys.exit(0) + + if args.decryptNT: + EncryptionManager().decrypt_naming_table_file( + cfg.img_dir / "naming_table.enc", + cfg.img_dir / "naming_table.dec", + cfg.private_key, + ) + sys.exit(0) + + if args.decode_all: + image_repository.decode_all(cfg.private_key) + sys.exit(0) + + + if args.decode_argument: + image_repository.decode_images( + pattern=args.decode_argument, + private_key=cfg.private_key, + conf_dir=cfg.conf_dir, + remove=args.rm + ) + sys.exit(0) + + image_repository.convert_to_jpg() + image_repository.check_consecutive_filenames(correct=True) + + if not args.disable_encryption and ( + args.check_encryption or args.mode != "default" or args.folder is not None + ): + new_images_folder = Path("~").expanduser() / ".wallpaper" / "New" + image_repository.check_encryption( + new_images_folder, cfg.private_key, replace=args.replace + ) + image_repository.check_encryption( + cfg.img_dir, cfg.private_key, replace=args.replace + ) + + imgs = image_repository.scan( cfg.private_key, - ) - sys.exit(0) - -if args.decryptNT: - EncryptionManager().decrypt_naming_table_file( - cfg.img_dir / "naming_table.enc", - cfg.img_dir / "naming_table.dec", - cfg.private_key, - ) - sys.exit(0) - -if not args.disable_encryption and (args.check_encryption or args.folder != "default"): - new_images_folder = Path("~").expanduser() / "Wallpaper" / "New" - image_repository.check_encryption( - new_images_folder, cfg.private_key, replace=args.replace - ) - image_repository.check_encryption( - cfg.img_dir, cfg.private_key, replace=args.replace - ) - -if args.decode_all: - image_repository.decode_all(cfg.private_key) - sys.exit(0) - - -if args.decode_argument: - image_repository.decode_images( - pattern=args.decode_argument, - private_key=cfg.private_key, - remove=args.rm, - ) - sys.exit(0) - -image_repository.convert_to_jpg() -image_repository.check_consecutive_filenames(correct=True) -imgs = image_repository.scan( - cfg.private_key, - disable_encryption=args.disable_encryption, -) - -monitor_image_dir = cfg.img_dir.parent if cfg.host == "serverhost" else cfg.img_dir -monitors = MonitorManager().get_screensize( - cfg.host, - monitor_image_dir, - args.monitor_config, -) - -if args.times < 1: - raise ValueError("--times must be at least 1") - -wallpaper_builder = WallpaperBuilder() -wallpaper_setter = WallpaperSetter() -for _ in range(args.times): - wallpaper_path = wallpaper_builder.build( - imgs, - monitors, - private_key=cfg.private_key, - search_terms=args.search, - exclude=args.exclude, - decode_and_leave=args.decode_and_leave, disable_encryption=args.disable_encryption, ) - wallpaper_setter.set_ubuntu(wallpaper_path) - print(wallpaper_path) + + monitor_image_dir = cfg.img_dir.parent if cfg.host == "serverhost" else cfg.img_dir + monitors = MonitorManager().get_screensize( + cfg.host, + monitor_image_dir, + args.monitor_config, + ) + + if args.times < 1: + raise ValueError("--times must be at least 1") + + wallpaper_builder = WallpaperBuilder() + wallpaper_setter = WallpaperSetter() + for _ in range(args.times): + wallpaper_path = wallpaper_builder.build( + imgs, + monitors, + private_key=cfg.private_key, + conf_dir=cfg.conf_dir, + search_terms=args.search, + exclude=args.exclude, + decode_and_leave=args.decode_and_leave, + disable_encryption=args.disable_encryption, + ) + wallpaper_setter.set_ubuntu(wallpaper_path) + +if __name__ == "__main__": + main()