
    gB                     N    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 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).

    Given that code files tend to be small, and that github does not support
    retrieving partial content, we always fetch whole files.

    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}z;https://raw.githubusercontent.com/{org}/{repo}/{sha}/{path}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       y )Nz%Auth required both username and tokenz)https://api.github.com/repos/{org}/{repo}orgrepotimeoutdefault_branch  )super__init__r   r   
ValueErrorusernametokenr   requestsgetformatkwraise_for_statusjsonrootls)selfr   r   shar   r   r   kwargsur	__class__s             R/var/www/openai/venv/lib/python3.12/site-packages/fsspec/implementations/github.pyr   zGithubFileSystem.__init__+   s     	"6"	$/DEE 
"DL;;ASt,6:llFJggA  &&(+,C	    c                 R    | j                   rd| j                   | j                  fiS i S )Nauth)r   r   r"   s    r(   r   zGithubFileSystem.kwC   s&    ==T]]DJJ788	r)   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.reposI   sf    " LL%w&7&?%@+fUKK
 	
)*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/r0   z/tagsr   r1   r   r   r   r   r   r   r   r   r"   r&   ts      r(   tagszGithubFileSystem.tagsa   sv     LL+DHH:QtyykG
LL
 gg

 	
#$668,8a&	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 repor7   r0   z	/branchesr   r1   r8   r9   s      r(   brancheszGithubFileSystem.branchesl   sv     LL+DHH:QtyykK
LL
 gg

 	
#$668,8a&	8,,,r<   c                 4    | j                   | j                  dS )z#Named references, tags and branchesr;   r>   r@   r,   s    r(   refszGithubFileSystem.refsw   s     		t}}==r)   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   Nr0   T)r#   _shar1   r   typefiler#   )r   r   r#   r     	directory)blobtreerI   pathmodesize)r1   rK   rD   rL   r#   )_strip_protocolr    rstripsplitr!   FileNotFoundErrordircacher   r   urlr   r   r   r   r   status_coder   r   sorted)r"   rJ   detailr#   rC   r$   partsso_farpartoutor&   typesfs                 r(   r!   zGithubFileSystem.ls|   s]     ##D)2:#$))D<KK$**3/EF#$))Dggfd$g?#*D8"%=#Q6f)<q#=+D11!fv;&( #u#5z  t}}$DIIt3D(DDHH499$G ''A
 }}#'-- #[9E &)
 *AV9% 7;D3J62&	fI!!F),EE&!,U8 *  
 tyy$''&)d#--%CJc2c1V9c233M >(
$ 3s   HHAHH!c                 8    | j                   j                          y N)rQ   clear)r"   rJ   s     r(   invalidate_cachez!GithubFileSystem.invalidate_cache   s    r)   c                 h    t        |      }d|vrt        | 	  |      S |d   j                  d      S )Nr   rJ   r0   )r   r   rM   lstrip)r2   rJ   optsr'   s      r(   rM   z GithubFileSystem._strip_protocol   s:    $T*T!7*400F|""3''r)   c                 X    t        |       }d|vri S |d   |d   d}|d   r|d   |d<   |S )Nr   passwordr   hostr#   r   )rJ   rc   rY   s      r(   _get_kwargs_from_urlsz&GithubFileSystem._get_kwargs_from_urls   sF    $T*T!I:&Z0@A<fCJ
r)   c                 t   |dk7  rt         | j                  j                  | j                  | j                  ||xs | j
                        }t        j                  |fd| j                  i| j                  }	|	j                  dk(  rt        |      |	j                          t        d d |	j                        S )Nrbr   r   rJ   r#   r   rF   )NotImplementedErrorrurlr   r   r   r    r   r   r   r   rS   rP   r   r   content)
r"   rJ   rK   
block_size
autocommitcache_optionsr#   r$   rR   r&   s
             r(   _openzGithubFileSystem._open   s     4<%%iityyt9I		  
 LL>dll>dgg>==C#D))	$aii00r)   c           
         | j                  ||      }|D cg c]B  \  }}| j                  j                  | j                  | j                  || j
                        D }}}t        j                  d      }	|	j                  |d      }
t        |
j                         |      D ci c]  \  \  }}}|| c}}}S c c}}w c c}}}w )N)	recursiverj   httpreturn)on_error)expand_pathrl   r   r   r   r    fsspec
filesystemcatzipitems)r"   rJ   rs   rv   r$   pathsr%   shurlsfsdatakvs                r(   rz   zGithubFileSystem.cat   s       ; 
2 II		tyyQ 	 
 v&vvdXv.(+DJJL$(?@(?&1a!1(?@@
 As   AB>)C)NNNN)T)FNNr^   )ri   NTNN)Fraise)__name__
__module____qualname____doc__rR   rl   protocolr   r   propertyr   classmethodr5   r;   r>   rA   r!   r`   rM   staticmethodrg   rq   rz   __classcell__)r'   s   @r(   r
   r
      s    2 FCHDHG GK0  
 3 3. - - - - > >@4D ( (   1*Ar)   r
   )	r   rx   specr   utilsr   memoryr   r
   r   r)   r(   <module>r      s%      % ) 
cA) cAr)   