From 1d7cc009629fb918d9dcd4e8e3887bbb1bc3e90f Mon Sep 17 00:00:00 2001 From: Andy Kipp Date: Sat, 29 Jun 2024 10:56:50 +0200 Subject: [PATCH] refactoring: move shell to shells.shell to avoid unwanted init (#5556) Continue #5550 for https://github.com/xonsh/xonsh/issues/5538 --------- Co-authored-by: a <1@1.1> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Noorhteen Raja NJ --- docs/api/index.rst | 2 +- docs/conf.py | 2 +- pyproject.toml | 10 +++++----- tests/shell/test_base_shell.py | 6 +++--- tests/shell/test_ptk_completer.py | 2 +- tests/shell/test_ptk_history.py | 2 +- tests/shell/test_ptk_multiline.py | 12 ++++++------ tests/shell/test_ptk_shell.py | 10 +++++----- tests/shell/test_readline_shell.py | 2 +- tests/shell/test_shell.py | 4 ++-- xonsh/environ.py | 2 +- xonsh/main.py | 2 +- xonsh/pytest/plugin.py | 4 ++-- xonsh/pytest/tools.py | 2 +- xonsh/shells/__init__.py | 1 + xonsh/{shell => shells}/base_shell.py | 2 +- xonsh/{shell => shells}/dumb_shell.py | 2 +- xonsh/{shell => shells}/ptk_shell/__init__.py | 12 ++++++------ xonsh/{shell => shells}/ptk_shell/completer.py | 0 xonsh/{shell => shells}/ptk_shell/formatter.py | 2 +- xonsh/{shell => shells}/ptk_shell/history.py | 0 xonsh/{shell => shells}/ptk_shell/key_bindings.py | 2 +- xonsh/{shell => shells}/ptk_shell/updator.py | 4 ++-- xonsh/{shell => shells}/readline_shell.py | 2 +- xonsh/{shell/__init__.py => shells/shell.py} | 8 ++++---- xonsh/tools.py | 2 +- 26 files changed, 50 insertions(+), 49 deletions(-) create mode 100644 xonsh/shells/__init__.py rename xonsh/{shell => shells}/base_shell.py (99%) rename xonsh/{shell => shells}/dumb_shell.py (86%) rename xonsh/{shell => shells}/ptk_shell/__init__.py (98%) rename xonsh/{shell => shells}/ptk_shell/completer.py (100%) rename xonsh/{shell => shells}/ptk_shell/formatter.py (96%) rename xonsh/{shell => shells}/ptk_shell/history.py (100%) rename xonsh/{shell => shells}/ptk_shell/key_bindings.py (99%) rename xonsh/{shell => shells}/ptk_shell/updator.py (98%) rename xonsh/{shell => shells}/readline_shell.py (99%) rename xonsh/{shell/__init__.py => shells/shell.py} (96%) diff --git a/docs/api/index.rst b/docs/api/index.rst index f2e56c37a..475bd2564 100644 --- a/docs/api/index.rst +++ b/docs/api/index.rst @@ -37,7 +37,7 @@ For those of you who want the gritty details. xonsh.completer xonsh.completers xonsh.prompt - xonsh.shell + xonsh.shells xonsh.base_shell xonsh.readline_shell xonsh.ptk_shell diff --git a/docs/conf.py b/docs/conf.py index 2523b3c15..308a69f73 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -35,7 +35,7 @@ xmain.setup() spec = importlib.util.find_spec("prompt_toolkit") if spec is not None: # hacky runaround to import PTK-specific events - from xonsh.shell.ptk_shell import events + from xonsh.shells.ptk_shell import events else: from xonsh.events import events diff --git a/pyproject.toml b/pyproject.toml index e4673d6e7..d8fb26d50 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,9 +26,9 @@ readme = {file = ["README.rst"]} [tool.setuptools] packages = [ "xonsh", + "xonsh.shells", + "xonsh.shells.ptk_shell", "xonsh.parsers.ply", - "xonsh.shell", - "xonsh.shell.ptk_shell", "xonsh.procs", "xonsh.platform", "xonsh.parsers", @@ -243,11 +243,11 @@ convention = "numpy" "xonsh/style_tools.py" = ["F821"] "xonsh/xoreutils/*.py" = ["E722"] "xonsh/completers/python.py" = ["E722"] +"xonsh/shells/ptk_shell/__init__.py" = ["E731"] +"xonsh/shells/readline_shell.py" = ["F401"] "xonsh/parsers/ast.py" = ["F401"] -"xonsh/shell/ptk_shell/__init__.py" = ["E731"] -"xonsh/shell/readline_shell.py" = ["F401"] "xonsh/commands_cache.py" = ["F841"] -"xonsh/shell/ptk_shell/key_bindings.py" = ["F841"] +"xonsh/shells/ptk_shell/key_bindings.py" = ["F841"] "xonsh/tools.py" = [ "E731", ] diff --git a/tests/shell/test_base_shell.py b/tests/shell/test_base_shell.py index bf24aa4f2..139c2d6f7 100644 --- a/tests/shell/test_base_shell.py +++ b/tests/shell/test_base_shell.py @@ -1,11 +1,11 @@ -"""(A down payment on) Testing for ``xonsh.shell.base_shell.BaseShell`` and associated classes""" +"""(A down payment on) Testing for ``xonsh.shells.base_shell.BaseShell`` and associated classes""" import os import pytest -from xonsh.shell import transform_command -from xonsh.shell.base_shell import BaseShell +from xonsh.shells.base_shell import BaseShell +from xonsh.shells.shell import transform_command def test_pwd_tracks_cwd(xession, xonsh_execer, tmpdir_factory, monkeypatch): diff --git a/tests/shell/test_ptk_completer.py b/tests/shell/test_ptk_completer.py index 77959e154..75e66e3ab 100644 --- a/tests/shell/test_ptk_completer.py +++ b/tests/shell/test_ptk_completer.py @@ -8,7 +8,7 @@ from prompt_toolkit.document import Document from xonsh.aliases import Aliases from xonsh.completer import Completer from xonsh.completers.tools import RichCompletion -from xonsh.shell.ptk_shell.completer import PromptToolkitCompleter +from xonsh.shells.ptk_shell.completer import PromptToolkitCompleter @pytest.mark.parametrize( diff --git a/tests/shell/test_ptk_history.py b/tests/shell/test_ptk_history.py index 804dcb7a7..664219ab4 100644 --- a/tests/shell/test_ptk_history.py +++ b/tests/shell/test_ptk_history.py @@ -9,7 +9,7 @@ except ImportError: @pytest.fixture def history_obj(): """Instantiate `PromptToolkitHistory` and append a line string""" - from xonsh.shell.ptk_shell.history import PromptToolkitHistory + from xonsh.shells.ptk_shell.history import PromptToolkitHistory hist = PromptToolkitHistory(load_prev=False) hist.append_string("line10") diff --git a/tests/shell/test_ptk_multiline.py b/tests/shell/test_ptk_multiline.py index e01cee27c..ba629f5ac 100644 --- a/tests/shell/test_ptk_multiline.py +++ b/tests/shell/test_ptk_multiline.py @@ -17,7 +17,7 @@ Context = namedtuple("Context", ["indent", "buffer", "accept", "cli", "cr"]) def ctx(xession): """Context in which the ptk multiline functionality will be tested.""" xession.env["INDENT"] = " " - from xonsh.shell.ptk_shell.key_bindings import carriage_return + from xonsh.shells.ptk_shell.key_bindings import carriage_return ptk_buffer = Buffer() ptk_buffer.accept_action = MagicMock(name="accept") @@ -53,14 +53,14 @@ def test_dedent(ctx): def test_nodedent(ctx): """don't dedent if first line of ctx.buffer""" mock = MagicMock(return_value=True) - with patch("xonsh.shell.ptk_shell.key_bindings.can_compile", mock): + with patch("xonsh.shells.ptk_shell.key_bindings.can_compile", mock): document = Document("pass") ctx.buffer.set_document(document) ctx.cr(ctx.buffer, ctx.cli) assert ctx.accept.mock_calls is not None mock = MagicMock(return_value=True) - with patch("xonsh.shell.ptk_shell.key_bindings.can_compile", mock): + with patch("xonsh.shells.ptk_shell.key_bindings.can_compile", mock): document = Document(ctx.indent + "pass") ctx.buffer.set_document(document) ctx.cr(ctx.buffer, ctx.cli) @@ -76,7 +76,7 @@ def test_continuation_line(ctx): def test_trailing_slash(ctx): mock = MagicMock(return_value=True) - with patch("xonsh.shell.ptk_shell.key_bindings.can_compile", mock): + with patch("xonsh.shells.ptk_shell.key_bindings.can_compile", mock): document = Document("this line will \\") ctx.buffer.set_document(document) ctx.cr(ctx.buffer, ctx.cli) @@ -88,7 +88,7 @@ def test_trailing_slash(ctx): def test_cant_compile_newline(ctx): mock = MagicMock(return_value=False) - with patch("xonsh.shell.ptk_shell.key_bindings.can_compile", mock): + with patch("xonsh.shells.ptk_shell.key_bindings.can_compile", mock): document = Document("for i in (1, 2, ") ctx.buffer.set_document(document) ctx.cr(ctx.buffer, ctx.cli) @@ -97,7 +97,7 @@ def test_cant_compile_newline(ctx): def test_can_compile_and_executes(ctx): mock = MagicMock(return_value=True) - with patch("xonsh.shell.ptk_shell.key_bindings.can_compile", mock): + with patch("xonsh.shells.ptk_shell.key_bindings.can_compile", mock): document = Document("ls") ctx.buffer.set_document(document) ctx.cr(ctx.buffer, ctx.cli) diff --git a/tests/shell/test_ptk_shell.py b/tests/shell/test_ptk_shell.py index 3c9917a1f..0edd76c70 100644 --- a/tests/shell/test_ptk_shell.py +++ b/tests/shell/test_ptk_shell.py @@ -6,8 +6,8 @@ import pyte import pytest from xonsh.platform import minimum_required_ptk_version -from xonsh.shell import Shell -from xonsh.shell.ptk_shell import tokenize_ansi +from xonsh.shells.ptk_shell import tokenize_ansi +from xonsh.shells.shell import Shell # verify error if ptk not installed or below min @@ -51,13 +51,13 @@ def test_prompt_toolkit_version_checks( return ptk_ver is not None monkeypatch.setattr( - "xonsh.shell.warnings.warn", mock_warning + "xonsh.shells.shell.warnings.warn", mock_warning ) # hardwon: patch the caller! monkeypatch.setattr( - "xonsh.shell.ptk_above_min_supported", mock_ptk_above_min_supported + "xonsh.shells.shell.ptk_above_min_supported", mock_ptk_above_min_supported ) # have to patch both callers monkeypatch.setattr( - "xonsh.platform.ptk_above_min_supported", mock_ptk_above_min_supported + "xonsh.shells.shell.ptk_above_min_supported", mock_ptk_above_min_supported ) monkeypatch.setattr("xonsh.platform.has_prompt_toolkit", mock_has_prompt_toolkit) diff --git a/tests/shell/test_readline_shell.py b/tests/shell/test_readline_shell.py index 65028686f..ece2ec3bc 100644 --- a/tests/shell/test_readline_shell.py +++ b/tests/shell/test_readline_shell.py @@ -1,7 +1,7 @@ import pytest from xonsh.completers.tools import RichCompletion -from xonsh.shell.readline_shell import _render_completions +from xonsh.shells.readline_shell import _render_completions @pytest.mark.parametrize( diff --git a/tests/shell/test_shell.py b/tests/shell/test_shell.py index 98a6a176d..a15c480f1 100644 --- a/tests/shell/test_shell.py +++ b/tests/shell/test_shell.py @@ -1,11 +1,11 @@ -"""Testing for ``xonsh.shell.Shell``""" +"""Testing for ``xonsh.shells.Shell``""" import os from xonsh.history.dummy import DummyHistory from xonsh.history.json import JsonHistory from xonsh.history.sqlite import SqliteHistory -from xonsh.shell import Shell +from xonsh.shells.shell import Shell def test_shell_with_json_history(xession, xonsh_execer, tmpdir_factory): diff --git a/xonsh/environ.py b/xonsh/environ.py index 330f6fa22..f2b5403c6 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -1535,7 +1535,7 @@ class PromptSetting(Xettings): "`prompt_toolkit `_" " library installed. To specify which shell should be used, do so in " "the run control file. " - "It also accepts a class type that inherits from ``xonsh.shell.base_shell.BaseShell``", + "It also accepts a class type that inherits from ``xonsh.shells.base_shell.BaseShell``", doc_default="``best``", ) SUGGEST_COMMANDS = Var.with_default( diff --git a/xonsh/main.py b/xonsh/main.py index 2ec3c9bd8..d5fd9fb46 100644 --- a/xonsh/main.py +++ b/xonsh/main.py @@ -22,7 +22,7 @@ from xonsh.lazyimps import pyghooks, pygments from xonsh.lib.pretty import pretty from xonsh.platform import HAS_PYGMENTS, ON_WINDOWS from xonsh.procs.jobs import ignore_sigtstp -from xonsh.shell import Shell +from xonsh.shells.shell import Shell from xonsh.timings import setup_timings from xonsh.tools import ( display_error_message, diff --git a/xonsh/pytest/plugin.py b/xonsh/pytest/plugin.py index 8ef9d584d..91be2de2f 100644 --- a/xonsh/pytest/plugin.py +++ b/xonsh/pytest/plugin.py @@ -363,7 +363,7 @@ def ptk_shell(xonsh_execer): from prompt_toolkit.input import create_pipe_input from prompt_toolkit.output import DummyOutput - from xonsh.shell.ptk_shell import PromptToolkitShell + from xonsh.shells.ptk_shell import PromptToolkitShell out = DummyOutput() with create_pipe_input() as inp: @@ -375,7 +375,7 @@ def ptk_shell(xonsh_execer): @pytest.fixture def readline_shell(xonsh_execer, tmpdir, mocker): - from xonsh.shell.readline_shell import ReadlineShell + from xonsh.shells.readline_shell import ReadlineShell inp_path = tmpdir / "in" inp = inp_path.open("w+") diff --git a/xonsh/pytest/tools.py b/xonsh/pytest/tools.py index fe16a1cc7..63caa19a8 100644 --- a/xonsh/pytest/tools.py +++ b/xonsh/pytest/tools.py @@ -12,7 +12,7 @@ from collections import defaultdict import pytest -from xonsh.shell.base_shell import BaseShell +from xonsh.shells.base_shell import BaseShell VER_MAJOR_MINOR = sys.version_info[:2] VER_FULL = sys.version_info[:3] diff --git a/xonsh/shells/__init__.py b/xonsh/shells/__init__.py new file mode 100644 index 000000000..007bcc2fd --- /dev/null +++ b/xonsh/shells/__init__.py @@ -0,0 +1 @@ +"""The xonsh interactive shells""" diff --git a/xonsh/shell/base_shell.py b/xonsh/shells/base_shell.py similarity index 99% rename from xonsh/shell/base_shell.py rename to xonsh/shells/base_shell.py index 23659a342..1476605ac 100644 --- a/xonsh/shell/base_shell.py +++ b/xonsh/shells/base_shell.py @@ -20,7 +20,7 @@ from xonsh.events import events from xonsh.lazyimps import pyghooks, pygments from xonsh.platform import HAS_PYGMENTS, ON_WINDOWS from xonsh.prompt.base import PromptFormatter, multiline_prompt -from xonsh.shell import transform_command +from xonsh.shells.shell import transform_command from xonsh.tools import ( DefaultNotGiven, XonshError, diff --git a/xonsh/shell/dumb_shell.py b/xonsh/shells/dumb_shell.py similarity index 86% rename from xonsh/shell/dumb_shell.py rename to xonsh/shells/dumb_shell.py index 8d11b85f9..514126417 100644 --- a/xonsh/shell/dumb_shell.py +++ b/xonsh/shells/dumb_shell.py @@ -1,7 +1,7 @@ """A dumb shell for when $TERM == 'dumb', which usually happens in emacs.""" from xonsh.built_ins import XSH -from xonsh.shell.readline_shell import ReadlineShell +from xonsh.shells.readline_shell import ReadlineShell class DumbShell(ReadlineShell): diff --git a/xonsh/shell/ptk_shell/__init__.py b/xonsh/shells/ptk_shell/__init__.py similarity index 98% rename from xonsh/shell/ptk_shell/__init__.py rename to xonsh/shells/ptk_shell/__init__.py index a92552fe3..ea541bb2d 100644 --- a/xonsh/shell/ptk_shell/__init__.py +++ b/xonsh/shells/ptk_shell/__init__.py @@ -29,12 +29,12 @@ from xonsh.events import events from xonsh.lazyimps import pyghooks, pygments, winutils from xonsh.platform import HAS_PYGMENTS, ON_POSIX, ON_WINDOWS from xonsh.pygments_cache import get_all_styles -from xonsh.shell import transform_command -from xonsh.shell.base_shell import BaseShell -from xonsh.shell.ptk_shell.completer import PromptToolkitCompleter -from xonsh.shell.ptk_shell.formatter import PTKPromptFormatter -from xonsh.shell.ptk_shell.history import PromptToolkitHistory, _cust_history_matches -from xonsh.shell.ptk_shell.key_bindings import load_xonsh_bindings +from xonsh.shells.base_shell import BaseShell +from xonsh.shells.ptk_shell.completer import PromptToolkitCompleter +from xonsh.shells.ptk_shell.formatter import PTKPromptFormatter +from xonsh.shells.ptk_shell.history import PromptToolkitHistory, _cust_history_matches +from xonsh.shells.ptk_shell.key_bindings import load_xonsh_bindings +from xonsh.shells.shell import transform_command from xonsh.style_tools import DEFAULT_STYLE_DICT, _TokenType, partial_color_tokenize from xonsh.tools import carriage_return, print_exception, print_warning diff --git a/xonsh/shell/ptk_shell/completer.py b/xonsh/shells/ptk_shell/completer.py similarity index 100% rename from xonsh/shell/ptk_shell/completer.py rename to xonsh/shells/ptk_shell/completer.py diff --git a/xonsh/shell/ptk_shell/formatter.py b/xonsh/shells/ptk_shell/formatter.py similarity index 96% rename from xonsh/shell/ptk_shell/formatter.py rename to xonsh/shells/ptk_shell/formatter.py index c6d1e2dc5..adfac08f6 100644 --- a/xonsh/shell/ptk_shell/formatter.py +++ b/xonsh/shells/ptk_shell/formatter.py @@ -4,7 +4,7 @@ import functools import typing as tp from xonsh.prompt.base import DEFAULT_PROMPT, PromptFormatter -from xonsh.shell.ptk_shell.updator import AsyncPrompt, PromptUpdator +from xonsh.shells.ptk_shell.updator import AsyncPrompt, PromptUpdator class PTKPromptFormatter(PromptFormatter): diff --git a/xonsh/shell/ptk_shell/history.py b/xonsh/shells/ptk_shell/history.py similarity index 100% rename from xonsh/shell/ptk_shell/history.py rename to xonsh/shells/ptk_shell/history.py diff --git a/xonsh/shell/ptk_shell/key_bindings.py b/xonsh/shells/ptk_shell/key_bindings.py similarity index 99% rename from xonsh/shell/ptk_shell/key_bindings.py rename to xonsh/shells/ptk_shell/key_bindings.py index 96f29411f..a199c7b5c 100644 --- a/xonsh/shell/ptk_shell/key_bindings.py +++ b/xonsh/shells/ptk_shell/key_bindings.py @@ -19,7 +19,7 @@ from prompt_toolkit.keys import Keys from xonsh.aliases import xonsh_exit from xonsh.built_ins import XSH from xonsh.platform import ON_WINDOWS -from xonsh.shell import transform_command +from xonsh.shells.shell import transform_command from xonsh.tools import ( check_for_partial_string, ends_with_colon_token, diff --git a/xonsh/shell/ptk_shell/updator.py b/xonsh/shells/ptk_shell/updator.py similarity index 98% rename from xonsh/shell/ptk_shell/updator.py rename to xonsh/shells/ptk_shell/updator.py index 6745caea5..272fa8bc3 100644 --- a/xonsh/shell/ptk_shell/updator.py +++ b/xonsh/shells/ptk_shell/updator.py @@ -119,7 +119,7 @@ class AsyncPrompt: """Create a timer to update the prompt. The timing can be configured through env variables. threading.Timer is used to stop calling invalidate frequently. """ - from xonsh.shell.ptk_shell import tokenize_ansi + from xonsh.shells.ptk_shell import tokenize_ansi if self.timer: self.timer.cancel() @@ -158,7 +158,7 @@ class PromptUpdator: """Handle updating multiple AsyncPrompt instances prompt/rprompt/bottom_toolbar""" def __init__(self, shell): - from xonsh.shell.ptk_shell import PromptToolkitShell + from xonsh.shells.ptk_shell import PromptToolkitShell self.prompts: dict[str, AsyncPrompt] = {} self.shell: PromptToolkitShell = shell diff --git a/xonsh/shell/readline_shell.py b/xonsh/shells/readline_shell.py similarity index 99% rename from xonsh/shell/readline_shell.py rename to xonsh/shells/readline_shell.py index 13493eb79..77e4bbecc 100644 --- a/xonsh/shell/readline_shell.py +++ b/xonsh/shells/readline_shell.py @@ -39,7 +39,7 @@ from xonsh.platform import ( os_environ, ) from xonsh.prompt.base import multiline_prompt -from xonsh.shell.base_shell import BaseShell +from xonsh.shells.base_shell import BaseShell from xonsh.tools import ( carriage_return, columnize, diff --git a/xonsh/shell/__init__.py b/xonsh/shells/shell.py similarity index 96% rename from xonsh/shell/__init__.py rename to xonsh/shells/shell.py index 0e08617c9..bb8117f94 100644 --- a/xonsh/shell/__init__.py +++ b/xonsh/shells/shell.py @@ -213,13 +213,13 @@ class Shell: ) if backend == "none": - from xonsh.shell.base_shell import BaseShell as cls + from xonsh.shells.base_shell import BaseShell as cls elif backend == "prompt_toolkit" or is_stdin_to_interactive: - from xonsh.shell.ptk_shell import PromptToolkitShell as cls + from xonsh.shells.ptk_shell import PromptToolkitShell as cls elif backend == "readline": - from xonsh.shell.readline_shell import ReadlineShell as cls + from xonsh.shells.readline_shell import ReadlineShell as cls elif backend == "dumb": - from xonsh.shell.dumb_shell import DumbShell as cls + from xonsh.shells.dumb_shell import DumbShell as cls else: raise XonshError(f"{backend} is not recognized as a shell type") return cls(**kwargs) diff --git a/xonsh/tools.py b/xonsh/tools.py index 59c68e121..6041bc187 100644 --- a/xonsh/tools.py +++ b/xonsh/tools.py @@ -1824,7 +1824,7 @@ def to_completion_mode(x): def is_tok_color_dict(x): from pygments.token import _TokenType, string_to_tokentype - from xonsh.shell.ptk_shell import _style_from_pygments_dict + from xonsh.shells.ptk_shell import _style_from_pygments_dict """Tests if something is a Token:Style dictionary""" if not isinstance(x, dict):