Skip to content

Commit

Permalink
removed _types.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostOps77 committed Apr 14, 2024
1 parent 0a95035 commit f22d778
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 203 deletions.
10 changes: 10 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ Welcome to click-repl's Documentation

documentation/index

.. toctree::
:hidden:
:caption: Others

changelog.rst
:ref:`search`
:ref:`genindex`
:ref:`modindex`


.. include:: intro.rst

.. include:: changelog.rst
Expand Down
2 changes: 1 addition & 1 deletion src/click_repl/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from click.parser import _OptionParser as OptionParser # type:ignore
from click.parser import _ParsingState as ParsingState # type:ignore
from click.parser import _split_opt as split_opt # type:ignore
from click.shell_completion import split_arg_string
from click.shell_completion import split_arg_string # type:ignore

else:
from click.core import MultiCommand # type:ignore
Expand Down
37 changes: 23 additions & 14 deletions src/click_repl/_repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
import traceback
from contextlib import contextmanager
from typing import Any, Callable, Generator, Sequence, cast
from typing import Any, Callable, Generator, Sequence

import click
from click import Context
Expand All @@ -26,7 +26,6 @@
from .formatting import print_error
from .globals_ import DEFAULT_PROMPTSESSION_STYLE_CONFIG, ISATTY, get_current_repl_ctx
from .internal_commands import InternalCommandSystem
from .utils import _get_group_ctx
from .validator import ClickValidator

__all__ = ["Repl", "repl", "ReplCli"]
Expand All @@ -50,7 +49,7 @@ class Repl:
Parameters
----------
ctx
The "class:`~click.Context` object of the root/parent/CLI group.
The :class:`~click.Context` object of the root/parent/CLI group.
prompt_kwargs
Keyword arguments to be passed to the :class:`~prompt_toolkit.PromptSession`
Expand Down Expand Up @@ -99,20 +98,25 @@ def __init__(
Initializes the `Repl` class.
"""

self.group_ctx: Context = _get_group_ctx(ctx)
"""Parent group for the repl to retrieve subcommands from."""
# Check if there's a parent command, if it's there, then use it.
if ctx.parent is not None and not isinstance(ctx.command, MultiCommand):
ctx = ctx.parent

self.group: MultiCommand = cast(MultiCommand, self.group_ctx.command)
"""Group used in the `group_ctx`"""
ctx.protected_args = []

self.group_ctx: Context = ctx
"""Parent group for the repl to retrieve it's subcommands."""

self.internal_commands_system: InternalCommandSystem = InternalCommandSystem(
internal_command_prefix, system_command_prefix
)
"""Handles and executes internal commands that are invoked in repl."""

self.bottom_bar: AnyFormattedText | BottomBar = None
"""To change the command description that's displayed in the bottom bar
accordingly based on the current parsing state."""
""":class:`~click_repl.bototm_bar.BottomBar` obeject to change the command
description that's displayed in the bottom bar accordingly based on the
current parsing state.
"""

if ISATTY:
bottom_bar = prompt_kwargs.get("bottom_toolbar", BottomBar())
Expand All @@ -138,7 +142,7 @@ def __init__(
prompt_kwargs,
)

self.repl_ctx = ReplContext(
self.repl_ctx: ReplContext = ReplContext(
self.group_ctx,
self.internal_commands_system,
bottombar=self.bottom_bar,
Expand All @@ -159,7 +163,7 @@ def _get_command() -> str:
self.repl_ctx._history.append(inp)
return inp

self._get_command = _get_command
self._get_command: Callable[[], str] = _get_command

def get_command(self) -> str:
"""Retrieves input for the repl.
Expand Down Expand Up @@ -219,7 +223,7 @@ def _get_default_validator_kwargs(
Parameters
----------
validator_cls
A :class:`~prompt_toolkit.validation.Validator` type class
A :class:`~prompt_toolkit.validation.Validator` type class.
validator_kwargs
Contains keyword arguments that has to be passed to the
Expand Down Expand Up @@ -277,6 +281,9 @@ def _get_default_prompt_kwargs(
Contains keyword arguments that's passed to the
:class:`~prompt_toolkit.validation.Validator` class.
style_config_dict
Style configuration for the repl.
Returns
-------
dict[str,Any]
Expand Down Expand Up @@ -369,11 +376,13 @@ def execute_click_command(self, command: str | Sequence[str]) -> None:
if isinstance(command, str):
command = split_arg_string(command)

ctx, _ = _generate_next_click_ctx(self.group, self.group_ctx, tuple(command))
ctx, _ = _generate_next_click_ctx(
self.group_ctx.command, self.group_ctx, tuple(command)
)
ctx.command.invoke(ctx)

def loop(self) -> None:
"""Runs the main REPL loop."""
"""Starts the repl."""

with self.repl_ctx:
while True:
Expand Down
91 changes: 0 additions & 91 deletions src/click_repl/_types.py

This file was deleted.

17 changes: 11 additions & 6 deletions src/click_repl/bottom_bar.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,28 @@
import click
from click import Parameter
from click.types import FloatRange, IntRange, ParamType
from prompt_toolkit.formatted_text import OneStyleAndTextTuple as Token
from prompt_toolkit.formatted_text import StyleAndTextTuples as ListOfTokens
from typing_extensions import TypedDict

from ._compat import RANGE_TYPES_TUPLE, MultiCommand
from .globals_ import HAS_CLICK_GE_8, ISATTY
from .tokenizer import Marquee, TokenizedFormattedText, append_classname_to_all_tokens
from .utils import is_param_value_incomplete, iterate_command_params

if t.TYPE_CHECKING:
from ._types import ListOfTokens, ParamInfo, Token
from .core import ReplContext
from .parser import ReplParsingState


__all__ = ["BottomBar"]


class ParamInfo(TypedDict):
name: Token
type_info: ListOfTokens
nargs_info: ListOfTokens


def _describe_click_range_param_type(param_type: IntRange | FloatRange) -> str:
"""
Returns the metavar of the range-type :class:`~click.types.ParamType` type objects.
Expand Down Expand Up @@ -83,12 +90,10 @@ def __init__(
self.show_hidden_params = show_hidden_params
"""Flag that determines whether to display hidden params at bottom bar"""

self.current_repl_ctx: ReplContext | None = None
"""Context object of the current Repl session."""

self.parent_token_class_name: str = "bottom-bar"
"""
Parent class name for tokens that are related to :class:`~.BottomBar`."""
Parent class name for tokens that are related to :class:`~.BottomBar`.
"""

def __call__(self) -> ListOfTokens:
return self.get_formatted_text()
Expand Down
55 changes: 26 additions & 29 deletions src/click_repl/click_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .proxies import _create_proxy_command

if TYPE_CHECKING:
from ._types import InfoDict
from .parser import InfoDict


def get_option_flag_sep(option_names: list[str]) -> str:
Expand All @@ -20,41 +20,37 @@ def get_option_flag_sep(option_names: list[str]) -> str:


def join_options(options: list[str]) -> tuple[list[str], str]:
"""
Same implementation as :meth:`~click.formatting.join_options`, but much simpler.
Parameters
----------
options
List of option flags that needs to be joined together.
References
----------
:meth:`~click.formatting.join_options`
"""
# Same implementation as :meth:`~click.formatting.join_options`, but much simpler.

# Parameters
# ----------
# options
# List of option flags that needs to be joined together.

# References
# ----------
# :meth:`~click.formatting.join_options`
return sorted(options, key=len), get_option_flag_sep(options)


@lru_cache(maxsize=128)
def get_info_dict(
obj: Context | Command | Parameter | click.ParamType,
) -> InfoDict:
"""
Similar to the ``get_info_dict`` method implementation in click objects,
but it only retrieves the essential attributes required to
differentiate between different ``ReplParsingState`` objects.
Parameters
----------
obj
Click object for which the info dict needs to be generated.
Returns
-------
InfoDict
Dictionary that holds crucial details about the given click object
that can be used to uniquely identify it.
"""
# Similar to the ``get_info_dict`` method implementation in click objects,
# but it only retrieves the essential attributes required to
# differentiate between different ``ReplParsingState`` objects.

# Parameters
# ----------
# obj
# Click object for which the info dict needs to be generated.

# Returns
# -------
# InfoDict
# Dictionary that holds crucial details about the given click object
# that can be used to uniquely identify it.

if isinstance(obj, Context):
return {
Expand Down Expand Up @@ -160,6 +156,7 @@ def _generate_next_click_ctx(
proxy: bool = False,
**ctx_kwargs: dict[str, Any],
) -> tuple[Context, Command | None]:

if not args:
return parent_ctx, None

Expand Down
Loading

0 comments on commit f22d778

Please sign in to comment.