diff --git a/photoalbum/cli.py b/photoalbum/cli.py index aa906e0..e1fda54 100644 --- a/photoalbum/cli.py +++ b/photoalbum/cli.py @@ -1,12 +1,16 @@ +import logging from argparse import ArgumentParser, Namespace from pathlib import Path from photoalbum.config import DEFAULT_CONFIG_PATH, Config from photoalbum.generate import generate +logger = logging.getLogger("photoalbum.cli") + def main() -> None: args = parse_args() + setup_logging(args.logging) # TODO: load config from file config = Config() @@ -18,6 +22,28 @@ def main() -> None: cmd_clean(args, config) +######################################## +# Command functions +def cmd_init(args: Namespace, config: Config) -> None: + """ + Generate a basic config and template files + """ + + +def cmd_generate(args: Namespace, config: Config) -> None: + logger.debug(f"Generating in {args.path}") + generate(config, Path(args.path)) + + +def cmd_clean(args: Namespace, config: Config) -> None: + """ + Clean the photo album by all files that photoalbum generated + """ + pass + + +######################################## +# CLI Util functions def parse_args() -> Namespace: parser = ArgumentParser() parser.add_argument( @@ -26,6 +52,12 @@ def parse_args() -> Namespace: default=DEFAULT_CONFIG_PATH, help="Path to photoalbum.config.json for the album", ) + parser.add_argument( + "--logging", + default="warning", + choices=[level.lower() for level in logging.getLevelNamesMapping().keys()], + help="Log level", + ) subcommands = parser.add_subparsers(title="subcommands") @@ -52,21 +84,10 @@ def parse_args() -> Namespace: return parser.parse_args() -def cmd_init(args: Namespace, config: Config) -> None: - """ - Generate a basic config and template files - """ - - -def cmd_generate(args: Namespace, config: Config) -> None: - generate(config, Path(args.path)) - - -def cmd_clean(args: Namespace, config: Config) -> None: - """ - Clean the photo album by all files that photoalbum generated - """ - pass +def setup_logging(level: str) -> None: + levels = logging.getLevelNamesMapping() + # TODO: Set up a better formatter with date/time and stuff + logging.basicConfig(level=levels[level.upper()]) if __name__ == "__main__": diff --git a/photoalbum/config.py b/photoalbum/config.py index 4bea4db..ad61aca 100644 --- a/photoalbum/config.py +++ b/photoalbum/config.py @@ -1,7 +1,6 @@ -from dataclasses import dataclass, field -from typing import Iterator +from dataclasses import dataclass -DEFAULT_CONFIG_PATH = "photoalbum.config.json" +DEFAULT_CONFIG_PATH = "photoalbum.conf.yml" @dataclass diff --git a/photoalbum/generate.py b/photoalbum/generate.py index c619edc..aa3c299 100644 --- a/photoalbum/generate.py +++ b/photoalbum/generate.py @@ -1,26 +1,56 @@ +import logging from pathlib import Path -from rich.progress import track from PIL import Image, UnidentifiedImageError +from rich.progress import track from photoalbum.config import Config +logger = logging.getLogger(__name__) -def generate(config: Config, path: Path) -> None: + +def generate(config: Config, album_path: Path) -> None: """ Main generation function """ - skeleton_created = maybe_create_skeleton(config) - generate_thumbnails(config, path) - generate_html_dir(config, path) + skel_files_created, skel_files = maybe_create_skeleton(config, album_path) + # generate_thumbnails(config, album_path) + # generate_html(config, album_path) + + if skel_files_created: + print( + "Some basic files have been created for your album. Edit them as you need:" + ) + for p in skel_files: + print(f" - {p}") -def maybe_create_skeleton(config: Config) -> bool: +def maybe_create_skeleton(config: Config, album_path: Path) -> tuple[bool, list[Path]]: """ Create basic config, template, and static files if they don't already exist in the repo """ - return False + files_created = False + skel_files = [] + + skel_dir = Path(__file__).parent / "skel" + logging.debug(f"Skeleton dir: {skel_dir}") + + for parent_path, dirnames, filenames in skel_dir.walk(): + for filename in filenames: + skel_file_path = parent_path / filename + rel_path = skel_file_path.relative_to(skel_dir) + album_file_path = album_path / rel_path + + skel_files.append(album_file_path) + + if not album_file_path.exists(): + album_file_path.parent.mkdir(exist_ok=True) + album_file_path.write_bytes(skel_file_path.read_bytes()) + logger.debug(f"Created skeleton file {album_file_path}") + files_created = True + + return files_created, skel_files def generate_thumbnails(config: Config, path: Path) -> None: @@ -33,11 +63,11 @@ def generate_thumbnails(config: Config, path: Path) -> None: try: Image.open(parent_path / filename) except UnidentifiedImageError: - continue # Not an image file + continue # Not an image file # If we modify dirnames in-place, walk() will skip anything we remove - if 'slides' in dirnames: - dirnames.remove('slides') + if "slides" in dirnames: + dirnames.remove("slides") images.append((parent_path, filename)) @@ -59,7 +89,7 @@ def generate_thumbnails(config: Config, path: Path) -> None: screen_img.save(slides_path / screen_filename) -def generate_html_dir(config: Config, path: Path) -> None: +def generate_html(config: Config, path: Path) -> None: """ Recursively generate HTML files for this directory and all children """ diff --git a/photoalbum/skel/_templates/album.jinja2 b/photoalbum/skel/_templates/album.jinja2 new file mode 100644 index 0000000..e69de29 diff --git a/photoalbum/skel/_templates/photo.jinja2 b/photoalbum/skel/_templates/photo.jinja2 new file mode 100644 index 0000000..e69de29 diff --git a/photoalbum/skel/photoalbum.conf.yml b/photoalbum/skel/photoalbum.conf.yml new file mode 100644 index 0000000..727da4f --- /dev/null +++ b/photoalbum/skel/photoalbum.conf.yml @@ -0,0 +1,5 @@ +# Size of thumbnails when viewing an album +thumbnail_size: [128, 128] + +# Size of images when viewing a single one on screen +view_size: [1920, 1080] diff --git a/photoalbum/skel/static/index.css b/photoalbum/skel/static/index.css new file mode 100644 index 0000000..e69de29 diff --git a/pyproject.toml b/pyproject.toml index 18c27b8..745df4e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ license = "TODO" photoalbum = 'photoalbum.cli:main' [tool.poetry.dependencies] +# TODO: make sure we support >=3.10 via tests python = "^3.12" jinja2 = "^3.1.4" pillow = "^10.4.0"