
    |;3h                      F    d dl Z ddlmZ ddlmZ ddlmZ  G d de      Zy)	    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 )GistFileSystema9  
    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 (required if token is given).
    token : str (optional)
        GitHub personal access token (required if username is given).
    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}c                     t         |           || _        || _        || _        |d u |d u z  r|s|rt        d      || _        || _        || _        ||nd| _	        | j                         | j                  d<   y )Nz2Auth requires both username and token, or neither.)<   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	           [/var/www/html/audio-gradio/venv/lib/python3.12/site-packages/fsspec/implementations/gist.pyr   zGistFileSystem.__init__(   s     	"$/5 !UVV 
 ")"5w8 !113b    c                     | j                   2| j                  &d| j                   | j                  fi| j                  S | j                  S )z?Auth parameters passed to 'requests' if we have username/token.auth)r   r   r   )r   s    r   kwzGistFileSystem.kwC   sB     ==$)?T]]DJJ7K4??KKr   c                    | j                   r2| j                  j                  | j                  | j                         }n&| j                  j                  | j                        }t        j                  |fd| j                  i| j                  }|j                  dk(  r)t        d| j                   d| j                   xs d       |j                          |j                         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_metadataJ   s     88##**4<<TXX*NC--&&t||&<CLL>dll>dgg>==C#"4<<.$((2Fh1GH  	
vvxr   c           	         | j                         }| j                  r=|j                  di       }i }| j                  D ]  }||vrt        |      ||   ||<    n|j                  di       }g }|j	                         D ]=  \  }}|	|j                  |d|j                  dd      |j                  d      d       ? |S )zz
        Returns a list of dicts describing each file in the gist. These get stored
        in self.dircache[""].
        filesfilesizer   raw_url)nametyper5   r6   )r1   r   r*   r,   itemsappend)r   metaavailable_filesr3   fnoutfnamefinfos           r   r   zGistFileSystem._fetch_file_list[   s    
 ((*>>"hhw3OEnn 0_,+B//+B/b	0
 HHWb)E!KKM 	LE5}JJ!"!IIfa0$yy3		 
r   c                 Z    t        |      j                  d|      }|j                  d      S )z=
        Remove 'gist://' from the path, if present.
        path/)r   r*   lstrip)clsrB   s     r   _strip_protocolzGistFileSystem._strip_protocol{   s*     %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   rA|d   j                  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   rB   rC   r   Nr   r   r   r   )r   rsplitlen)rB   sor>   
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2237J:!#a=!+ACJa=(21C$
r   c                     | j                  |xs d      }|dk(  r| j                  d   }n4| j                  d   }|D cg c]  }|d   |k(  s| }}|st        |      |r|S t        d |D              S c c}w )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   r7   c              3   &   K   | ]	  }|d      yw)r7   N ).0fs     r   	<genexpr>z$GistFileSystem.ls.<locals>.<genexpr>   s     5!F)5s   )rF   r   r,   sorted)r   rB   detailr   results	all_filesrT   s          r   lszGistFileSystem.ls   s     ##DJB/2:mmB'G b)I"+AQqyD/@qAGA'--N5W555 Bs   A5A5c                    |dk7  rt        d      | j                  |      }| j                  d   D cg c]  }|d   |k(  s| }}|st        |      |d   }|j	                  d      }|st        d|       t        j                  |fd| j                  i| j                  }	|	j                  d	k(  rt        |      |	j                          t        |d
|	j                        S c c}w )z3
        Read a single file from the gist.
        rbz'GitHub Gist FS is read-only (no write).r   r7   r   r6   zNo raw_url for file: r   r#   N)NotImplementedErrorrF   r   r,   r*   r)   r   r!   r+   r-   r   content)
r   rB   mode
block_sizer   rT   matchesr@   r6   r0   s
             r   _openzGistFileSystem._open   s     4<%&OPP##D)"mmB/E1V93D1EE#D))
))I&#&;D6$BCCLLB$,,B$''B==C#D))	$aii00 Fs   C'C'c                    | j                  ||      }i }|D ]1  }	 | j                  |d      5 }|j                         ||<   ddd       3 |S # 1 sw Y   xY w# t        $ r}	|dk(  r|	|dk(  rn|	||<   Y d}	~	bd}	~	ww xY w)z
        Return {path: contents} for the given file or files. If 'recursive' is True,
        and path is empty, returns all files in the gist.
        )	recursiver\   Nraiseomit)expand_pathopenreadr,   )
r   rB   rd   on_errorr   pathsr>   prT   es
             r   catzGistFileSystem.cat   s    
    ; 
	A	YYq$' &1VVXCF&
	 
& &$ w&G'CFs.   AAAA	A	A?#A::A?)NNNNN)r   F)r\   N)Fre   )__name__
__module____qualname____doc__protocolr(   r&   r   propertyr!   r1   r   classmethodrF   staticmethodrO   rZ   rb   rn   __classcell__)r   s   @r   r	   r	      s    6 H7HAL
 46  "@      86610r   r	   )r)   specr   utilsr   memoryr   r	   rR   r   r   <module>r{      s      % ) `' `r   