diff --git a/pyproject.toml b/pyproject.toml index 9fb98c3..6950554 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "flask-moreshell" -version = "0.1.2" +version = "0.1.3" description = "Flask Moreshell" authors = ["JAEGYUN JUNG "] license = "MIT" diff --git a/src/flask_moreshell/__main__.py b/src/flask_moreshell/__main__.py index f474eaa..3939f19 100644 --- a/src/flask_moreshell/__main__.py +++ b/src/flask_moreshell/__main__.py @@ -1,21 +1,9 @@ +import importlib import sys import click from flask.cli import with_appcontext -from flask_moreshell.shells import BPythonShell -from flask_moreshell.shells import IpythonShell -from flask_moreshell.shells import PTPythonShell -from flask_moreshell.shells import PythonShell - - -shells = { - "ipython": IpythonShell, - "bpython": BPythonShell, - "ptpython": PTPythonShell, - "python": PythonShell, -} - @click.command(context_settings=dict(ignore_unknown_options=True)) @click.option( @@ -35,23 +23,31 @@ def shell(shelltype: str) -> None: :param shelltype: type of shell to use. """ + shell_classes = { + "ipython": "IPythonShell", + "bpython": "BPythonShell", + "ptpython": "PTPythonShell", + "python": "PythonShell", + } - def try_load_shell(_shelltype: str) -> None: - shell_class = shells.get(_shelltype) - assert shell_class is not None # noqa S101 - shell_class().load() # type: ignore - - # If the user specifies a shell type, try to load it if shelltype: - try_load_shell(shelltype) + shell_class = shell_classes[shelltype] + try: + shell_module = importlib.import_module( + f"flask_moreshell.shells.{shelltype}_shell" + ) + shell_class = getattr(shell_module, shell_class) + shell_class().load() + except ModuleNotFoundError as e: + print(e) else: - preferred_shells = ["ipython", "bpython", "ptpython", "python"] - for shelltype in preferred_shells: + for shell_class in shell_classes.keys(): try: - try_load_shell(shelltype) - break + shell_module = importlib.import_module( + f"flask_moreshell.shells.{shell_class}_shell" + ) + shell_class = getattr(shell_module, shell_classes[shell_class]) + shell_class().load() + sys.exit(0) except ModuleNotFoundError: continue - if not shelltype: - print("No shell type is installed or recognized on your system.") - sys.exit(1) diff --git a/src/flask_moreshell/shells/__init__.py b/src/flask_moreshell/shells/__init__.py index 2e83013..e69de29 100644 --- a/src/flask_moreshell/shells/__init__.py +++ b/src/flask_moreshell/shells/__init__.py @@ -1,14 +0,0 @@ -from .base_shell import BaseShell -from .bpython_shell import BPythonShell -from .ipython_shell import IpythonShell -from .ptpython_shell import PTPythonShell -from .python_shell import PythonShell - - -__all__ = [ - "BaseShell", - "BPythonShell", - "IpythonShell", - "PTPythonShell", - "PythonShell", -] diff --git a/src/flask_moreshell/shells/bpython_shell.py b/src/flask_moreshell/shells/bpython_shell.py index b14942d..9067e53 100644 --- a/src/flask_moreshell/shells/bpython_shell.py +++ b/src/flask_moreshell/shells/bpython_shell.py @@ -1,4 +1,4 @@ -from flask_moreshell.shells import BaseShell +from flask_moreshell.shells.base_shell import BaseShell try: diff --git a/src/flask_moreshell/shells/ipython_shell.py b/src/flask_moreshell/shells/ipython_shell.py index b2d72e5..78e2f07 100644 --- a/src/flask_moreshell/shells/ipython_shell.py +++ b/src/flask_moreshell/shells/ipython_shell.py @@ -1,8 +1,10 @@ from typing import Any +from typing import Optional +from typing import Union from flask.globals import current_app -from flask_moreshell.shells import BaseShell +from flask_moreshell.shells.base_shell import BaseShell try: @@ -13,7 +15,7 @@ raise ModuleNotFoundError("IPython is not installed on your system.") from e -class IpythonShell(BaseShell): +class IPythonShell(BaseShell): def get_shell_name(self) -> str: return "IPython" @@ -31,7 +33,7 @@ def load(self) -> None: ) @staticmethod - def _get_config() -> Any | None: + def _get_config() -> Union[Config, Optional[Any]]: if "IPYTHON_CONFIG" in current_app.config: return Config(current_app.config["IPYTHON_CONFIG"]) return load_default_config() # type: ignore diff --git a/src/flask_moreshell/shells/ptpython_shell.py b/src/flask_moreshell/shells/ptpython_shell.py index 7690a9c..160607a 100644 --- a/src/flask_moreshell/shells/ptpython_shell.py +++ b/src/flask_moreshell/shells/ptpython_shell.py @@ -1,9 +1,13 @@ import sys from importlib.metadata import version -from ptpython.repl import embed -from flask_moreshell.shells import BaseShell +try: + from ptpython.repl import embed +except ModuleNotFoundError as e: + raise ModuleNotFoundError("PTPython is not installed on your system.") from e + +from flask_moreshell.shells.base_shell import BaseShell class PTPythonShell(BaseShell): diff --git a/src/flask_moreshell/shells/python_shell.py b/src/flask_moreshell/shells/python_shell.py index 9ed285c..95f2ddc 100644 --- a/src/flask_moreshell/shells/python_shell.py +++ b/src/flask_moreshell/shells/python_shell.py @@ -1,7 +1,7 @@ import code import sys -from flask_moreshell.shells import BaseShell +from flask_moreshell.shells.base_shell import BaseShell class PythonShell(BaseShell): diff --git a/tests/shells/test_bpython_shell.py b/tests/shells/test_bpython_shell.py index 7a6ba96..9241040 100644 --- a/tests/shells/test_bpython_shell.py +++ b/tests/shells/test_bpython_shell.py @@ -2,7 +2,7 @@ import pytest -from flask_moreshell.shells import BPythonShell +from flask_moreshell.shells.bpython_shell import BPythonShell @pytest.fixture diff --git a/tests/shells/test_ipython_shell.py b/tests/shells/test_ipython_shell.py index 0e01c66..6ad992a 100644 --- a/tests/shells/test_ipython_shell.py +++ b/tests/shells/test_ipython_shell.py @@ -1,11 +1,11 @@ import pytest -from flask_moreshell.shells import IpythonShell +from flask_moreshell.shells.ipython_shell import IPythonShell @pytest.fixture def ipython_shell(): - return IpythonShell() + return IPythonShell() def test_get_shell_name(ipython_shell): diff --git a/tests/shells/test_ptpython_shell.py b/tests/shells/test_ptpython_shell.py index e41e565..f0f6e43 100644 --- a/tests/shells/test_ptpython_shell.py +++ b/tests/shells/test_ptpython_shell.py @@ -3,7 +3,7 @@ import pytest from flask import Flask -from flask_moreshell.shells import PTPythonShell +from flask_moreshell.shells.ptpython_shell import PTPythonShell @pytest.fixture