|
|
@@ -3,6 +3,7 @@ |
|
|
|
import os |
|
|
|
import shutil |
|
|
|
import re |
|
|
|
import yaml |
|
|
|
|
|
|
|
from operator import itemgetter |
|
|
|
|
|
|
@@ -22,21 +23,44 @@ def list_galleries_in(path): |
|
|
|
photos = list(list_photos_in(gallery_dir)) |
|
|
|
if len(photos) == 0: |
|
|
|
continue |
|
|
|
|
|
|
|
photos.sort(key=itemgetter("name")) |
|
|
|
cover_index = (len(gallery_dir.name) + 42) % len(photos) |
|
|
|
|
|
|
|
metadata = {} |
|
|
|
metadata_path = os.path.join(gallery_dir.path, "metadata.yml") |
|
|
|
if os.path.exists(metadata_path): |
|
|
|
with open(metadata_path, "r") as metadata_file: |
|
|
|
metadata = yaml.load(metadata_file) |
|
|
|
|
|
|
|
# Try to get cover from metadata, if it doesn't exist, take one by |
|
|
|
# default. |
|
|
|
cover_photo = None |
|
|
|
cover_name = metadata.get("cover") |
|
|
|
if cover_name: |
|
|
|
cover_photo = find_photo(photos, cover_name) |
|
|
|
if cover_photo is None: |
|
|
|
cover_index = (len(gallery_dir.name) + 42) % len(photos) |
|
|
|
cover_photo = photos[cover_index] |
|
|
|
|
|
|
|
gallery = { |
|
|
|
"name": gallery_dir.name, |
|
|
|
"name": metadata.get("name", gallery_dir.name), |
|
|
|
"path": gallery_dir.path, |
|
|
|
"output_path": gallery_dir.name, |
|
|
|
"url": f"{gallery_dir.name}.html", |
|
|
|
"num_photos": len(photos), |
|
|
|
"photos": photos, |
|
|
|
"cover_photo": photos[cover_index], |
|
|
|
"cover_photo": cover_photo, |
|
|
|
} |
|
|
|
yield gallery |
|
|
|
|
|
|
|
|
|
|
|
def find_photo(photos, photo_name): |
|
|
|
for photo in photos: |
|
|
|
if photo["name"] == photo_name: |
|
|
|
return photo |
|
|
|
else: |
|
|
|
return None |
|
|
|
|
|
|
|
|
|
|
|
def list_photos_in(gallery_dir): |
|
|
|
photo_files = [ |
|
|
|
f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I) |