-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP] list_depends: allow to sort topologically
- Loading branch information
1 parent
c3ab1f8
commit 748eda0
Showing
4 changed files
with
64 additions
and
2 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,44 @@ | ||
from typing import Dict, Iterable, Set | ||
|
||
import typer | ||
from graphlib import TopologicalSorter | ||
|
||
from manifestoo_core.addons_set import AddonsSet | ||
|
||
from . import echo | ||
|
||
|
||
class AddonSorter: | ||
@staticmethod | ||
def from_name(name: str) -> "AddonSorter": | ||
if name == "alphabetical": | ||
return AddonSorterAlphabetical() | ||
elif name == "topological": | ||
return AddonSorterTopological() | ||
else: | ||
echo.error(f"Unknown sorter {name}", err=False) | ||
raise typer.Exit(1) | ||
|
||
def sort( | ||
self, addons_selection: Iterable[str], addon_set: AddonsSet | ||
) -> Iterable[str]: | ||
return addons_selection | ||
|
||
|
||
class AddonSorterAlphabetical(AddonSorter): | ||
def sort( | ||
self, addons_selection: Iterable[str], addon_set: AddonsSet | ||
) -> Iterable[str]: | ||
return sorted(addons_selection) | ||
|
||
|
||
class AddonSorterTopological(AddonSorter): | ||
def sort( | ||
self, addons_selection: Iterable[str], addon_set: AddonsSet | ||
) -> Iterable[str]: | ||
result_dict: Dict[str, Set[str]] = {} | ||
for addon_name in addons_selection: | ||
addon = addon_set.get(addon_name) | ||
result_dict[addon_name] = set(addon.manifest.depends) if addon else set() | ||
topological_sorted_res = TopologicalSorter(result_dict) | ||
return list(topological_sorted_res.static_order()) |
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