"""Summarize scenario evaluation results (implements the script ``diles-render-plot-summary``). """ import glob import optparse import os import shutil import markdown import Image as pil import jinja2 from diles.util import readfile, writefile RESDIR = os.path.join(os.path.dirname(__file__), "html") # ============================================================================= # command line interface # ============================================================================= def options(): op = optparse.OptionParser("%prog -d DIR") op.add_option("-d", "--dest", metavar="DIR", help="destination directory where plot files are located " "and the HTML file will be stored in") opts, args = op.parse_args() if args: op.error("invalid arguments") if opts.dest is None: op.error("need a destination directory") return opts def render(pth): def fname2params(fname): return dict(x.split("-", 1) for x in f.split(".")[1:-1]) fnames = glob.glob(os.path.join(pth, "*.png")) fnames += glob.glob(os.path.join(pth, "*.md")) fnames = [os.path.basename(f) for f in fnames] sections = sorted(set(f.split(".", 1)[0] for f in fnames)) for section in sections: plots, params, html = [None] * 3 sfnames = [f for f in fnames if f.startswith("%s." % section)] stype = sfnames[0].rsplit(".", 1)[1] if stype == "png": img = pil.open(os.path.join(pth, sfnames[0])) width = img.size[0] plots = [{'params': fname2params(f), 'file': f, 'width': width} for f in sfnames] plots.sort(key=lambda p: sorted(p['params'].items())) params = {} for plot in plots: for name, value in plot['params'].items(): params.setdefault(name, set()).add(value) elif stype == "md": assert len(sfnames) == 1 mdfname = os.path.join(pth, sfnames[0]) html = markdown.markdown(readfile(mdfname)) else: assert False tmpl = readfile(os.path.join(RESDIR, "plots.tmpl")) osections = [s for s in sections if s != section] context = {'section': section, 'params': params, 'plots': plots, 'othersections': osections, 'html': html} html = jinja2.Template(tmpl).render(**context) writefile(os.path.join(pth, "plots.%s.html" % section), html) # meta information ixsrc = os.path.join(pth, "plots.%s.html" % sections[0]) ixdst = os.path.join(pth, "index.html") shutil.copy(ixsrc, ixdst) for res in ("*.js", "*.css"): for src in glob.glob(os.path.join(RESDIR, res)): dst = os.path.join(pth, os.path.basename(src)) shutil.copy(src, dst) def main(): opts = options() parts = [] for fname in sorted(glob.glob(os.path.join(opts.dest, "*"))): if os.path.isdir(fname): bfn = os.path.basename(fname) print "- %s" % os.path.basename(fname) render(fname) info = markdown.markdown(readfile(os.path.join(fname, "info.md"))) link = os.path.join(bfn, "index.html") parts.append((link, bfn, info)) tmpl = readfile(os.path.join(RESDIR, "index.tmpl")) context = {'parts': parts} html = jinja2.Template(tmpl).render(**context) writefile(os.path.join(opts.dest, "index.html"), html) for res in ("*.css",): for src in glob.glob(os.path.join(RESDIR, res)): dst = os.path.join(opts.dest, os.path.basename(src)) shutil.copy(src, dst) if __name__ == '__main__': main()