Skip to content

Commit

Permalink
add %there pin command
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b committed Feb 13, 2021
1 parent 8ae60e2 commit ab835ac
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Changelog
-----

* Fixed config loss when updating on Android
* Added *pin* command: create script shortcut
* Allow to start server again if exception occur
* UI changes:

Expand Down
15 changes: 15 additions & 0 deletions pythonhere/magic_here/shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
sys.stderr.write(encoded_screenshot())
"""

PIN_COMMAND_TEMPLATE = """
from android_here import pin_shortcut
pin_shortcut(script="{script}", label="{label}")
"""


@there_code_shortcut
@click.option(
Expand Down Expand Up @@ -69,3 +74,13 @@ def screenshot(ctx, width, output):
output.close()

display(img)


@there_code_shortcut
@click.option("-l", "--label", type=str, default="", help="Label for shortcut")
@click.argument("script", nargs=1)
def pin(code: str, script: str, label: str) -> str: # pylint: disable=unused-argument
"""Create pinned shortcut to run a Python script."""
if not label:
label = script.split("/")[-1]
return PIN_COMMAND_TEMPLATE.format(script=script, label=label)
1 change: 0 additions & 1 deletion tests/test_android_here.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest
from pathlib import Path


@pytest.mark.parametrize(
Expand Down
22 changes: 22 additions & 0 deletions tests/test_magic.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ async def test_screenshot_saved_to_file(tmpdir, app_instance, call_there_group):
assert not os.path.exists(output)
call_there_group(["screenshot", "-o", output], "")
assert os.path.exists(output)


@pytest.mark.asyncio
async def test_pin_command_pin_shortcut_called(mocker, capfd, mocked_android_modules, call_there_group,
test_py_script):
pin_shortcut = mocker.patch("android_here.pin_shortcut")
call_there_group(["pin", test_py_script, "--label", "Test label"], "")
captured = capfd.readouterr()
assert not captured.out and not captured.err

pin_shortcut.assert_called_once_with(script=test_py_script, label="Test label")


@pytest.mark.asyncio
async def test_pin_command_default_label(mocker, capfd, mocked_android_modules, call_there_group,
test_py_script):
pin_shortcut = mocker.patch("android_here.pin_shortcut")
call_there_group(["pin", test_py_script], "")
captured = capfd.readouterr()
assert not captured.out and not captured.err

pin_shortcut.assert_called_once_with(script=test_py_script, label="test.py")

0 comments on commit ab835ac

Please sign in to comment.