From c173f75676823dbd6d1e7e6181b156771a636ae8 Mon Sep 17 00:00:00 2001 From: Marc Wouts Date: Tue, 26 Feb 2019 18:29:56 +0100 Subject: [PATCH] Allow spaces between ? or ! and python or bash command Fixes #189 --- HISTORY.rst | 7 +++++++ jupytext/magics.py | 2 +- tests/test_escape_magics.py | 14 +++++++------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e0c0218e9..db4c9d090 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -3,6 +3,13 @@ Release History --------------- +1.0.2 (2019-02-??) +++++++++++++++++++++++ + +**BugFixes** +- Allow spaces between ``?`` or ``!`` and python or bash command (#189) + + 1.0.1 (2019-02-23) ++++++++++++++++++++++ diff --git a/jupytext/magics.py b/jupytext/magics.py index e089abae4..3704673f3 100644 --- a/jupytext/magics.py +++ b/jupytext/magics.py @@ -16,7 +16,7 @@ _COMMENT = {_SCRIPT_EXTENSIONS[ext]['language']: _SCRIPT_EXTENSIONS[ext]['comment'] for ext in _SCRIPT_EXTENSIONS} # Commands starting with a question or exclamation mark have to be escaped -_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)[A-Za-z]") +_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)\s*[A-Za-z]") _PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})(\s|$)".format('|'.join( # posix diff --git a/tests/test_escape_magics.py b/tests/test_escape_magics.py index 77fb01d71..190f9dc88 100644 --- a/tests/test_escape_magics.py +++ b/tests/test_escape_magics.py @@ -107,22 +107,22 @@ def test_force_comment_using_contents_manager(tmpdir): assert '%pylab inline' in stream.read().splitlines() -@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b']) +@pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b', '! mkdir tmp']) def test_comment_bash_commands_in_python(magic_cmd): - comment_magic([magic_cmd]) == ['# ' + magic_cmd] - uncomment_magic(['# ' + magic_cmd]) == magic_cmd + assert comment_magic([magic_cmd]) == ['# ' + magic_cmd] + assert uncomment_magic(['# ' + magic_cmd]) == [magic_cmd] @pytest.mark.parametrize('not_magic_cmd', ['copy(a)', 'copy.deepcopy']) def test_do_not_comment_python_cmds(not_magic_cmd): - comment_magic([not_magic_cmd]) == [not_magic_cmd] - uncomment_magic([not_magic_cmd]) == not_magic_cmd + assert comment_magic([not_magic_cmd]) == [not_magic_cmd] + assert uncomment_magic([not_magic_cmd]) == [not_magic_cmd] @pytest.mark.parametrize('magic_cmd', ['ls', '!ls', 'ls -al', '!whoami', '# ls', '# mv a b']) def test_do_not_comment_bash_commands_in_R(magic_cmd): - comment_magic([magic_cmd], language='R') == ['# ' + magic_cmd] - uncomment_magic(['# ' + magic_cmd], language='R') == magic_cmd + assert comment_magic([magic_cmd], language='R') == [magic_cmd] + assert uncomment_magic([magic_cmd], language='R') == [magic_cmd] def test_markdown_image_is_not_magic():