Пример #1
0
    def do_validation(self, validator_dict):
        """ validate with functions
        """
        comparator = utils.get_uniform_comparator(validator_dict["comparator"])
        validate_func = self.testcase_parser.get_bind_item(
            "function", comparator)

        if not validate_func:
            raise exception.FunctionNotFound(
                "comparator not found: {}".format(comparator))

        check_item = validator_dict["check"]
        check_value = validator_dict["check_value"]
        expect_value = validator_dict["expect"]

        if (check_value is None or expect_value is None) \
                and comparator not in ["is", "eq", "equals", "=="]:
            raise exception.ParamsError(
                "Null value can only be compared with comparator: eq/equals/=="
            )

        try:
            validate_func(validator_dict["check_value"],
                          validator_dict["expect"])
        except (AssertionError, TypeError):
            err_msg = "\n" + "\n".join([
                "\tcheck item name: %s;" % check_item,
                "\tcheck item value: %s (%s);" %
                (check_value, type(check_value).__name__),
                "\tcomparator: %s;" % comparator,
                "\texpected value: %s (%s)." %
                (expect_value, type(expect_value).__name__)
            ])
            raise exception.ValidationError(err_msg)
Пример #2
0
def search_conf_item(start_path, item_type, item_name):
    """ search expected function or variable recursive upward
    @param
        start_path: search start path
        item_type: "function" or "variable"
        item_name: function name or variable name
    """
    dir_path = os.path.dirname(os.path.abspath(start_path))
    target_file = os.path.join(dir_path, "debugtalk.py")

    if os.path.isfile(target_file):
        imported_module = get_imported_module_from_file(target_file)
        items_dict = filter_module(imported_module, item_type)
        if item_name in items_dict:
            return items_dict[item_name]
        else:
            return search_conf_item(dir_path, item_type, item_name)

    if dir_path == start_path:
        # system root path
        err_msg = "{} not found in recursive upward path!".format(item_name)
        if item_type == "function":
            raise exception.FunctionNotFound(err_msg)
        else:
            raise exception.VariableNotFound(err_msg)

    return search_conf_item(dir_path, item_type, item_name)
Пример #3
0
def parse_function(content):
    """ parse function name and args from string content.
    @param (str) content
    @return (dict) function name and args

    e.g. func() => {'func_name': 'func', 'args': [], 'kwargs': {}}
         func(5) => {'func_name': 'func', 'args': [5], 'kwargs': {}}
         func(1, 2) => {'func_name': 'func', 'args': [1, 2], 'kwargs': {}}
         func(a=1, b=2) => {'func_name': 'func', 'args': [], 'kwargs': {'a': 1, 'b': 2}}
         func(1, 2, a=3, b=4) => {'func_name': 'func', 'args': [1, 2], 'kwargs': {'a':3, 'b':4}}
    """
    matched = function_regexp_compile.match(content)
    if not matched:
        raise exception.FunctionNotFound("{} not found!".format(content))

    function_meta = {"func_name": matched.group(1), "args": [], "kwargs": {}}

    args_str = matched.group(2).strip()
    if args_str == "":
        return function_meta

    args_list = args_str.split(',')
    for arg in args_list:
        arg = arg.strip()
        if '=' in arg:
            key, value = arg.split('=')
            function_meta["kwargs"][key.strip()] = parse_string_value(
                value.strip())
        else:
            function_meta["args"].append(parse_string_value(arg))

    return function_meta