def _api_rename(node: LN, capture: Capture, filename: Filename): code = '' for leaf in node.leaves(): code = code + leaf.value found_rename = False found_warning = False api = None for _api in rename_map.keys(): if utils.startswith(code, _api): found_rename = True api = _api break for _api in warning_map.keys(): if utils.startswith(code, _api): found_warning = True api = _api break if not found_rename and not found_warning: return # if found rename, replace old_api with new_api if found_rename: utils.replace_module_path(node, api, rename_map[api]) # if not found rename and found warning, print warning elif found_warning: log_warning(filename, node.get_lineno(), warning_map[api])
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))