
    sh                      F    S SK r SSKJr  SSKJr  SSKJr   " S S\5      rg)	    N   )AbstractFileSystem)infer_storage_options   )
MemoryFilec                      ^  \ rS rSrSrSrSrSr     SU 4S jjr\	S 5       r
S rS	 r\S
 5       r\S 5       rSS jrSS jrSS jrSrU =r$ )GistFileSystem   a  
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U ]  5         Xl        X l        X0l        US L US L -  (       a  U(       d  U(       a  [        S5      eX@l        XPl        Xpl        Ub  UOSU l	        U R                  5       U R                  S'   g )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	           o/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/fsspec/implementations/gist.pyr   GistFileSystem.__init__(   sw     	"$/5 !UVV 
 ")"5w8 !113b    c                     U R                   b3  U R                  b&  SU R                   U R                  40U R                  E$ U R                  $ )z?Auth parameters passed to 'requests' if we have username/token.auth)r   r   r   )r   s    r   kwGistFileSystem.kwC   sB     ==$)?T]]DJJ7K4??KKr    c                    U R                   (       a/  U R                  R                  U R                  U R                   S9nO#U R                  R                  U R                  S9n[
        R                  " U4SU R                  0U R                  D6nUR                  S:X  a.  [        SU R                   SU R                   =(       d    S 35      eUR                  5         UR                  5       $ )zK
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_metadata#GistFileSystem._fetch_gist_metadataJ   s     88##**4<<TXX*NC--&&t||&<CLL>dll>dgg>==C#"4<<.$((2Fh1GH  	
vvxr    c           	         U R                  5       nU R                  (       a>  UR                  S0 5      n0 nU R                   H  nXB;  a  [        U5      eX$   X4'   M     OUR                  S0 5      n/ nUR	                  5        H?  u  pgUc  M
  UR                  USUR                  SS5      UR                  S5      S.5        MA     U$ )zb
Returns a list of dicts describing each file in the gist. These get stored
in self.dircache[""].
filesfilesizer   raw_url)nametyper9   r:   )r4   r   r-   r/   itemsappend)r   metaavailable_filesr7   fnoutfnamefinfos           r   r   GistFileSystem._fetch_file_list[   s    
 ((*>>"hhw3OEnn,+B//+/	 %
 HHWb)E!KKMLE}JJ!"!IIfa0$yy3	 * 
r    c                 Z    [        U5      R                  SU5      nUR                  S5      $ )z-
Remove 'gist://' from the path, if present.
path/)r   r-   lstrip)clsrG   s     r   _strip_protocolGistFileSystem._strip_protocol{   s*     %T*..vt<{{3r    c                 h   [        U 5      n0 nSU;   a  US   (       a  US   US'   SU;   a  US   (       a  US   US'   SU;   a  US   (       a  US   US'   SU;   aV  US   (       aL  US   R                  SS5      S	S
 n[        U5      S:X  a%  US   (       a  US   US'   US   (       a	  US   /US'   U$ )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   rG   rH   r   Nr   r   r   r   )r   rsplitlen)rG   sorB   
path_partss       r   _get_kwargs_from_urls$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                    U R                  U=(       d    S5      nUS:X  a  U R                  S   nO=U R                  S   nU Vs/ s H  ofS   U:X  d  M  UPM     nnU(       d  [        U5      eU(       a  U$ [        S U 5       5      $ s  snf )aI  
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   r;   c              3   *   #    U  H	  oS    v   M     g7f)r;   N ).0fs     r   	<genexpr>$GistFileSystem.ls.<locals>.<genexpr>   s     5WF)Ws   )rK   r   r/   sorted)r   rG   detailr   results	all_filesr[   s          r   lsGistFileSystem.ls   s     ##DJB/2:mmB'G b)I"+A)QyD/@q)GA'--N5W555 Bs   B	B	c                    US:w  a  [        S5      eU R                  U5      nU R                  S    Vs/ s H  oUS   U:X  d  M  UPM     nnU(       d  [        U5      eUS   nUR	                  S5      nU(       d  [        SU 35      e[
        R                  " U4SU R                  0U R                  D6n	U	R                  S	:X  a  [        U5      eU	R                  5         [        US
U	R                  5      $ s  snf )z#
Read a single file from the gist.
rbz'GitHub Gist FS is read-only (no write).r   r;   r   r:   zNo raw_url for file: r   r&   N)NotImplementedErrorrK   r   r/   r-   r,   r   r#   r.   r0   r   content)
r   rG   mode
block_sizer   r[   matchesrD   r:   r3   s
             r   _openGistFileSystem._open   s     4<%&OPP##D)"mmB/E/V93D1/E#D))
))I&#&;D6$BCCLLB$,,B$''B==C#D))	$aii00 Fs   C8C8c                    U R                  XS9n0 nU H1  n U R                  US5       nUR                  5       Xg'   SSS5        M3     U$ ! , (       d  f       MF  = f! [         a#  n	US:X  a  U	eUS:X  a   Sn	A	Mg  XU'    Sn	A	Mq  Sn	A	ff = f)z
Return {path: contents} for the given file or files. If 'recursive' is True,
and path is empty, returns all files in the gist.
)	recursivere   Nraiseomit)expand_pathopenreadr/   )
r   rG   rn   on_errorr   pathsrB   pr[   es
             r   catGistFileSystem.cat   s    
    ;A	YYq$'1VVXCF ('  
 ('$ w&G'Fs9   AAA
A	AA
B
'B;BB
)r   r   r   r   r   r   r   )NNNNN)r   F)re   N)Fro   )__name__
__module____qualname____firstlineno____doc__protocolr+   r)   r   propertyr#   r4   r   classmethodrK   staticmethodrU   rb   rk   rx   __static_attributes____classcell__)r   s   @r   r	   r	      s    6 H7HAL
 46  "@      86610 r    r	   )r,   specr   utilsr   memoryr   r	   rY   r    r   <module>r      s      % ) `' `r    