Prechádzať zdrojové kódy

Cleaned up the different modes of the wrapper a bit

Yentl Van Tendeloo 8 rokov pred
rodič
commit
8b80200807
1 zmenil súbory, kde vykonal 48 pridanie a 40 odobranie
  1. 48 40
      wrappers/modelverse.py

+ 48 - 40
wrappers/modelverse.py

@@ -7,6 +7,12 @@ import sys
 
 COMPILER_PATH = "interface/HUTN"
 
+MODE_UNCONNECTED = 0
+MODE_UNAUTHORIZED = 1
+MODE_MODELLING = 2
+MODE_MODIFY = 3
+MODE_DIALOG = 4
+
 # Bind to the compiler (might have to update path manually!)
 sys.path.append(COMPILER_PATH)
 from hutn_compiler.compiler import main as do_compile
@@ -58,7 +64,7 @@ class InterfaceMismatch(ModelverseException):
 taskname = random.random()
 address = None
 last_output = None
-mode = 0
+mode = MODE_UNCONNECTED
 
 def _input(value):
     # Ugly json encoding of primitives
@@ -156,13 +162,13 @@ def init(address_param="http://127.0.0.1:8001"):
     # raises UnknownError
     # raises InvalidMode
     global mode
-    if mode != 0:
+    if mode != MODE_UNCONNECTED:
         raise InvalidMode()
     global address
     address = address_param
     try:
         _input_raw('"%s"' % taskname, "task_manager")
-        mode = 1
+        mode = MODE_UNAUTHORIZED
     except URLError as e:
         raise ConnectionError(e.reason)
 
@@ -173,7 +179,7 @@ def login(username, password):
     # raises PermissionDenied
     # raises InterfaceMismatch
     global mode
-    if mode != 1:
+    if mode != MODE_UNAUTHORIZED:
         raise InvalidMode()
     _output("Log on as which user?")
     _input(username)
@@ -182,7 +188,7 @@ def login(username, password):
         if _output() == "Welcome to the Model Management Interface v2.0!":
             _output("Use the 'help' command for a list of possible commands")
             _input("quiet")
-            mode = 2
+            mode = MODE_MODELLING
         elif _last_output() == "Wrong password!":
             raise PermissionDenied()
         else:
@@ -195,7 +201,7 @@ def login(username, password):
             _output("Welcome to the Model Management Interface v2.0!")
             _output("Use the 'help' command for a list of possible commands")
             _input("quiet")
-            mode = 2
+            mode = MODE_MODELLING
         elif _last_output() == "Not the same password!":
             # We just sent the same password, so it should be identical, unless the interface changed
             raise InterfaceMismatch(_last_output())
@@ -212,7 +218,7 @@ def model_add(model_name, metamodel_name, model_code=None):
     # raises UnknownError
     # raises PermissionDenied
     # raises CompilationError
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
     # Do this before creating the model, as otherwise compilation errors would make us inconsistent
     if model_code is not None:
@@ -235,13 +241,13 @@ def model_modify(model_name):
     # raises PermissionDenied
     # raises UnknownError
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     _input(["model_modify", model_name])
     _handle_output("Success")
     # Mode has changed
-    mode = 3
+    mode = MODE_MODIFY
     _output("Model loaded, ready for commands!")
     if ("r/w" in _output()):
         write = True
@@ -254,7 +260,7 @@ def model_list():
     """List all models."""
     # return [(model1, metamodel1), (model2, metamodel2), ...]
     # raises UnknownError
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
     _input("model_list")
     output = _handle_output("Success: ", split=" ")
@@ -275,7 +281,7 @@ def model_list_full():
     """List full information on all models."""
     # return [(model1, metamodel1, owner1, group1, permissions1), (model2, metamodel2, owner2, group2, permissions2), ...]
     # raises UnknownError
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
     _input("model_list_full")
     lst = []
@@ -294,7 +300,7 @@ def verify(model):
     # return "verification_result"
     # raises UnknownError
     # raises UnknownModel
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
     _input(["verify", model])
     return _handle_output("Success: ", split=" ")
@@ -306,7 +312,7 @@ def model_overwrite(model_name, new_model=None):
     # raises PermissionDenied
     # raises CompilationError
     # raises UnknownError
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     if new_model is not None:
@@ -327,20 +333,20 @@ def user_logout():
     # return None
     # raises UnknownException
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
     _input("exit")
-    mode = 0
+    mode = MODE_UNCONNECTED
 
 def user_delete():
     """Removes the current user and break the connection."""
     # return None
     # raises UnknownException
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
     _input("self-destruct")
-    mode = 0
+    mode = MODE_UNCONNECTED
 
 def model_render(model, mapper):
     """Fetch a rendered verion of a model."""
@@ -349,7 +355,7 @@ def model_render(model, mapper):
     # raises UnknownIdentifier
     # raises InterfaceMismatch
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     _input(["model_render", model, mapper])
@@ -357,7 +363,7 @@ def model_render(model, mapper):
 
 def transformation_between(source, target):
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     _input(["transformation_between", source, target])
@@ -369,7 +375,7 @@ def transformation_between(source, target):
 def transformation_add_MT_language(metamodels, RAMified_name):
     """Create a new Model Transformation language out of a set of metamodels."""
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     _input(["transformation_add_MT_language"] + metamodels + ["", RAMified_name])
@@ -378,7 +384,7 @@ def transformation_add_MT_language(metamodels, RAMified_name):
 def transformation_add_MT(RAMified_metamodel, source_metamodels, target_metamodels, operation_name, code):
     """Create a new model transformation."""
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     try:
@@ -394,7 +400,7 @@ def transformation_add_MT(RAMified_metamodel, source_metamodels, target_metamode
 def transformation_add_AL(source_metamodels, target_metamodels, operation_name, code):
     """Create a new action language model, which can be executed."""
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     try:
@@ -414,7 +420,7 @@ def transformation_add_MANUAL():
 def transformation_execute_AL(operation_name, input_models_dict, output_models_dict, callback=lambda i: None):
     """Execute an existing model operation."""
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     mv_dict_rep = []
@@ -430,7 +436,9 @@ def transformation_execute_AL(operation_name, input_models_dict, output_models_d
 
     # We are now executing, so everything we get is part of the dialog, except if it is the string for transformation termination
     while _output() not in ["Success", "Failure"]:
+        mode = MODE_DIALOG
         reply = callback(_last_output())
+        mode = MODE_MODELLING
         if reply is not None:
             _input(reply)
 
@@ -443,7 +451,7 @@ def transformation_execute_AL(operation_name, input_models_dict, output_models_d
 def transformation_execute_MT(operation_name, input_models_dict, output_models_dict, callback=lambda i: None):
     """Execute an existing model operation."""
     global mode
-    if mode != 2:
+    if  mode != MODE_MODELLING:
         raise InvalidMode()
 
     mv_dict_rep = []
@@ -544,7 +552,7 @@ def element_list(model_name):
     # raises UnknownError
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -569,7 +577,7 @@ def types(model_name):
     # raises UnknownError
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -593,7 +601,7 @@ def types_full(model_name):
     # raises UnknownError
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -619,7 +627,7 @@ def read(model_name, ID):
     # raises UnknownIdentifier
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -645,7 +653,7 @@ def read_attrs(model_name, ID):
     # raises UnknownIdentifier
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -687,7 +695,7 @@ def instantiate(model_name, typename, edge=None):
     # raises NotAnEdge
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -706,7 +714,7 @@ def delete_element(model_name, ID):
     # raises UnknownIdentifier
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -724,7 +732,7 @@ def attr_assign(model_name, ID, attr, value):
     # raises UnsupportedValue
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -747,7 +755,7 @@ def attr_assign_code(model_name, ID, attr, code):
 
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -762,7 +770,7 @@ def attr_delete(model_name, ID, attr):
     """Remove an attribute."""
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -778,7 +786,7 @@ def read_outgoing(model_name, ID, typename):
     # raises UnknownIdentifier
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -799,7 +807,7 @@ def read_incoming(model_name, ID, typename):
     # raises UnknownType
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -820,7 +828,7 @@ def read_association_source(model_name, ID):
     # raises NotAnAssociation
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -837,7 +845,7 @@ def read_association_destination(model_name, ID):
     # raises NotAnAssociation
     model_modify(model_name)
 
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
 
     try:
@@ -851,8 +859,8 @@ def model_exit():
     # return None
     # raises UnknownError
     global mode
-    if mode != 3:
+    if mode != MODE_MODIFY:
         raise InvalidMode()
     _input("exit")
     _output("Success")
-    mode = 2
+    mode = MODE_MODELLING