How to use _import_names method in autotest

Best Python code snippet using autotest_python

code_imports_analyzer.py

Source:code_imports_analyzer.py Github

copy

Full Screen

1"""CodeImportsAnalyzer uses the ast module from Python's standard library2to get what modules are imported in given python files, then uses networkx to generate imports graph3"""4import ast5import asyncio6import aiohttp7import pybase648from .graph_analyzer import GraphAnalyzer9def construct_fetch_program_text_api_url(api_url):10 import os11 # to increase api rate limiting12 # https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting13 USER = os.environ.get("USER", "")14 PERSONAL_ACCESS_TOKEN = os.environ.get("PERSONAL_ACCESS_TOKEN", "")15 if USER and PERSONAL_ACCESS_TOKEN:16 protocol, api_url_components = api_url.split("://")17 new_api_url_components = f"{USER}:{PERSONAL_ACCESS_TOKEN}@{api_url_components}"18 return f"{protocol}://{new_api_url_components}"19 else:20 return api_url21async def get_program_text(session, python_file):22 # about Retry-After23 # https://docs.github.com/en/rest/guides/best-practices-for-integrators#dealing-with-secondary-rate-limits24 async with session.get(25 construct_fetch_program_text_api_url(python_file["url"]),26 headers={"Accept": "application/vnd.github.v3+json", "Retry-After": "5"},27 ) as response:28 if response.status == 200:29 data = await response.json()30 if data["encoding"] == "base64":31 return data["content"], python_file["path"]32 else:33 print(34 f"WARNING: {python_file['path']}'s encoding is {data['encoding']}, not base64"35 )36class CodeImportsAnalyzer:37 class _NodeVisitor(ast.NodeVisitor):38 def __init__(self, imports):39 self.imports = imports40 def visit_Import(self, node):41 for alias in node.names:42 self.imports[-1]["imports"].append(43 {"module": None, "name": alias.name, "level": -1}44 )45 self.generic_visit(node)46 def visit_ImportFrom(self, node):47 for alias in node.names:48 self.imports[-1]["imports"].append(49 {"module": node.module, "name": alias.name, "level": node.level}50 )51 self.generic_visit(node)52 def __init__(self, python_files):53 self.python_imports = []54 self.graph_analyzer = GraphAnalyzer(is_directed=True)55 self.python_files = python_files56 self._node_visitor = CodeImportsAnalyzer._NodeVisitor(self.python_imports)57 async def parse_python_files(self):58 async with aiohttp.ClientSession() as session:59 tasks = []60 for python_file in self.python_files:61 tasks.append(62 asyncio.ensure_future(get_program_text(session, python_file))63 )64 results = await asyncio.gather(*tasks)65 if results:66 for base64_program_text, python_file_path in results:67 if base64_program_text:68 self.python_imports += [69 {70 "file_name": python_file_path.split("/")[-1],71 "file_path": python_file_path,72 "imports": [],73 }74 ]75 program = pybase64.b64decode(base64_program_text)76 tree = ast.parse(program)77 self._node_visitor.visit(tree)78 def generate_imports_graph(self):79 # TODO: thought on how to improve the graph generation logic80 # generate a dictionary of lists data structure81 # generate a graph based on a dictionary of lists82 for python_import in self.python_imports:83 _nodes = python_import["file_path"].split("/")84 if len(_nodes):85 # generate graph based on file_path86 # node/edge relationship means file/folder structure87 if len(_nodes) > 1:88 # make last node and second last node as one node89 # to solve the issue of duplicated file names using only last node90 if len(_nodes) >= 3:91 _nodes[-2] = _nodes[-2] + "/" + _nodes[-1]92 del _nodes[-1]93 self.graph_analyzer.add_edges_from_nodes(_nodes)94 else:95 self.graph_analyzer.add_node(_nodes[0])96 # generate graph based on imported modules in each file97 if python_import["file_name"] != "__init__.py":98 for _import in python_import["imports"]:99 if _import["module"] is None:100 _import_names = _import["name"].split(".")101 _new_nodes = _import_names + [_nodes[-1]]102 self.graph_analyzer.add_edges_from_nodes(_new_nodes)103 else:104 _import_names = _import["module"].split(".") + [105 _import["name"]106 ]107 _new_nodes = _import_names + [_nodes[-1]]108 self.graph_analyzer.add_edges_from_nodes(_new_nodes)109 return self.graph_analyzer.graph110 def report(self):111 from pprint import pprint...

Full Screen

Full Screen

utils.py

Source:utils.py Github

copy

Full Screen

...6 def __init__(self):7 self.lines = []8 self.line_numbers = []9 def visit_Import(self, node):10 line = 'import {}'.format(self._import_names(node.names))11 self.line_numbers.append(node.lineno)12 self.lines.append(line)13 def visit_ImportFrom(self, node):14 line = 'from {}{} import {}'.format(15 node.level * '.',16 node.module or '',17 self._import_names(node.names))18 self.line_numbers.append(node.lineno)19 self.lines.append(line)20 def _import_names(self, names):21 return ', '.join(map(attrgetter('name'), names))22def extract_imports(source, verbose=True):23 tree = ast.parse(source)24 import_parser = ImportParser()25 import_parser.visit(tree)26 import_lines = []27 for line in import_parser.lines:28 if 'print_function' in line:29 import_lines.append(line + '\n')30 # skip imports for pycharm and eclipse31 elif '_pydev_' in line or 'java.lang' in line:32 continue33 else:34 import_lines.append('try:\n {}\nexcept:\n pass\n'.format(line))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful