
    2'h#                     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 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              d/home/kushmeetdev/Regenta/Chatbot/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		 ,)3F3DL 	 DL	 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.kwK   s&    ==T]]DJJ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.reposQ   sb    " 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/r4   z/tagsr   r5   r   r   r   r   r   r   r   r    r&   r*   ts      r,   tagszGithubFileSystem.tagsi   sr     LL+DHH:QtyykG
LL
 gg

 	
#$668,a&	,,,   *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.branchest   sr     LL+DHH:QtyykK
LL
 gg

 	
#$668,a&	,,,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   sX     ##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 &)
 V9% 7;D3J62&	fI!!F),EE&!,U8
C 
 tyy$''&)d#--%CJc21V9233M >(
$ 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:&Z0@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   rp   rq   r'   r(   rV   r*   content_jsonrn   s              r,   _openzGithubFileSystem._open   s0    4<%% %%tyyt9I		 & 

 LL>dll>dgg>==C#D))	vvx 	"&&|I'>?G %%&LM!$g66
 <<,  !t||  (
!'	

 
 	
r0   )NNNN)T)FNNrb   )rm   NNN)__name__
__module____qualname____doc__rV   rs   protocolr   r   propertyr   classmethodr9   r?   rB   rE   r"   rd   rQ   staticmethodrk   ry   __classcell__)r+   s   @r,   r
   r
      s    6 FCWKHG GK <  
 3 3. - - - - > >@4D ( (   1
r0   r
   )	rt   r   specr   utilsr   memoryr   r
   r   r0   r,   <module>r      s#      % ) 

) 
r0   