Преглед изворни кода

Added check for type of primitive attributes and cleaned up

Yentl Van Tendeloo пре 8 година
родитељ
комит
87cb488069
1 измењених фајлова са 9 додато и 94 уклоњено
  1. 9 94
      wrappers/modelverse.py

+ 9 - 94
wrappers/modelverse.py

@@ -31,15 +31,6 @@ class UnknownError(ModelverseException):
 class UnknownIdentifier(ModelverseException):
     pass
 
-class UnknownType(ModelverseException):
-    pass
-
-class NotAnAssociation(ModelverseException):
-    pass
-
-class UnsupportedValue(ModelverseException):
-    pass
-
 class CompilationError(ModelverseException):
     pass
 
@@ -72,6 +63,10 @@ mode = MODE_UNCONNECTED
 prev_mode = None
 current_model = None
 
+def _check_value(value):
+    if not isinstance(value, [int, long, float, str, unicode, bool])
+        raise UnsupportedValue("%s : %s" % (value, str(type(value))))
+
 def _dict_to_list(python_dict):
     lst = []
     for k, v in python_dict.items():
@@ -118,6 +113,8 @@ def _input_raw(value, taskname):
     # Ugly json encoding of primitives
     urllib2.urlopen(urllib2.Request(address, urllib.urlencode({"op": "set_input", "value": value, "taskname": taskname}))).read()
 
+    #TODO check that this is actually a Modelverse!
+
 def _compile_AL(code):
     # Compile an action language file and send the compiled code
     code_fragments = code.split("\n")
@@ -196,9 +193,6 @@ def _handle_output(requested=None, split=None):
     
 def _model_modify(model_name):
     """Modify an existing model."""
-    # raises UnknownModel
-    # raises PermissionDenied
-    # raises UnknownError
     global mode
     global prev_mode
 
@@ -220,8 +214,6 @@ def _model_modify(model_name):
 
 def _model_exit():
     """Leave model modify mode."""
-    # return None
-    # raises UnknownError
     global mode
     global prev_mode
 
@@ -239,10 +231,6 @@ def _model_exit():
 # Main MvC operations
 def init(address_param="http://127.0.0.1:8001", timeout=20.0):
     """Starts up the connection to the Modelverse."""
-    # return None
-    # raises ConnectionError
-    # raises UnknownError
-    # raises InvalidMode
     global mode
     global address
     global taskname
@@ -262,10 +250,6 @@ def init(address_param="http://127.0.0.1:8001", timeout=20.0):
 
 def login(username, password):
     """Log in a user, if user doesn't exist, it is created."""
-    # return None
-    # raises UnknownError
-    # raises PermissionDenied
-    # raises InterfaceMismatch
     global mode
     _goto_mode(MODE_UNAUTHORIZED)
 
@@ -300,12 +284,6 @@ def login(username, password):
 
 def model_add(model_name, metamodel_name, model_code=None):
     """Instantiate a new model."""
-    # return None
-    # raises UnknownModel
-    # raises ModelExists
-    # raises UnknownError
-    # raises PermissionDenied
-    # raises CompilationError
     _goto_mode(MODE_MODELLING)
 
     # Do this before creating the model, as otherwise compilation errors would make us inconsistent
@@ -339,8 +317,6 @@ def model_delete(model_name):
 
 def model_list():
     """List all models."""
-    # return [(model1, metamodel1), (model2, metamodel2), ...]
-    # raises UnknownError
     _goto_mode(MODE_MODELLING)
     _input("model_list")
     output = _handle_output("Success: ", split=" ")
@@ -359,8 +335,6 @@ def model_list():
 
 def model_list_full():
     """List full information on all models."""
-    # return [(model1, metamodel1, owner1, group1, permissions1), (model2, metamodel2, owner2, group2, permissions2), ...]
-    # raises UnknownError
     _goto_mode(MODE_MODELLING)
     _input("model_list_full")
     output = _handle_output("Success: ", split=" ")
@@ -380,20 +354,12 @@ def model_list_full():
 
 def verify(model):
     """Verify if a model conforms to its metamodel."""
-    # return "verification_result"
-    # raises UnknownError
-    # raises UnknownModel
     _goto_mode(MODE_MODELLING)
     _input(["verify", model])
     return _handle_output("Success: ", split=" ")
 
 def model_overwrite(model_name, new_model=None):
     """Upload a new model and overwrite an existing model."""
-    # return None
-    # raises UnknownModel
-    # raises PermissionDenied
-    # raises CompilationError
-    # raises UnknownError
     if mode not in [MODE_MODELLING, MODE_MODIFY, MODE_MANUAL]:
         raise InvalidMode()
 
@@ -421,8 +387,6 @@ def model_overwrite(model_name, new_model=None):
 
 def user_logout():
     """Log out the current user and break the connection."""
-    # return None
-    # raises UnknownError
     global mode
     _goto_mode(MODE_MODELLING)
     _input("exit")
@@ -430,8 +394,6 @@ def user_logout():
 
 def user_delete():
     """Removes the current user and break the connection."""
-    # return None
-    # raises UnknownError
     global mode
     _goto_mode(MODE_MODELLING)
     _input("self-destruct")
@@ -439,10 +401,6 @@ def user_delete():
 
 def model_render(model_name, mapper_name):
     """Fetch a rendered verion of a model."""
-    # return JSON_representation
-    # raises UnknownError
-    # raises UnknownIdentifier
-    # raises InterfaceMismatch
     _goto_mode(MODE_MODELLING)
 
     _input(["model_render", model_name, mapper_name])
@@ -726,8 +684,6 @@ def admin_demote():
 # Actual operations on the model
 def element_list(model_name):
     """Return a list of all IDs and the type of the element"""
-    # return [(name1, type1), (name2, type2), ...]
-    # raises UnknownError
     _goto_mode(MODE_MODIFY, model_name)
 
     _input("list_full")
@@ -744,8 +700,6 @@ def element_list(model_name):
 
 def types(model_name):
     """Return a list of all types usable in the model"""
-    # return [type1, type2, ...]
-    # raises UnknownError
     _goto_mode(MODE_MODIFY, model_name)
 
     _input("types")
@@ -761,8 +715,6 @@ def types(model_name):
 
 def types_full(model_name):
     """Return a list of full types usable in the model"""
-    # return [(type1, typetype1), (type2, typetype2), ...]
-    # raises UnknownError
     _goto_mode(MODE_MODIFY, model_name)
 
     _input("types")
@@ -779,9 +731,6 @@ def types_full(model_name):
 
 def read(model_name, ID):
     """Return a tuple of information on the element: its type and source/target (None if not an edge)"""
-    # return (type, (source, target))
-    # raises UnknownError
-    # raises UnknownIdentifier
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["read", ID])
@@ -798,9 +747,6 @@ def read(model_name, ID):
 
 def read_attrs(model_name, ID):
     """Return a dictionary of attribute value pairs"""
-    # return {attr1: value1, attr2: value2, ...}
-    # raises UnknownError
-    # raises UnknownIdentifier
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["read", ID])
@@ -831,11 +777,6 @@ def read_attrs(model_name, ID):
 
 def instantiate(model_name, 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)"""
-    # return instantiated_ID
-    # raises UnknownError
-    # raises UnknownType
-    # raises UnknownIdentifier
-    # raises NotAnEdge
     _goto_mode(MODE_MODIFY, model_name)
 
     if edge is None:
@@ -846,9 +787,6 @@ def instantiate(model_name, typename, edge=None, ID=""):
 
 def delete_element(model_name, ID):
     """Delete the element with the given ID"""
-    # return None
-    # raises UnknownError
-    # raises UnknownIdentifier
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["delete", ID])
@@ -856,11 +794,7 @@ def delete_element(model_name, ID):
 
 def attr_assign(model_name, ID, attr, value):
     """Assign a value to an attribute"""
-    # return None
-    # raises UnknownError
-    # raises UnknownIdentifier
-    # raises NoSuchAttribute
-    # raises UnsupportedValue
+    _check_type(value)
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["attr_add", ID, attr, value])
@@ -868,11 +802,7 @@ def attr_assign(model_name, ID, attr, value):
 
 def attr_assign_code(model_name, ID, attr, code):
     """Assign a piece of Action Language code to the attribute"""
-    # return None
-    # raises UnknownError
-    # raises UnknownIdentifier
-    # raises NoSuchAttribute
-    # raises UnsupportedValue
+    _check_type(code)
     try:
         compiled = _compile_AL(code)
     except Exception as e:
@@ -894,9 +824,6 @@ def attr_delete(model_name, ID, attr):
 
 def read_outgoing(model_name, ID, typename):
     """Returns a list of all outgoing associations of a specific type ("" = all)"""
-    # return [name1, name2, ...]
-    # raises UnknownError
-    # raises UnknownIdentifier
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["read_outgoing", ID, typename])
@@ -908,10 +835,6 @@ def read_outgoing(model_name, ID, typename):
 
 def read_incoming(model_name, ID, typename):
     """Returns a list of all incoming associations of a specific type ("" = all)"""
-    # return [name1, name2, ...]
-    # raises UnknownError
-    # raises UnknownIdentifier
-    # raises UnknownType
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["read_incoming", ID, typename])
@@ -923,10 +846,6 @@ def read_incoming(model_name, ID, typename):
 
 def read_association_source(model_name, ID):
     """Returns the source of an association."""
-    # returns name
-    # raises UnknownError
-    # raises UnknownIdentifier
-    # raises NotAnAssociation
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["read_association_source", ID])
@@ -934,10 +853,6 @@ def read_association_source(model_name, ID):
 
 def read_association_destination(model_name, ID):
     """Returns the destination of an association."""
-    # returns name
-    # raises UnknownError
-    # raises UnknownIdentifier
-    # raises NotAnAssociation
     _goto_mode(MODE_MODIFY, model_name)
 
     _input(["read_association_destination", ID])
@@ -957,7 +872,6 @@ def service_register(name, function):
     global mode
     _goto_mode(MODE_MODELLING)
 
-    # return None
     _input(["service_register", name])
 
     # Now we are in service-mode
@@ -984,6 +898,7 @@ def service_get(port):
 
 def service_set(port, value):
     """Set a value on a specified port."""
+    _check_value(value)
     _goto_mode(MODE_SERVICE)
 
     _input(value, port=port)