jattool-export.skel 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. shellfu import jat
  6. usage() {
  7. mkusage "$@" "[options] YLOG" \
  8. -- \
  9. "Export logfile YLOG to a concise report. Format and target" \
  10. "paths can be specified using -d, -n and -f. Note that one log" \
  11. "may contain multiple sessions; each will be saved to separate" \
  12. "file." \
  13. -o \
  14. "-d EDIR Save exported files to EDIR (make it if it does not" \
  15. " exist). Default is current directory." \
  16. "-f EFMT Export in format EFMT. Default is 'html'." \
  17. " exist). Default is current directory." \
  18. "-n NAME Set template for exported files to NAME. NAME must" \
  19. " contain '%s', which will be replaced with session" \
  20. " id. Default name is jat_report%s.html"
  21. }
  22. render_with_jinja() {
  23. #
  24. # Render template $1 with Jinja2
  25. #
  26. debug -v ExpDir ExpName ExpFmt YLog TFile
  27. {
  28. echo 'import sys'
  29. echo 'from jinja2 import Template'
  30. echo 'import jat'
  31. echo ''
  32. echo "epath = '$ExpDir/$ExpName.$ExpFmt'"
  33. echo "ypath = '$YLog'"
  34. echo "tpath = '$TFile'"
  35. echo ''
  36. echo 'sessions = jat.load(ypath)'
  37. echo ''
  38. echo 'with open(tpath) as tf:'
  39. echo ' t = Template(tf.read())'
  40. echo ''
  41. echo 'for session in sessions:'
  42. echo ' with open(epath % session.sessid, "w") as ef:'
  43. echo ' ef.write(t.render('
  44. echo ' session=session'
  45. echo ' ))'
  46. } | PYTHONPATH="__JATTOOL_PYTHONPATH__" python2
  47. }
  48. cmd_export() {
  49. #
  50. # Export to HTML
  51. #
  52. local TFile
  53. mkdir -p "$ExpDir" || {
  54. warn "cannot create export directory: $ExpDir"
  55. return 3
  56. }
  57. case $ExpName in
  58. *%s*) : ;;
  59. *) warn "NAME does not contain %s, adding suffix -%s"
  60. ExpName+=-%s ;;
  61. esac
  62. TFile="__JATTOOL_TEMPLATES__/$ExpFmt.j2"
  63. test -f "$TFile" || {
  64. warn "unsupported format: $ExpFmt"
  65. return 3
  66. }
  67. render_with_jinja
  68. }
  69. main() {
  70. local YLog
  71. local ExpName=jat_report-%s
  72. local ExpDir=.
  73. local ExpFmt=html
  74. while true; do case $1 in
  75. -d) ExpDir=$2; shift 2 || usage -w "missing EDIR" ;;
  76. -f) ExpFmt=$2; shift 2 || usage -w "missing EFMT" ;;
  77. -n) ExpName=$2; shift 2 || usage -w "missing NAME" ;;
  78. -*) usage ;;
  79. *) break ;;
  80. esac done
  81. YLog=$1; shift
  82. test -n "$YLog" || usage -w "no YLOG specified"
  83. test -f "$YLog" || usage -w "no such file: $YLog"
  84. debug -v Ylog ExpName ExpDir ExpFmt
  85. cmd_export
  86. }
  87. main "$@"