Skip to content

Commit

Permalink
Piping into black, flake8 or autopep8 made simpler
Browse files Browse the repository at this point in the history
Standard input '-' argument is automatically added by Jupytext #154 #142
  • Loading branch information
mwouts committed Jan 27, 2019
1 parent 6fd5d0c commit a554968
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
13 changes: 10 additions & 3 deletions jupytext/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ def parse_jupytext_args(args=None):
action='append',
help='Pipe the text representation of the notebook into another program, and read the'
'notebook back. For instance, reformat your notebook with:'
" jupytext notebook.ipynb --pipe 'black -'"
" jupytext notebook.ipynb --pipe black"
'If you want to reformat it and sync the paired representation, execute:'
" jupytext notebook.ipynb --sync --pipe 'black -'")
" jupytext notebook.ipynb --sync --pipe black")
parser.add_argument('--exec',
action='append',
help='Pipe the text representation of the notebook into another program, and test that '
'the returned value is non zero. For instance, test that your notebook is pep8 compliant '
'with:'
" jupytext notebook.ipynb --exec 'flake8 -'")
" jupytext notebook.ipynb --exec flake8")
parser.add_argument('--pipe-fmt',
default='auto:percent',
help='The format in which the notebook should be piped to other programs, when using the '
Expand Down Expand Up @@ -406,6 +406,9 @@ def load_paired_notebook(notebook, nb_file, log):
def pipe_notebook(notebook, command, fmt='py:percent', update=True, preserve_outputs=True):
"""Pipe the notebook, in the desired representation, to the given command. Update the notebook
with the returned content if desired."""
if command in ['black', 'flake8', 'autopep8']:
command = command + ' -'

fmt = long_form_one_format(fmt, notebook.metadata)
text = writes(notebook, fmt)
process = subprocess.Popen(command.split(' '), stdout=subprocess.PIPE, stdin=subprocess.PIPE)
Expand All @@ -419,6 +422,10 @@ def pipe_notebook(notebook, command, fmt='py:percent', update=True, preserve_out
if not update:
return notebook

if not cmd_output:
sys.stderr.write("[jupytext] The command '{}' had no output. As a result, the notebook is empty. "
"Is this expected? If not, use --exec rather than --pipe for this command.".format(command))

piped_notebook = reads(cmd_output.decode('utf-8'), fmt)

if preserve_outputs:
Expand Down
12 changes: 6 additions & 6 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_pipe_into_black():
nb_org = new_notebook(cells=[new_code_cell('1 +1')])
nb_dest = new_notebook(cells=[new_code_cell('1 + 1')])

nb_pipe = pipe_notebook(nb_org, 'black -')
nb_pipe = pipe_notebook(nb_org, 'black')
compare(nb_dest, nb_pipe)


Expand All @@ -71,12 +71,12 @@ def test_pipe_into_autopep8():
def test_pipe_into_flake8():
# Notebook OK
nb = new_notebook(cells=[new_code_cell('# correct code\n1 + 1')])
pipe_notebook(nb, 'flake8 -', update=False)
pipe_notebook(nb, 'flake8', update=False)

# Notebook not OK
nb = new_notebook(cells=[new_code_cell('incorrect code')])
with pytest.raises(SystemExit):
pipe_notebook(nb, 'flake8 -', update=False)
pipe_notebook(nb, 'flake8', update=False)


@requires_black
Expand All @@ -96,14 +96,14 @@ def test_apply_black_through_jupytext(tmpdir, nb_file):

# Black in place
writef(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--pipe', 'black -'])
jupytext([tmp_ipynb, '--pipe', 'black'])
nb_now = readf(tmp_ipynb)
compare(nb_black, nb_now)

# Write to another folder using dots
script_fmt = os.path.join('..', 'script_folder//py:percent')
writef(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--to', script_fmt, '--pipe', 'black -'])
jupytext([tmp_ipynb, '--to', script_fmt, '--pipe', 'black'])
assert os.path.isfile(tmp_py)
nb_now = readf(tmp_py)
nb_now.metadata = metadata
Expand All @@ -113,7 +113,7 @@ def test_apply_black_through_jupytext(tmpdir, nb_file):
# Map to another folder based on file name
writef(nb_org, tmp_ipynb)
jupytext([tmp_ipynb, '--from', 'notebook_folder//ipynb', '--to', 'script_folder//py:percent',
'--pipe', 'black -', '--exec', 'flake8 -'])
'--pipe', 'black', '--exec', 'flake8'])
assert os.path.isfile(tmp_py)
nb_now = readf(tmp_py)
nb_now.metadata = metadata
Expand Down

0 comments on commit a554968

Please sign in to comment.