Skip to content

Commit

Permalink
Utils: Move offer_missing_install to libs/installation.py
Browse files Browse the repository at this point in the history
  • Loading branch information
mawildoer committed Sep 5, 2024
1 parent e263abb commit 37622b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
3 changes: 0 additions & 3 deletions src/faebryk/exporters/visualize/interactive_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@
from faebryk.core.node import Node
from faebryk.exporters.visualize.util import (
generate_pastel_palette,
offer_missing_install,
)
from faebryk.libs.util import FuncSet


def interactive_graph(G: Graph):
offer_missing_install("dash_cytoscape")
offer_missing_install("dash")
import dash_cytoscape as cyto
from dash import Dash, html

Expand Down
45 changes: 0 additions & 45 deletions src/faebryk/exporters/visualize/util.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,4 @@
import colorsys
import importlib
import subprocess
import sys
from types import ModuleType

from rich.prompt import Confirm


class InstallationError(Exception):
"""Raised when there's a problem installing a module."""


def offer_install(module_name, install_name=None, ex=None) -> ModuleType | None:
"""
Offer to install a missing module using pip.
"""
cmd = [sys.executable, "-m", "pip", "install", install_name or module_name]

print(f"The module '{module_name}' is not installed.")

if Confirm.ask(
f"Do you want to run the install command [cyan mono]`{' '.join(cmd)}`[/]"
):
try:
# Attempt to install the module using pip
subprocess.check_call(cmd)

except subprocess.CalledProcessError:
print(f"Failed to install {module_name}. Please install it manually.")
raise ex or InstallationError(f"Failed to install {module_name}")

print(f"Successfully installed {module_name}")
return importlib.import_module(module_name)


def offer_missing_install(
module_name: str, install_name: str = None
) -> ModuleType | None:
"""
Offer to install a missing module using pip.
"""
try:
return importlib.import_module(module_name)
except ModuleNotFoundError:
return offer_install(module_name, install_name)


def generate_pastel_palette(num_colors: int) -> list[str]:
Expand Down
46 changes: 46 additions & 0 deletions src/faebryk/libs/installation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import importlib
import subprocess
import sys
from types import ModuleType

from rich.prompt import Confirm


class InstallationError(Exception):
"""Raised when there's a problem installing a module."""


def offer_install(module_name, install_name=None, ex=None) -> ModuleType | None:
"""
Offer to install a missing module using pip.
"""
cmd = [sys.executable, "-m", "pip", "install", install_name or module_name]

print(f"The module '{module_name}' is not installed.")

if Confirm.ask(
f"Do you want to run the install command [cyan mono]`{' '.join(cmd)}`[/]"
):
try:
# Attempt to install the module using pip
subprocess.check_call(cmd)

except subprocess.CalledProcessError:
print(f"Failed to install {module_name}. Please install it manually.")
raise ex or InstallationError(f"Failed to install {module_name}")

print(f"Successfully installed {module_name}")
return importlib.import_module(module_name)


def offer_missing_install(
module_name: str, install_name: str = None
) -> ModuleType | None:
"""
Offer to install a missing module using pip.
"""
try:
return importlib.import_module(module_name)
except ModuleNotFoundError:
return offer_install(module_name, install_name)

0 comments on commit 37622b7

Please sign in to comment.