Skip to content

Commit

Permalink
Test bash commands in python notebooks #181
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Feb 18, 2019
1 parent 9b06786 commit 4a78650
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion jupytext/cell_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def paragraph_is_fully_commented(lines, comment, main_language):
"""Is the paragraph fully commented?"""
for i, line in enumerate(lines):
if line.startswith(comment):
if line.startswith((comment + ' %', comment + ' ?')) and is_magic(line, main_language):
if line.startswith((comment + ' %', comment + ' ?', comment + ' !')) and is_magic(line, main_language):
return False
continue
return i > 0 and _BLANK_LINE.match(line)
Expand Down
2 changes: 1 addition & 1 deletion jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"^(# |#)*(\?|!)")
_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*(\?|!)[A-Za-z]")

_PYTHON_MAGIC_CMD = re.compile(r"^(# |#)*({})(\s|$)".format('|'.join(
# posix
Expand Down
7 changes: 6 additions & 1 deletion tests/test_escape_magics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from nbformat.v4.nbbase import new_code_cell, new_notebook
from jupytext.magics import comment_magic, uncomment_magic, unesc
from jupytext.magics import comment_magic, uncomment_magic, unesc, is_magic
from jupytext.compare import compare_notebooks
import jupytext

Expand Down Expand Up @@ -123,3 +123,8 @@ def test_do_not_comment_python_cmds(not_magic_cmd):
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


def test_markdown_image_is_not_magic():
assert is_magic('# !cmd', 'python')
assert not is_magic('# ![Image name](image.png', 'python')
18 changes: 18 additions & 0 deletions tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,24 @@ def j(x):
compare(script, script2)


def test_notebook_with_magic_and_bash_cells(script="""# This is a test for issue #181
# %load_ext line_profiler
# !head -4 data/president_heights.csv
"""):
notebook = jupytext.reads(script, 'py')
for cell in notebook.cells:
lines = cell.source.splitlines()
assert lines[0]
assert lines[-1]
assert not cell.metadata, cell.source

script2 = jupytext.writes(notebook, 'py')

compare(script, script2)


def test_notebook_one_blank_line_before_first_markdown_cell(script="""
# This is a markdown cell
Expand Down

0 comments on commit 4a78650

Please sign in to comment.