diff --git a/mirva/__init__.py b/mirva/__init__.py index 8090ba2..ccd7f8b 100644 --- a/mirva/__init__.py +++ b/mirva/__init__.py @@ -1,4 +1,4 @@ -__version__ = "20220328.0" +__version__ = "20220607.0" def get_version(): diff --git a/mirva/mirva.py b/mirva/mirva.py index d6f32f9..8b9d042 100755 --- a/mirva/mirva.py +++ b/mirva/mirva.py @@ -4,8 +4,11 @@ import sys import re import shutil import subprocess -from argparse import ArgumentParser +import random +from argparse import ArgumentParser, HelpFormatter + from mirva import get_version +from tqdm import tqdm class Mirva: @@ -22,23 +25,48 @@ class Mirva: ".*\.jpg$|.*\.jpeg$|.*\.png$|.*\.gif$|.*\.tif$", re.I ) self.video_match = re.compile(".*\.mp4$", re.I) + self.site_defaults = { + "title": {"default": "", "help": "Title of the site"}, + "sub_title": { + "default": "", + "help": "Subtitle of the site, shown under the title", + }, + "intro": { + "default": "", + "help": "Intro text shown before first image", + }, + "image_size": { + "default": "1920", + "help": "Resize images for faster loading. Use 'link' for symbolic links without resizing.", + }, + "scroll": { + "default": "smooth", + "help": "Transition to next image with keyboard: smooth or auto", + }, + } + + ## Init ## self.get_options() os.chdir(self.options.folder) - self.write_resources() self.file_list = self.get_files() - if self.options.config or not os.path.exists(self.config_file): - self.create_config() - print( - "Config created: Exiting without gallery creation. Check config first." - ) - return - self.get_config() - if self.options.exif: - self.append_exif() - self.create_posts() - self.write_index() - self.write_mediums() - print("Gallery written.") + + if self.run_commands["config"]: + + self.write_resources() + updated = self.create_config() + if updated: + print( + "Config created or updated: Check config contents: {}".format( + self.config_file + ) + ) + + if self.run_commands["generator"]: + self.get_config() + self.create_posts() + self.write_index() + self.write_mediums() + print("Gallery written.") def create_config(self): @@ -46,23 +74,48 @@ class Mirva: self.config.read(self.config_file) config_changed = False if not "SITE" in self.config: - self.config["SITE"] = { - "title": "", - "sub_title": "", - "intro": "", - "image_size": 850, - } + self.config["SITE"] = {} config_changed = True + for key in self.site_defaults: + if not key in self.config["SITE"]: + config_changed = True + self.config["SITE"][key] = self.site_defaults[key]["default"] + + if self.options.set: + for key, value in self.options.set: + if key not in self.site_defaults: + raise KeyError("Key '{}' is not a config keyword".format(key)) + self.config["SITE"][key] = value + config_changed = True + for f in self.file_list: if not f in self.config: title, _ = os.path.splitext(f) title = title.replace("_", " ") self.config[f] = {"title": title, "description": ""} config_changed = True + print("Added {}".format(f)) + + if self.options.purge: + unnecessary = [] + for f in self.config: + if f in ("SITE", "DEFAULT"): + continue + if f not in self.file_list: + print("{} not found in files".format(f)) + unnecessary.append(f) + for f in unnecessary: + del self.config[f] + config_changed = True + + if self.options.exif: + self.append_exif() + config_changed = True if config_changed: self.write_config() + return config_changed def create_posts(self): @@ -93,7 +146,6 @@ class Mirva: self.config.write(fp) def get_files(self): - image_match = re.compile(".*\.jpg$|.*\.jpeg$|.*\.png$|.*\.gif$|.*\.tif$", re.I) files = [] for f in sorted(os.listdir(".")): if f.startswith("."): @@ -104,24 +156,24 @@ class Mirva: return files def get_options(self): - parser = ArgumentParser( - prog="mirva", - epilog='Configuration note: item "image_size = [integer]" is the ' - + "middle sized image max width/height in pixels. It also accepts a special " - + 'value "link" to make symbolic links.', - ) - # parser.add_argument("-v", default=False, action="store_true") + parser = ArgumentParser(prog="mirva", formatter_class=SmartFormatter) parser.add_argument( - "--config", - default=False, - action="store_true", - help="Write config and exit. Required if more images are added.", + "--version", + action="version", + version="%(prog)s {version}".format(version=get_version()), ) parser.add_argument( - "--force", + "--folder", + type=str, + default=".", + help="Folder for gallery. Default current folder.", + ) + + parser.add_argument( + "--purge", default=False, action="store_true", - help="Force regeneration of middle sized images", + help="Remove non-existent files in image list. Required if images are removed.", ) parser.add_argument( "--exif", @@ -130,20 +182,43 @@ class Mirva: help="Append EXIF information to image descriptions", ) parser.add_argument( - "--version", - action="version", - version="%(prog)s {version}".format(version=get_version()), + "--set", + "-s", + default=None, + nargs=2, + action="append", + metavar=("key", "value"), + help="smart|Configurable options: \n" + + "\n".join( + ["{}: {}".format(k, v["help"]) for k, v in self.site_defaults.items()] + ), + ) + + parser.add_argument( + "--force", + default=False, + action="store_true", + help="Force regeneration of middle sized images", ) parser.add_argument( - "folder", - type=str, - default=".", - nargs="?", - help="Folder for gallery", + "--command", + "-c", + default="all", + action="store", + choices=["all", "config", "generator"], + help="Run only part of the process. Defaults to all. Config is run always if it doesn't exist.", ) - self.options = parser.parse_args() - def get_index(self, page_title, sub_title, intro, posts): + self.options = parser.parse_args() + self.run_commands = { + "config": self.options.command == "config" + or self.options.command == "all" + or not os.path.exists(self.config_file), + "generator": self.options.command == "generator" + or self.options.command == "all", + } + + def get_index(self, posts): return """