Exemple #1
0
    def _full_module_path(node: LN, capture: Capture, filename: Filename):
        if not (isinstance(node, Leaf) and node.type == token.NAME):
            return
        if filename not in imports_map:
            return
        logger.debug("{} [{}]: {}".format(filename, list(capture), node))

        # skip import statement
        if utils.is_import_node(node):
            return
        # skip left operand in argument list
        if utils.is_argument_node(node) and utils.is_left_operand(node):
            return
        # skip if it's already a full module path
        if node.prev_sibling is not None and node.prev_sibling.type == token.DOT:
            return

        rename_dict = imports_map[filename]
        if node.value in rename_dict:
            # find old_name and new_name
            old_name = node.value
            new_name = rename_dict[old_name]
            if node.parent is not None:
                _node = utils.code_repr(new_name).children[0].children[0]
                _node.parent = None
                new_node = _node
                new_node.children[0].prefix = node.prefix
                if node.parent.type == python_symbols.power:
                    node.replace(new_node.children)
                else:
                    node.replace(new_node)
                log_info(
                    filename, node.get_lineno(),
                    "{} -> {}".format(utils.node2code(node),
                                      utils.node2code(new_node)))
Exemple #2
0
 def _remove_import(node: LN, capture: Capture, filename: Filename):
     if not is_import(node):
         return
     _node = capture.get('as_import', None) or capture.get(
         'from_import', None)
     if _node is not None:
         prefix = _node.prefix
         p = _node.parent
         _node.remove()
         log_warning(filename, p.get_lineno(),
                     'remove "{}"'.format(utils.node2code(_node)))
         # delete NEWLINE node after delete as_import or from_import
         if p and p.children and len(
                 p.children) == 1 and p.children[0].type == token.NEWLINE:
             p.children[0].remove()
             # restore comment
             p.next_sibling.prefix = prefix + p.next_sibling.prefix
Exemple #3
0
    def _modify_args_to_kwargs(node, capture, filename):
        #get full api, e.g. paddle.fluid.layers.Layer
        api_name = utils.node2code(capture["api"]).strip()
        if api_name not in change_spec:
            return
        trailer_node = capture["trailer_node"]
        utils.norm_arglist(trailer_node)
        args_list = change_spec[api_name].get('args_list', None)

        encounter_kwarg = False
        idx = 0

        def _add_arg_name(argument_node):
            nonlocal encounter_kwarg
            nonlocal idx
            if args_list is None:
                return
            if encounter_kwarg:
                return
            if idx >= len(args_list):
                msg = 'args_list: "{}" is shorter than positional arguments.'.format(
                    args_list)
                log_error(filename, argument_node.get_lineno(), msg)
                return
            if len(argument_node.children) >= 3:
                encounter_kwarg = True
                msg = 'args_list: "{}" is longer than positional arguments, redundant arguments will be skipped.'.format(
                    args_list)
                log_info(filename, argument_node.get_lineno(), msg)
                return
            key = args_list[idx]
            argument_node.insert_child(0, Leaf(token.EQUAL, "="))
            argument_node.insert_child(0, Name(key))
            argument_node.children[0].prefix = argument_node.children[2].prefix
            argument_node.children[2].prefix = ""
            idx += 1
            msg = 'add argument name "{}" for {}-th argument.'.format(key, idx)
            log_debug(filename, argument_node.get_lineno(), msg)

        utils.apply_argument(filename, trailer_node, _add_arg_name)
Exemple #4
0
    def _refector_args(node: LN, capture: Capture, filename: Filename):
        #get full api, e.g. paddle.fluid.layers.Layer
        api_name = utils.node2code(capture["api"]).strip()
        if api_name not in change_spec:
            return
        trailer_node = capture["trailer_node"]
        utils.norm_arglist(trailer_node)
        args_change = change_spec[api_name].get('args_change', [])

        for change in args_change:
            # add new keyword argument
            if len(change) == 3:
                old_arg = change[0].strip()
                new_arg = change[1].strip()
                arg_val = change[2].strip()
                # old_arg is not empty, do nothing
                if old_arg != "" or new_arg == "":
                    logger.error(
                        'add argument error. api: "{}", args_change: "{}", format should be ["", "new_arg", "default_value"]'
                        .format(api_name, change))
                    continue

                utils.add_argument(filename, trailer_node, new_arg, arg_val)
            # delete or rename keyword argument
            elif len(change) == 2:
                old_arg = change[0].strip()
                new_arg = change[1].strip()
                if old_arg == "" and new_arg == "":
                    logger.error(
                        'api: "{}", args_change: "{}", format should be ["arg", ""] or ["old_arg", "new_arg"]'
                        .format(api_name, change))
                    continue

                if new_arg == '':
                    removed_value = utils.remove_argument(
                        filename, trailer_node, old_arg)
                    if old_arg == 'act' and removed_value is not None:
                        transformers.act_transformer(filename, trailer_node,
                                                     removed_value)
                else:
                    utils.rename_argument(filename, trailer_node, old_arg,
                                          new_arg)
            else:
                logger.error(
                    'api: "{}", args_change: "{}", format should be ["arg", ""] or ["old_arg", "new_arg"] or ["", "new_arg", "default_value"]'
                    .format(api_name, change))

        # if api in args_warning, print warning info
        args_warning = change_spec[api_name].get("args_warning", {})

        def _print_warning(argument_node):
            if argument_node.type != python_symbols.argument:
                return
            if len(argument_node.children) == 3:
                key = argument_node.children[0].value
                if key in args_warning:
                    warning_msg = args_warning[key]
                    log_warning(filename, argument_node.get_lineno(),
                                warning_msg)

        utils.apply_argument(filename, trailer_node, _print_warning)

        # run customized transformer
        if "args_transformer" in change_spec[api_name]:
            transformer_func = eval("transformers." +
                                    change_spec[api_name]["args_transformer"])
            transformer_func(node, capture, filename)