Skip to content

Commit

Permalink
add %there kv command
Browse files Browse the repository at this point in the history
  • Loading branch information
b3b committed Dec 11, 2020
1 parent fcad786 commit 2a38902
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pythonhere/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Python Here Jupyter magic."""
from herethere.magic import load_ipython_extension

from .magic_here import shortcuts # noqa


__all__ = ("load_ipython_extension",)
Empty file.
29 changes: 29 additions & 0 deletions pythonhere/magic_here/shortcuts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""%there magic Python code shortcuts."""
# pylint: disable=invalid-name

import click
from herethere.there.commands import there_code_shortcut

KV_COMMAND_TEMPLATE = r"""
from kivy.lang import Builder
{unload_file}
_pythonhere_new_root = Builder.load_string(r'''{code} ''', filename='_pythonhere')
if _pythonhere_new_root:
root.clear_widgets()
root.add_widget(_pythonhere_new_root)
del _pythonhere_new_root
"""


@there_code_shortcut
@click.option(
"-c", "--clear-style", is_flag=True, help="Unload previously applied rules."
)
def kv(code: str, clear_style: bool) -> str:
"""Insert given rules into the Kivy Language Builder.
:param code: KV language rules
"""
unload_file = "Builder.unload_file('_pythonhere')" if clear_style else ""
code = code.replace("'''", '"""')
return KV_COMMAND_TEMPLATE.format(code=code, unload_file=unload_file)
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -e

cd pythonhere
PYTHONPATH=. xvfb-run --auto-servernum pytest --cov=. --cov-config=../.coveragerc --cov-report=xml ../tests
coverage report
coverage report -i
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
"ifaddr",
],
extras_require={
"magic": [
"ipython",
"ipywidgets",
],
"dev": [
"black",
"codecov",
Expand Down
22 changes: 22 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import asyncio

import nest_asyncio
import pytest

from herethere.everywhere import ConnectionConfig
from herethere.there.client import Client
from herethere.there.commands import ContextObject, there_group

from main import PythonHereApp, run_ssh_server



@pytest.fixture
def connection_config():
return ConnectionConfig(
Expand Down Expand Up @@ -35,3 +39,21 @@ async def there(app_instance, connection_config):
await asyncio.wait_for(app_instance.ssh_server_started.wait(), 5)
await client.connect(connection_config)
yield client


@pytest.fixture
def nested_event_loop(event_loop):
nest_asyncio.apply()


@pytest.fixture
async def call_there_group(nested_event_loop, app_instance, there):
def _callable(args, code):
there_group(
args,
"test",
standalone_mode=False,
obj=ContextObject(client=there, code=code),
)

return _callable
21 changes: 21 additions & 0 deletions tests/test_magic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from herethere.there.commands import ContextObject

from magic_here import shortcuts # noqa

@pytest.mark.asyncio
async def test_kv_command_runcode_called(call_there_group, mocker):
runcode = mocker.patch.object(ContextObject, 'runcode', autospec=True)

call_there_group(["kv"], "Label:")

runcode.assert_called_once()
ctx_obj = runcode.call_args[0][0]
assert "Builder.load_string(r'''Label: '''" in ctx_obj.code

@pytest.mark.asyncio
async def test_kv_command_executed(capfd, app_instance, call_there_group):
call_there_group(["kv"], "Label:\n text: '''Hello there'''")
captured = capfd.readouterr()
assert not captured.out and not captured.err
assert app_instance.root.children[0].text == 'Hello there'
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ envlist = py37,py38

[testenv]
extras =
magic
dev
commands =
./run_tests.sh
Expand Down

0 comments on commit 2a38902

Please sign in to comment.