From 38deb98d5a961d2e8aadcb43bad84ee1891a6612 Mon Sep 17 00:00:00 2001 From: Aaron Griffith Date: Sat, 19 Mar 2011 20:45:40 -0400 Subject: [PATCH] added command line option for web_assets postprocessing hook --- README.rst | 9 +++++++++ gmap.py | 21 +++++++++++++++++++-- quadtree.py | 8 +++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index f3f23b9..3164ef7 100644 --- a/README.rst +++ b/README.rst @@ -246,6 +246,15 @@ Options --night This option enables --lighting, and renders the world at night. +--web-assets-hook=HOOK + This option lets you specify a script to run after the web assets have been + copied into the output directory, but before any tile rendering takes + place. This is an ideal time to do any custom postprocessing for markers.js + or other web assets. + + The script should be executable, and it should accept one argument: + the path to the output directory. + Viewing the Results ------------------- Within the output directory you will find two things: an index.html file, and a diff --git a/gmap.py b/gmap.py index 65545ad..856eaee 100755 --- a/gmap.py +++ b/gmap.py @@ -24,6 +24,7 @@ import os import os.path from optparse import OptionParser import re +import subprocess import multiprocessing import time import logging @@ -55,6 +56,7 @@ def main(): parser.add_option("--spawn", dest="spawn", help="Renders shadows using light data from each chunk, as if it were night, while also highlighting areas that are dark enough to spawn mobs. Implies --lighting and --night.", action="store_true") parser.add_option("--imgformat", dest="imgformat", help="The image output format to use. Currently supported: png(default), jpg. NOTE: png will always be used as the intermediate image format.") parser.add_option("--optimize-img", dest="optimizeimg", help="If using png, perform image file size optimizations on the output. Specify 1 for pngcrush, 2 for pngcrush+optipng+advdef. This may double (or more) render times, but will produce up to 30% smaller images. NOTE: requires corresponding programs in $PATH or %PATH%") + parser.add_option("--web-assets-hook", dest="web_assets_hook", help="If provided, run this script after the web assets have been copied, but before actual tile rendering begins. See the README for details.", action="store", metavar="SCRIPT", type="string") parser.add_option("-q", "--quiet", dest="quiet", action="count", default=0, help="Print less output. You can specify this option multiple times.") parser.add_option("-v", "--verbose", dest="verbose", action="count", default=0, help="Print more output. You can specify this option multiple times.") parser.add_option("--skip-js", dest="skipjs", action="store_true", help="Don't output marker.js or regions.js") @@ -129,7 +131,22 @@ def main(): optimizeimages.check_programs(optimizeimg) else: optimizeimg = None - + + if options.web_assets_hook: + if not os.path.exists(options.web_assets_hook): + parser.error("Provided hook script does not exist!") + def web_assets_hook(quadtree): + if options.web_assets_hook == None: + return + try: + subprocess.check_call((options.web_assets_hook, os.path.abspath(quadtree.destdir))) + except OSError, e: + logging.error("could not call web assets hook: %s" % (e,)) + sys.exit(1) + except subprocess.CalledProcessError: + logging.error("web assets hook returned error") + sys.exit(1) + logging.getLogger().setLevel( logging.getLogger().level + 10*options.quiet) logging.getLogger().setLevel( @@ -151,7 +168,7 @@ def main(): w.go(options.procs) # Now generate the tiles - q = quadtree.QuadtreeGen(w, destdir, depth=options.zoom, imgformat=imgformat, optimizeimg=optimizeimg) + q = quadtree.QuadtreeGen(w, destdir, depth=options.zoom, imgformat=imgformat, optimizeimg=optimizeimg, web_assets_hook=web_assets_hook) q.write_html(options.skipjs) q.go(options.procs) diff --git a/quadtree.py b/quadtree.py index eefc7de..f376608 100644 --- a/quadtree.py +++ b/quadtree.py @@ -81,7 +81,7 @@ def catch_keyboardinterrupt(func): return newfunc class QuadtreeGen(object): - def __init__(self, worldobj, destdir, depth=None, imgformat=None, optimizeimg=None): + def __init__(self, worldobj, destdir, depth=None, imgformat=None, optimizeimg=None, web_assets_hook=None): """Generates a quadtree from the world given into the given dest directory @@ -94,6 +94,7 @@ class QuadtreeGen(object): assert(imgformat) self.imgformat = imgformat self.optimizeimg = optimizeimg + self.web_assets_hook = web_assets_hook # Make the destination dir if not os.path.exists(destdir): @@ -182,6 +183,8 @@ class QuadtreeGen(object): output.write(index) if skipjs: + if self.web_assets_hook: + self.web_assets_hook(self) return # since we will only discover PointsOfInterest in chunks that need to be @@ -210,6 +213,9 @@ class QuadtreeGen(object): output.write(' // ]},\n') output.write('];') + if self.web_assets_hook: + self.web_assets_hook(self) + def _get_cur_depth(self): """How deep is the quadtree currently in the destdir? This glances in config.js to see what maxZoom is set to.