Skip to content

Commit

Permalink
Add 'shell' command to cli (#738)
Browse files Browse the repository at this point in the history
* Add 'shell' command to cli

* Add test

* Add ptpython as optional dep
  • Loading branch information
rytilahti committed Feb 6, 2024
1 parent 6ab17d8 commit 4589491
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
21 changes: 21 additions & 0 deletions kasa/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,5 +1081,26 @@ async def update_credentials(dev, username, password):
return await dev.update_credentials(username, password)


@cli.command()
@pass_dev
async def shell(dev: Device):
"""Open interactive shell."""
echo("Opening shell for %s" % dev)
from ptpython.repl import embed

logging.getLogger("parso").setLevel(logging.WARNING) # prompt parsing
logging.getLogger("asyncio").setLevel(logging.WARNING)
loop = asyncio.get_event_loop()
try:
await embed(
globals=globals(),
locals=locals(),
return_asyncio_coroutine=True,
patch_stdout=True,
)
except EOFError:
loop.stop()


if __name__ == "__main__":
cli()
18 changes: 18 additions & 0 deletions kasa/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,3 +558,21 @@ async def _state(dev: Device):
)
assert res.exit_code == 0
assert isinstance(result_device, expected_type)


@pytest.mark.skip(
"Skip until pytest-asyncio supports pytest 8.0, https://github.com/pytest-dev/pytest-asyncio/issues/737"
)
async def test_shell(dev: Device, mocker):
"""Test that the shell commands tries to embed a shell."""
mocker.patch("kasa.Discover.discover", return_value=[dev])
# repl = mocker.patch("ptpython.repl")
mocker.patch.dict(
"sys.modules",
{"ptpython": mocker.MagicMock(), "ptpython.repl": mocker.MagicMock()},
)
embed = mocker.patch("ptpython.repl.embed")
runner = CliRunner()
res = await runner.invoke(cli, ["shell"], obj=dev)
assert res.exit_code == 0
embed.assert_called()
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ sphinxcontrib-programoutput = { version = "^0", optional = true }
myst-parser = { version = "*", optional = true }
docutils = { version = ">=0.17", optional = true }

# shell support
# ptpython = { version = "*", optional = true }

[tool.poetry.group.dev.dependencies]
pytest = "*"
pytest-cov = "*"
Expand All @@ -57,6 +60,7 @@ coverage = {version = "*", extras = ["toml"]}
[tool.poetry.extras]
docs = ["sphinx", "sphinx_rtd_theme", "sphinxcontrib-programoutput", "myst-parser", "docutils"]
speedups = ["orjson", "kasa-crypt"]
# shell = ["ptpython"]

[tool.coverage.run]
source = ["kasa"]
Expand Down

0 comments on commit 4589491

Please sign in to comment.