Ë
    #täi…-  ã                   óV   — d dl Z d dlZ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Z	 dˆ fd„	Ze	d„ «       Z
edd„«       Ze	d	„ «       Ze	d
„ «       Ze	d„ «       Zdd„Zdd„Zeˆ fd„«       Zed„ «       Z	 	 	 	 dd„Zdd„Zdd„Zd„ Zˆ xZS )ÚGithubFileSystema–  Interface to files in github

    An instance of this class provides the files residing within a remote github
    repository. You may specify a point in the repos history, by SHA, branch
    or tag (default is current master).

    For files less than 1 MB in size, file content is returned directly in a
    MemoryFile. For larger files, or for files tracked by git-lfs, file content
    is returned as an HTTPFile wrapping the ``download_url`` provided by the
    GitHub API.

    When using fsspec.open, allows URIs of the form:

    - "github://path/file", in which case you must specify org, repo and
      may specify sha in the extra args
    - 'github://org:repo@/precip/catalog.yml', where the org and repo are
      part of the URI
    - 'github://org:repo@sha/precip/catalog.yml', where the sha is also included

    ``sha`` can be the full or abbreviated hex of the commit you want to fetch
    from, or a branch or tag name (so long as it doesn't contain special characters
    like "/", "?", which would have to be HTTP-encoded).

    For authorised access, you must provide username and token, which can be made
    at https://github.com/settings/tokens
    z9https://api.github.com/repos/{org}/{repo}/git/trees/{sha}zChttps://api.github.com/repos/{org}/{repo}/contents/{path}?ref={sha}Úgithub)é<   r   c                 óâ  •— t        ‰| �  d	i |¤Ž || _        || _        |d u |d u z  rt	        d«      ‚|| _        || _        |�|| _        |€bd}t        j                  |j                  ||¬«      fd| j                  i| j                  ¤Ž}	|	j                  «        |	j                  «       d   }|| _        | j                  d«       	 ddlm}
  |
d	i |¤Ž| _        y # t&        $ r
 d | _        Y y w xY w)
Nz%Auth required both username and tokenz)https://api.github.com/repos/{org}/{repo}©ÚorgÚrepoÚtimeoutÚdefault_branchÚ r   )ÚHTTPFileSystem© )ÚsuperÚ__init__r   r   Ú
ValueErrorÚusernameÚtokenr   ÚrequestsÚgetÚformatÚkwÚraise_for_statusÚjsonÚrootÚlsÚhttpr   Úhttp_fsÚImportError)Úselfr   r   Úshar   r   r   ÚkwargsÚuÚrr   Ú	__class__s              €úl/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/fsspec/implementations/github.pyr   zGithubFileSystem.__init__,   sù   ø€ ô 	‰ÑÑ"˜6Ò"ØˆŒØˆŒ	Ø˜Ð ¨$ Ò/ÜÐDÓEÐEØ ˆŒØˆŒ
ØÐØ"ˆDŒLØˆ;à;ˆAÜ—‘Ø—‘˜S t�Ó,ñØ6:·l±lðØFJÇgÁgñˆAð ×ÑÔ Ø—&‘&“(Ð+Ñ,ˆCàˆŒ	Ø�‰�Œð	 Ý,á)Ñ3¨FÑ3ˆD�LøÜò 	 ØˆDŽLð	 ús   ÃC ÃC.Ã-C.c                 óR   — | j                   rd| j                   | j                  fiS i S )NÚauth)r   r   ©r&   s    r,   r   zGithubFileSystem.kwJ   s&   € à�=Š=Ø˜TŸ]™]¨D¯J©JÐ7Ð8Ð8Øˆ	ó    c                 óÈ   — t        j                  dddg|   › d|› d�| j                  ¬«      }|j                  «        |j	                  «       D �cg c]  }|d   ‘Œ	 c}S c c}w )av  List repo names for given org or user

        This may become the top level of the FS

        Parameters
        ----------
        org_or_user: str
            Name of the github org or user to query
        is_org: bool (default True)
            Whether the name is an organisation (True) or user (False)

        Returns
        -------
        List of string
        zhttps://api.github.com/ÚusersÚorgsÚ/z/repos)r   Úname)r   r   r   r   r    )ÚclsÚorg_or_userÚis_orgr*   r   s        r,   ÚreposzGithubFileSystem.reposP   sf   € ô" �L‰LØ% w°Ð&7¸Ñ&?Ð%@ÀÀ+ÀÈfÐUØ—K‘Kô
ˆð 	
×ÑÔØ)*¯©¬Ó2© ��V“¨Ñ2Ð2ùÒ2s   ÁAc                 óü   — t        j                  d| j                  › d| j                  › d�fd| j                  i| j
                  ¤Ž}|j                  «        |j                  «       D �cg c]  }|d   ‘Œ	 c}S c c}w )zNames of tags in the repoúhttps://api.github.com/repos/r4   z/tagsr   r5   ©r   r   r   r   r   r   r   r    ©r&   r*   Úts      r,   ÚtagszGithubFileSystem.tagsh   sv   € ô �L‰LØ+¨D¯H©H¨:°Q°t·y±y°kÀÐGñ
à—L‘Lð
ð �g‰gñ
ˆð
 	
×ÑÔØ#$§6¡6¤8Ó,¡8˜a��&“	 8Ñ,Ð,ùÒ,ó   Á*A9c                 óü   — t        j                  d| j                  › d| j                  › d�fd| j                  i| j
                  ¤Ž}|j                  «        |j                  «       D �cg c]  }|d   ‘Œ	 c}S c c}w )zNames of branches in the repor;   r4   z	/branchesr   r5   r<   r=   s      r,   ÚbrancheszGithubFileSystem.branchess   sv   € ô �L‰LØ+¨D¯H©H¨:°Q°t·y±y°kÀÐKñ
à—L‘Lð
ð �g‰gñ
ˆð
 	
×ÑÔØ#$§6¡6¤8Ó,¡8˜a��&“	 8Ñ,Ð,ùÒ,r@   c                 ó4   — | j                   | j                  dœS )z#Named references, tags and branches©r?   rB   rD   r/   s    r,   ÚrefszGithubFileSystem.refs~   s   € ð Ÿ	™	¨t¯}©}Ñ=Ð=r0   c           
      óL  — | j                  |«      }|dk(  r|xs | j                  }|€œ|j                  d«      j                  d«      }d}|xs | j                  }|D ]e  }| j	                  |d||¬«      }	||rd|z   n|z  }|	D �
cg c]  }
|
d   |k(  sŒ|
‘Œ }	}
|	st        |«      ‚|	d   }	|	d   d	k(  r|r|	gc S |c S |	d
   }Œg || j                  vs|| j                  dfv�rt        j                  | j                  j                  | j                  | j                  |¬«      fd| j                  i| j                  ¤Ž}|j                  dk(  rt        |«      ‚|j!                  «        d	ddœ}|j#                  «       d   D �cg c]=  }|d   |v r4|r|dz   |d   z   n|d   |d   ||d      |j                  dd«      |d
   dœ‘Œ? }	}|| j                  dfv r|	| j                  |<   n| j                  |   }	|r|	S t%        |	D �cg c]  }|d   ‘Œ	 c}«      S c c}
w c c}w c c}w )a	  List files at given path

        Parameters
        ----------
        path: str
            Location to list, relative to repo root
        detail: bool
            If True, returns list of dicts, one per file; if False, returns
            list of full filenames only
        sha: str (optional)
            List at the given point in the repo history, branch or tag name or commit
            SHA
        _sha: str (optional)
            List this specific tree object (used internally to descend into trees)
        r   Nr4   T)r'   Ú_shar5   r   ÚtypeÚfiler'   )r   r   r'   r   é”  Ú	directory)ÚblobÚtreerM   ÚpathÚmodeÚsize)r5   rO   rH   rP   r'   )Ú_strip_protocolr!   ÚrstripÚsplitr"   ÚFileNotFoundErrorÚdircacher   r   Úurlr   r   r   r   r   Ústatus_coder   r    Úsorted)r&   rN   Údetailr'   rG   r(   ÚpartsÚso_farÚpartÚoutÚor*   ÚtypesÚfs                 r,   r"   zGithubFileSystem.lsƒ   s]  € ð  ×#Ñ# DÓ)ˆØ�2Š:ØÒ#˜$Ÿ)™)ˆDØˆ<Ø—K‘K Ó$×*Ñ*¨3Ó/ˆEØˆFØÒ#˜$Ÿ)™)ˆDÛ�Ø—g‘g˜f d°¸$�gÓ?�Ø©˜# š*°DÑ8�Ù"%Ó=¡#˜Q¨¨6©°fÓ)<’q #�Ð=ÙÜ+¨DÓ1Ð1Ø˜!‘f�Ø�v‘; &Ò(ÙØ #˜ušà#šØ˜5‘z‘ð ð �t—}‘}Ñ$¨°D·I±I¸tÐ3DÒ(DÜ—‘Ø—‘—‘ D§H¡H°4·9±9À$�ÓGñàŸ™ðð —'‘'ñˆAð
 �}‰} Ò#Ü'¨Ó-Ð-Ø×ÑÔ Ø#¨[Ñ9ˆEð Ÿ™› &Ò)ó
ñ *�AØ�V‘9 Ñ%ñ 7;˜D 3™J¨¨6©Ò2ÀÀ&Á	Ø˜f™IØ! ! F¡)Ñ,ØŸE™E &¨!Ó,Ø˜U™8óð *ð ð 
ð �t—y‘y $Ð'Ñ'Ø&)�—‘˜dÒ#à—-‘- Ñ%ˆCÙØˆJä©cÓ2©c¨˜1˜V›9¨cÑ2Ó3Ð3ùòM >ùò(
ùò$ 3s   ÂHÂHÆAHÈH!c                 ó8   — | j                   j                  «        y ©N)rU   Úclear)r&   rN   s     r,   Úinvalidate_cachez!GithubFileSystem.invalidate_cacheÅ   s   € Ø�‰×ÑÕr0   c                 óh   •— t        |«      }d|vrt        ‰| �	  |«      S |d   j                  d«      S )Nr   rN   r4   )r   r   rQ   Úlstrip)r6   rN   Úoptsr+   s      €r,   rQ   z GithubFileSystem._strip_protocolÈ   s:   ø€ ä$ TÓ*ˆØ˜TÑ!Ü‘7Ñ*¨4Ó0Ð0Ø�F‰|×"Ñ" 3Ó'Ð'r0   c                 óX   — t        | «      }d|vri S |d   |d   dœ}|d   r|d   |d<   |S )Nr   Úpasswordr   Úhostr'   r   )rN   rg   r]   s      r,   Ú_get_kwargs_from_urlsz&GithubFileSystem._get_kwargs_from_urlsÏ   sF   € ä$ TÓ*ˆØ˜TÑ!ØˆIØ˜:Ñ&°°ZÑ0@ÑAˆØ�Š<Ø˜f™ˆC�‰JØˆ
r0   c                 óR  — |dk7  rt         ‚| j                  j                  | j                  | j                  ||xs | j
                  ¬«      }t        j                  |fd| j                  i| j                  ¤Ž}|j                  dk(  rt        |«      ‚|j                  «        |j                  «       }	|	d   r6t        j                  |	d   «      }
|
j!                  d«      st#        d d |
«      S | j$                  €t'        d«      ‚ | j$                  j(                  |	d   f|||d	œ|¤ŽS )
NÚrb©r   r   rN   r'   r   rJ   Úcontents#   version https://git-lfs.github.com/zRPlease install fsspec[http] to access github files >1 MB or git-lfs tracked files.Údownload_url)rO   Ú
block_sizeÚcache_options)ÚNotImplementedErrorÚcontent_urlr   r   r   r!   r   r   r   r   rW   rT   r   r    Úbase64Ú	b64decodeÚ
startswithr   r$   r%   Úopen)r&   rN   rO   rq   rr   r'   r(   rV   r*   Úcontent_jsonro   s              r,   Ú_openzGithubFileSystem._openÙ   s0  € ð �4Š<Ü%Ð%ð ×Ñ×%Ñ%Ø—‘˜tŸy™y¨t¸Ò9IÀÇ	Á	ð &ó 
ˆô
 �L‰L˜Ñ> d§l¡lÐ>°d·g±gÑ>ˆØ�=‰=˜CÒÜ# DÓ)Ð)Ø	×ÑÔØ—v‘v“xˆð ˜	Ò"Ü×&Ñ& |°IÑ'>Ó?ˆGð ×%Ñ%Ð&LÔMÜ! $¨¨gÓ6Ð6ð
 �<‰<ÐÜð,óð ð !ˆt�|‰|× Ñ Ø˜Ñ(ð
àØ!Ø'ñ	
ð
 ñ
ð 	
r0   c                 ór   — | j                  |||¬«      }t        |«      D ]  }| j                  ||¬«       Œ y )N)Ú	recursiveÚmaxdepth)Úmessage)Úexpand_pathÚreversedÚrm_file)r&   rN   r|   r}   r~   Úps         r,   ÚrmzGithubFileSystem.rm  s7   € Ø×Ñ °	ÀHÐÓMˆÜ˜$–ˆAØ�L‰L˜ GˆLÕ,ñ  r0   c                 óÀ  — | j                   st        d«      ‚| j                  |«      }| j                  |«      }|sµ| j                  j                  | j                  | j                  |j                  d«      | j                  ¬«      }t        j                  |fd| j                  i| j                  ¤Ž}|j                  dk(  rt        |«      ‚|j!                  «        |j#                  «       d   }| j                  j                  | j                  | j                  || j                  ¬«      }| j                  }|xs d|› �|dœ|rd	|ini ¥}	t        j$                  |f|	| j                  d
œ| j                  ¤Ž}|j#                  «       j                  dd«      }
t'        j(                  d|
«      rd}t        |«      ‚|j!                  «        | j+                  |«       y)aW  
        Remove a file from a specified branch using a given commit message.

        Since Github DELETE operation requires a branch name, and we can't reliably
        determine whether the provided SHA refers to a branch, tag, or commit, we
        assume it's a branch. If it's not, the user will encounter an error when
        attempting to retrieve the file SHA or delete the file.

        Parameters
        ----------
        path: str
            The file's location relative to the repository root.
        message: str, optional
            The commit message for the deletion.
        zAuthentication requiredr4   rn   r   rJ   r'   zDelete )r~   r'   Úbranch)r    r   r~   r   zBranch .+ not foundzTRemove only works when the filesystem is initialised from a branch or default (None)N)r   r   rQ   Ú_get_sha_from_cachert   r   r   r   rf   r!   r   r   r   r   rW   rT   r   r    ÚdeleteÚreÚsearchrd   )r&   rN   r~   r(   r'   rV   r*   Ú
delete_urlr…   ÚdataÚerror_messageÚerrors               r,   r�   zGithubFileSystem.rm_file  s¥  € ð" �}Š}ÜÐ6Ó7Ð7à×#Ñ# DÓ)ˆð ×&Ñ& tÓ,ˆÙØ×"Ñ"×)Ñ)Ø—H‘H 4§9¡9°4·;±;¸sÓ3CÈÏÉð *ó ˆCô —‘˜SÑB¨$¯,©,ÐB¸$¿'¹'ÑBˆAØ�}‰} Ò#Ü'¨Ó-Ð-Ø×ÑÔ Ø—&‘&“(˜5‘/ˆCð ×%Ñ%×,Ñ,Ø—‘˜tŸy™y¨t¸¿¹ð -ó 
ˆ
ð —‘ˆàÒ2 G¨D¨6Ð"2Øñ
ñ &,�˜&Ñ!°ð
ˆô �O‰O˜JÐS¨T¸4¿<¹<ÑSÈ4Ï7É7ÑSˆØŸ™›Ÿ™ Y°Ó3ˆÜ�9‰9Ð+¨]Ô;ØjˆEÜ˜UÓ#Ð#Ø	×ÑÔà×Ñ˜dÕ#r0   c                 óž   — | j                   j                  «       D ]0  }|D ])  }|j                  d«      }|sŒ||k(  sŒd|v sŒ"|d   c c S  Œ2 y )Nr5   r'   )rU   Úvaluesr   )r&   rN   ÚentriesÚentryÚ
entry_paths        r,   r†   z$GithubFileSystem._get_sha_from_cacheG  sP   € Ø—}‘}×+Ñ+Ö-ˆGÛ �Ø"ŸY™Y vÓ.�
Ú *°Ó"4¸À%ºØ  ™<Ô'ñ !ð .ð
 r0   )NNNN)T)FNNrb   )rm   NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rV   rt   Úprotocolr   r   Úpropertyr   Úclassmethodr9   r?   rB   rE   r"   rd   rQ   Ústaticmethodrk   rz   rƒ   r�   r†   Ú__classcell__)r+   s   @r,   r
   r
      sç   ø„ ñð6 F€CØW€KØ€HØ€Gð GKõ ð< ñó ðð
 ò3ó ð3ð. ñ-ó ð-ð ñ-ó ð-ð ñ>ó ð>ó@4óDð ó(ó ð(ð ñó ðð ØØØó1
óf-ó
4$ölr0   r
   )
ru   rˆ   r   Úspecr   Úutilsr   Úmemoryr   r
   r   r0   r,   Ú<module>rŸ      s&   ðÛ Û 	ã å %Ý )Ý ôBÐ)õ Br0   