def transform_cell(code, indent_only=False): """Transform IPython code to Python code.""" number_empty_lines = count_leading_empty_lines(code) if indent_only: if not code.endswith('\n'): code += '\n' # Ensure the cell has a trailing newline lines = code.splitlines(keepends=True) lines = leading_indent(leading_empty_lines(lines)) code = ''.join(lines) else: tm = TransformerManager() code = tm.transform_cell(code) return '\n' * number_empty_lines + code
def transform_cell(code, indent_only=False): """Transform ipython code to python code.""" number_empty_lines = count_leading_empty_lines(code) if indent_only: # Not implemented for PY2 if LooseVersion(ipy_version) < LooseVersion('7.0.0'): return code if not code.endswith('\n'): code += '\n' # Ensure the cell has a trailing newline lines = code.splitlines(keepends=True) lines = leading_indent(leading_empty_lines(lines)) code = ''.join(lines) else: if LooseVersion(ipy_version) < LooseVersion('7.0.0'): tm = IPythonInputSplitter() return tm.transform_cell(code) else: tm = TransformerManager() code = tm.transform_cell(code) return '\n' * number_empty_lines + code
def test_leading_empty_lines(): for sample, expected in [LEADING_EMPTY_LINES, ONLY_EMPTY_LINES]: nt.assert_equal( ipt2.leading_empty_lines(sample.splitlines(keepends=True)), expected.splitlines(keepends=True), )