def _find_imports(node: LN, capture: Capture, filename: Filename): if not is_import(node): return True if capture and 'name_import' in capture: paddle_imported.add(filename) paddle_found.add(filename) if capture and ('module_import' in capture or 'module_imports' in capture or 'module_nickname' in capture): paddle_found.add(filename) if filename not in imports_map: imports_map[filename] = {} if 'module_import' in capture: leaf = capture['module_import'] if leaf.type == token.NAME: old_name = leaf.value.strip() new_name = str( capture['module_name']).strip() + '.' + old_name imports_map[filename][old_name] = new_name if 'module_imports' in capture: for leaf in capture['module_imports']: if leaf.type == token.NAME: old_name = leaf.value.strip() new_name = str( capture['module_name']).strip() + '.' + old_name imports_map[filename][old_name] = new_name if 'module_nickname' in capture: old_name = str(capture['module_nickname']).strip() new_name = str(capture['module_name']).strip() imports_map[filename][old_name] = new_name return True
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
def convert_regex_to_path_modifier(node: Node, capture: Capture, filename: Filename) -> None: # Replace the import if is_import(node): name_leafs = [ Name("path", prefix=" "), Comma(), Name("re_path", prefix=" "), ] node.replace([FromImport("django.url", name_leafs=name_leafs)]) # And function calls from url to path, re_path if capture and "function_arguments" in capture: function_node: Node = next(node.leaves()) args = capture.get("function_arguments") regex_leaf: Leaf = next(args[0].leaves()) converted = update_regex_to_path(regex_leaf.value) if converted == regex_leaf.value: function_node.replace(Name("re_path", prefix=function_node.prefix)) else: function_node.replace(Name("path", prefix=function_node.prefix)) regex_leaf.value = update_regex_to_path(regex_leaf.value)