
    BVh                         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j                  dddg      Zd Z	d Z
d Zd	 Zd
 Zd Zy)z-A module to support operations on ipynb files    NCodeLinecell_numbercodec                 T    | d   dk(  xr | d   xr | d   d   j                  d       S )z+Checks if the cell consists of Python code.	cell_typer   sourcer   z%%
startswith)cells    T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/tools/compatibility/ipynb.py	is_pythonr      s?    
{
v
% 58n5x.#..t446    c                 4   t        d       t        |       \  }}|D cg c]  }|j                   }}t        j                  dd      5 }|j                  dj                  |      |       \  }}	}
}|r5|r3t        |||	j                  d            }t        j                  ||       nt        ddj                  |
      z        |}|j                  |
| |      }|}ddd       t        j                  j                  |       fS c c}w # 1 sw Y   3xY w)	z;The function where we inject the support for ipynb upgrade.z,Extracting code lines from original notebookwF)delete
z&Was not able to process the file: 
%s
 N)print	_get_coder   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cl	raw_lines	temp_fileprocessed_filenew_file_contentlogprocess_errorsnew_notebookfiles_processedreport_texterrorss                   r   process_filer3   !   s   67 -(H!)*2rww*)* ""3u5  	$$TYYy%9;G :N$c> ^%h&6&<&<T&BDl
iii(
4rwws|
CE E %O&&sKFKF" 	++innl+	+v	--- + s   D	
BDDc                 8    |D ]  }| j                  |      s y y)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magics      r   
skip_magicr8   >   s)      eE" 
r   c                 .    t        j                  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)r5   s    r   check_line_splitr<   S   s     
;		**r   c                    g }t        |       5 }t        j                  |      }ddd       d}d   D ]  }t        |      s|d   }d}t	        |      D ]  \  }}	t        |	g d      s|rd|	z   }	t        |	      }|rt        |	      }|t        |      dz
  k(  r#|	j                  d	      r|	j                  d	d
      }	|j                  t        ||	j                         j                  d	d
                    |dz  } ||fS # 1 sw Y   xY w)z5Loads the ipynb file and returns a list of CodeLines.Nr   cellsr   F)%!?###!!!   r   ###===)openr   loadr   	enumerater8   r<   lenendswithreplaceappendr   rstrip)

input_filer&   in_filer'   
cell_indexr   
cell_linesis_line_splitline_idxr5   s
             r   r   r   c   s1    (J "7yy!H" *w d>jm!*:!6 B
(I i1]*) +95-*95-
 J!++1C1CD1I''h7) 	Z%%'//h?A	B-B4 Aoj?B 
8	K" "s   DD
c                    t        j                  |       }t        |      t        |      k(  s)J dj                  t        |      t        |                   d}|d   D ]  }t	        |      st        |      D cg c]  \  }}|j                  |k(  r| }}}|D cg c]  }||   	 }	}dj                  |	      j                  dd      j                  dd      |d<   |d	z  } |S c c}}w c c}w )
z)Updates notebook, once migration is done.zCThe lengths of input and converted files are not the same: {} vs {}r   r>   r   rB   r   rD   r   rC   )	copydeepcopyrH   formatr   rG   r   r   rJ   )
original_notebookoriginal_raw_linesupdated_code_linesr/   code_cell_idxr   idxr5   applicable_linesnew_codes
             r   r   r      s    01, 
	 C(:$;	; Js-.4F0GHJ	; -7# dT? #,,>"?Y  M1 	 
 4DDC"3'DHDYYx(002>FF$DNQM 

 Es   :C'C-)__doc__collectionsrT   r   r:   r    r   
namedtupler   r   r3   r8   r<   r   r    r   r   <module>rb      sT    4    	  !;!!*}f.EF6.:*+ *Zr   