|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- #!/usr/bin/env python3
-
- import os
- import shutil
- import re
-
- from operator import itemgetter
-
- from jinja2 import Environment, PackageLoader, select_autoescape
-
- from configuration import SITE_TITLE, THEME
-
-
- PICTURES_DIR_NAME = "photos"
- OUTPUT_DIR_NAME = "output"
-
-
- def list_galleries_in(path):
- gallery_dirs = [f for f in os.scandir(path) if f.is_dir()]
- for gallery_dir in gallery_dirs:
- photos = list(list_photos_in(gallery_dir))
- if len(photos) == 0:
- continue
-
- photos.sort(key=itemgetter("name"))
- gallery = {
- "name": gallery_dir.name,
- "path": gallery_dir.path,
- "url": gallery_dir.name,
- "num_photos": len(photos),
- "photos": photos,
- "cover_photo": photos[0],
- }
- yield gallery
-
-
- def list_photos_in(gallery_dir):
- photo_files = [
- f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I)
- ]
- for photo_file in photo_files:
- url = os.path.join(gallery_dir.name, photo_file.name)
- photo = {"name": photo_file.name, "url": url}
- yield photo
-
-
- def generate_output_dir():
- output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
- shutil.rmtree(output_path)
- if not os.path.isdir(output_path):
- os.mkdir(output_path)
- return output_path
-
-
- def generate_style(output_path):
- style_path = os.path.join(os.curdir, THEME, "style")
- style_output_path = os.path.join(output_path, "style")
- shutil.copytree(style_path, style_output_path)
-
-
- def generate_index(output_path, galleries):
- index_path = os.path.join(output_path, "index.html")
-
- theme_path = os.path.join(os.curdir, THEME)
- jinja_env = Environment(
- loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
- )
- index_template = jinja_env.get_template("index.html.j2")
-
- with open(index_path, "w") as index_file:
- index_file.write(
- index_template.render(galleries=galleries, site_title=SITE_TITLE)
- )
-
-
- def generate_gallery(output_path, gallery):
- generate_gallery_index(output_path, gallery)
- generate_gallery_dir(output_path, gallery)
-
-
- def generate_gallery_index(output_path, gallery):
- gallery_index_path = os.path.join(output_path, f"{gallery['name']}.html")
-
- theme_path = os.path.join(os.curdir, THEME)
- jinja_env = Environment(
- loader=PackageLoader("boop", theme_path), autoescape=select_autoescape(["html"])
- )
- gallery_template = jinja_env.get_template("gallery.html.j2")
-
- with open(gallery_index_path, "w") as gallery_file:
- gallery_file.write(
- gallery_template.render(gallery=gallery, site_title=SITE_TITLE)
- )
-
-
- def generate_gallery_dir(output_path, gallery):
- gallery_output_path = os.path.join(output_path, gallery["name"])
- if os.path.isdir(gallery_output_path):
- os.mkdir(gallery_output_path)
- shutil.copytree(gallery["path"], gallery_output_path)
-
-
- def main():
- pictures_folder = os.path.join(os.curdir, PICTURES_DIR_NAME)
- galleries = list(list_galleries_in(pictures_folder))
- if len(galleries) == 0:
- return
-
- galleries.sort(key=itemgetter("name"))
- output_path = generate_output_dir()
- generate_style(output_path)
- generate_index(output_path, galleries)
- for gallery in galleries:
- generate_gallery(output_path, gallery)
-
-
- if __name__ == "__main__":
- main()
|