o
    2h                     @   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.	cell_typer   sourcer   z%%
startswith)cell r
   _/var/www/html/chatgem/venv/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NamedTemporaryFileupdate_string_pastajoin_update_notebooksplitjsondumpSyntaxError_format_logshutilmovename)in_filenameout_filenameupgraderraw_codenotebook	raw_lines	temp_fileprocessed_filenew_file_contentlogprocess_errorsnew_notebookfiles_processedreport_texterrorsr
   r
   r   process_file!   s(   

r2   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_line
magic_listmagicr
   r
   r   
skip_magic>   s
   
r6   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)r3   r
   r
   r   check_line_splitS   s   r9   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   	enumerater6   r9   lenendswithreplaceappendr   rstrip)

input_filer&   in_filer'   
cell_indexr	   
cell_linesis_line_splitline_idxr3   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   idxr3   )code_cell_idxr
   r   r      s
    
z$_update_notebook.<locals>.<listcomp>c                    s   g | ]} | qS r
   r
   )r   rO   )updated_code_linesr
   r   r      s    r   r>   r   r@   r   r?   )copydeepcopyrD   formatr   rC   r   rF   )original_notebookoriginal_raw_linesrQ   r.   r	   applicable_linesnew_coder
   )rP   rQ   r   r      s$   


r   )__doc__collectionsrR   r   r7   r    r   
namedtupler   r   r2   r6   r9   r   r   r
   r
   r
   r   <module>   s   -