Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 1, 2024
1 parent aa1c02c commit 7e565d5
Showing 1 changed file with 33 additions and 5 deletions.
38 changes: 33 additions & 5 deletions src/ape_console/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from typing import TYPE_CHECKING, Any, Optional, cast

import click
from IPython import InteractiveShell

from ape.cli.commands import ConnectedProviderCommand
from ape.cli.options import ape_cli_context, project_option
Expand All @@ -21,17 +22,23 @@
CONSOLE_EXTRAS_FILENAME = "ape_console_extras.py"


def _code_callback(ctx, param, value) -> list[str]:
breakpoint()
return value.splitlines()


@click.command(
cls=ConnectedProviderCommand,
short_help="Load the console",
context_settings=dict(ignore_unknown_options=True),
)
@ape_cli_context()
@project_option(hidden=True) # Hidden as mostly used for test purposes.
def cli(cli_ctx, project):
@click.option("-c", "--code", help="Program passed in as a string", callback=_code_callback)
def cli(cli_ctx, project, code):
"""Opens a console for the local project."""
verbose = cli_ctx.logger.level == logging.DEBUG
return console(project=project, verbose=verbose)
return console(project=project, verbose=verbose, code=code)


def import_extras_file(file_path) -> ModuleType:
Expand Down Expand Up @@ -95,6 +102,7 @@ def console(
verbose: bool = False,
extra_locals: Optional[dict] = None,
embed: bool = False,
code: Optional[list[str]] = None,
):
import IPython
from IPython.terminal.ipapp import Config as IPythonConfig
Expand Down Expand Up @@ -149,16 +157,25 @@ def console(
# Required for click.testing.CliRunner support.
embed = True

_launch_console(namespace, ipy_config, embed, banner)
_launch_console(namespace, ipy_config, embed, banner, code=code)


def _launch_console(namespace: dict, ipy_config: "IPythonConfig", embed: bool, banner: str):
def _launch_console(
namespace: dict,
ipy_config: "IPythonConfig",
embed: bool,
banner: str,
code: Optional[list[str]],
):
import IPython

from ape_console.config import ConsoleConfig

ipython_kwargs = {"user_ns": namespace, "config": ipy_config}
if embed:
if code:
raise ValueError("Cannot use `code` argument with embed mode.")

IPython.embed(**ipython_kwargs, colors="Neutral", banner1=banner)
else:
ipy_config.TerminalInteractiveShell.colors = "Neutral"
Expand All @@ -168,4 +185,15 @@ def _launch_console(namespace: dict, ipy_config: "IPythonConfig", embed: bool, b
if console_config.plugins:
ipy_config.InteractiveShellApp.extensions.extend(console_config.plugins)

IPython.start_ipython(**ipython_kwargs, argv=())
if code:
_execute_code(code, **ipython_kwargs)

else:
IPython.start_ipython(**ipython_kwargs, argv=())


def _execute_code(code: list[str], **ipython_kwargs):
shell = InteractiveShell.instance(**ipython_kwargs)
for line in code:
result = shell.run_cell(line)
click.echo(result.result)

0 comments on commit 7e565d5

Please sign in to comment.