
    |;3h-                     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              ]/var/www/html/audio-gradio/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.kwJ   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.reposP   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.tagsh   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.branchess   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   rq   rr   r'   r(   rV   r*   content_jsonro   s              r,   _openzGithubFileSystem._open   s0    4<%% %%tyyt9I		 & 

 LL>dll>dgg>==C#D))	vvx 	"&&|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  s<    	HM$ 	-ALLGL,	-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,""))HH4994;;s3C * C SB$,,B$''BA}}#'-- &&(5/C %%,,tyyt - 

 2GD6"2
 &,&!
 OOJST4<<S477SY399+];jE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  sZ    }}++- 	(G  ("YYv.
*"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WKHG GK <  
 3 3. - - - - > >@4D ( (   1
f-
4$lr0   r
   )
ru   r   r   specr   utilsr   memoryr   r
   r   r0   r,   <module>r      s&     	  % ) B) Br0   