More hacking... on the verge of working now

Mon, 17 Nov 2008 02:36:44 +0000

author
andy
date
Mon, 17 Nov 2008 02:36:44 +0000
changeset 6
fffee8292ba7
parent 5
9c52ea3cb310
child 7
3c6914b3cfae

More hacking... on the verge of working now

emin file | annotate | diff | comparison | revisions
emin-default.html.templ file | annotate | diff | comparison | revisions
     1.1 --- a/emin	Fri Nov 07 10:18:51 2008 +0000
     1.2 +++ b/emin	Mon Nov 17 02:36:44 2008 +0000
     1.3 @@ -20,26 +20,14 @@
     1.4  keyboard layout) --- trebles all round!
     1.5  
     1.6  Usage: 
     1.7 -%prog [-d|-F|-H|-h|-i|-I|-n|-r|-R|-t|-T|-v|-y|-z|-Z]
     1.8 - -d DIR       build gallery in given directory
     1.9 - -F | -H FILE specify footer and header includes
    1.10 - -h           displays this oh-so-helpful message
    1.11 - -i | -I      enable or disable header and footer-writing (default is enabled)
    1.12 - -l | -L      enable or disable Lightbox2 effects (default is enabled)
    1.13 - -n FILE      specifies the index page filename
    1.14 - -r | -R      enable or disable thumbnail re-building
    1.15 - -t TITLE     specifies the gallery title
    1.16 - -T TAG       specifies the title tag (default is <h1>)
    1.17 - -v           enables verbose output while building the gallery
    1.18 - -y HEIGHT    specifies the max height of the generated thumbnails
    1.19 - -z | -Z      enable or disable the zip archive
    1.20 +%prog [opts] dir [outdir]
    1.21  
    1.22  TODO:
    1.23   * Use Cheetah templates (from file or built-in string)
    1.24   * Try to import BeautifulSoup for validating/pretty-printing the output
    1.25   * Use thickbox, jQuery, Lightbox2 etc.
    1.26 - * Split into blocks of N
    1.27   * Rename images before starting
    1.28 + * Allow complete rollback if any failure (or on demand?)
    1.29  """
    1.30  
    1.31  
    1.32 @@ -50,9 +38,9 @@
    1.33                    help="title of this gallery")
    1.34  parser.add_option("--template", dest="TEMPLATE", default=None, 
    1.35                    help="specify the template file to be used for the index pages")
    1.36 -parser.add_option("--zipfile", dest="", default=None, 
    1.37 +parser.add_option("--zipfile", dest="ZIPFILE", default=None, 
    1.38                    help="name of zip archive file. Default is based on the title.")
    1.39 -parser.add_option("-Z", "--no-zipfile", action="store_false",
    1.40 +parser.add_option("--no-zipfile", action="store_false",
    1.41                    dest="WRITE_ZIPFILE", default=True, 
    1.42                    help="disable writing out of a zipped archive of photos from this gallery")
    1.43  parser.add_option("-c", "--num-cols", dest="NUM_COLS", default=5, type=int,
    1.44 @@ -122,11 +110,17 @@
    1.45      newstr = s.replace(" ", "-").replace(",", "").replace("/", "").replace(".", "")
    1.46      return newstr
    1.47  
    1.48 -
    1.49  logging.debug("Title: %s" % opts.TITLE)
    1.50  logging.debug("Thumb height: %d" % opts.THUMB_HEIGHT)
    1.51  
    1.52  
    1.53 +class ImageInfo:
    1.54 +    def __init__(self):
    1.55 +        self.path = None
    1.56 +        self.x = None
    1.57 +        self.y = None
    1.58 +
    1.59 +
    1.60  ## Go to the gallery directory and test if it's writeable
    1.61  if not os.access(opts.OUTDIR, os.W_OK):
    1.62      logging.error("Directory %s is not writeable" % opts.OUTDIR)
    1.63 @@ -136,7 +130,7 @@
    1.64  ## Rename images
    1.65  if opts.RENAME:
    1.66      ## TODO: types: PNG/GIF, JPEG, PDF
    1.67 -    ## TODO: match types
    1.68 +    ## TODO: match formats & store thumb filenames
    1.69      for n, img in enumerate(glob.glob(["*.jpg", "*.jpeg", "*.JPG"])):
    1.70          newname = "%s-%03d.jpg" % (safename(opts.OUTDIR), n)
    1.71          if img != newname:
    1.72 @@ -144,7 +138,7 @@
    1.73              shutils.mv(img, newname)
    1.74  
    1.75  
    1.76 -## TODO: Convert unviewables to JPG/PNG (keep link to originals)
    1.77 +## TODO: Convert unviewables (TIF(F), PDF, EPS, ...) to JPG/PNG (keep link to originals)
    1.78  if opts.CONVERT:
    1.79      pass
    1.80  
    1.81 @@ -158,6 +152,7 @@
    1.82  os.chdir(opts.SRCDIR)
    1.83  for e in EXTENSIONS:
    1.84      pics += glob.glob(e)
    1.85 +    pics += glob.glob(e.upper())
    1.86  os.chdir(ROOTDIR)
    1.87  print sorted(pics)
    1.88  
    1.89 @@ -172,7 +167,6 @@
    1.90      logging.error("Problem when making thumbnails dir... exiting")
    1.91      #traceback.print_exc()
    1.92      sys.exit(1)
    1.93 -
    1.94  try:
    1.95      for pic in pics:
    1.96          ## TODO: Un-hard-code PNG thumb format
    1.97 @@ -198,15 +192,18 @@
    1.98      sys.exit(2)
    1.99  
   1.100  
   1.101 -sys.exit(0)
   1.102 -
   1.103  
   1.104  ## Calculate how many pages will be needed
   1.105  ## TODO: allow all on one page
   1.106 -NUM_PER_PAGE = opts.NUMX * opts.NUMY
   1.107 +NUM_PER_PAGE = opts.NUM_ROWS * opts.NUM_COLS
   1.108  NUM_PAGES = math.ceil( len(pics)/float(NUM_PER_PAGE) )
   1.109  
   1.110  
   1.111 +## TODO: Move HTML extension-setting to option parser 
   1.112 +## (or take from template name, e.g. page.html.template -> html)
   1.113 +opts.EXTN = "html"
   1.114 +
   1.115 +
   1.116  def getPageFilename(pagenum):
   1.117      if pagenum == 0:
   1.118          pagefile = "index.%s" % opts.EXTN
   1.119 @@ -219,7 +216,7 @@
   1.120      global NUM_PAGES
   1.121      out = ""
   1.122      if NUM_PAGES > 1:
   1.123 -        out += "<div class=\"pagelinks\">Pages: "
   1.124 +        out += ""
   1.125          ## Previous
   1.126          prev = pagenum - 1
   1.127          if prev > 0:
   1.128 @@ -238,17 +235,20 @@
   1.129              out += '[<a href="%s">prev</a>] ' % getPageFilename(next)
   1.130          else:
   1.131              out += '[next] '
   1.132 -        out = '</div>'
   1.133      return out
   1.134  
   1.135  
   1.136  # Make a zip archive
   1.137 -if opts.WRITEZIPFILE:
   1.138 +if opts.WRITE_ZIPFILE:
   1.139      logging.debug("Making zipped picture archive")
   1.140      ZIPFILE = "photo-album.zip"
   1.141 -    if opts.TITLE is not None or len(opts.TITLE) > 0:
   1.142 +    if opts.ZIPFILE is not None:
   1.143 +        ZIPFILE = opts.ZIPFILE
   1.144 +    elif opts.TITLE is not None or len(opts.TITLE) > 0:
   1.145          ZIPFILE = safeencode(opts.TITLE)
   1.146          ZIPFILE = safename(opts.OUTDIR)
   1.147 +    if ZIPFILE.split(".")[-1] != "zip":
   1.148 +        ZIPFILE += ".zip"
   1.149      import zipfiles
   1.150      #rm -f *.zip
   1.151      ## TODO: Do the zipping
   1.152 @@ -257,79 +257,38 @@
   1.153  
   1.154  ## Make each index page
   1.155  for n in range(NUM_PAGES):
   1.156 -    pagenum = n + 1
   1.157 +    PAGENUM = n + 1
   1.158  
   1.159      ## Choose and open page file
   1.160 -    pagefile = getPageFilename(pagenum)
   1.161 -    logging.info("Writing to index file %s" % pagefile)
   1.162 -    f = open(pagefile, "w")
   1.163 -    
   1.164 -    ## Write the HTML header
   1.165 -    if opts.WRITEHEADER:
   1.166 -        ## TODO: Allow header and footer to come from user-specified file
   1.167 -        logging.debug("Writing default header")
   1.168 -        f.write('<html>\n')
   1.169 -        f.write('<head>\n')
   1.170 -        f.write('<title>%s</title>\n' % opts.TITLE)
   1.171 -        f.write('<style>\n')
   1.172 -        f.write('  img { border:0; margin:5px; }\n')
   1.173 -        f.write('  body { padding:1em; background:white; font-family:sans-serif; }\n')
   1.174 -        f.write('  h1 { font-family:sans-serif; }\n')
   1.175 -        f.write('</style>\n')
   1.176 -        ## TODO: thickbox / Lightbox2 / ...
   1.177 -        if opts.USE_JS:
   1.178 -            f.write('<script type="text/javascript" src="/lightbox/js/prototype.js"></script>\n')
   1.179 -            f.write('<script type="text/javascript" src="/lightbox/js/scriptaculous.js?load=effects"></script>\n')
   1.180 -            f.write('<script type="text/javascript" src="/lightbox/js/lightbox.js"></script>\n')
   1.181 -            f.write('<link rel="stylesheet" href="/lightbox/css/lightbox.css" type="text/css" media="screen" />\n')
   1.182 -        f.write('</head>\n')
   1.183 -        f.write('<body>\n')
   1.184 +    PAGEFILE = getPageFilename(PAGENUM)
   1.185 +    logging.info("Writing to index file %s" % PAGEFILE)
   1.186 +    f = open(PAGEFILE, "w")
   1.187      
   1.188      ## Write the title
   1.189 -    pagetitle=opts.TITLE
   1.190 -    if pagenum > 0:
   1.191 -        pagetitle += " (page %s)" % pagenum
   1.192 -    ## TODO: Allow overriding of title tag
   1.193 -    titletagstr = "<%s>%s</%s>" % ("h1", pagetitle, "h1")
   1.194 -    logging.debug("Writing title as %s" % titletagstr)
   1.195 -    f.write(titletagstr + "\n")
   1.196 -        
   1.197 +    PAGETITLE = opts.TITLE
   1.198 +    if PAGENUM > 0:
   1.199 +        PAGETITLE += " (page %s)" % pagenum
   1.200 +    
   1.201      ## Write the linked page list
   1.202 -    f.write(mkPageLinkStr(pagenum) + "\n")
   1.203 +    LINKSTR = mkPageLinkStr(pagenum)
   1.204      
   1.205      ## Make thumbnails if needed and write the image HTML
   1.206      ## Display in table
   1.207 -    f.write('<p>\n')
   1.208      pics_start = n * NUM_PER_PAGE
   1.209      pics_end = (n+1) * NUM_PER_PAGE - 1
   1.210      if pics_end >= len(pics):
   1.211          pics_end = len(pics) - 1
   1.212 -    for npic, pic in enumerate(pics[pics_start, pics_end]):
   1.213 -        if not npic in (0, NUM_PER_PAGE-1) and npic % opts.NUMX == 0:
   1.214 -            f/write("<br/>\n")
   1.215 -        ## TODO: file extensions
   1.216 -        thumbpic = "thumbs/%s.jpg" % pic
   1.217 -        thumb_width = getWidth(thumbpic)
   1.218 -        thumb_height = getHeight(thumbpic)
   1.219 -        f.write('<a href="%s">' % pic)
   1.220 -        f.write('<img alt="$thumbpic" src="$thumbpic" ' % (thumbpic, thumbpic))
   1.221 -        f.write('style="border:0;" width="%d" height="%d"/>' % (thumb_width, thumb_height))
   1.222 -        f.write('</a>\n')
   1.223 -    f.write('</p>\n')
   1.224  
   1.225 -    ## Write the linked page list
   1.226 -    f.write(mkPageLinkStr(pagenum) + "\n")
   1.227 +    PAGEPICS = pics[pics_start, pics_end]
   1.228 +    PAGEPICNUMS = range(len(pics))
   1.229 +    ## Generalise this dir
   1.230 +    THUMBDIR = "thumbs"
   1.231 +    PAGETHUMBS = [os.path/join(THUMBDIR, t) for t in thumbs[pics_start, pics_end]]
   1.232 +    ## TODO: Build a list of thumb dims during the minimisation process, using ImageInfo
   1.233 +    #PAGETHUMBDIMS = [(getWidth(t), getHeight(t)) ) for t in PAGETHUMBS]
   1.234  
   1.235 -    ## Link to the zip file
   1.236 -    if opts.WRITEZIPFILE:
   1.237 -        f.write('<p>All photos zipped up: <a href="%s">%s</a></p>' % (ZIPFILE, ZIPFILE))
   1.238 -
   1.239 -    ## Write the HTML footer
   1.240 -    if opts.WRITEFOOTER:
   1.241 -        ## TODO: Allow user-specified footer file
   1.242 -        logging.debug("Writing default footer")
   1.243 -        f.write("</body>\n")
   1.244 -        f.write("</html>")
   1.245 +    ## Use template file
   1.246 +    ## TODO
   1.247  
   1.248      ## Close page file
   1.249      f.close()
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/emin-default.html.templ	Mon Nov 17 02:36:44 2008 +0000
     2.3 @@ -0,0 +1,39 @@
     2.4 +<html>
     2.5 +  <head>
     2.6 +    <title>$PAGETITLE</title>
     2.7 +    <style>
     2.8 +      img { border:0; margin:5px; }
     2.9 +      body { padding:1em; background:white; font-family:sans-serif; }
    2.10 +      h1 { font-family:sans-serif; }
    2.11 +    </style>
    2.12 +#if $opts.USE_JS:
    2.13 +    <script type="text/javascript" src="/lightbox/js/prototype.js"></script>
    2.14 +    <script type="text/javascript" src="/lightbox/js/scriptaculous.js?load=effects"></script>
    2.15 +    <script type="text/javascript" src="/lightbox/js/lightbox.js"></script>
    2.16 +    <link rel="stylesheet" href="/lightbox/css/lightbox.css" type="text/css" media="screen" />
    2.17 +#endif
    2.18 +  </head>
    2.19 +  <body>
    2.20 +    <h1>$PAGETITLE</h1>
    2.21 +#if $NUM_PAGES > 0
    2.22 +    <div class="pagelinks">Pages: $LINKSTR</div>
    2.23 +#endif
    2.24 +
    2.25 +    <p>
    2.26 +#for npic in $PAGETHUMBS
    2.27 +##if not npic in (0, NUM_PER_PAGE-1) and npic % opts.NUM_COLS == 0: f/write("<br/>\n")
    2.28 +      <a href="$PAGEPICS[$npic]"><img alt="$PAGETHUMBS[$npic]" src="$PAGETHUMBS[$npic]" 
    2.29 +                                      style="border:0;" width="$PAGETHUMBDIMS[$npic][0]" 
    2.30 +                                      height="$PAGETHUMBDIMS[$npic][1]" /></a>
    2.31 +#endfor
    2.32 +    </p>
    2.33 +
    2.34 +#if $NUM_PAGES > 0
    2.35 +    <div class="pagelinks">Pages: $LINKSTR</div>
    2.36 +#endif
    2.37 +
    2.38 +#if $opts.WRITE_ZIPFILE:
    2.39 +    <p>All photos zipped up: <a href="$ZIPFILE">$ZIPFILE</a></p>
    2.40 +#endif
    2.41 +  </body>
    2.42 +</html>

mercurial