
    11i\                      D    S r SSKrSSKrS rS rS rS r " S S5      rg)	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SS.nT R                  S5      4 Vs/ s H  o"(       d  M  UPM     nnT S   SS0S.nS	T ;   Ga  T S	   S:X  a  T R                  S
5      S:X  a
  SUS   S'   GOT S	   S:X  a  T R                  S
5      S:X  a
  SUS   S'   GO^T S	   U;   a  UT S	      US   S'   GOET S	   S;   Ga  T S	   US   S'   T S	   S:X  a!  T R                  S
5      (       a  T S
   US   S'   T S	   S:X  aP  T R                  S5      (       a:  T R                  S5      (       a$  SR                  T S   S   T S   S   5      US   S
'   T S	   S;   ap  T R                  S5      SL a  [        S5      e[        U 4S jS 5       5      (       a7  0 US   S
'   S H)  nT R                  U5      (       d  M  T U   US   S
   U'   M+     O3T S	   S;   a  SUS   S'   SUS   S'   OT S	   S:X  a  SUS   S'   S US   S'   U(       a  X4S!'   S"T ;   a	  T S"   /US#'   S$T ;   a  T S$   US%'   T R                  S&0 5      nS' H0  nXv;   a
  Xg   US   U'   S(U;   d  M  S
US   ;  d  M%  US(   US   S
'   M2     U$ s  snf ))zZ
https://specs.frictionlessdata.io/table-schema/#field-descriptors

:param spec:
:return:
gYear
gYearMonth)year	yearmonth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   ,   >#    U  H	  oT;   v   M     g 7fN ).0propspecs     K/home/james-whalen/.local/lib/python3.13/site-packages/csvw/frictionless.py	<genexpr>&convert_column_spec.<locals>.<genexpr>5   s     M0Lt|0Ls   )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!   typemaptr+   respr0   r    s   `       r"   convert_column_specr>      s    !G
 ((7+,2,Aa,F2<fh-?
@C~<8#(:h(F&.C
OF#&\X%$((8*<*E&.C
OF#&\W$&-d6l&;C
OF#&\ 
 
 '+6lC
OF#F|x'DHHX,>,>/3H~J,F|y(TXXl-C-CQ^H_H_,3NN&q)4+>q+A-CJ)F|4488L)T1-56 6
 M0LMMM02C
OH-988A;;;?7C
OH5a8 : &\00&,C
OF#+=C
OK(&\Y&&,C
OF#+AC
OK(H!%m!4 5D!)_M((="-KL$/$5C
OD!$83z?+J(3I(>C
OH%	 M Jk 3s
   
I"I"c           	      Z    [        US   [        US   S   X!S   S   =(       d    U    S9S9$ )z>
https://specs.frictionlessdata.io/table-schema/#foreign-keys
fields	referenceresource)columnReferencerB   )rC   rA   )dict)rsc_namefkresource_maps      r"   convert_foreignKeyrH   S   s@     8{OH5![/*"="IJ
     c           	         [        US    Vs/ s H  n[        U5      PM     snS9nS HU  n[        U[        5      (       a  Uu  pVOUnXQ;   d  M&  X   XF'   US:X  d  M4  XF    Vs/ s H  n[	        XU5      PM     snXF'   MW     U$ s  snf s  snf )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.
r@   )columns))missingValuesnull
primaryKeyforeignKeysrO   )rD   r>   
isinstancetuplerH   )rE   schemarG   fr<   r    toproprF   s           r"   convert_table_schemarU   b   s     171AB1AA$Q'1ABC
 dE""LD&F> ,CK}$X[XcdXcRT1(MXcd J C es   B#B	c                    U R                  S0 5      nSU;   a  US   n0 nUR                  S5      (       a  US   US'   U R                  S5      (       a  U S   US'   S H  nX1;   d  M
  X   X#'   M     SU;   a  US   US'   U$ )z_
Limitations: lineTerminator is not supported.

https://specs.frictionlessdata.io/csv-dialect/
dialectcsv	delimiterencoding)rY   	quoteChardoubleQuoteskipInitialSpaceheadercommentCharcommentPrefix)r7   )rscdr<   r    s       r"   convert_dialectrc   ~   s     		2AzeH
Cuu[[>K
wwzj/J 9CI  /OJrI   c                   (    \ rS rSrSS jrSS jrSrg)DataPackage   Nc                 "   [        U[        5      (       a#  UR                  U l        UR                  U l        g [        U[        5      (       a%  [
        R                  " U=(       d    S5      U l        O[        U[
        R                  5      (       a>  U=(       d    UR                  U l        [        R                  " UR                  SS95      nO:[        R                  " U5      n[
        R                  " U=(       d    S5      U l        Xl        g )N.utf8)rZ   )
rP   re   r)   dirrD   pathlibPathparentloads	read_text)selfr!   	directorys      r"   __init__DataPackage.__init__   s    dK((		DIxxDHdD!!||I$45DHgll++ /DKKDH::dnnfn=>D::d#D||I$45DH	rI   c           	         SSK Jn  SS0n[        R                  " U R                  5      US'   S H'  u  pEX@R                  ;   d  M  U R                  U   X5'   M)     SU R                  ;   aE  SU R                  ;  a  U R                  S   US	'   O"S
U R                  ;  a  U R                  S   US'   U R                  R	                  S/ 5       Vs/ s H  nSU;   d  M  UPM     nnU Vs0 s H  nSU;   d  M  US   US   _M     nnU H  nUR	                  S5      n	U	(       d  M  UR	                  S5      S:X  d  M4  UR	                  S5      S:X  d  MK  UR                  S/ 5        [        US   [        UR	                  S5      X5      [        U5      S9n
US   R                  U
5        M     U=(       d    UnUR                  U5      nU R                  S-  Ul        U$ s  snf s  snf )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	   rv   rw   r   ry   	resourcespathrR   schemefiler   rX   tables)urltableSchemarW   zcsvw-metadata.json)csvwru   r)   dumpsr7   
setdefaultrD   rU   rc   append	fromvaluerj   _fname)rp   clsru   mdflpropcsvwpropra   r   rG   rR   tabler<   s               r"   to_tablegroupDataPackage.to_tablegroup   s   #56 JJtyy1=
!
F "#yy0!
 TYY499$&*ii&7?#		)!%6!2: %)IIMM+r$BT$BSfPSmS$B	T<EWISSV0FS[0IWCWWX&FvGGH%/GGH%.h+F 4SWWV_f [+C0
 8##E*  ZmmBXX 44

' UWs   
GG(
G#6G#)rj   r)   r   )__name__
__module____qualname____firstlineno__rr   r   __static_attributes__r   rI   r"   re   re      s    "3rI   re   )__doc__r)   rk   r>   rH   rU   rc   re   r   rI   r"   <module>r      s4   	  AH8:E ErI   