o
    QDiB(                     @  s   d dl mZ d dlZd dlmZmZmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ erTd dlmZ d dlmZ G dd deZd$ddZ	d%d&d"d#ZdS )'    )annotationsN)TYPE_CHECKINGAnyDictListUnion   )CatalogDataset)FrictionlessException)Package)platform)Resource)AdapterPublishResult   )GithubControl)ContentFile)
Repositoryc                   @  s8   e Zd ZdZdddZddd	ZdddZdddZdS )GithubAdapterz"Read and write data from/to Githubcontrolr   c                 C  s
   || _ d S N)r   )selfr    r   \/var/www/Datamplify/venv/lib/python3.10/site-packages/frictionless/portals/github/adapter.py__init__   s   
zGithubAdapter.__init__returnr   c           
   
   C  s   | j jr| j jsd}t|| j jsJ tj| j j}d	| j j| j jg}z|
|}W n tyD } z
dt| }t|d }~ww d| d|j }|d}t||}t|||| j j}	t|	trk|	jrk|	S d}t|)NzRepo and user is required/Github API error"https://raw.githubusercontent.com/ Package/s not found)r   repouserr   formatsr   githubGithubapikeyjoinget_repo	Exceptionreprdefault_branchget_contentsget_resourcesget_package
isinstancer   	resources)
r   noteclientlocation
repository	exception	base_pathcontentsresource_pathpackager   r   r   read_package   s,   

zGithubAdapter.read_packager:   c              
   C  s^  | j jsJ | j jsJ d }d }ztj| j j}| }|j| j jddd}W n ty> } z
dt	| }t
|d }~ww | }| j jpHd}| j jrVtj| j j|}|| j j}| j jpg|jpg| j j}	|jps| j jps|	 d}
|
sxJ |	s|J tj|	|
}|j}z|j|d||j||d W n ty } z
dt	| }t
|d }~ww z'|jD ]!}|jpd	}| j jrtj| j j|}|j|d
| |||d qW n ty } z
dt	| }t
|d }~ww |j}| j jr&z|jjd|j ddddiid d|j d|j }W n ty% } z
dt	| }t
|d }~ww t|t |ddS )NTPython)name	auto_initgitignore_templatezGithub API error:datapackage.jsonz@users.noreply.github.comzCreate "datapackage.json")pathmessagecontentbranch	committerauthorr    zCreate "resource_path"POSTz/pagessourcerD   main)inputzhttps://z.github.io/)r5   )urlcontext)!r   r"   r'   r   r%   r&   get_usercreate_repor*   r+   r   to_jsonfilenamebasepathosrA   r(   r)   r=   r#   emailInputGitAuthorr,   create_filer1   
read_byteshtml_urlenable_pages
_requesterrequestJsonAndCheckrK   r   dict)r   r:   r5   r#   r3   r6   r2   rC   package_pathusernamerS   rF   rD   resourcer9   rK   r   r   r   write_package8   s   







zGithubAdapter.write_packager	   c              
   C  s  g }i }| j js| j js| j jsd}t|| j jsJ | j jr| j jrtj| j j	}d
| j j| j jg}z||}W n tyT } z
dt| }t|d }~ww d| d|j }|d}	t|	|}
t|
||| j jdd}|rt|tr|| }tdd	 |D d
S d}t|i }| j jrd| j ji}| j jrd| j jvr| j j|d< | j js| j jrdd| j j i}| j jrd|vrd| j j |d< | j jr| j j|d< | j jr| j j|d< zc| j jsJ | j jsJ tjj| j j	| j jd}|j|d fi |}| j jr|| j j}|D ]/}d|j d|j }|d}	t|	|}
t|
||| j j}t|tr@|jr@|| qW n tyZ } z
dt| }t|d }~ww |rhtdd	 |D d
S d}t|)Nz'Repo or user or search text is requiredr   r   r   r    T)catalogc                 S     g | ]	}t |j|d qS )r=   r:   r
   r=   .0r:   r   r   r   
<listcomp>       z.GithubAdapter.read_catalog.<locals>.<listcomp>)datasetsr!   qzuser:r#   zrepo:ordersort)per_pagec                 S  ra   rb   rc   rd   r   r   r   rf      rg   )r   r"   r#   searchr   r$   r   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   listr	   rj   rk   rl   search_repositoriespageget_page	full_namer   r1   append)r   packagesqueryr2   r3   r4   r5   r6   r7   r8   r9   all_packagesoptionsrepositoriesr:   r   r   r   read_catalog   s   





	zGithubAdapter.read_catalogN)r   r   )r   r   )r:   r   )r   r	   )__name__
__module____qualname____doc__r   r;   r_   ry   r   r   r   r   r      s    


Tr   r8   %Union[List[ContentFile], ContentFile]r5   r   r   List[ContentFile]c                 C  sB   g }| r|  d}|jdkr| ||j n|| | s|S )Nr   dir)poptypeextendr-   rA   rs   )r8   r5   pathsfile_contentr   r   r   r.      s   


r.   Fr   r7   strr$   	List[str]r`   boolUnion[Package, List[Package]]c           	        s   dfdd}|r||S t |j|d}D ]. | d j } jdv r.t |}|  S t fdd	|D rDt jd
}|| q|S )Nr7   r   c                   sL   g }D ] |  d j  }t fdddD r#t|}|| q|S )Nr   c                 3      | ]	} j |V  qd S r   rA   endswith)re   rP   filer   r   	<genexpr>       z9get_package.<locals>.multiple_packages.<locals>.<genexpr>r@   )rA   anyr   from_descriptorrs   )r7   rt   fullpathr:   )r   r   r   multiple_packages   s   

z&get_package.<locals>.multiple_packages)r=   rQ   r   r   c                 3  r   r   r   )re   extr   r   r   r     r   zget_package.<locals>.<genexpr>)rA   )r7   r   )r   r=   rA   r   r   r   add_resource)	r   r5   r7   r$   r`   r   r:   r   r^   r   )r   r   r   r/      s   	


r/   )r8   r~   r5   r   r   r   )F)r   r   r5   r   r7   r   r$   r   r`   r   r   r   )
__future__r   rR   typingr   r   r   r   r   r`   r	   r
   r6   r   r:   r   r   r^   r   systemr   r   r   r   github.ContentFiler   github.Repositoryr   r   r.   r/   r   r   r   r   <module>   s$     
T