Skip to content
Snippets Groups Projects
Commit 37c1d10f authored by Andreas Gattringer's avatar Andreas Gattringer
Browse files

imports: also handle imports in top-level try...except blocks

parent 73df413f
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,25 @@ def get_module_names(node: ast.Import):
return names
def import_names(node):
result = []
if isinstance(node, ast.Import):
print("\t", ",".join(get_module_names(node)))
result.extend(get_module_names(node))
elif isinstance(node, ast.ImportFrom):
print("\t", node.module)
result.append(node.module)
if isinstance(node, ast.Try):
for child in ast.iter_child_nodes(node):
result.extend(import_names(child))
for handler in node.handlers:
for child in ast.iter_child_nodes(handler):
result.extend(import_names(child))
return result
def get_imported_files(
fn,
modules=None,
......@@ -22,12 +41,10 @@ def get_imported_files(
current_modules = []
with open(fn) as f:
print(f"Reading {fn}")
root = ast.parse(f.read(), fn)
for node in ast.iter_child_nodes(root):
if isinstance(node, ast.Import):
current_modules.extend(get_module_names(node))
elif isinstance(node, ast.ImportFrom):
current_modules.append(node.module)
current_modules.extend(import_names(node))
current_modules = sorted(list(set(current_modules)))
for module in current_modules:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment