o
    ?e                     @   sx   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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| d d  d S )z+Checks if the cell consists of Python code.Z	cell_typer   sourcer   z%%
startswith)cell r	   e/home/www/facesmatcher.com/pyenv/lib/python3.10/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|| \}}}	}
|r:|r:t|||d}t	|| n	t
dd	|	 |}||	| |}|
}W d
   n1 sXw   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>%   s    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_notebook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 ]
}|  |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_magic>   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_splitS   s   r,   c           
   
   C   s   g }t | }t|}W d   n1 sw   Y  d}|d D ]Q}t|rr|d }d}t|D ]<\}}	t|	g ds>|rFd|	 }	t|	}|rLt|	}|t|d kr_|	d	r_|		d	d
}	|
t||	 	d	d
 q1|d7 }q!||fS )z5Loads the ipynb file and returns a list of CodeLines.Nr   cellsr   F)%!?###!!!   r   ###===)openr   loadr   	enumerater)   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   c   s2   
r   c                    s   t | }t|tksJ dt|td |d D ].}t|s&q 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   r-   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      s    r   r1   r   r3   r   r2   )copydeepcopyr7   formatr   r6   r   r9   )Zoriginal_notebookZoriginal_raw_linesr?   r$   r   Zapplicable_linesZnew_coder	   )r>   r?   r
   r      s$   


r   )__doc__collectionsr@   r   r*   r   r   
namedtupler   r   r&   r)   r,   r   r   r	   r	   r	   r
   <module>   s   -