make init an explicit thing to avoid generating in a non-photo dir

This commit is contained in:
Nick Pegg 2024-08-03 09:31:17 -07:00
parent 7b4db89867
commit abea88bf52
3 changed files with 71 additions and 13 deletions

1
.gitignore vendored
View file

@ -1,5 +1,6 @@
# Python stuff # Python stuff
__pycache__ __pycache__
.testmondata
# Project specific files # Project specific files
test_album test_album

View file

@ -2,9 +2,10 @@ import logging
from argparse import ArgumentParser, Namespace from argparse import ArgumentParser, Namespace
from pathlib import Path from pathlib import Path
from rich.logging import RichHandler
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
from rich.logging import RichHandler
logger = logging.getLogger("photoalbum.cli") logger = logging.getLogger("photoalbum.cli")
@ -18,12 +19,17 @@ def main() -> None:
if conf_path.exists(): if conf_path.exists():
logger.debug(f"Reading config from {conf_path}") logger.debug(f"Reading config from {conf_path}")
config = Config.from_yaml(conf_path.read_bytes()) config = Config.from_yaml(conf_path.read_bytes())
else: elif args.action != "init":
logger.warning(f"No config file found at {conf_path}. Using defaults") logger.error(
config = Config() f"No config file found at {conf_path}. If this is a new photo directory, "
"please run `photoalbum init` in there first."
)
return
# Call the subcommand function # Call the subcommand function
match args.action: match args.action:
case "init":
cmd_init(args)
case "generate": case "generate":
cmd_generate(args, config) cmd_generate(args, config)
case "clean": case "clean":
@ -32,10 +38,39 @@ def main() -> None:
######################################## ########################################
# Command functions # Command functions
def cmd_init(args: Namespace, config: Config) -> None: def cmd_init(args: Namespace) -> None:
""" """
Generate a basic config and template files Generate a basic config and template files
""" """
album_path = Path(args.album_path)
config_path = album_path / args.config
if config_path.exists():
logger.warning(
f"Looks like {album_path} is already set up. If you want to start over and "
f"overwrite any of your customizations, remove {config_path}"
)
return
skel_dir = Path(__file__).parent / "skel"
logger.debug(f"Skeleton dir: {skel_dir}")
skel_files = []
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}")
print("Some basic files have been created for your album. Edit them as you need:")
for p in skel_files:
print(f" - {p}")
def cmd_generate(args: Namespace, config: Config) -> None: def cmd_generate(args: Namespace, config: Config) -> None:
@ -66,21 +101,33 @@ def parse_args() -> Namespace:
choices=[level.lower() for level in logging.getLevelNamesMapping().keys()], choices=[level.lower() for level in logging.getLevelNamesMapping().keys()],
help="Log level", help="Log level",
) )
parser.add_argument(
"--album-path", subcommands = parser.add_subparsers(title="subcommands")
"-p",
init_cmd = subcommands.add_parser(
"init",
help="Initialize an photo directory",
)
init_cmd.set_defaults(action="init")
init_cmd.add_argument(
"album_path",
nargs="?",
default=".", default=".",
help="Path to the main photos directory", help="Path to the main photos directory",
) )
subcommands = parser.add_subparsers(title="subcommands")
# Generate subcommand # Generate subcommand
generate_cmd = subcommands.add_parser( generate_cmd = subcommands.add_parser(
"generate", "generate",
help="Generate the HTML photo album", help="Generate the HTML photo album",
) )
generate_cmd.set_defaults(action="generate") generate_cmd.set_defaults(action="generate")
generate_cmd.add_argument(
"album_path",
nargs="?",
default=".",
help="Path to the main photos directory",
)
# Clean subcommand # Clean subcommand
clean_cmd = subcommands.add_parser( clean_cmd = subcommands.add_parser(
@ -88,6 +135,12 @@ def parse_args() -> Namespace:
help="Remove all generated content from the photo album directory", help="Remove all generated content from the photo album directory",
) )
clean_cmd.set_defaults(action="clean") clean_cmd.set_defaults(action="clean")
clean_cmd.add_argument(
"album_path",
nargs="?",
default=".",
help="Path to the main photos directory",
)
return parser.parse_args() return parser.parse_args()
@ -97,6 +150,7 @@ def setup_logging(level_str: str) -> None:
level = levels[level_str.upper()] level = levels[level_str.upper()]
logging.basicConfig( logging.basicConfig(
level=level, level=level,
format="[%(name)s] %(message)s",
handlers=[RichHandler(rich_tracebacks=True)], handlers=[RichHandler(rich_tracebacks=True)],
) )
# Override PIL logging because debug is really noisy # Override PIL logging because debug is really noisy
@ -104,6 +158,5 @@ def setup_logging(level_str: str) -> None:
logging.getLogger("PIL").setLevel(logging.INFO) logging.getLogger("PIL").setLevel(logging.INFO)
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -82,13 +82,17 @@ def generate_thumbnails(config: Config, path: Path) -> None:
thumb_img.thumbnail(config.thumbnail_size) thumb_img.thumbnail(config.thumbnail_size)
thumb_filename = file_path.stem + ".thumb" + file_path.suffix thumb_filename = file_path.stem + ".thumb" + file_path.suffix
thumb_img.save(slides_path / thumb_filename) thumb_img.save(slides_path / thumb_filename)
logger.info(f"Generated thumbnail size {parent_path / filename} -> {thumb_filename}") logger.info(
f"Generated thumbnail size {parent_path / filename} -> {thumb_filename}"
)
screen_img = orig_img.copy() screen_img = orig_img.copy()
screen_img.thumbnail(config.view_size) screen_img.thumbnail(config.view_size)
screen_filename = file_path.stem + ".screen" + file_path.suffix screen_filename = file_path.stem + ".screen" + file_path.suffix
screen_img.save(slides_path / screen_filename) screen_img.save(slides_path / screen_filename)
logger.info(f"Generated screen size {parent_path / filename} -> {screen_filename}") logger.info(
f"Generated screen size {parent_path / filename} -> {screen_filename}"
)
def generate_html(config: Config, path: Path) -> None: def generate_html(config: Config, path: Path) -> None: