
    giP!                     N    d dl Z ddlmZ ddlmZ ddlmZ  G d de          ZdS )	    N   )AbstractFileSystem)infer_storage_options   )
MemoryFilec                        e Zd ZdZdZdZdZ	 	 	 	 	 d fd	Zed             Z	d Z
d	 Zed
             Zed             ZddZddZddZ x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.
    gistz&https://api.github.com/gists/{gist_id}z,https://api.github.com/gists/{gist_id}/{sha}Nc                    t                                                       || _        || _        || _        ||t          d          || _        || _        || _        ||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__s	           s/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/fsspec/implementations/gist.pyr   zGistFileSystem.__init__(   s     	"EM9::: 
 ")"5ww8 !1133b    c                     ddddi}|                     | j                   | j        r| j        r| j        | j        f|d<   n| 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   kws     r   r%   zGistFileSystem.kwA   s     7(4 
 			$/"""= 	DTZ 	D-4BvJJZ 	D-Ctz-C-CByM/*	r   c                    | j         r'| j                            | j        | j                   }n | j                            | j                  }t          j        |fd| j        i| j        }|j	        dk    r!t          d| j         d| j         pd           |                                 |                                S )z[
        Fetch the JSON metadata for this gist (possibly for a specific revision).
        )r   r   )r   r     zGist not found: @latest)r   gist_rev_urlformatr   gist_urlrequestsgetr   r%   status_codeFileNotFoundErrorraise_for_statusjson)r   urlrs      r   _fetch_gist_metadataz#GistFileSystem._fetch_gist_metadataQ   s     8 	=#**4<TX*NNCC-&&t|&<<CL>>dl>dg>>=C#H4<HH$(2FhHH   	
vvxxr   c           	         |                                  }| j        rA|                    di           }i }| j        D ] }||vrt          |          ||         ||<   !n|                    di           }g }|                                D ]I\  }}||                    |d|                    dd          |                    d          d           J|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typer9   r:   )r5   r   r.   r0   itemsappend)r   metaavailable_filesr7   fnoutfnamefinfos           r   r   zGistFileSystem._fetch_file_listb   s   
 ((**> 	*"hhw33OEn 0 0_,,+B///+B/b		0
 HHWb))E!KKMM 	 	LE5}JJ!"!IIfa00$yy33	     
r   c                 r    t          |                              d|          }|                    d          S )z=
        Remove 'gist://' from the path, if present.
        path/)r   r.   lstrip)clsrF   s     r   _strip_protocolzGistFileSystem._strip_protocol   s3     %T**..vt<<{{3r   c                    t          |           }i }d|v r|d         r|d         |d<   d|v r|d         r|d         |d<   d|v r|d         r|d         |d<   d|v rf|d         r^|d                             dd          d	d
         }t          |          dk    r'|d         r|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   rF   rG   r   Nr   r   r   r   )r   rsplitlen)rF   sorB   
path_partss       r   _get_kwargs_from_urlsz$GistFileSystem._get_kwargs_from_urls   s     #4((: nC
O:j>CLR<<BvJ<ZC	N R<<BvJ<F**3222337J:!##a= /!+ACJa= 7(21C$
r   r   Fc                     |                      pd          dk    r| j        d         }n,| j        d         }fd|D             }|st                    |r|S t          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    |S r;    .0frF   s     r   
<listcomp>z%GistFileSystem.ls.<locals>.<listcomp>   s'    AAAQqyD/@/@q/@/@/@r   c              3   &   K   | ]}|d          V  dS )r;   NrW   )rY   rZ   s     r   	<genexpr>z$GistFileSystem.ls.<locals>.<genexpr>   s&      55!F)555555r   )rJ   r   r0   sorted)r   rF   detailr   results	all_filess    `    r   lszGistFileSystem.ls   s     ##DJB//2::mB'GG b)IAAAA)AAAG .'--- 	6N55W555555r   rbc                    |dk    rt          d          |                               fd| j        d         D             }|st                    |d         }|                    d          }|st          d           t          j        |fd| j        i| j        }|j        d	k    rt                    |	                                 t          d
|j                  S )z3
        Read a single file from the gist.
        rc   z'GitHub Gist FS is read-only (no write).c                 ,    g | ]}|d          k    |S rV   rW   rX   s     r   r[   z(GistFileSystem._open.<locals>.<listcomp>   s'    EEE1V93D3D13D3D3Dr   r   r   r:   zNo raw_url for file: r   r'   N)NotImplementedErrorrJ   r   r0   r.   r-   r   r%   r/   r1   r   content)	r   rF   mode
block_sizer   matchesrD   r:   r4   s	    `       r   _openzGistFileSystem._open   s    4<<%&OPPP##D))EEEEdmB/EEE 	*#D)))
))I&& 	D#$BD$B$BCCCLBB$,B$'BB=C#D)))	$ai000r   raisec                 r   |                      ||          }i }|D ]s}	 |                     |d          5 }|                                ||<   ddd           n# 1 swxY w Y   I# t          $ r}	|dk    r|	|dk    rn|	||<   Y d}	~	ld}	~	ww xY 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.
        )	recursiverc   Nrl   omitr   r   )expand_pathopenreadr0   rP   )
r   rF   rn   on_errorr   pathsrB   prZ   es
             r   catzGistFileSystem.cat   s&   
    ;; 
	 
	A	YYq$'' &1VVXXCF& & & & & & & & & & & & & & &$   w&&G''CF u::??uQx4//t9
s:   A%AA%A	A% A	!A%%
B/BB)NNNNN)r   F)rc   N)Frl   )__name__
__module____qualname____doc__protocolr,   r*   r   propertyr%   r5   r   classmethodrJ   staticmethodrS   rb   rk   rw   __classcell__)r   s   @r   r	   r	      s        6 H7HAL
 4 4 4 4 4 42   X  "  @     [    \86 6 6 661 1 1 10       r   r	   )r-   specr   utilsr   memoryr   r	   rW   r   r   <module>r      s     % % % % % % ) ) ) ) ) )      i i i i i' i i i i ir   