Skip to content

Commit

Permalink
Ignore magic commands when black painting notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Bultako committed Sep 2, 2018
1 parent 2c4e535 commit e0851fc
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions black_notebooks.py
Expand Up @@ -11,6 +11,22 @@

logging.basicConfig(level=logging.INFO)


def comment_magics(input):
"""Coment magic commands."""
lines = input.splitlines(True)
output = ""
for line in lines:
new_line = ""
if line.startswith("%") or line.startswith("!"):
new_line = new_line + "###-MAGIC COMMAND-" + line
if new_line:
output = output + new_line
else:
output = output + line
return output


# check gammapy-extra
if 'GAMMAPY_EXTRA' not in os.environ:
logging.info('GAMMAPY_EXTRA environment variable not set.')
Expand Down Expand Up @@ -44,10 +60,12 @@
fmt = nb.cells[cellnumber]['source']
if nb.cells[cellnumber]['cell_type'] == 'code':
try:
fmt = comment_magics(fmt)
fmt = format_str(src_contents=fmt,
line_length=79).rstrip()
except Exception as ex:
logging.info(ex)
fmt = fmt.replace("###-MAGIC COMMAND-", "")
nb.cells[cellnumber]['source'] = fmt

# write formatted notebook
Expand Down

0 comments on commit e0851fc

Please sign in to comment.