Read config from file

This commit is contained in:
Nick Pegg 2024-08-03 08:35:17 -07:00
parent e3816dfdb7
commit a3c949c7d5
5 changed files with 110 additions and 13 deletions

View file

@ -11,8 +11,15 @@ logger = logging.getLogger("photoalbum.cli")
def main() -> None:
args = parse_args()
setup_logging(args.logging)
# TODO: load config from file
config = Config()
# Load config from file if it exists
conf_path = Path(args.album_path) / Path(args.config)
if conf_path.exists():
logger.debug(f"Reading config from {conf_path}")
config = Config.from_yaml(conf_path.read_bytes())
else:
logger.warning(f"No config file found at {conf_path}. Using defaults")
config = Config()
# Call the subcommand function
match args.action:
@ -31,8 +38,8 @@ def cmd_init(args: Namespace, config: Config) -> None:
def cmd_generate(args: Namespace, config: Config) -> None:
logger.debug(f"Generating in {args.path}")
generate(config, Path(args.path))
logger.debug(f"Generating in {args.album_path}")
generate(config, Path(args.album_path))
def cmd_clean(args: Namespace, config: Config) -> None:
@ -58,6 +65,12 @@ def parse_args() -> Namespace:
choices=[level.lower() for level in logging.getLevelNamesMapping().keys()],
help="Log level",
)
parser.add_argument(
"--album-path",
"-p",
default=".",
help="Path to the main photos directory",
)
subcommands = parser.add_subparsers(title="subcommands")
@ -67,12 +80,6 @@ def parse_args() -> Namespace:
help="Generate the HTML photo album",
)
generate_cmd.set_defaults(action="generate")
generate_cmd.add_argument(
"path",
nargs="?",
default=".",
help="Path to dir with photos in it",
)
# Clean subcommand
clean_cmd = subcommands.add_parser(

View file

@ -1,7 +1,12 @@
import logging
from dataclasses import dataclass
import yaml
DEFAULT_CONFIG_PATH = "photoalbum.conf.yml"
logger = logging.getLogger(__name__)
@dataclass
class Config:
@ -11,4 +16,17 @@ class Config:
# Size of the image when looking at the standalone image page
view_size: tuple[int, int] = (1920, 1080)
# TODO: to/from file classmethods
@classmethod
def from_yaml(cls, contents: bytes) -> "Config":
conf = cls()
data = yaml.safe_load(contents)
if data is None:
return conf
for key, val in data.items():
match key:
case "thumnail_size":
conf.thumbnail_size = tuple(val)
case "view_size":
conf.view_size = tuple(val)
return conf

View file

@ -34,7 +34,7 @@ def maybe_create_skeleton(config: Config, album_path: Path) -> tuple[bool, list[
skel_files = []
skel_dir = Path(__file__).parent / "skel"
logging.debug(f"Skeleton dir: {skel_dir}")
logger.debug(f"Skeleton dir: {skel_dir}")
for parent_path, dirnames, filenames in skel_dir.walk():
for filename in filenames: