#!/usr/bin/python
"""
Photo pages generator
Create thumbnails of JPG photos & generate HTML files showing these thumbnails.
Usage:
python photopages.py [option]
Options:
publish
Publish photos to the site
Regenerates the HTML files and any needed thumbnail images
example : python photopages.py publish
newalbum [albumname] [date] [title]
Creates folders to store JPG and a create a config file
albumname: [a-z0-9_]+ only please, will appear in URLs
date : date of the event in yyyy-mm-dd format
title : title of the event, e.g. "Laserquest competition"
example : python photopages.py newalbum bbq 2006-10-29 "First BBQ"
"""
import sys
import os
import re
import ConfigParser
import shutil
from string import Template
thumbdir = 'thumbnails'
def main():
if len(sys.argv)<2:
print __doc__
sys.exit()
option = sys.argv[1]
params = sys.argv[2:]
# change to the directory of this script
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
if option=="publish":
print publish(params)
elif option=="newalbum":
print newalbum(params)
else:
print __doc__
sys.exit()
def publish(params):
years = readdir('', regexp = '^\d+$', isdir = True)
years.sort()
html = ''
for year in years:
html += '<h2>' + year + '</h2>\n'
albums = readdir(year, isdir = True)
for album in albums:
print "Processing", year, album
albumpath = os.path.join(year, album)
# read year/album/details.ini
configfile = os.path.join(albumpath, 'details.ini')
config = ConfigParser.ConfigParser()
config.read(configfile)
details = dict(config.items("event"))
title = "No title"
if 'title' in details:
title = details["title"]
html += '<h3>' + title + '</h3>\n'
thumbnailpaths = generatethumbs(albumpath, year, album)
if len(thumbnailpaths)==0:
continue
html += '<table border="0" cellspacing="1" cellpadding="1" width="100%">\n'
count = 0
column = 1
columncount = 3
while count<len(thumbnailpaths):
if column==1:
html+= '<tr>\n'
html+= '<td><img src=\"' + thumbdir + "/" + year + "/" + album + "/" + thumbnailpaths[count] + '\" width=200/></td>\n'
if column==columncount:
html+= '</tr>\n'
column = 1
else:
column = column + 1
count = count + 1
if column!=1:
while column!=columncount:
html+= '<td> </td>\n'
column = column + 1
html+= '</tr>\n'
html+= '</table>\n'
# generate some html
html = rendertemplate(html)
f = open("index.html", "w")
f.write(html)
f.close()
def generatethumbs(srcfolder, year, album):
# create the thumbnail dir if it doesn't already exist
if not os.path.isdir(thumbdir):
os.mkdir(thumbdir)
thumbyeardir = os.path.join(thumbdir, year)
if not os.path.isdir(thumbyeardir):
os.mkdir(thumbyeardir)
thumbalbumdir = os.path.join(thumbdir, year, album)
if not os.path.isdir(thumbalbumdir):
os.mkdir(thumbalbumdir)
# temp create thumbnails
images = readdir(srcfolder, isfile=True, regexp='^.*(jpg|JPG)$')
for image in images:
shutil.copy(os.path.join(srcfolder, image), os.path.join(thumbalbumdir, image))
return images
def readdir(path, isfile=False, isdir=False, regexp=None):
"""
Get a list of directories/files at a path,
optionally only directories matching a regular expression are returned
"""
dirs = []
items = os.listdir(path)
for item in items:
itempath = os.path.join(path, item)
if (isdir and os.path.isdir(itempath)) or (isfile and os.path.isfile(itempath)):
if regexp==None or re.match(regexp, item):
dirs.append(item)
return dirs
def rendertemplate(content):
f = open("template.html")
html = f.read()
f.close()
t = Template(html)
return t.safe_substitute(body=content)
def newalbum(params):
pass
if __name__ == '__main__':
main()