def has_import_like(module_name, ast_tree): pat = prepare_pattern("import ??") matches = _get_matches(pat, ast_tree) if not matches: pat = prepare_pattern("from ?? import ??") matches = _get_matches(pat, ast_tree) if not matches: return False ret = False for match in matches: if isinstance(match, (ast.Import, ast.ImportFrom)): for name in match.names: if name.name.lower().startswith(module_name.lower()): return True # Repeat with from lookup if not ret: pat = prepare_pattern("from ?? import ??") matches = _get_matches(pat, ast_tree) for match in matches: if isinstance(match, ast.ImportFrom): if match.module.lower().startswith(module_name.lower()): return True for name in match.names: if name.name.lower().startswith(module_name.lower()): return True return ret
def has_import(module_name, ast_tree): pat = prepare_pattern(f"import {module_name}") matches = _get_matches(pat, ast_tree) if not matches: pat = prepare_pattern(f"from {module_name} import ??") matches = _get_matches(pat, ast_tree) if not matches: return False for match in matches: if isinstance(match, (ast.Import, ast.ImportFrom)): for name in match.names: if name.name.lower() == module_name.lower(): return True return False
def has_method_call(pattern, ast_tree): pat = prepare_pattern(pattern) node_list = _get_matches(pat, ast_tree) for node in node_list: if isinstance(node, ast.Call): return True return False
def get_assignments_as_dict(pattern, ast_tree): pat = prepare_pattern(pattern) node_list = _get_matches(pat, ast_tree) literals_dict = {} if not node_list: return literals_dict for node in node_list: if isinstance(node, ast.Assign): for target in node.targets: left_hand_side = target right_hand_side = node.value if isinstance(right_hand_side, ast.Call): right_hand_side = node.value.func if isinstance(right_hand_side, ast.Constant): right_hand_side = node.value if isinstance(target, ast.Subscript): left_hand_side = target.slice.value key = "" if hasattr(left_hand_side, "value"): key = left_hand_side.value elif hasattr(left_hand_side, "id"): key = left_hand_side.id if key: literals_dict[key] = { "left_hand_side": left_hand_side, "right_hand_side": right_hand_side, } elif isinstance(node, ast.Call): for keyword in node.keywords: if isinstance(keyword.value, ast.Constant): literals_dict[keyword.arg] = { "left_hand_side": keyword, "right_hand_side": keyword.value, } return literals_dict
def get_method_as_dict(pattern, ast_tree): pat = prepare_pattern(pattern) node_list = _get_matches(pat, ast_tree) if not node_list: return None invocations = [] for node in node_list: if isinstance(node, ast.Call): node_obj = ast2dict(node) invocations.append(node_obj) return invocations
def get_comparison_as_dict(pattern, ast_tree): pat = prepare_pattern(pattern) node_list = _get_matches(pat, ast_tree) literals_dict = {} for node in node_list: if isinstance(node, ast.Compare): left_hand_side = node.comparators[0] right_hand_side = node.comparators[-1] key = "" if isinstance(left_hand_side, ast.Name): key = left_hand_side.id elif isinstance(left_hand_side, ast.Attribute): key = left_hand_side.attr if key: literals_dict[key] = { "left_hand_side": left_hand_side, "right_hand_side": right_hand_side, } return literals_dict