Album skeleton creation

This commit is contained in:
Nick Pegg 2024-08-03 08:22:01 -07:00
parent 4e6cf1d499
commit e3816dfdb7
8 changed files with 85 additions and 29 deletions

View file

@ -1,12 +1,16 @@
import logging
from argparse import ArgumentParser, Namespace from argparse import ArgumentParser, Namespace
from pathlib import Path from pathlib import Path
from photoalbum.config import DEFAULT_CONFIG_PATH, Config from photoalbum.config import DEFAULT_CONFIG_PATH, Config
from photoalbum.generate import generate from photoalbum.generate import generate
logger = logging.getLogger("photoalbum.cli")
def main() -> None: def main() -> None:
args = parse_args() args = parse_args()
setup_logging(args.logging)
# TODO: load config from file # TODO: load config from file
config = Config() config = Config()
@ -18,6 +22,28 @@ def main() -> None:
cmd_clean(args, config) 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: def parse_args() -> Namespace:
parser = ArgumentParser() parser = ArgumentParser()
parser.add_argument( parser.add_argument(
@ -26,6 +52,12 @@ def parse_args() -> Namespace:
default=DEFAULT_CONFIG_PATH, default=DEFAULT_CONFIG_PATH,
help="Path to photoalbum.config.json for the album", 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") subcommands = parser.add_subparsers(title="subcommands")
@ -52,21 +84,10 @@ def parse_args() -> Namespace:
return parser.parse_args() return parser.parse_args()
def cmd_init(args: Namespace, config: Config) -> None: def setup_logging(level: str) -> None:
""" levels = logging.getLevelNamesMapping()
Generate a basic config and template files # TODO: Set up a better formatter with date/time and stuff
""" logging.basicConfig(level=levels[level.upper()])
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
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,7 +1,6 @@
from dataclasses import dataclass, field from dataclasses import dataclass
from typing import Iterator
DEFAULT_CONFIG_PATH = "photoalbum.config.json" DEFAULT_CONFIG_PATH = "photoalbum.conf.yml"
@dataclass @dataclass

View file

@ -1,26 +1,56 @@
import logging
from pathlib import Path from pathlib import Path
from rich.progress import track
from PIL import Image, UnidentifiedImageError from PIL import Image, UnidentifiedImageError
from rich.progress import track
from photoalbum.config import Config 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 Main generation function
""" """
skeleton_created = maybe_create_skeleton(config) skel_files_created, skel_files = maybe_create_skeleton(config, album_path)
generate_thumbnails(config, path) # generate_thumbnails(config, album_path)
generate_html_dir(config, 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 Create basic config, template, and static files if they don't already exist in the
repo 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: def generate_thumbnails(config: Config, path: Path) -> None:
@ -33,11 +63,11 @@ def generate_thumbnails(config: Config, path: Path) -> None:
try: try:
Image.open(parent_path / filename) Image.open(parent_path / filename)
except UnidentifiedImageError: 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 we modify dirnames in-place, walk() will skip anything we remove
if 'slides' in dirnames: if "slides" in dirnames:
dirnames.remove('slides') dirnames.remove("slides")
images.append((parent_path, filename)) images.append((parent_path, filename))
@ -59,7 +89,7 @@ def generate_thumbnails(config: Config, path: Path) -> None:
screen_img.save(slides_path / screen_filename) 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 Recursively generate HTML files for this directory and all children
""" """

View file

View file

View file

@ -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]

View file

View file

@ -11,6 +11,7 @@ license = "TODO"
photoalbum = 'photoalbum.cli:main' photoalbum = 'photoalbum.cli:main'
[tool.poetry.dependencies] [tool.poetry.dependencies]
# TODO: make sure we support >=3.10 via tests
python = "^3.12" python = "^3.12"
jinja2 = "^3.1.4" jinja2 = "^3.1.4"
pillow = "^10.4.0" pillow = "^10.4.0"