123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #!/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)
- thumb_url = os.path.join(gallery_dir.name, f"thumb_{photo_file.name}")
- photo = {
- "name": photo_file.name,
- "path": photo_file.path,
- "url": url,
- "thumb_url": thumb_url,
- }
- yield photo
-
-
- def generate_output_dir():
- output_path = os.path.join(os.curdir, OUTPUT_DIR_NAME)
- 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")
- if os.path.isdir(style_output_path):
- shutil.rmtree(style_output_path)
- 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 not os.path.isdir(gallery_output_path):
- os.mkdir(gallery_output_path)
-
- for photo in gallery["photos"]:
- photo_output_path = os.path.join(output_path, photo["url"])
- shutil.copyfile(photo["path"], photo_output_path)
-
- thumb_output_path = os.path.join(output_path, photo["thumb_url"])
- shutil.copyfile(photo["path"], thumb_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()
|