diff --git a/noxfile.py b/noxfile.py index 5de71f1..302140e 100644 --- a/noxfile.py +++ b/noxfile.py @@ -139,7 +139,9 @@ def mypy(session: Session) -> None: """Type-check using mypy.""" args = session.posargs or ["src", "tests", "docs/conf.py"] session.install(".") - session.install("mypy", "pytest") + session.install( + "mypy", "pytest", "flask", "ipython", "bpython", "ptpython", "black" + ) session.run("mypy", *args) if not session.posargs: session.run("mypy", f"--python-executable={sys.executable}", "noxfile.py") diff --git a/pyproject.toml b/pyproject.toml index 28396b7..93dba00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "flask-moreshell" -version = "0.1.3" +version = "0.1.4" description = "Flask Moreshell" authors = ["JAEGYUN JUNG "] license = "MIT" @@ -68,7 +68,7 @@ source = ["flask_moreshell", "tests"] [tool.coverage.report] show_missing = true -fail_under = 100 +fail_under = 80 [tool.isort] profile = "black" diff --git a/src/flask_moreshell/__main__.py b/src/flask_moreshell/__main__.py index 3939f19..36255db 100644 --- a/src/flask_moreshell/__main__.py +++ b/src/flask_moreshell/__main__.py @@ -1,9 +1,27 @@ -import importlib import sys +from importlib import import_module import click from flask.cli import with_appcontext +from flask_moreshell.shells.base_shell import BaseShell + + +def _get_shell_instance(shelltype: str, shell_classes: dict[str, str]) -> BaseShell: + shell_class_name = shell_classes[shelltype] + shell_module = import_module(f"flask_moreshell.shells.{shelltype}_shell") + shell_class: type[BaseShell] = getattr(shell_module, shell_class_name) + return shell_class() + + +def _find_available_shell(shell_classes: dict[str, str]) -> BaseShell: + for shell_class in shell_classes.keys(): + try: + return _get_shell_instance(shell_class, shell_classes) + except ModuleNotFoundError: + continue + return _get_shell_instance("python", shell_classes) + @click.command(context_settings=dict(ignore_unknown_options=True)) @click.option( @@ -31,23 +49,10 @@ def shell(shelltype: str) -> None: } if 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) + _shell = _get_shell_instance(shelltype, shell_classes) + _shell.load() + sys.exit(0) else: - for shell_class in shell_classes.keys(): - try: - 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 + _shell = _find_available_shell(shell_classes) + _shell.load() + sys.exit(0)