#!/bin/bash #shellcheck disable=SC1090 . "$(sfpath)" || exit 3 shellfu import pretty shellfu import jat usage() { mkusage "$@" "[options] YLOG" \ -- \ "Export logfile YLOG to a concise report. Format and target" \ "paths can be specified using -d, -n and -f. Note that one log" \ "may contain multiple sessions; each will be saved to separate" \ "file." \ -o \ "-d EDIR Save exported files to EDIR (make it if it does not" \ " exist). Default is current directory." \ "-f EFMT Export in format EFMT. Default is 'html'." \ " exist). Default is current directory." \ "-n NAME Set template for exported files to NAME. NAME must" \ " contain '%s', which will be replaced with session" \ " id. Default name is jat_report%s.html" } render_with_jinja() { # # Render template $1 with Jinja2 # debug -v ExpDir ExpName ExpFmt YLog TFile { echo 'import sys' echo 'from jinja2 import Template' echo 'import jat' echo '' echo "epath = '$ExpDir/$ExpName.$ExpFmt'" echo "ypath = '$YLog'" echo "tpath = '$TFile'" echo '' echo 'sessions = jat.load(ypath)' echo '' echo 'with open(tpath) as tf:' echo ' t = Template(tf.read())' echo '' echo 'for session in sessions:' echo ' with open(epath % session.sessid, "w") as ef:' echo ' ef.write(t.render(' echo ' session=session' echo ' ))' } | PYTHONPATH="__JATTOOL_PYTHONPATH__" python2 } cmd_export() { # # Export to HTML # local TFile mkdir -p "$ExpDir" || { warn "cannot create export directory: $ExpDir" return 3 } case $ExpName in *%s*) : ;; *) warn "NAME does not contain %s, adding suffix -%s" ExpName+=-%s ;; esac TFile="__JATTOOL_TEMPLATES__/$ExpFmt.j2" test -f "$TFile" || { warn "unsupported format: $ExpFmt" return 3 } render_with_jinja } main() { local YLog local ExpName=jat_report-%s local ExpDir=. local ExpFmt=html while true; do case $1 in -d) ExpDir=$2; shift 2 || usage -w "missing EDIR" ;; -f) ExpFmt=$2; shift 2 || usage -w "missing EFMT" ;; -n) ExpName=$2; shift 2 || usage -w "missing NAME" ;; -*) usage ;; *) break ;; esac done YLog=$1; shift test -n "$YLog" || usage -w "no YLOG specified" test -f "$YLog" || usage -w "no such file: $YLog" debug -v Ylog ExpName ExpDir ExpFmt cmd_export } main "$@"