o
    iP!                     @   s@   d dl Z ddlmZ ddlmZ ddlmZ G dd deZdS )	    N   )AbstractFileSystem)infer_storage_options   )
MemoryFilec                       s   e Zd ZdZdZdZdZ					d fdd	Zedd	 Z	d
d Z
dd Zedd Zedd ZdddZdddZdddZ  ZS ) GistFileSystema  
    Interface to files in a single GitHub Gist.

    Provides read-only access to a gist's files. Gists do not contain
    subdirectories, so file listing is straightforward.

    Parameters
    ----------
    gist_id: str
        The ID of the gist you want to access (the long hex value from the URL).
    filenames: list[str] (optional)
        If provided, only make a file system representing these files, and do not fetch
        the list of all files for this gist.
    sha: str (optional)
        If provided, fetch a particular revision of the gist. If omitted,
        the latest revision is used.
    username: str (optional)
        GitHub username for authentication.
    token: str (optional)
        GitHub personal access token (required if username is given), or.
    timeout: (float, float) or float, optional
        Connect and read timeouts for requests (default 60s each).
    kwargs: dict
        Stored on `self.request_kw` and passed to `requests.get` when fetching Gist
        metadata or reading ("opening") a file.
    Zgistz&https://api.github.com/gists/{gist_id}z,https://api.github.com/gists/{gist_id}/{sha}Nc                    sj   t    || _|| _|| _|d ur|d u rtd|| _|| _|| _|d ur)|nd| _	| 
 | jd< d S )NzUser auth requires a token)<   r    )super__init__gist_id	filenamessha
ValueErrorusernametoken
request_kwtimeout_fetch_file_listdircache)selfr   r   r   r   r   r   kwargs	__class__ j/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/fsspec/implementations/gist.pyr   (   s   

zGistFileSystem.__init__c                 C   sX   ddddi}| | j | jr| jr| j| jf|d< |S | jr*d| j |d d< |S )z?Auth parameters passed to 'requests' if we have username/token.headerszapplication/vnd.github+jsonz
2022-11-28)AcceptzX-GitHub-Api-VersionauthzBearer Authorization)updater   r   r   )r   kwr   r   r   r!   A   s   zGistFileSystem.kwc                 C   s|   | j r| jj| j| j d}n| jj| jd}tj|fd| ji| j}|j	dkr6t
d| j d| j p2d |  | S )z[
        Fetch the JSON metadata for this gist (possibly for a specific revision).
        )r   r   )r   r     zGist not found: @Zlatest)r   gist_rev_urlformatr   gist_urlrequestsgetr   r!   status_codeFileNotFoundErrorraise_for_statusjson)r   urlrr   r   r   _fetch_gist_metadataQ   s   
z#GistFileSystem._fetch_gist_metadatac              	   C   s   |   }| jr$|di }i }| jD ]}||vrt||| ||< qn|di }g }| D ]\}}|du r9q0||d|dd|dd q0|S )zz
        Returns a list of dicts describing each file in the gist. These get stored
        in self.dircache[""].
        filesNfilesizer   raw_url)nametyper2   r3   )r/   r   r(   r*   itemsappend)r   metaZavailable_filesr0   fnoutfnamefinfor   r   r   r   b   s,   

zGistFileSystem._fetch_file_listc                 C   s   t |d|}|dS )z=
        Remove 'gist://' from the path, if present.
        path/)r   r(   lstrip)clsr=   r   r   r   _strip_protocol   s   
zGistFileSystem._strip_protocolc                 C   s   t | }i }d|v r|d r|d |d< d|v r"|d r"|d |d< d|v r0|d r0|d |d< d|v r_|d r_|d ddd	d
 }t|dkr_|d rT|d |d< |d r_|d g|d< |S )z
        Parse 'gist://' style URLs into GistFileSystem constructor kwargs.
        For example:
          gist://:TOKEN@<gist_id>/file.txt
          gist://username:TOKEN@<gist_id>/file.txt
        r   passwordr   hostr   r=   r>   r   Nr   r   r   r   )r   rsplitlen)r=   sor:   
path_partsr   r   r   _get_kwargs_from_urls   s    z$GistFileSystem._get_kwargs_from_urlsr	   Fc                    sd   |   pd  dkr| jd }n| jd } fdd|D }|s%t |r)|S tdd |D S )a  
        List files in the gist. Gists are single-level, so any 'path' is basically
        the filename, or empty for all files.

        Parameters
        ----------
        path : str, optional
            The filename to list. If empty, returns all files in the gist.
        detail : bool, default False
            If True, return a list of dicts; if False, return a list of filenames.
        r	   c                       g | ]
}|d   kr|qS r4   r   .0fr=   r   r   
<listcomp>       z%GistFileSystem.ls.<locals>.<listcomp>c                 s   s    | ]}|d  V  qdS )r4   Nr   rL   r   r   r   	<genexpr>   s    z$GistFileSystem.ls.<locals>.<genexpr>)rA   r   r*   sorted)r   r=   detailr   results	all_filesr   rO   r   ls   s   
zGistFileSystem.lsrbc           	         s   |dkrt d|    fdd| jd D }|st |d }|d}|s1td  tj|fd	| ji| j}|jd
krGt |	  t
 d|jS )z3
        Read a single file from the gist.
        rX   z'GitHub Gist FS is read-only (no write).c                    rJ   rK   r   rL   rO   r   r   rP      rQ   z(GistFileSystem._open.<locals>.<listcomp>r	   r   r3   zNo raw_url for file: r   r"   N)NotImplementedErrorrA   r   r*   r(   r'   r   r!   r)   r+   r   content)	r   r=   mode
block_sizer   matchesr<   r3   r.   r   rO   r   _open   s   


zGistFileSystem._openraisec           
      K   s   | j ||d}i }|D ]A}z| |d}| ||< W d   n1 s%w   Y  W q tyL }	 z|dkr9|	|dkr>n|	||< W Y d}	~	qd}	~	ww t|dkr]|d |kr]|| S |S )z
        Return {path: contents} for the given file or files. If 'recursive' is True,
        and path is empty, returns all files in the gist.
        )	recursiverX   Nr_   Zomitr   r   )Zexpand_pathopenreadr*   rF   )
r   r=   r`   Zon_errorr   pathsr:   prN   er   r   r   cat   s&   zGistFileSystem.cat)NNNNN)r	   F)rX   N)Fr_   )__name__
__module____qualname____doc__protocolr&   r$   r   propertyr!   r/   r   classmethodrA   staticmethodrI   rW   r^   rf   __classcell__r   r   r   r   r      s,    
 




r   )r'   specr   utilsr   Zmemoryr   r   r   r   r   r   <module>   s
    