# Author: Antonio Maiorano (amaiorano@gmail.com)

import os
import sys
import tempfile

def PrintUsage():
	print """
Plots an HSM defined in cpp file(s) via hsmToDot -> dot -> default image viewer
Requires GraphViz 2.18 (Windows: http://www.graphviz.org/pub/graphviz/stable/windows/graphviz-2.18.exe)

Usage: {} <filespec>
	""".format(os.path.basename(sys.argv[0]))
	
def ExecCommand(command):
	print('[Exec] ' + command)
	os.system(command)

def main(argv = None):
	if argv is None:
		argv = sys.argv
	
	if len(argv) < 2:
		PrintUsage()
		return 0

	filespec = argv[1]
		
	# Write dot file
	dotFile = tempfile.gettempdir() + os.path.sep + os.path.basename(filespec) + '.dot'
	ExecCommand('hsmToDot.py ' + filespec + '>' + dotFile)
	
	# Invoke dot to produce image
	pngFile = dotFile + '.png'
	ExecCommand('dot ' + dotFile + ' -Tpng -o' + pngFile)
	
	# Open default image viewer
	ExecCommand(pngFile)

if __name__ == "__main__":
	sys.exit(main())

@@ -361,16 +361,97 @@
 %5B0%5D))%0A%09%0A
+def GetScriptPath():%0A    return os.path.dirname(os.path.realpath(sys.argv%5B0%5D))%0A%09%0A
 def Exec
@@ -497,16 +497,25 @@
 mmand)%0A%09
+result = 
 os.syste
@@ -524,16 +524,70 @@
 command)
+%0A%09if result != 0:%0A%09%09raise Exception(%22Command failed!%22)
 %0A%0Adef ma
@@ -739,16 +739,29 @@
 tFile = 
+os.path.join(
 tempfile
@@ -777,24 +777,9 @@
 ir()
- + os.path.sep +
+,
  os.
@@ -810,16 +810,17 @@
 + '.dot'
+)
 %0A%09ExecCo
@@ -825,16 +825,46 @@
 Command(
+os.path.join(GetScriptPath(), 
 'hsmToDo
@@ -867,16 +867,22 @@
 ToDot.py
+') + '
  ' + fil
@@ -890,17 +890,19 @@
 spec + '
-%3E
+ %3E 
 ' + dotF

Make plotHsm.py execute hsmToDot.py from same directory.