Skip to content

Commit

Permalink
Remove dead code in _layers.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter554 committed Dec 14, 2023
1 parent e304e82 commit f1b008a
Showing 1 changed file with 0 additions and 191 deletions.
191 changes: 0 additions & 191 deletions src/grimp/adaptors/_layers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import copy
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Iterator, Sequence, TypedDict, Union

Expand Down Expand Up @@ -172,193 +171,3 @@ def _module_from_layer(layer: str, container: str | None = None) -> _Module:
else:
name = layer
return _Module(name)


def _search_for_package_dependency(
higher_layer_package: _Module,
lower_layer_package: _Module,
layers: Sequence[str],
container: str | None,
graph: ImportGraph,
) -> PackageDependency | None:
"""
Return a PackageDependency containing illegal chains between two layers, if they exist.
"""
temp_graph = copy.deepcopy(graph)
_remove_other_layers(
temp_graph,
layers=layers,
container=container,
layers_to_preserve=(higher_layer_package, lower_layer_package),
)
# Assemble direct imports between the layers, then remove them.
import_details_between_layers = _pop_direct_imports(
higher_layer_package=higher_layer_package,
lower_layer_package=lower_layer_package,
graph=temp_graph,
)
routes: set[Route] = set()

for import_details_list in import_details_between_layers:
any_element = tuple(import_details_list)[0]
routes.add(
Route(
heads=frozenset({any_element.importer}),
middle=(),
tails=frozenset({any_element.imported}),
)
)

indirect_routes = _get_indirect_routes(
temp_graph,
importer_package=lower_layer_package,
imported_package=higher_layer_package,
)

routes |= indirect_routes

if routes:
return PackageDependency(
importer=lower_layer_package.name,
imported=higher_layer_package.name,
routes=frozenset(routes),
)
else:
return None


def _get_indirect_routes(
graph: ImportGraph, importer_package: _Module, imported_package: _Module
) -> set[Route]:
"""
Squashes the two packages.
Gets a list of paths between them, called segments.
Add the heads and tails to the segments.
"""
temp_graph = copy.deepcopy(graph)

temp_graph.squash_module(importer_package.name)
temp_graph.squash_module(imported_package.name)

middles = _find_middles(
temp_graph,
importer=importer_package,
imported=imported_package,
)
return _middles_to_routes(graph, middles, importer=importer_package, imported=imported_package)


def _remove_other_layers(
graph: ImportGraph,
layers: Sequence[str],
container: str | None,
layers_to_preserve: tuple[_Module, ...],
) -> None:
for index, layer in enumerate(layers): # type: ignore
candidate_layer = _module_from_layer(layer, container)
if candidate_layer.name in graph.modules and candidate_layer not in layers_to_preserve:
_remove_layer(graph, layer_package=candidate_layer)


def _remove_layer(graph: ImportGraph, layer_package: _Module) -> None:
for module in graph.find_descendants(layer_package.name):
graph.remove_module(module)
graph.remove_module(layer_package.name)


def _pop_direct_imports(
higher_layer_package, lower_layer_package, graph: ImportGraph
) -> set[frozenset[_Link]]:
import_details_set: set[frozenset[_Link]] = set()

lower_layer_modules = {lower_layer_package.name} | graph.find_descendants(
lower_layer_package.name
)
for lower_layer_module in lower_layer_modules:
imported_modules = graph.find_modules_directly_imported_by(lower_layer_module).copy()
for imported_module in imported_modules:
if _Module(imported_module) == higher_layer_package or _Module(
imported_module
).is_descendant_of(higher_layer_package):
import_details = frozenset(
{
_Link(
importer=lower_layer_module,
imported=imported_module,
),
}
)
import_details_set.add(import_details)
graph.remove_import(importer=lower_layer_module, imported=imported_module)
return import_details_set


def _find_middles(graph: ImportGraph, importer: _Module, imported: _Module) -> set[_Chain]:
"""
Return set of headless and tailless chains.
"""
middles: set[_Chain] = set()

for chain in _pop_shortest_chains(graph, importer=importer.name, imported=imported.name):
if len(chain) == 2:
raise ValueError("Direct chain found - these should have been removed.")
middles.add(chain[1:-1])

return middles


def _middles_to_routes(
graph: ImportGraph, middles: set[_Chain], importer: _Module, imported: _Module
) -> set[Route]:
"""
Build a set of routes from the chains between one package and another.
The middles are the chains that exist from the importer package to
the importer package. This function works out the head and tail packages of
those chains by consulting the graph.
"""
routes: set[Route] = set()

for middle in middles:
heads: set[str] = set()
imported_module = middle[0]
candidate_modules = sorted(graph.find_modules_that_directly_import(imported_module))
for module in [
m
for m in candidate_modules
if _Module(m) == importer or _Module(m).is_descendant_of(importer)
]:
heads.add(module)

tails: set[str] = set()
importer_module = middle[-1]
candidate_modules = sorted(graph.find_modules_directly_imported_by(importer_module))
for module in [
m
for m in candidate_modules
if _Module(m) == imported or _Module(m).is_descendant_of(imported)
]:
tails.add(module)

routes.add(
Route(
heads=frozenset(heads),
middle=middle,
tails=frozenset(tails),
)
)

return routes


def _pop_shortest_chains(
graph: ImportGraph, importer: str, imported: str
) -> Iterator[tuple[str, ...]]:
chain: tuple[str, ...] | bool | None = True
while chain:
chain = graph.find_shortest_chain(importer, imported)
if chain:
# Remove chain of imports from graph.
for index in range(len(chain) - 1):
graph.remove_import(importer=chain[index], imported=chain[index + 1])
yield chain

0 comments on commit f1b008a

Please sign in to comment.