Add support for line splits

This commit is contained in:
Sergii Khomenko 2019-04-14 15:12:57 +02:00
parent 71eb168f45
commit 62d12da2e3

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
"""A module to support operation on ipynb files"""
"""A module to support operations on ipynb files"""
from __future__ import absolute_import
from __future__ import division
@ -62,8 +62,36 @@ def process_file(in_filename, out_filename, upgrader):
return files_processed, report_text, errors
def skip_magic(code_line, magic_list):
"""
Checks if the cell has magic, that is not python based
>>> skip_magic('!ls -laF')
True
"""
for magic in magic_list:
if code_line.startswith(magic):
return True
return False
def check_line_split(code_line):
r"""
Checks if line was splitted with `\`
>>> skip_magic("!gcloud ml-engine models create ${MODEL} \\\n")
True
"""
if code_line.endswith('\\\n'):
return True
return False
def _get_code(input_file):
"""Load the ipynb file and return a list of CodeLines."""
"""Loads the ipynb file and returns a list of CodeLines."""
raw_code = []
@ -75,15 +103,17 @@ def _get_code(input_file):
if is_python(cell):
cell_lines = cell["source"]
is_line_split = False
for line_idx, code_line in enumerate(cell_lines):
# Sometimes, jupyter has more than python code
# Idea is to comment these lines, for upgrade time
if code_line.startswith("%") or code_line.startswith("!") \
or code_line.startswith("?"):
if skip_magic(code_line, ['%', '!', '?']) or is_line_split:
# Found a special character, need to "encode"
code_line = "###!!!" + code_line
is_line_split = check_line_split(code_line)
# Sometimes, people leave \n at the end of cell
# in order to migrate only related things, and make the diff
# the smallest -> here is another hack
@ -102,7 +132,7 @@ def _get_code(input_file):
def _update_notebook(original_notebook, original_raw_lines, updated_code_lines):
"""Update notebook, once migration is done."""
"""Updates notebook, once migration is done."""
new_notebook = copy.deepcopy(original_notebook)