forked from py2many/py2many
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cross module: toposort modules before transpiling
This avoids referencing modules on which scopes haven't been computed.
- Loading branch information
Showing
5 changed files
with
76 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import ast | ||
from pathlib import Path | ||
from toposort import toposort_flatten | ||
from collections import defaultdict | ||
from typing import Tuple | ||
|
||
|
||
def module_for_path(path: Path) -> str: | ||
# strip out .py at the end | ||
return ".".join(path.parts)[:-3] | ||
|
||
|
||
class ImportDependencyVisitor(ast.NodeVisitor): | ||
def __init__(self, modules): | ||
self.deps = defaultdict(set) | ||
self._modules = modules | ||
|
||
def visit_Module(self, node): | ||
self._current = module_for_path(node.__file__) | ||
self.generic_visit(node) | ||
|
||
def visit_ImportFrom(self, node): | ||
if node.module in self._modules: | ||
self.deps[self._current].add(node.module) | ||
self.generic_visit(node) | ||
|
||
def visit_Import(self, node): | ||
names = [n.name for n in node.names] | ||
for n in names: | ||
if n in self._modules: | ||
self.deps[self._current].add(n) | ||
self.generic_visit(node) | ||
|
||
|
||
def get_dependencies(trees): | ||
modules = {module_for_path(node.__file__) for node in trees} | ||
visitor = ImportDependencyVisitor(modules) | ||
for t in trees: | ||
visitor.visit(t) | ||
for m in modules: | ||
if m not in visitor.deps: | ||
visitor.deps[m] = set() | ||
return visitor.deps | ||
|
||
|
||
def toposort(trees) -> Tuple: | ||
deps = get_dependencies(trees) | ||
tree_dict = {module_for_path(node.__file__): node for node in trees} | ||
return tuple([tree_dict[t] for t in toposort_flatten(deps, sort=True)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters