Sfoglia il codice sorgente

Fixed more bugs in wrapper

Yentl Van Tendeloo 8 anni fa
parent
commit
345221e0d4
2 ha cambiato i file con 70 aggiunte e 40 eliminazioni
  1. 56 37
      wrappers/modelverse.py
  2. 14 3
      wrappers/test_modelverse.py

+ 56 - 37
wrappers/modelverse.py

@@ -72,6 +72,10 @@ def _output():
 def _last_output():
     return last_output
 
+def _consume_to_end():
+    while (_output() not in ["Ready for command...", "Please give your command."]):
+        pass
+
 # Exceptions
 class ModelverseException(Exception):
     pass
@@ -137,8 +141,7 @@ def login(username, password):
         raise InvalidMode()
     _input(username)
     _input(password)
-    while (_output() != "Ready for command..."):
-        pass
+    _consume_to_end()
     mode = 2
 
 def register(username, password):
@@ -152,8 +155,7 @@ def register(username, password):
     _input(username)
     _input(password)
     _input(password)
-    while (_output() != "Ready for command..."):
-        pass
+    _consume_to_end()
     mode = 2
 
 def model_add(model_name, metamodel_name, model_code):
@@ -177,15 +179,15 @@ def model_add(model_name, metamodel_name, model_code):
                 _compile_model(model_code)
             except Exception:
                 raise CompilationError()
-
-            while (_output() != "Ready for command..."):
-                print(_last_output())
-                pass
+            _consume_to_end()
         else:
+            _consume_to_end()
             raise ModelExists()
     elif _last_output() == "Could not find type model!":
+        _consume_to_end()
         raise UnknownModel()
     elif _last_output() == "Permission denied":
+        _consume_to_end()
         raise PermissionDenied()
 
 def model_modify(model_name):
@@ -202,8 +204,10 @@ def model_modify(model_name):
     _output()
     _input(model_name)
     if _output() == "Permission denied":
+        _consume_to_end()
         raise PermissionDenied()
     elif _last_output() == "Could not find model!":
+        _consume_to_end()
         raise UnknownModel()
     elif _last_output() == "Model loaded, ready for commands!":
         mode = 3
@@ -211,9 +215,11 @@ def model_modify(model_name):
             write = True
         else:
             write = False
-        while (_output() != "Please give your command."):
-            pass
+        _consume_to_end()
         return write
+    else:
+        _consume_to_end()
+        raise UnknownException()
 
 def model_list():
     """List all models."""
@@ -226,8 +232,8 @@ def model_list():
     while (_output() != "Ready for command..."):
         v = _last_output()
         m, mm = v.split(":")
-        m.strip()
-        mm.strip()
+        m = m.strip()
+        mm = mm.strip()
         lst.append((m, mm))
     return lst
 
@@ -242,8 +248,8 @@ def model_list_full():
     while (_output() != "Ready for command..."):
         v = _last_output()
         m, mm = v.split(":")
-        m.strip()
-        mm.strip()
+        m = m.strip()
+        mm = mm.strip()
         perm, own, grp, m = m.split(" ")
         lst.append((m, mm, own, grp, perm))
     return lst
@@ -294,8 +300,8 @@ def element_list():
     while (_output() != "Please give your command."):
         v = _last_output()
         m, mm = v.split(":")
-        m.strip()
-        mm.strip()
+        m = m.strip()
+        mm = mm.strip()
         lst.append((m, mm))
     return lst
 
@@ -311,7 +317,7 @@ def types():
     while (_output() != "Please give your command."):
         v = _last_output()
         m, mm = v.split(":")
-        m.strip()
+        m = m.strip()
         lst.append(m)
     return lst
 
@@ -323,38 +329,51 @@ def read(ID):
     if mode != 3:
         raise InvalidMode()
     _input("read")
+    _output()
     _input(ID)
     _output()
-    t = _output().split(":")[1].strip()
-    if (not _output().startswith("Source:")):
-        rval = (t, None)
+    if _last_output() == "Unknown element; aborting":
+        _consume_to_end()
+        raise UnknownIdentifier()
     else:
-        src = _last_output().split(":")[1].strip()
-        trg = _output().split(":")[1].strip()
-        rval = (t, (src, trg))
-    while (_output() != "Please give your command."):
-        pass
-    return rval
+        t = _output().split(":")[1].strip()
+        if (not _output().startswith("Source:")):
+            rval = (t, None)
+        else:
+            src = _last_output().split(":")[1].strip()
+            trg = _output().split(":")[1].strip()
+            rval = (t, (src, trg))
+        while (_output() != "Please give your command."):
+            pass
+        return rval
 
 def read_attrs(ID):
     """Return a dictionary of attribute value pairs"""
     # return {attr1: value1, attr2: value2, ...}
     # raises UnknownError
+    # raises UnknownIdentifier
     if mode != 3:
         raise InvalidMode()
     _input("read")
+    _output()
     _input(ID)
-    rval = {}
-    while (_output() != "Attributes:"):
-        pass
-    while (_output() != "Please give your command."):
-        r = _last_output()
-        key, value = r.split(":")
-        _, value = value.split("=")
-        key.strip()
-        value.strip()
-        rval[key] = value
-    return rval
+    _output()
+    if _last_output() == "Unknown element; aborting":
+        _consume_to_end()
+        raise UnknownIdentifier()
+    else:
+        rval = {}
+        while (_output() != "Attributes:"):
+            pass
+        while (_output() != "Please give your command."):
+            r = _last_output()
+            key, value = r.split(":")
+            _, value = value.split("=")
+            key = key.strip()
+            value = value.strip()
+            value = None if value == "None" else json.loads(value)
+            rval[key] = value
+        return rval
 
 def instantiate(typename, edge=None, ID=""):
     """Create a new instance of the specified typename, between the selected elements (if not None), and with the provided ID (if any)"""

+ 14 - 3
wrappers/test_modelverse.py

@@ -7,11 +7,22 @@ password = str(random())
 init()
 register(username, password)
 
-model_add("PetriNets", "SimpleClassDiagrams", open("../models/petrinet_ports.mvc", "r").read())
-model_modify("PetriNets")
+try:
+    model_add("PetriNets", "SimpleClassDiagrams", open("../models/petrinet_ports.mvc", "r").read())
+except ModelExists:
+    pass
+
+model_modify("SimpleClassDiagrams")
 print(element_list())
 try:
     model_list()
 except InvalidMode:
     print(types())
-    print(read("Place"))
+    try:
+        print(read("Place"))
+    except UnknownIdentifier:
+        print(read("Class"))
+        try:
+            print(read_attrs("Place"))
+        except UnknownIdentifier:
+            print(read_attrs("Class"))