Skip to content

Commit

Permalink
Preserve indentation when commenting out magic commands (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Jul 21, 2020
1 parent e37795e commit 1f7dc95
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.5.1+dev
---------

**Fixed**
- Preserve indentation when commenting out magic commands (#437)


1.5.1 (2020-07-05)
------------------

Expand Down
26 changes: 18 additions & 8 deletions jupytext/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
_MAGIC_FORCE_ESC_RE["csharp"] = re.compile(r"^(// |//)*#![a-zA-Z](.*)//\s*noescape")

# Commands starting with a question or exclamation mark have to be escaped
_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^(# |#)*\s*(\?|!)\s*[A-Za-z]")
_PYTHON_HELP_OR_BASH_CMD = re.compile(r"^\s*(# |#)*\s*(\?|!)\s*[A-Za-z]")

# A bash command not followed by an equal sign or a parenthesis is a magic command
_PYTHON_MAGIC_CMD = re.compile(
r"^(# |#)*({})($|\s$|\s[^=,])".format(
r"^\s*(# |#)*({})($|\s$|\s[^=,])".format(
"|".join(
# posix
["cat", "cd", "cp", "mv", "rm", "rmdir", "mkdir"]
Expand All @@ -55,7 +55,7 @@
)
)
# Python help commands end with ?
_IPYTHON_MAGIC_HELP = re.compile(r"^(# )*[^\s]*\?\s*$")
_IPYTHON_MAGIC_HELP = re.compile(r"^\s*(# )*[^\s]*\?\s*$")

_SCRIPT_LANGUAGES = [_SCRIPT_EXTENSIONS[ext]["language"] for ext in _SCRIPT_EXTENSIONS]

Expand Down Expand Up @@ -109,7 +109,15 @@ def comment_magic(
next_is_magic
or is_magic(line, language, global_escape_flag, explicitly_code)
):
source[pos] = _COMMENT[language] + " " + line
if next_is_magic:
# this is the continuation line of a magic command on the previous line,
# so we don't want to indent the comment
unindented = line
indent = ""
else:
unindented = line.lstrip()
indent = line[: len(line) - len(unindented)]
source[pos] = indent + _COMMENT[language] + " " + unindented
next_is_magic = language == "python" and _LINE_CONTINUATION_RE.match(line)
parser.read_line(line)
return source
Expand All @@ -118,10 +126,12 @@ def comment_magic(
def unesc(line, language):
"""Uncomment once a commented line"""
comment = _COMMENT[language]
if line.startswith(comment + " "):
return line[len(comment) + 1 :]
if line.startswith(comment):
return line[len(comment) :]
unindented = line.lstrip()
indent = line[: len(line) - len(unindented)]
if unindented.startswith(comment + " "):
return indent + unindented[len(comment) + 1 :]
if unindented.startswith(comment):
return indent + unindented[len(comment) :]
return line


Expand Down
7 changes: 7 additions & 0 deletions tests/test_escape_magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,10 @@ def test_configure_magic(no_jupytext_version_number):
""",
)
compare_notebooks(jupytext.reads(text, "py"), nb)


def test_indented_magic():
assert is_magic(" !rm file", "python")
assert is_magic(" # !rm file", "python")
assert comment_magic([" !rm file"]) == [" # !rm file"]
assert uncomment_magic([" # !rm file"]) == [" !rm file"]
22 changes: 21 additions & 1 deletion tests/test_read_simple_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ def test_indented_bash_command(
]
),
text="""try:
# !echo jo
# !echo jo
pass
except:
pass
Expand Down Expand Up @@ -1182,3 +1182,23 @@ def test_arg(arg):
assert cell.cell_type == "code"
assert nb.cells[0].source == "import pytest"
assert nb.cells[0].metadata == {}


def test_indented_magic_commands(
text="""if True:
# # !rm file 1
# !rm file 2
""",
):

nb = jupytext.reads(text, "py")
assert len(nb.cells) == 1
assert nb.cells[0].cell_type == "code"
compare(
nb.cells[0].source,
"""if True:
# !rm file 1
!rm file 2""",
)
assert nb.cells[0].metadata == {}
compare(jupytext.writes(nb, "py"), text)

0 comments on commit 1f7dc95

Please sign in to comment.