
    Yh-                     ^    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	dS )	    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   Nc                     t                      j        d	i | || _        || _        |d u |d u z  rt	          d          || _        || _        ||| _        |cd}t          j	        |
                    ||          fd| j        i| j        }	|	                                 |	                                d         }|| _        |                     d           	 ddlm}
  |
d	i || _        d S # t&          $ r d | _        Y d S 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              o/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/fsspec/implementations/github.pyr   zGithubFileSystem.__init__,   sO    	""6"""	$/ 	FDEEE 
"DL;;ASt,, 6:lFJg A    &&((+,C		 ,,,,,,)>33F33DLLL 	  	  	 DLLLL	 s   C/ /DDc                 4    | j         rd| j         | j        fiS i S )Nauth)r   r   r&   s    r,   r   zGithubFileSystem.kwJ   s%    = 	9T]DJ788	    Tc                     t          j        dddg|          d| d| j                  }|                                 d |                                D             S )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   c                     g | ]
}|d          S namer   ).0r   s     r,   
<listcomp>z*GithubFileSystem.repos.<locals>.<listcomp>f   s    222V222r0   )r   r   r   r   r    )clsorg_or_useris_orgr*   s       r,   reposzGithubFileSystem.reposP   so    " LUw&7&?UU+UUUK
 
 
 	
222222r0   c                     t          j        d| j         d| j         dfd| j        i| j        }|                                 d |                                D             S )zNames of tags in the repohttps://api.github.com/repos/r4   z/tagsr   c                     g | ]
}|d          S r6   r   r8   ts     r,   r9   z)GithubFileSystem.tags.<locals>.<listcomp>q       ,,,a&	,,,r0   r   r   r   r   r   r   r   r    r&   r*   s     r,   tagszGithubFileSystem.tagsh   sz     LGDHGGtyGGG
 
L
 g
 

 	
,,16688,,,,r0   c                     t          j        d| j         d| j         dfd| j        i| j        }|                                 d |                                D             S )zNames of branches in the repor?   r4   z	/branchesr   c                     g | ]
}|d          S r6   r   rA   s     r,   r9   z-GithubFileSystem.branches.<locals>.<listcomp>|   rC   r0   rD   rE   s     r,   brancheszGithubFileSystem.branchess   sz     LKDHKKtyKKK
 
L
 g
 

 	
,,16688,,,,r0   c                      | j         | j        dS )z#Named references, tags and branchesrF   rI   rK   r/   s    r,   refszGithubFileSystem.refs~   s     	t}===r0   Fc                 |  
 |                                dk    r	|p| j        }|                    d                              d          }d
|p| j        }|D ]m}|                     
d||          }

rd|z   n|z  

fd|D             }|st                    |d         }|d         d	k    r|r|gc S c S |d
         }n| j        vs|| j        dfvrt          j        | j	        
                    | j        | j        |          fd| j        i| j        }	|	j        dk    rt                    |	                                 d	ddfd|	                                d         D             }|| j        dfv r
|| j        <   n| j                 }|r|S t%          d |D                       S )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'   _shac                 ,    g | ]}|d          k    |S r6   r   )r8   oso_fars     r,   r9   z'GithubFileSystem.ls.<locals>.<listcomp>   s'    ===Q6f)<)<q)<)<)<r0   r   typefiler'   )r   r   r'   r     	directory)blobtreec           	          g | ]V}|d          v rdz   |d         z   n|d         |d         |d                   |                     dd          |d         dWS )rR   r4   pathmodesizer   r'   )r7   rZ   rR   r[   r'   )r   )r8   frY   typess     r,   r9   z'GithubFileSystem.ls.<locals>.<listcomp>   s     
 
 
 V9%% 7;ID3J622&	fI!!F),EE&!,,U8  &%%r0   rW   c                     g | ]
}|d          S r6   r   )r8   r\   s     r,   r9   z'GithubFileSystem.ls.<locals>.<listcomp>   s    2221V9222r0   )_strip_protocolr!   rstripsplitr"   FileNotFoundErrordircacher   r   urlr   r   r   r   r   status_coder   r    sorted)r&   rY   detailr'   rN   r(   partspartoutr*   rQ   r]   s    `        @@r,   r"   zGithubFileSystem.ls   sE     ##D))2::#$)D<KK$$**3//EF#$)D " "ggfd$g??8#**D8====#=== 2+D111!fv;&(( $ #u#5zt}$$DIt3D(D(DDH49$GG  ' A
 }##'---   #[99E
 
 
 
 
 &)
 
 
C ty$'''&)d#-%C 	4J22c222333r0   c                 8    | j                                          d S N)rc   clear)r&   rY   s     r,   invalidate_cachez!GithubFileSystem.invalidate_cache   s    r0   c                     t          |          }d|vr!t                                          |          S |d                             d          S )Nr   rY   r4   )r   r   r_   lstrip)r:   rY   optsr+   s      r,   r_   z GithubFileSystem._strip_protocol   sK    $T**T!!77**40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   )rY   rq   rj   s      r,   _get_kwargs_from_urlsz&GithubFileSystem._get_kwargs_from_urls   sT    $T**T!!I:&Z0@AA< 	&fCJ
r0   rbc                 0   |dk    rt           | j                            | j        | j        ||p| j                  }t          j        |fd| j        i| j	        }|j
        dk    rt          |          |                                 |                                }	|	d         r@t          j        |	d                   }
|
                    d          st#          d d |
          S | j        t'          d           | j        j        |	d         f|||d	|S )
Nrv   r   r   rY   r'   r   rT   contents#   version https://git-lfs.github.com/zRPlease install fsspec[http] to access github files >1 MB or git-lfs tracked files.download_url)rZ   
block_sizecache_options)NotImplementedErrorcontent_urlr   r   r   r!   r   r   r   r   re   rb   r   r    base64	b64decode
startswithr   r$   r%   open)r&   rY   rZ   r{   r|   r'   r(   rd   r*   content_jsonry   s              r,   _openzGithubFileSystem._open   sR    4<<%% %%tyt9I	 & 
 

 L>>dl>dg>>=C#D)))	vvxx 	" 	7&|I'>??G %%&LMM 7!$g666
 <,   !t| (
!'	
 

 
 
 	
r0   c                     |                      |||          }t          |          D ]}|                     ||           d S )N)	recursivemaxdepth)message)expand_pathreversedrm_file)r&   rY   r   r   r   ps         r,   rmzGithubFileSystem.rm  sT    	HMM$ 	- 	-ALLGL,,,,	- 	-r0   c                    | j         st          d          |                     |          }|                     |          }|s| j                            | j        | j        |                    d          | j	                  }t          j        |fd| j        i| j        }|j        dk    rt          |          |                                 |                                d         }| j                            | j        | j        || j	                  }| j	        }|pd| |d|rd	|ini }	t          j        |f|	| j        d
| j        }|                                                    dd          }
t'          j        d|
          rd}t          |          |                                 |                     |           dS )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   rx   r   rT   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   r_   _get_sha_from_cacher~   r   r   r   rp   r!   r   r   r   r   re   rb   r   r    deleteresearchrn   )r&   rY   r   r(   r'   rd   r*   
delete_urlr   dataerror_messageerrors               r,   r   zGithubFileSystem.rm_file  s   " } 	86777##D)) &&t,, 	""))H494;;s3C3C *  C SBB$,B$'BBA}##'---   &&((5/C %,,tyt - 
 

 2"2D"2"2
 
 &,3&!!
 OJST4<SS47SSY339+];; 	$jEU###	d#####r0   c                     | j                                         D ]4}|D ]/}|                    d          }|r||k    rd|v r|d         c c S 05d S )Nr7   r'   )rc   valuesr   )r&   rY   entriesentry
entry_paths        r,   r   z$GithubFileSystem._get_sha_from_cacheG  s|    }++-- 	( 	(G  ( ("YYv..
 (*"4"4% <'''''( tr0   )NNNN)T)FNNrl   )rv   NNN)__name__
__module____qualname____doc__rd   r~   protocolr   r   propertyr   classmethodr=   rF   rI   rL   r"   rn   r_   staticmethodru   r   r   r   r   __classcell__)r+   s   @r,   r
   r
      s        6 FCWKHG GK           <   X
 3 3 3 [3. - - X- - - X- > > X>@4 @4 @4 @4D    ( ( ( ( [(   \ 1
 1
 1
 1
f- - - -
4$ 4$ 4$ 4$l      r0   r
   )
r   r   r   specr   utilsr   memoryr   r
   r   r0   r,   <module>r      s     				  % % % % % % ) ) ) ) ) )      B B B B B) B B B B Br0   