Ejemplo n.º 1
0
Archivo: parsing.py Proyecto: rbax/DAT
def parse_expression(expression):
    equal = expression.find('=')
    if equal == -1:
        raise InvalidOperation("Missing target variable name",
                               "new_var = %s" % expression, (0, 7))
    else:
        target = expression[:equal].strip()
        if not _variable_format.match(target):
            right = equal
            if right > 0 and expression[right - 1] == ' ':
                right -= 1
            if iswhitespace(expression[0:right]):
                raise InvalidOperation("Missing target variable name",
                                       "new_var %s" % expression.lstrip(),
                                       (0, 7))
            else:
                raise InvalidOperation("Invalid target variable name", None,
                                       (0, right))
        expression = expression[equal + 1:]
    try:
        return target, lexer.parse(expression)
    except LexerError, e:
        raise InvalidOperation("Error while parsing expression",
                               None,
                               select=(equal + 1 + e.position,
                                       equal + 1 + len(expression)))
Ejemplo n.º 2
0
def find_operation(name, args):
    """Choose the operation with the given name that accepts these arguments.
    """
    from dat.operations.builtins import builtin_operations

    # Initial list of considered operations: correct name
    operations = set([
        op for op in GlobalManager.variable_operations
        if op.name == name and op.usable_in_command
    ])
    operations.update(builtin_operations.get(name, []))
    if not operations:
        raise InvalidOperation("There is no operation %r" % name)
    operations = set(
        [op for op in operations if len(op.parameters) == len(args)])
    if not operations:
        raise InvalidOperation("There is no operation %r with %d arguments" %
                               (name, len(args)))

    # Loop on arguments
    for i, actual in enumerate(args):
        retained_operations = set()
        current_score = sys.maxint
        # All base classes
        bases = parent_modules(actual.module)
        for op in operations:
            for desc in op.parameters[i].types:
                expected = desc.module
                # Score of this operation for this argument
                try:
                    score = bases[expected]
                except KeyError:
                    pass  # This operation is not compatible with the argument
                else:
                    if score < current_score:
                        # Forget the previous operations, this one is better
                        # (i.e. the expected argument is more specific)
                        retained_operations = set([op])
                        current_score = score
                    elif score == current_score:
                        # This is as good as the other ones, add it to the list
                        # Their next argument will be examined
                        retained_operations.add(op)
                    # Else, not as good as the ones we have, discard

        operations = retained_operations
        if len(operations) == 0:
            break

    if len(operations) == 0:
        raise InvalidOperation("Found no match for operation %r with given "
                               "%d args" % (name, len(args)))
    if len(operations) > 1:
        warnings.warn(
            "Found several operations %r matching the given %d args" %
            (name, len(args)),
            category=OperationWarning)
    return next(iter(operations))
Ejemplo n.º 3
0
Archivo: parsing.py Proyecto: rbax/DAT
 def led(self, left, context):
     # Binary operator: corresponds to the function call contruct, as in
     # 2 * abc(7, 31) + 18
     params = []
     if left[0] != SYMBOL:
         raise InvalidOperation("Function call syntax only allowed on "
                                "symbols")
     if not isinstance(context.current_token, RightParen):
         while True:
             params.append(context.expression(self.rbp))
             if not isinstance(context.current_token, Comma):
                 break
             context.consume(expect_class=Comma)
     context.consume(RightParen)
     return (OP, left[1]) + tuple(params)
Ejemplo n.º 4
0
def perform_operation(expression, controller=None):
    """Perform a variable operation from the given string.
    """
    # First, parse the expressions
    target, expr_tree = parse_expression(expression)

    # Find the actual operations & variables
    controller, root_version, output_module_id = (
        Variable._get_variables_root(controller))
    vistraildata = VistrailManager(controller)
    if vistraildata.get_variable(target) is not None:
        raise InvalidOperation("Target variable %r already exists" % target)
    op_tree = resolve_symbols(vistraildata, expr_tree)

    # Build the new variable
    variable = op_tree.execute(controller)
    vistraildata.new_variable(target, variable)
Ejemplo n.º 5
0
def apply_operation(controller, op, args):
    """Apply an operation to build a new variable.

    Either load the subworkflow or wrap the parameter variables correctly and
    call the callback function.
    """
    if op.callback is not None:
        # FIXME : controller is ignored here...
        assert controller == VistrailManager().controller
        result = vistrails_interface.call_operation_callback(
            op, op.callback, args)
        if result is None:
            raise InvalidOperation("Package error: operation callback "
                                   "returned None")
    else:  # op.subworkflow is not None:
        result = vistrails_interface.apply_operation_subworkflow(
            controller, op, op.subworkflow, args)

    if result.provenance is None:
        result.provenance = data_provenance.Operation(operation=op,
                                                      arg_list=args)
    return result
Ejemplo n.º 6
0
 def __init__(self, vistraildata, varname):
     self._variable = vistraildata.get_variable(varname)
     if self._variable is None:
         raise InvalidOperation("Unknown variable %r" % varname)
     self.type = self._variable.type
Ejemplo n.º 7
0
Archivo: parsing.py Proyecto: rbax/DAT

def parse_expression(expression):
    equal = expression.find('=')
    if equal == -1:
        raise InvalidOperation("Missing target variable name",
                               "new_var = %s" % expression, (0, 7))
    else:
        target = expression[:equal].strip()
        if not _variable_format.match(target):
            right = equal
            if right > 0 and expression[right - 1] == ' ':
                right -= 1
            if iswhitespace(expression[0:right]):
                raise InvalidOperation("Missing target variable name",
                                       "new_var %s" % expression.lstrip(),
                                       (0, 7))
            else:
                raise InvalidOperation("Invalid target variable name", None,
                                       (0, right))
        expression = expression[equal + 1:]
    try:
        return target, lexer.parse(expression)
    except LexerError, e:
        raise InvalidOperation("Error while parsing expression",
                               None,
                               select=(equal + 1 + e.position,
                                       equal + 1 + len(expression)))
    except Error:
        raise InvalidOperation("Error while parsing expression")