a
    _=ics                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlZedddgZ	dd Z
dd	 Zd
d Zdd Zdd Zdd ZdS )z-A module to support operations on ipynb files    NCodeLinecell_numbercodec                 C   s.   | d dko,| d o,t | d d d S )z+Checks if the cell consists of Python code.Z	cell_typer   sourcer   z%%six
ensure_str
startswith)cell r   u/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/tensorflow/tools/compatibility/ipynb.py	is_python   s
    r   c                 C   s   t d t| \}}dd |D }tjddd}|d|| \}}}	}
|rz|rzt||t|	d}t
|| ntdd	|	 |}||	| |}|
}W d
   n1 s0    Y  t|j| |||fS )z;The function where we inject the support for ipynb upgrade.z,Extracting code lines from original notebookc                 S   s   g | ]
}|j qS r   )r   ).0clr   r   r   
<listcomp>(       z process_file.<locals>.<listcomp>wF)delete
z&Was not able to process the file: 
%s
 N)print	_get_codetempfileNamedTemporaryFileZupdate_string_pastajoin_update_notebookr   r   splitjsondumpSyntaxErrorZ_format_logshutilmovename)Zin_filenameZout_filenameZupgraderraw_codenotebookZ	raw_lines	temp_fileZprocessed_fileZnew_file_contentlogZprocess_errorsnew_notebookZfiles_processedZreport_texterrorsr   r   r   process_file$   s(    
"r)   c                 C   s$   |D ]}t | |r dS qdS )a  Checks if the cell has magic, that is not Python-based.

  Args:
      code_line: A line of Python code
      magic_list: A list of jupyter "magic" exceptions

  Returns:
    If the line jupyter "magic" line, not Python line

   >>> skip_magic('!ls -laF', ['%', '!', '?'])
  True
  TFr   )	code_lineZ
magic_listmagicr   r   r   
skip_magicB   s    r,   c                 C   s   t d| S )zChecks if a line was split with `\`.

  Args:
      code_line: A line of Python code

  Returns:
    If the line was split with `\`

  >>> skip_magic("!gcloud ml-engine models create ${MODEL} \\\n")
  True
  z\\\s*\n$)research)r*   r   r   r   check_line_splitW   s    r/   c           
   
   C   s  g }t | }t|}W d   n1 s,0    Y  d}|d D ]}t|rB|d }d}t|D ]\}}	t|	g ds||rdt|	 }	t|	}|rt|	}|t	|d krt|	
d	rt|	d	d
}	|t|t|	 d	d
 qb|d7 }qB||fS )z5Loads the ipynb file and returns a list of CodeLines.Nr   cellsr   F)%!?###!!!   r   ###===)openr   loadr   	enumerater,   r   r   r/   lenendswithreplaceappendr   rstrip)
Z
input_filer#   in_filer$   Z
cell_indexr
   Z
cell_linesZis_line_splitZline_idxr*   r   r   r   r   g   s:    
(
r   c                    s   t | }t|tks2J dt|td |d D ]\}t|sLq> fddt|D }fdd|D }d|dd	d
d|d<  d7  q>|S )z)Updates notebook, once migration is done.zCThe lengths of input and converted files are not the same: {} vs {}r   r0   c                    s   g | ]\}}|j  kr|qS r   )r   )r   idxr*   )code_cell_idxr   r   r      s   
z$_update_notebook.<locals>.<listcomp>c                    s   g | ]} | qS r   r   )r   r@   )updated_code_linesr   r   r      r   r   r4   r   r6   r   r5   )copydeepcopyr:   formatr   r9   r   r<   )Zoriginal_notebookZoriginal_raw_linesrB   r'   r
   Zapplicable_linesZnew_coder   )rA   rB   r   r      s&    


r   )__doc__collectionsrC   r   r-   r    r   r   
namedtupler   r   r)   r,   r/   r   r   r   r   r   r   <module>   s   /