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)
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)))
def _add_import(node: LN, capture: Capture, filename: Filename): if node.type != python_symbols.file_input: return if filename in paddle_imported: return if filename in paddle_found: touch_import(None, 'paddle', node, force=True) log_info(filename, node.get_lineno(), 'add "import paddle"') paddle_imported.add(filename)
def _norm(node: LN, capture: Capture, filename: Filename): code = '' for leaf in node.leaves(): code = code + leaf.value found_alias = False alias = None for _alias in alias_map.keys(): if utils.startswith(code, _alias): found_alias = True alias = _alias break if not found_alias: return main_alias = alias_map[alias] update_to = change_spec[main_alias].get('update_to', None) # if main_alias contains "update_to" field, rename alias to "update_to" directly utils.replace_module_path(node, alias, main_alias) log_info(filename, node.get_lineno(), '{} -> {}'.format(alias, main_alias))