o
    i\                      @   sF   d Z ddlZddlZdd Zdd Zdd Zd	d
 ZG dd dZdS )a  
Functionality to convert tabular data in Frictionless Data Packages to CSVW.

We translate [table schemas](https://specs.frictionlessdata.io/table-schema/) defined
for [data resources](https://specs.frictionlessdata.io/data-resource/) in a
[data package](https://specs.frictionlessdata.io/data-package/) to a CVSW TableGroup.

This functionality can be used together with the `frictionless describe` command to add
CSVW metadata to "raw" CSV tables.
    Nc                    s  ddd}dd   dfD } d dd	id
}d v r d d	kr1  ddkr1d|d d< nĈ d d	krE  ddkrEd|d d< n d |v rV| d  |d d< n d dv rЈ d |d d<  d d	krw  drw d |d d<  d dkr  dr  drd d d  d d |d d<  d dv rψ  ddu rtdt fdddD ri |d d< dD ]}  |rΈ | |d d |< qn% d dv rd |d d< d!|d d< n d d"krd |d d< d#|d d< |r||d$< d% v r d% g|d&< d' v r d' |d(<   d)i }d*D ]$}||v r)|| |d |< d+|v r=d|d vr=|d+ |d d< q|S ),zj
    https://specs.frictionlessdata.io/table-schema/#field-descriptors

    :param spec:
    :return:
    gYear
gYearMonth)year	yearmonthc                 S   s   g | ]}|r|qS  r   ).0tr   r   E/home/ubuntu/.local/lib/python3.10/site-packages/csvw/frictionless.py
<listcomp>       z'convert_column_spec.<locals>.<listcomp>titlenamebasestring)r   datatypetypeformatbinaryr   urianyURI)r   numberintegerbooleandatetimedatetimedurationz	dc:formatr   
trueValuesfalseValuesz{}|{}r   )r   r   
bareNumberTzbareNumber is not supported in CSVW. It may be possible to translate to a number pattern, though. See https://www.w3.org/TR/2015/REC-tabular-data-model-20151217/#formats-for-numeric-typesc                 3   s    | ]}| v V  qd S Nr   )r   propspecr   r	   	<genexpr>5   s    z&convert_column_spec.<locals>.<genexpr>)decimalChar	groupChar)objectarrayjsonzapplication/jsongeojsonzapplication/geo+jsontitlesdescriptiondc:descriptionrdfTypepropertyUrlconstraints)required	minLength	maxLengthminimummaximumpattern)getr   NotImplementedErrorany)r#   typemapr+   respr0   r!   r   r"   r	   convert_column_spec   sf    



r=   c                 C   s0   t |d t |d d ||d d p|  ddS )zF
    https://specs.frictionlessdata.io/table-schema/#foreign-keys
    fields	referenceresource)columnReferencer@   )rA   r?   )dict)rsc_namefkresource_mapr   r   r	   convert_foreignKeyS   s   
rF   c                    sv   t dd |d D d}dD ]*}t|tr|\}}n|}||v r8|| ||< |dkr8 fdd|| D ||< q|S )a  
    :param rsc_name: `name` property of the resource the schema belongs to. Needed to resolve     self-referential foreign keys.
    :param schema: `dict` parsed from JSON representing a frictionless Table Schema object.
    :param resource_map: `dict` mapping resource names to resource paths, needed to convert foreign    key constraints.
    :return: `dict` suitable for instantiating a `csvw.metadata.Schema` object.
    c                 S   s   g | ]}t |qS r   )r=   )r   fr   r   r	   r
   l   r   z(convert_table_schema.<locals>.<listcomp>r>   )columns))missingValuesnull
primaryKeyforeignKeysrL   c                    s   g | ]}t | qS r   )rF   )r   rD   rE   rC   r   r	   r
   z       )rB   
isinstancetuple)rC   schemarE   r;   r!   topropr   rM   r	   convert_table_schemab   s   	

rS   c                 C   s   |  di }d|v r|d }i }| dr|d |d< |  dr&| d |d< dD ]}||v r4|| ||< q(d|v r?|d |d< |S )zk
    Limitations: lineTerminator is not supported.

    https://specs.frictionlessdata.io/csv-dialect/
    dialectcsv	delimiterencoding)rV   	quoteChardoubleQuoteskipInitialSpaceheadercommentCharcommentPrefix)r7   )rscdr;   r!   r   r   r	   convert_dialect~   s   

r`   c                   @   s    e Zd ZdddZdddZdS )DataPackageNc                 C   s   t |tr|j| _|j| _d S t |trt|pd| _n#t |tjr3|p'|j| _t|j	dd}nt|}t|p=d| _|| _d S )N.utf8)rW   )
rO   ra   r)   dirrB   pathlibPathparentloads	read_text)selfr#   	directoryr   r   r	   __init__   s   



zDataPackage.__init__c                 C   s@  ddl m} ddi}t| j|d< dD ]\}}|| jv r$| j| ||< qd| jv rCd| jvr7| jd |d	< nd
| jvrC| jd |d< dd | jdg D }dd |D }|D ]6}|d}	|	r|ddkr|ddkr|dg  t|d t|d|	|t|d}
|d 	|
 qX|p|}|
|}| jd |_|S )Nr   )
TableGroupz@contextzhttp://www.w3.org/ns/csvwzdc:replaces)
)iddc:identifier)licensesz
dc:license)r   dc:title)homepagezdcat:accessURL)r,   r-   )sourcesz	dc:source)contributorszdc:contributor)profilezdc:conformsTo)keywordsz
dc:subject)createdz
dc:createdr   rn   ro   r   rq   c                 S   s   g | ]}d |v r|qS )pathr   r   r^   r   r   r	   r
      rN   z-DataPackage.to_tablegroup.<locals>.<listcomp>	resourcesc                 S   s"   i | ]}d |v r|d  |d qS )r   rx   r   ry   r   r   r	   
<dictcomp>   s   " z-DataPackage.to_tablegroup.<locals>.<dictcomp>rQ   schemefiler   rU   tablesrx   )urltableSchemarT   zcsvw-metadata.json)csvwrm   r)   dumpsr7   
setdefaultrB   rS   r`   append	fromvaluerd   _fname)rj   clsrm   mdflpropcsvwproprz   rE   r^   rQ   tabler;   r   r   r	   to_tablegroup   s>   





zDataPackage.to_tablegroupr    )__name__
__module____qualname__rl   r   r   r   r   r	   ra      s    
ra   )__doc__r)   re   r=   rF   rS   r`   ra   r   r   r   r	   <module>   s    
D