Bläddra i källkod

Convert scripts to command line args.

bentleyjoakes 5 år sedan
förälder
incheckning
8412ed40c8
2 ändrade filer med 30 tillägg och 16 borttagningar
  1. 15 6
      LoLAConvert.py
  2. 15 10
      LoLADraw.py

+ 15 - 6
LoLAConvert.py

@@ -1,6 +1,7 @@
 import sys
 import xml.etree.ElementTree as ET
 
+
 class LoLAConvert:
 
     def __init__(self):
@@ -25,7 +26,7 @@ class LoLAConvert:
 
             for i, place in enumerate(places):
                 f.write("\t" + place)
-                if i < len(places) -1:
+                if i < len(places) - 1:
                     f.write(",\n")
                 else:
                     f.write(";\n")
@@ -33,7 +34,7 @@ class LoLAConvert:
             f.write("\nMARKING\n")
             for i, pm in enumerate(places_with_markings):
                 f.write("\t" + pm[0] + " : " + pm[1])
-                if i < len(places_with_markings) -1:
+                if i < len(places_with_markings) - 1:
                     f.write(",\n")
                 else:
                     f.write(";\n")
@@ -65,6 +66,8 @@ class LoLAConvert:
                     else:
                         f.write(";\n")
 
+        print("Wrote to: " + str(lola_file))
+
     def parse_pnml(self, filename):
         tree = ET.parse(filename)
         root = tree.getroot()
@@ -102,16 +105,22 @@ class LoLAConvert:
 
                 arcs.append((source, target, inscription))
 
-
         return places, transitions, arcs
 
     def get_child_with_name(self, node, name):
         for child in node:
             if child.tag == name:
                 return child
-        raise Exception("On node: " + str(node) +", the child " + name + " was not found!")
+        raise Exception("On node: " + str(node) + ", the child " + name + " was not found!")
+
 
 if __name__ == "__main__":
-    filename = "dining_philo.xml"
+
+    print("Starting LoLAConvert...")
+    if len(sys.argv) < 2:
+        print("Error: Please pass a filename as the first argument.")
+        exit(1)
+
+    filename = sys.argv[1]
     lc = LoLAConvert()
-    lc.convert_file(filename)
+    lc.convert_file(filename)

+ 15 - 10
LoLADraw.py

@@ -1,4 +1,5 @@
 import subprocess
+import sys
 
 try:
     import pydot
@@ -7,15 +8,11 @@ try:
 except ImportError:
     has_pydot = False
 
-class LoLADraw:
 
-    def __init__(self):
-        print("Starting LoLADraw...")
-        print("Please ensure that GraphViz is installed before use.")
+class LoLADraw:
 
     def get_net(self, filename):
 
-
         places = []
         markings = {}
         transitions = {}
@@ -126,29 +123,37 @@ class LoLADraw:
             c, p = conpro
 
             for c_name, weight in c.items():
-                graph.add_edge(pydot.Edge(nodes[c_name], nodes[t_name]))#, color=color))
+                graph.add_edge(pydot.Edge(nodes[c_name], nodes[t_name]))  # , color=color))
 
             for p_name, weight in p.items():
                 graph.add_edge(pydot.Edge(nodes[t_name], nodes[p_name], label=weight))
                 # , color=color))
 
-
         dot_filename = filename.replace(".lola", ".dot")
         graph.write(dot_filename, prog='dot')
 
-        command = "dot -Tsvg " + dot_filename + " -o " + filename.replace(".lola", ".svg")
-        subprocess.call(command, shell=True)
+        svg_filename = filename.replace(".lola", ".svg")
+        command = "dot -Tsvg " + dot_filename + " -o " + svg_filename
+        returncode = subprocess.call(command, shell=True)
+        if returncode != 0:
+            print("ERROR: Please ensure that GraphViz is installed!")
 
+        print("Exported drawing to file: " + svg_filename)
         # command = "rm " + dot_filename
         # subprocess.call(command, shell=True)
 
 
 if __name__ == "__main__":
 
+    print("Starting LoLADraw...")
+    if len(sys.argv) < 2:
+        print("Error: Please pass a filename as the first argument.")
+        exit(1)
+
     if not has_pydot:
         print("ERROR: Install pydot first!")
         exit(1)
 
-    filename = "dining_philo.lola"
+    filename = sys.argv[1]
     ld = LoLADraw()
     ld.draw(filename)