simple-s3-gallery/GenTemplate.py

58 lines
2.3 KiB
Python
Raw Normal View History

2019-04-09 06:42:32 +00:00
#!/usr/bin/env python3
2019-04-10 03:54:03 +00:00
# GenTemplate.py - Takes PathList files and thumbnails from GenThumb and generates a single page gallery
# Usage: ./GenTemplate.py -p "pathlist file.txt"
2019-04-09 06:42:32 +00:00
from pathlib import Path
2019-04-09 19:12:41 +00:00
import getopt
2019-04-10 03:54:03 +00:00
import random
2019-04-09 19:12:41 +00:00
import sys
import os
2019-04-09 06:42:32 +00:00
2019-04-09 19:12:41 +00:00
baseuri = os.environ.get('BASEURI',"https://s3.wasabisys.com/c0de-photography/")
thumb_path = os.environ.get('THUMBNAILS', "./thumbs")
try:
opts, args = getopt.getopt(sys.argv[1:],"hp:",["pathlist="])
except getopt.GetoptError:
2019-04-10 03:54:03 +00:00
print ('GenTemplate.py -p <pathlist file>')
2019-04-09 19:12:41 +00:00
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
2019-04-10 03:54:03 +00:00
print ('GenTemplate.py -p <pathlist file>')
2019-04-09 19:12:41 +00:00
sys.exit()
elif opt in ("-p", "--pathlist"):
pathlist_file = arg # Index file created by GenThumb.py
2019-04-09 06:42:32 +00:00
# Load templates into memory
gallery_template = open('templates/root_gallery.html', 'r').read()
card_template = open('templates/card.html', 'r').read()
2019-04-10 03:54:03 +00:00
crumb_template = """<li class="breadcrumb-item" aria-current="page">{{PATH}}</li>"""
# Get the paths of all JPGs in the thumbnail directory
2019-04-09 06:42:32 +00:00
thumblist = list(Path(thumb_path).rglob("*.[jJ][pP][gG]"))
2019-04-09 07:37:57 +00:00
with open(pathlist_file, 'r') as pathlist:
with open('index_%s.html' % pathlist_file.strip("pathlist_").strip(".txt"), 'w') as index:
2019-04-09 06:42:32 +00:00
pathlist = "%s" % pathlist.read()
pathlist = pathlist.splitlines()
2019-04-10 03:54:03 +00:00
pathlist.sort()
2019-04-09 06:42:32 +00:00
thumbrow = ""
for image in thumblist:
# Search the pathlist for any lines with a matching filename
2019-04-09 06:42:32 +00:00
indices = [i for i, s in enumerate(pathlist) if image.name in s]
if len(indices) > 0:
imagename = "%s" % image
the_template = card_template # Don't mutate the source
2019-04-09 06:42:32 +00:00
the_template = the_template.replace("{{FULLLINK}}", pathlist[indices[0]])
the_template = the_template.replace("{{TITLE}}", imagename.strip("thumbs/"))
thumbrow += the_template.replace("{{THUMBNAIL}}", "%s" % image)
2019-04-10 03:54:03 +00:00
breadcrumbs = pathlist_file.strip("pathlist_").strip(".txt").split(":")
crumblist = ""
for crumb in breadcrumbs:
the_template = crumb_template
crumblist += the_template.replace("{{PATH}}", crumb)
index.write(gallery_template.replace("{{THUMBROW}}", thumbrow).replace("{{BREADCRUMBS}}", crumblist))
2019-04-09 06:42:32 +00:00