B
    9‹dr  ã               @   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„ Zdd„ Zdd„ Z	d	d
„ Z
dd„ ZdS )z3Set of common tools to aid bundler implementations.é    Nc             C   s    t | |ƒ}ttj | ¡|ƒ}|S )a$  Gets a list of files referenced either in Markdown fenced code blocks
    or in HTML comments from the notebook. Expands patterns expressed in
    gitignore syntax (https://git-scm.com/docs/gitignore). Returns the
    fully expanded list of filenames relative to the notebook dirname.

    Parameters
    ----------
    abs_nb_path: str
        Absolute path of the notebook on disk
    version: int
        Version of the notebook document format to use

    Returns
    -------
    list
        Filename strings relative to the notebook path
    )Úget_reference_patternsÚexpand_referencesÚosÚpathÚdirname)Úabs_nb_pathÚversionZref_patternsÚexpanded© r
   úc/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/notebook/bundler/tools.pyÚget_file_references   s    
r   c             C   s8   t  | |¡}g }x"|jD ]}t|ƒ}|r|| }qW |S )am  Gets a list of reference patterns either in Markdown fenced code blocks
    or in HTML comments from the notebook.

    Parameters
    ----------
    abs_nb_path: str
        Absolute path of the notebook on disk
    version: int
        Version of the notebook document format to use

    Returns
    -------
    list
        Pattern strings from the notebook
    )ÚnbformatÚreadÚcellsÚget_cell_reference_patterns)r   r   ÚnotebookZreferenced_listÚcellÚ
referencesr
   r
   r   r   "   s    r   c             C   s  g }|   d¡ d¡r||   d¡ d¡r||   d¡tdƒd…  ¡ }xÌ|D ]2}| d¡rTP | d¡dk rD| d	¡sD| |¡ qDW n|   d¡ d¡r|   d¡ d
¡dkr|   d¡}| d
¡}||td
ƒ d…  ¡ }x:|D ]2}| d
¡ræP | d¡dk rÖ| d	¡sÖ| |¡ qÖW dd„ |D ƒS )a§  
    Retrieves the list of references from a single notebook cell. Looks for
    fenced code blocks or HTML comments in Markdown cells, e.g.,

    ```
    some.csv
    foo/
    !foo/bar
    ```

    or

    <!--associate:
    some.csv
    foo/
    !foo/bar
    -->

    Parameters
    ----------
    cell: dict
        Notebook cell object

    Returns
    -------
    list
        Reference patterns found in the cell
    Z	cell_typeÚmarkdownÚsourcez<!--associate:Nz-->z../r   ú#z```c             S   s   g | ]}|  ¡ r|‘qS r
   )Ústrip)Ú.0Úrefr
   r
   r   ú
<listcomp>p   s    z/get_cell_reference_patterns.<locals>.<listcomp>)ÚgetÚ
startswithÚlenÚ
splitlinesÚfindÚappend)r   Z
referencedÚlinesÚliner   Úoffsetr
   r
   r   r   :   s$     

(



r   c             C   s^  g }x6|D ].}t j |¡}| d¡r.|t j7 }| |¡ q
W |}g }g }g }x||D ]t}|r¸| t j¡dk r¸t  ¡ }	t  | ¡ | 	d¡rž|t
 
|dd… ¡ }n|t
 
|¡ }t  |	¡ qR|rR| |¡ qRW xD|D ]:}| 	d¡}
|
rô|dd… }n|}xt  | ¡D ]\}}}xô|D ]ì}t j |t| ƒd d… |¡}| t j¡rr| 	|¡r|
rf| |¡ n
| |¡ n| d¡dkrØ| d¡}t|ƒdkr| 	|d ¡r| |d ¡r|
rÌ| |¡ n
| |¡ n*t ||¡r|
rø| |¡ n
| |¡ qW qW qÒW xB|D ]:}y| |¡ W n$ tk
rN } zW dd}~X Y nX qW t|ƒS )am  Expands a set of reference patterns by evaluating them against the
    given root directory. Expansions are performed against patterns
    expressed in the same manner as in gitignore
    (https://git-scm.com/docs/gitignore).

    NOTE: Temporarily changes the current working directory when called.

    Parameters
    ----------
    root_path: str
        Assumed root directory for the patterns
    references: list
        Reference patterns from get_reference_patterns expressed with
        forward-slash directory separators

    Returns
    -------
    list
        Filename strings relative to the root path
    ú/r   ú!é   Nz**é   )r   r   ÚnormpathÚendswithÚsepr    r   ÚgetcwdÚchdirr   ÚglobÚwalkÚjoinr   ÚsplitÚfnmatchÚremoveÚ
ValueErrorÚset)Ú	root_pathr   Znormalized_referencesr   Znormalized_refZglobbedZ	negationsZ	must_walkÚpatternÚcwdZpattern_is_negationZtestpatternÚrootÚ_Ú	filenamesÚfilenameZjoinedZendsZnegatedÚerrr
   r
   r   r   r   sd    








 
r   c             C   sª   x¤|D ]œ}t j t j | |¡¡rt j |¡}|r‚t j ||¡}yt  |¡ W n4 tk
r€ } z|jtjkrln|‚W dd}~X Y nX t	 
t j | |¡t j ||¡¡ qW dS )a¶  Copies the given list of files, relative to src, into dst, creating
    directories along the way as needed and ignore existence errors.
    Skips any files that do not exist. Does not create empty directories
    from src in dst.

    Parameters
    ----------
    src: str
        Root of the source directory
    dst: str
        Root of the destination directory
    src_relative_filenames: list
        Filenames relative to src
    N)r   r   Úisfiler/   r   ÚmakedirsÚOSErrorÚerrnoÚEEXISTÚshutilÚcopy2)ÚsrcÚdstZsrc_relative_filenamesr;   Zparent_relativeZ
parent_dstÚexcr
   r
   r   Úcopy_filelistÉ   s    
rG   )Ú__doc__r   rB   r@   r   r1   r-   r   r   r   r   rG   r
   r
   r
   r   Ú<module>   s   8W