-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from TGoddessana/dev
Restructure the project
- Loading branch information
Showing
9 changed files
with
206 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
[flake8] | ||
select = B,B9,C,D,DAR,E,F,N,RST,W | ||
ignore = E203,E501,RST201,RST203,RST301,W503,D100,D104,C901,B904,B307 | ||
select = B,B9,C,D,DAR,E,F,N,RST,S,W | ||
ignore = E203,E501,RST201,RST203,RST301,W503,D100,D101,D102,D104 | ||
max-line-length = 80 | ||
max-complexity = 10 | ||
docstring-convention = google | ||
per-file-ignores = tests/*:S101 | ||
rst-roles = class,const,func,meth,mod,ref | ||
rst-directives = deprecated |
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,14 @@ | ||
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", | ||
] |
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,50 @@ | ||
import os | ||
import sys | ||
from abc import ABC | ||
from abc import abstractmethod | ||
|
||
from flask.globals import current_app | ||
|
||
|
||
class BaseShell(ABC): | ||
"""Base class for all shells. | ||
you can extend this class to implement your own shell. | ||
""" | ||
|
||
def get_banner(self) -> str: | ||
""" | ||
Return banner text to be displayed when shell starts. | ||
override this method to customize banner text. | ||
""" | ||
python_info = f"Python {sys.version} on {sys.platform}" | ||
shell_info = f"{self.get_shell_name()} {self.get_shell_version()}" | ||
app_info = ( | ||
f"App: {current_app.import_name} " | ||
f"[debug: {current_app.debug}, testing: {current_app.testing}]" | ||
) | ||
config = f"Config: {current_app.config_class.__name__}" | ||
return f"{python_info}\n{shell_info}\n{app_info}\n{config}" | ||
|
||
@staticmethod | ||
def load_context(ctx: dict) -> None: # type: ignore | ||
startup = os.environ.get("PYTHONSTARTUP") | ||
|
||
if startup and os.path.isfile(startup): | ||
with open(startup) as f: | ||
exec(f.read(), ctx) # noqa: S102 | ||
|
||
ctx.update(current_app.make_shell_context()) | ||
|
||
@abstractmethod | ||
def get_shell_name(self) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def get_shell_version(self) -> str: | ||
pass | ||
|
||
@abstractmethod | ||
def load(self) -> None: | ||
pass |
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,20 @@ | ||
from flask_moreshell.shells import BaseShell | ||
|
||
|
||
try: | ||
import bpython # type: ignore | ||
except ModuleNotFoundError as e: | ||
raise ModuleNotFoundError("BPython is not installed on your system.") from e | ||
|
||
|
||
class BPythonShell(BaseShell): | ||
def get_shell_name(self) -> str: | ||
return "BPython" | ||
|
||
def get_shell_version(self) -> str: | ||
return str(bpython.__version__) | ||
|
||
def load(self) -> None: | ||
ctx = {} # type: ignore | ||
self.load_context(ctx) | ||
bpython.embed(banner=self.get_banner(), locals_=ctx) |
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,37 @@ | ||
from typing import Any | ||
|
||
from flask.globals import current_app | ||
|
||
from flask_moreshell.shells import BaseShell | ||
|
||
|
||
try: | ||
import IPython | ||
from IPython.terminal.ipapp import load_default_config | ||
from traitlets.config.loader import Config | ||
except ModuleNotFoundError as e: | ||
raise ModuleNotFoundError("IPython is not installed on your system.") from e | ||
|
||
|
||
class IpythonShell(BaseShell): | ||
def get_shell_name(self) -> str: | ||
return "IPython" | ||
|
||
def get_shell_version(self) -> str: | ||
return IPython.__version__ | ||
|
||
def load(self) -> None: | ||
config = self._get_config() | ||
config.TerminalInteractiveShell.banner1 = self.get_banner() | ||
|
||
IPython.start_ipython( | ||
argv=[], | ||
user_ns=current_app.make_shell_context(), | ||
config=config, | ||
) | ||
|
||
@staticmethod | ||
def _get_config() -> Any | None: | ||
if "IPYTHON_CONFIG" in current_app.config: | ||
return Config(current_app.config["IPYTHON_CONFIG"]) | ||
return load_default_config() |
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,20 @@ | ||
import sys | ||
from importlib.metadata import version | ||
|
||
from ptpython.repl import embed | ||
|
||
from flask_moreshell.shells import BaseShell | ||
|
||
|
||
class PTPythonShell(BaseShell): | ||
def get_shell_name(self) -> str: | ||
return "PTPython" | ||
|
||
def get_shell_version(self) -> str: | ||
return version("ptpython") | ||
|
||
def load(self) -> None: | ||
ctx = {} # type: ignore | ||
self.load_context(ctx) | ||
sys.stdout.write(f"{self.get_banner()}\n") | ||
embed(globals=ctx) |
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,28 @@ | ||
import code | ||
import sys | ||
|
||
from flask_moreshell.shells import BaseShell | ||
|
||
|
||
class PythonShell(BaseShell): | ||
def get_shell_name(self) -> str: | ||
return "Python" | ||
|
||
def get_shell_version(self) -> str: | ||
return sys.version | ||
|
||
def load(self) -> None: | ||
ctx = {} # type: ignore | ||
self.load_context(ctx) | ||
|
||
interactive_hook = getattr(sys, "__interactivehook__", None) | ||
if interactive_hook is not None: | ||
try: | ||
import readline | ||
from rlcompleter import Completer | ||
except ImportError: | ||
pass | ||
else: | ||
readline.set_completer(Completer(ctx).complete) | ||
interactive_hook() | ||
code.interact(local=ctx, banner=self.get_banner()) |
Empty file.