o
    mi\                      @   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:
    ZgYearZ
gYearMonth)yearZ	yearmonthc                 S   s   g | ]}|r|qS  r   ).0tr   r   K/home/kim/smarthome/.venv/lib/python3.10/site-packages/csvw/frictionless.py
<listcomp>       z'convert_column_spec.<locals>.<listcomp>titlenamebasestring)r
   datatypetypeformatbinaryr   uriZanyURI)r   numberintegerbooleandatetimedatetimedurationz	dc:formatr   Z
trueValuesZfalseValuesz{}|{}r   )r   r   Z
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>)ZdecimalCharZ	groupChar)objectarrayjsonzapplication/jsonZgeojsonzapplication/geo+jsontitlesdescriptiondc:descriptionZrdfTypeZpropertyUrlconstraints)requiredZ	minLengthZ	maxLengthminimummaximumpattern)getr   NotImplementedErrorany)r   typemapr!   respr$   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)columnReferencer2   )r3   r1   )dict)rsc_namefkresource_mapr   r   r   convert_foreignKeyS   s   
r8   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>r0   )columns))ZmissingValuesnullZ
primaryKeyforeignKeysr<   c                    s   g | ]}t | qS r   )r8   )r   r6   r7   r5   r   r   r   z       )r4   
isinstancetuple)r5   schemar7   r-   r   Ztopropr   r=   r   convert_table_schemab   s   	

rB   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)rE   Z	quoteCharZdoubleQuoteZskipInitialSpaceheaderZcommentCharZcommentPrefix)r)   )rscdr-   r   r   r   r   convert_dialect~   s   

rJ   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)rF   )
r?   rK   r    dirr4   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)Zlicensesz
dc:license)r	   dc:title)Zhomepagezdcat:accessURL)r"   r#   )sourcesz	dc:source)Zcontributorszdc:contributor)Zprofilezdc:conformsTo)keywordsz
dc:subject)createdz
dc:createdr
   rX   rY   r	   rZ   c                 S   s   g | ]}d |v r|qS )pathr   r   rH   r   r   r   r      r>   z-DataPackage.to_tablegroup.<locals>.<listcomp>	resourcesc                 S   s"   i | ]}d |v r|d  |d qS )r
   r^   r   r_   r   r   r   
<dictcomp>   s   " z-DataPackage.to_tablegroup.<locals>.<dictcomp>rA   schemefiler   rD   Ztablesr^   )urlZtableSchemarC   zcsvw-metadata.json)ZcsvwrW   r    dumpsr)   
setdefaultr4   rB   rJ   appendZ	fromvaluerN   Z_fname)rT   clsrW   mdZflpropZcsvwpropr`   r7   rH   rA   tabler-   r   r   r   to_tablegroup   s>   





zDataPackage.to_tablegroupr   )__name__
__module____qualname__rV   rk   r   r   r   r   rK      s    
rK   )__doc__r    rO   r/   r8   rB   rJ   rK   r   r   r   r   <module>   s    
D