|
|
|
|
|
|
|
|
import os |
|
|
import os |
|
|
import shutil |
|
|
import shutil |
|
|
import re |
|
|
import re |
|
|
|
|
|
import yaml |
|
|
|
|
|
|
|
|
from operator import itemgetter |
|
|
from operator import itemgetter |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
photos = list(list_photos_in(gallery_dir)) |
|
|
photos = list(list_photos_in(gallery_dir)) |
|
|
if len(photos) == 0: |
|
|
if len(photos) == 0: |
|
|
continue |
|
|
continue |
|
|
|
|
|
|
|
|
photos.sort(key=itemgetter("name")) |
|
|
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 = { |
|
|
gallery = { |
|
|
"name": gallery_dir.name, |
|
|
|
|
|
|
|
|
"name": metadata.get("name", gallery_dir.name), |
|
|
"path": gallery_dir.path, |
|
|
"path": gallery_dir.path, |
|
|
"output_path": gallery_dir.name, |
|
|
"output_path": gallery_dir.name, |
|
|
"url": f"{gallery_dir.name}.html", |
|
|
"url": f"{gallery_dir.name}.html", |
|
|
"num_photos": len(photos), |
|
|
"num_photos": len(photos), |
|
|
"photos": photos, |
|
|
"photos": photos, |
|
|
"cover_photo": photos[cover_index], |
|
|
|
|
|
|
|
|
"cover_photo": cover_photo, |
|
|
} |
|
|
} |
|
|
yield gallery |
|
|
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): |
|
|
def list_photos_in(gallery_dir): |
|
|
photo_files = [ |
|
|
photo_files = [ |
|
|
f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I) |
|
|
f for f in os.scandir(gallery_dir) if re.match(".+\.jpg", f.name, re.I) |