Half-converted-ish to Python

Fri, 03 Oct 2008 23:00:37 +0000

author
andy
date
Fri, 03 Oct 2008 23:00:37 +0000
changeset 2
03c561ae154e
parent 1
57ac3ee72b07
child 3
9c426fbf27d4

Half-converted-ish to Python

emin file | annotate | diff | comparison | revisions
     1.1 --- a/emin	Fri Sep 26 22:02:19 2008 +0000
     1.2 +++ b/emin	Fri Oct 03 23:00:37 2008 +0000
     1.3 @@ -7,11 +7,39 @@
     1.4  ## by Andy Buckley
     1.5  ## http://www.insectnation.org
     1.6  ## 
     1.7 +## This is a weeny script for making static sets of Web
     1.8 +## pages for presenting lots of imagey things: photos, 
     1.9 +## PDFs, graphs...
    1.10 +## 
    1.11 +## As for the name, this is a program to make pretty 
    1.12 +## crappy galleries, so it's named after a pretty
    1.13 +## crappy artist. And, thankfully, it's not many characters
    1.14 +## to type --- trebles all round!
    1.15 +##
    1.16  ## TODO
    1.17  ##  * Use thickbox
    1.18  ##  * Specify Lightbox base path
    1.19  ##  * Split into blocks of N
    1.20  ##  * Rename images before starting
    1.21 +usage = """
    1.22 +Usage: $0 [-d|-F|-H|-h|-i|-I|-n|-r|-R|-t|-T|-v|-y|-z|-Z]
    1.23 + -d DIR       build gallery in given directory
    1.24 + -F | -H FILE specify footer and header includes
    1.25 + -h           displays this oh-so-helpful message
    1.26 + -i | -I      enable or disable header and footer-writing (default is enabled)
    1.27 + -l | -L      enable or disable Lightbox2 effects (default is enabled)
    1.28 + -n FILE      specifies the index page filename
    1.29 + -r | -R      enable or disable thumbnail re-building
    1.30 + -t TITLE     specifies the gallery title
    1.31 + -T TAG       specifies the title tag (default is <h1>)
    1.32 + -v           enables verbose output while building the gallery
    1.33 + -y HEIGHT    specifies the max height of the generated thumbnails
    1.34 + -z | -Z      enable or disable the zip archive"""
    1.35 +
    1.36 +
    1.37 +import sys, os, shutils, glob, re, commands
    1.38 +from optparse import OptionParser, OptionGroup
    1.39 +import logging
    1.40  
    1.41  
    1.42  ## Num per page / all on one page
    1.43 @@ -27,22 +55,25 @@
    1.44  ## Header & footer files; whole page / snippet; Cheetah format page template
    1.45  
    1.46  
    1.47 +## Configure log levels
    1.48 +logging.configRoot(level=opts.LOGLEVEL, format="$(message)")
    1.49  
    1.50 -# Image info functions
    1.51 -def getWidth(filename):
    1.52 -    dims=`identify "$1" | cut -d" " -f3 | cut -d"=" -f1`
    1.53 -    xdim=`echo $dims | cut -dx -f1`
    1.54 -    echo $xdim
    1.55  
    1.56 -def getHeight(filename):
    1.57 -    dims=`identify "$1" | cut -d" " -f3 | cut -d"=" -f1`
    1.58 -    ydim=`echo $dims | cut -dx -f2`
    1.59 -    echo $ydim
    1.60 +## Try to import Python Imaging Library
    1.61 +try:
    1.62 +    import PIL
    1.63 +except Exception, e:
    1.64 +    logging.error()
    1.65 +    sys.exit(1)
    1.66 +
    1.67 +
    1.68  
    1.69  def safeencode(string):
    1.70 +    """Encode a string for use as a filename."""
    1.71      #ZIPFILE=$( echo $TITLE | sed -e s/"\ "/"-"/g -e s/","//g -e s/"\/"//g -e s/"\."//g -e s/"'"//g )
    1.72      return string
    1.73  
    1.74 +
    1.75  ZIPFILE = "photo-album.zip"
    1.76  if opts.TITLE is not None or len(opts.TITLE) > 0:
    1.77      ZIPFILE = safeencode(opts.TITLE)
    1.78 @@ -50,90 +81,66 @@
    1.79  
    1.80  
    1.81  
    1.82 -if [[ -n $DEBUG ]]; then 
    1.83 -    echo "Title: $TITLE"
    1.84 -    echo "Write header: $writeHeader $headerFile"
    1.85 -    echo "Write footer: $writeFooter $footerFile"
    1.86 -    echo "Thumb height: $THUMBMAXHEIGHT"
    1.87 -    echo "Non-option args: $nonoptargs"
    1.88 -    echo
    1.89 -fi;
    1.90  
    1.91 +logging.debug("Title: $TITLE")
    1.92 +logging.debug("Write header: $writeHeader $headerFile")
    1.93 +logging.debug("Write footer: $writeFooter $footerFile")
    1.94 +logging.debug("Thumb height: $THUMBMAXHEIGHT")
    1.95 +logging.debug("Non-option args: $nonoptargs")
    1.96  
    1.97 -if [[ $HELP_MESSAGE == "true" ]]; then
    1.98 -    echo "Andy's static web gallery builder"
    1.99 -    echo "Usage: $0 [-d|-F|-H|-h|-i|-I|-n|-r|-R|-t|-T|-v|-y|-z|-Z]"
   1.100 -    echo " -d DIR       build gallery in given directory" 
   1.101 -    echo " -F | -H FILE specify footer and header includes"
   1.102 -    echo " -h           displays this oh-so-helpful message"
   1.103 -    echo " -i | -I      enable or disable header and footer-writing (default is enabled)"
   1.104 -    echo " -l | -L      enable or disable Lightbox2 effects (default is enabled)"
   1.105 -    echo " -n FILE      specifies the index page filename"
   1.106 -    echo " -r | -R      enable or disable thumbnail re-building"
   1.107 -    echo " -t TITLE     specifies the gallery title"
   1.108 -    echo " -T TAG       specifies the title tag (default is <h1>)"
   1.109 -    echo " -v           enables verbose output while building the gallery"
   1.110 -    echo " -y HEIGHT    specifies the max height of the generated thumbnails"
   1.111 -    echo " -z | -Z      enable or disable the zip archive"
   1.112 -    exit 0
   1.113 -fi
   1.114  
   1.115 +## Go to the gallery directory and test if it's writeable
   1.116 +if not os.access(opts.OUTDIR, os.W_OK):
   1.117 +    logging.error("Directory %s is not writeable" % opts.OUTDIR)
   1.118 +    sys.exit(1)
   1.119  
   1.120 -# Go to the gallery directory and test if it's writeable
   1.121 -if [[ -d $galleryDir && -w $galleryDir ]]; then
   1.122 -    cd $galleryDir
   1.123 -else
   1.124 -    echo "Directory $galleryDir either doesn't exist or is not writeable" 1>&2
   1.125 -    exit 1
   1.126 -fi
   1.127  
   1.128 -# Rename images
   1.129 -dirname=$(basename `pwd`)
   1.130 -let n=1
   1.131 -for i in *.jpg *.jpeg *.JPG; do
   1.132 -    num=$n
   1.133 -    if [[ $n -lt 10 ]]; then num="0$num"; fi
   1.134 -    if [[ $n -lt 100 ]]; then num="0$num"; fi
   1.135 -    newname=$( echo "$dirname-$num.jpg" | sed -e s:'/':'-':g -e s:'--*':'-':g )
   1.136 -    if [[ "$i" != "$newname" ]]; then mv $i $newname; fi
   1.137 -    let n++
   1.138 -done
   1.139 +if opts.RENAME:
   1.140 +    ## Rename images
   1.141 +    ## TODO: types: PNG/GIF, JPEG, PDF
   1.142 +    ## TODO: match types
   1.143 +    for n, img in enumerate(glob.glob(["*.jpg", "*.jpeg", "*.JPG"])):
   1.144 +        newname = "%s-%03d.jpg" % (safename(opts.OUTDIR), n)
   1.145 +        if img != newname:
   1.146 +            logging.debug("Renaming %s -> %s" % (img, newname))
   1.147 +            shutils.mv(img, newname)
   1.148  
   1.149 -# Build the list of pictures to display
   1.150 -pics=$( find . -maxdepth 1 -type f -name "*.jpg" | sort );
   1.151  
   1.152 +## TODO: Convert unviewables to JPG/PNG (keep link to originals)
   1.153 +if opts.CONVERT:
   1.154 +    pass
   1.155  
   1.156 -# Make thumbnails if needed
   1.157 -mkdir -p ./thumbs
   1.158 -for pic in $pics; do
   1.159 -    thumbpic="thumbs/${pic}.jpg"
   1.160 -    if [[ $regenThumbs == "true" || ! -e $thumbpic || ( `getHeight $thumbpic` -lt $THUMBMAXHEIGHT && `getHeight $pic` -ge $THUMBMAXHEIGHT ) ]]; then
   1.161 -        if [[ $VERBOSE == "true" ]]; then echo "Making new thumbnail for $pic (max height ${THUMBMAXHEIGHT})"; fi
   1.162 -        convert $pic $thumbpic
   1.163 -        mogrify -resize "x${THUMBMAXHEIGHT}>" $thumbpic
   1.164 -    fi
   1.165 -done
   1.166  
   1.167 +## Build the list of pictures to display
   1.168 +pics = sorted(glob.glob(extensions))
   1.169  
   1.170 -# Make a zipped tar archive
   1.171 -rm -f *.zip
   1.172 -if [[ $writeZipfile == "true" ]]; then
   1.173 -    if [[ $VERBOSE == "true" ]]; then echo "Making zipped picture archive"; fi
   1.174 -    zip $ZIPFILE $pics &> /dev/null
   1.175 -fi
   1.176 +
   1.177 +## Make thumbnails if needed
   1.178 +os.makedirs(os.path.join(os.OUTDIR, "thumbs")
   1.179 +for pic in pics:
   1.180 +    thumbpic = os.path.join("thumbs", pic)
   1.181 +    if opts.regenThumbs
   1.182 +        logging.debug("Making new thumbnail for %s (max height %d)" % (pic, opts.THUMBMAXHEIGHT))
   1.183 +        # convert $pic $thumbpic
   1.184 +        # mogrify -resize "x${THUMBMAXHEIGHT}>" $thumbpic
   1.185 +
   1.186 +
   1.187 +## Make a zipped tar archive
   1.188 +if opts.WRITEZIPFILE:
   1.189 +    import zipfiles
   1.190 +    #rm -f *.zip
   1.191 +    logging.debug("Making zipped picture archive")
   1.192 +    ZIPFILE = safename(opts.OUTDIR)
   1.193 +    ## TODO: Do the zipping
   1.194 +    ##zip $ZIPFILE $pics &> /dev/null
   1.195  
   1.196  
   1.197  # Count the pictures
   1.198 -let npics=0;
   1.199 -for pic in $pics; do
   1.200 -    #echo $pic
   1.201 -    let npics++
   1.202 -done;
   1.203 -if [[ $VERBOSE == "true" ]]; then echo "$npics pictures"; fi
   1.204 -if [[ $npics -eq 0 ]]; then
   1.205 -    echo "No pictures from which to build a gallery..."
   1.206 -    exit 2
   1.207 -fi
   1.208 +logging.debug("Number of pictures = %d" % len(pics))
   1.209 +if len(pics) == 0:
   1.210 +    logging.debug("No pictures from which to build a gallery...")
   1.211 +    sys.exit(2)
   1.212 +
   1.213  
   1.214  # Calculate how many pages
   1.215  let npages=$npics/$CHUNK_SIZE

mercurial