
    $HJje                        d Z ddlZddlmZmZmZmZmZ ddlZddl	Z	ddl
Z
ddlZddlZddlZddlmZmZ ddlZddlZddlmZ ddlmZmZ ddlmZ d	 Zej6                   G d
 d             Z ed       ed       ed ee       ee             ed ee       eej@                               ed      dZ! G d de      Z" G d de      Z#de$de$fdZ%ddde
jL                  de"de$dee$   de'dee   fd Z(de
jL                  de$de)e'e$   ef   fd!Z*ej6                   G d" d#             Z+ej6                   G d$ d%             Z,	 d-d&ej,                  d'ee   de'e,   fd(Z- G d) d*      Z.ej6                   G d+ d,             Z/y).a(  
SQLite as alternative storage backend for a TableGroup's data.

For the most part, translation of a TableGroup's tableSchema to SQL works as expected:

- each table is converted to a `CREATE TABLE` statement
- each column specifies a column in the corresponding `CREATE TABLE` statement
- `foreignKey` constraints are added according to the corresponding `tableSchema` property.

List-valued foreignKeys are supported as follows: For each pair of tables related through a
list-valued foreign key, an association table is created. To make it possible to distinguish
multiple list-valued foreign keys between the same two tables, the association table has
a column `context`, which stores the name of the foreign key column from which a row in the
assocation table was created.

Other list-valued columns work in two different ways: If the atomic datatype is `string`, the
specified separator is used to create a concatenated string representation in the database field.
Otherwise, the list of values is serialized as JSON.

SQL table and column names can be customized by passing a translator callable when instantiating
a :class:`Database`.

SQLite support has the following limitations:

- regex constraints on strings (as specified via a :class:`csvw.Datatype`'s format attribute) are
  not enforced by the database.
    N)OptionalUnionProtocolCallableAny)SequenceIterator)	DATATYPES)
TableGroupDatatype   )optcastc                     | S N )ss    Q/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/csvw/db.pyidentityr   -   s    H    c                   P    e Zd ZU dZeed<   eZee	ge	f   ed<   eZ
ee	ge	f   ed<   y)DBTypez2A DB datatype together with read/write converters.nameconvertreadN)__name__
__module____qualname____doc__str__annotations__r   r   r   r   r   r   r   r   r   r   1   s4    <
I$,GXseSj!,!)D(C5#:
)r   r   TEXTINTEGERREALBLOB)stringintegerbooleandecimal	hexBinaryc                   (    e Zd Zddedee   defdZy)SchemaTranslatorNtablecolumnreturnc                      y r   r   )selfr,   r-   s      r   __call__zSchemaTranslator.__call__C       r   r   )r   r   r   r   r   r1   r   r   r   r+   r+   B   s     c 8C= C r   r+   c                       e Zd ZdedefdZy)ColumnTranslatorr-   r.   c                      y r   r   )r0   r-   s     r   r1   zColumnTranslator.__call__H   r2   r   N)r   r   r   r   r1   r   r   r   r4   r4   G   s    s s r   r4   namesr.   c                  2    dj                  d | D              S )z=Returns a comma-separated list of quoted schema object names.,c              3   (   K   | ]
  }d | d   yw)`Nr   ).0r   s     r   	<genexpr>zquoted.<locals>.<genexpr>N   s     2EDavQKEs   )join)r6   s    r   quotedr>   L   s    882E222r   Fsingledb	translater,   keysrowsr@   c          
      b   |rkt        |D cg c]  } |||       c} }dj                  |D cg c]  }d c}      }	dt         ||             d| d|	 d}
	 | j                  |
|       y	y	c c}w c c}w #  |s|D ]  }t        | ||||d        Y y	t	        |
       t	        |        xY w)
a  
    Insert a sequence of rows into a table.

    :param db: Database connection.
    :param translate: Callable translating table and column names to proper schema object names.
    :param table: Untranslated table name.
    :param keys: Untranslated column names.
    :param rows: Sequence of rows to insert.
    :param single: Flag signaling whether to insert all rows at once using `executemany` or one at     a time, allowing for more focused debugging output in case of errors.
    r8   ?zINSERT INTO z (z
) VALUES ()Tr?   N)r>   r=   executemanyinsertprint)rA   rB   r,   rC   r@   rD   kcols_valssqlrows               r   rI   rI   Q   s    " T:T	%+T:;xxd+dd+,VIe$456bjaP		NN3% :+	C2y%s4H   c
ds   A/	A4A9 9B.B.c                     | j                  dt        |             }|j                  D cg c]  }|d   	 }}|t        |j	                               fS c c}w )z;Shortcut to construct and execute simple SELECT statements.zSELECT * FROM r   )executer>   descriptionlistfetchall)rA   r,   cudrL   s        r   selectrX   r   sR    	nVE]O4	5B..).QAaD.D)bkkm$$$ *s   Ac                       e Zd ZU dZeed<   dZeed<   dZeed<   dZe	ed<   dZ
eed	<   dZeed
<   d Zdedee   fdZdedefdZy)ColSpecze
    A `ColSpec` captures sufficient information about a :class:`csvw.Column` for the DB schema.
    r   r%   	csvw_typeN	separatordb_typeFrequiredcsvwc                    | j                   xs d| _         | j                   t        v rt        | j                      | _        nHt        dt        | j                      j
                  t        | j                      j                        | _        | j                  rK| j                  dk7  r;t        d| j                  j                  | j                  j                        | _        y y y )Nr%   r!   )
r[   TYPE_MAPr]   r   r
   	to_string	to_pythonr\   r   r   r0   s    r   __post_init__zColSpec.__post_init__   s    38>>X%#DNN3DL!	$..1;;Yt~~=V=`=`bDL>>dllf4!&$,,*>*>@Q@QRDL 5>r   rB   r.   c           
         | j                   sy| j                    || j                        }}g }|j                  |j                  dddj	                  | j
                        }|j                  K|r(|j                  | d| d| d|j                   d       n!|j                  d	| d
|j                          |j                  |r(|j                  | d| d| d|j                   d       n|j                  d	| d|j                          nt        d |j                  |j                  |j                  fD              r|j                  r!|j                  d| d|j                          |j                  r!|j                  d| d|j                          |j                  r!|j                  d| d|j                          dj                  |      S )a  
        We try to convert as many data constraints as possible into SQLite CHECK constraints.

        :param translate: Callable to translate column names between CSVW metadata and DB schema.
        :return: A string suitable as argument of an SQL CHECK constraint.
        Ndatedatetime)rg   rh   z(`z`) >= z('z')r:   z` >= z`) <= z` <= c              3   $   K   | ]  }|d u 
 y wr   r   )r;   ccs     r   r<   z ColSpec.check.<locals>.<genexpr>   s     O*NB4*Ns   zlength(`z`) = z AND )r_   r   minimummaximumgetr[   appendanylength	minLength	maxLengthr=   )r0   rB   ccnameconstraintsfuncs         r   checkzColSpec.check   s    yy99i		25II!qyy'<& c$..!  yy$&&$r%tfBqyykQS'TU&&5'qyyk'BCyy$&&$r%tfBqyykQS'TU&&5'qyyk'BCO188Q[[!++*NOOxx""XeWE!((#DE{{""XeWF1;;-#HI{{""XeWF1;;-#HI||K((r   c                     | j                  |      }| j                  rdnd}|rd| dnd}d || j                         d| j                  j                   | | S )zNFormat the column metadata suitable for inclusion in a CREATE TABLE statement.z	 NOT NULL z CHECK (rG   r:   z` )rw   r^   r   r]   )r0   rB   _checknull_constraintcheck_constraints        r   rO   zColSpec.sql   sc    I&)-+B39XfXQ/r9TYY'(4<<+<+<*=o=NO_N`aar   )r   r   r   r   r   r    r[   r\   r]   r   r^   boolr_   r   re   r4   r   rw   rO   r   r   r   rZ   rZ   y   sv     IIsIsGVHdD(S!)/ !)HSM !)Fb- b# br   rZ   c                   R   e Zd ZU dZeed<    ej                  e      Z	ee
   ed<    ej                  e      Zeed<    ej                  ej                        Zej                  ed<   dZeee      ed<   e	 dd	ej&                  d
ee   dd fd       Zedd       ZdedefdZy)	TableSpeca7  
    A `TableSpec` captures sufficient information about a :class:`csvw.Table` for the DB schema.

    .. note::

        We support "light-weight" many-to-many relationships by allowing list-valued foreign key
        columns in CSVW. In the database these columns are turned into an associative table, adding
        the name of the column as value a `context` column. Thus, multiple columns in a table my be
        specified as targets of many-to-many relations with the same table.

        .. seealso:: `<https://en.wikipedia.org/wiki/Associative_entity>`_
    r   default_factorycolumnsforeign_keysmany_to_manyNprimary_keyr,   drop_self_referential_fksr.   c                 T    | |j                   |j                  j                        }|j                  j                  D ch c]  }|j                  s|j
                   }}|j                  j                  D ]  }|j                  j                  rt        |j                        dk(  r#|j                  d   |v rt        |j                  j                        dk(  s6J d|j                  j                   d|j                  j                   d       |j                  rt        |j                        dk(  sJ d|j                   d       t        j                  |j                  |j                  d   |j                  j                  j                   |j                  j                  d         |j"                  |j                  d   <   W|r/|j                  j                  j                   |j                  k(  r|j$                  j'                  t)        |j                        |j                  j                  j                   t)        |j                  j                        f        |j                  j                  D ]  }|j
                  |j"                  vs|j+                  d	      }|j                  j'                  t-        |j
                  |r|j.                  n||j+                  d
      |j+                  d      |j+                  d	                    |S c c}w )a  
        Create a `TableSpec` from the schema description of a `csvw.metadata.Table`.

        :param table: `csvw.metadata.Table` instance.
        :param drop_self_referential_fks: Flag signaling whether to drop self-referential foreign         keys. This may be necessary, if the order of rows in a CSVW table does not guarantee         referential integrity when inserted in order (e.g. an eralier row refering to a later one).
        :return: `TableSpec` instance.
        )r   r   r   r   zComposite key z
 in table z referencedzTable zJ referenced by list-valued foreign key must have non-composite primary keydatatyper\   r^   )r   r[   r\   r^   r_   )
local_nametableSchema
primaryKeyr   r\   headerforeignKeys	referenceschemaReferencelencolumnReferenceresourcer   r   r   association_tabler%   r   r   rn   sortedinheritrZ   base)clsr,   r   specrs   list_valuedfkr   s           r   from_table_metadatazTableSpec.from_table_metadata   s    ((e6G6G6R6RS).):):)B)BR)BAakkqxx)BR##//B<<//r))*a/B4F4Fq4I[4Xr||;;<A @)",,*F*F)GzLL112+?@A  ++D4D4D0E0J 7!$)) -5 67J @I?Z?Z		((+--4444Q7	@D%%b&8&8&;< 48M8M8T8TX\XaXa8a%%,,r112--44r||;;<. % 0. ""**Axxt00099Z0##G/7hmmXii4YYz2:.%  + E Ss   L%L%c                 @   t        | d|       }t        | d|       }|j                  |j                  k(  r*|xj                  dz  c_        |xj                  dz  c_         | | d| ||t        d      g|j                  g||gf|j                  g||gfg      S )a  
        List-valued foreignKeys are supported as follows: For each pair of tables related through a
        list-valued foreign key, an association table is created. To make it possible to distinguish
        multiple list-valued foreign keys between the same two tables, the association table has
        a column `context`, which stores the name of the foreign key column from which a row in the
        assocation table was created.
        rM   _1_2context)r   r   r   )rZ   r   )r   atableapkbtablebpkafkbfks          r   r   zTableSpec.association_table  s     #'(#'(88sxxHHHHHH81VH%#wy12((VcU+((VcU+
 	
r   rB   c           
         t        j                  || j                        }| j                  D cg c]  }|j	                  |       }}| j
                  r;t        | j
                  D cg c]
  } ||       c} }|j                  d| d       | j                  D ]k  \  }}}	t        |D cg c]
  } ||       c} }
t         ||            }t        |	D cg c]  } |||       c} }|j                  d|
 d| d| d       m dj                  |      }dj                  d	 || j                         d
| dg      S c c}w c c}w c c}w c c}w )z[
        :param translate:
        :return: The SQL statement to create the table.
        zPRIMARY KEY(rG   zFOREIGN KEY(z) REFERENCES (z) ON DELETE CASCADEz,
    
zCREATE TABLE IF NOT EXISTS `z` ()
	functoolspartialr   r   rO   r   r>   rn   r   r=   )r0   rB   col_translatecolclausesrs   qcolsr   refrefcolsfkcolsrtablepkcolss                r   rO   zTableSpec.sql  s^   
 ")))TYY?59\\B\c377=)\Bt7G7GH7G!]1-7GHIENN\%23 $ 1 1BW;1mA.;<FIcN+FAAiQ/ABFNN\&vhaxObcd	 !2 ..)yy*9TYY+?*@D	TWY Z 	Z C I <As   E(E	,E
E
T)r.   r   )r   r   r   r   r   r    dataclassesfieldrT   r   rZ   r   collectionsOrderedDictr   r   r   classmethodr_   Tabler}   r   r   r+   rO   r   r   r   r   r      s     I.[..tDGT']D***4@L$@,=K,=,=#//-1L+)) 1'+K$s)$+ IM/#'::/7?~/Q\/ /b 
 
,Z- Z# Zr   r   tgr   c                 <   i }| j                   j                         D ]V  t        j                  |      }|||j                  <   |j
                  j                         D ]  }|||j                  <    X t        j                         d}|rh|dk  rc|dz  }t        |j                               D ]:  t        fd|   j                  D              s&|j                        <    n |r|dk  rc|rt        d      t        j                               S )a  
    Convert the table and column descriptions of a `TableGroup` into specifications for the
    DB schema.

    :param tg: CSVW TableGroup.
    :param drop_self_referential_fks: Flag signaling whether to drop self-referential foreign     keys. This may be necessary, if the order of rows in a CSVW table does not guarantee     referential integrity when inserted in order (e.g. an eralier row refering to a later one).
    :return: A pair (tables, reference_tables).
    r   r   d   r   c              3   @   K   | ]  }|d    v xs |d    k(    yw)r   Nr   )r;   r   orderedr,   s     r   r<   zschema.<locals>.<genexpr>L  s-     `E_cCFg%9#a&E/9E_s   z7there seem to be cyclic dependencies between the tables)	tabledictvaluesr   r   r   r   r   r   rT   rC   allr   pop
ValueError)r   r   tablestatir   r,   s         @@r   schemar   /  s    F$$&))-F * Hqvv..'')B F277O *	 ' %%'G	A QW	Q&++-(E`VE]E_E_``!'E!2	 ) QW RSS !!r   c            
          e Zd ZdZ	 	 	 ddedeeej                  e	f      dee
   dee   fdZddedefdZed	ee	ef   fd
       Zedde	dee	   d	e	fd       Zd	eej*                  ej.                  f   fdZdde	dee	   d	e	fdZd	ee	eee	e	f   e	f   f   fdZde	de	d	ee	   fdZde	de	d	eee	   e	df   fdZd	ee	eej@                     f   fdZ!d Z"ddZ#d Z$dddddZ%y) Databasea  
    Represents a SQLite database associated with a :class:`csvw.TableGroup` instance.

    :param tg: `TableGroup` instance defining the schema of the database.
    :param fname: Path to which to write the database file.
    :param translate: Schema object name translator.
    :param drop_self_referential_fks: Flag signaling whether to drop or enforce self-referential     foreign-key constraints.

    .. warning::

        We write rows of a table to the database sequentially. Since CSVW does not require ordering
        rows in tables such that self-referential foreign-key constraints are satisfied at each row,
        we don't enforce self-referential foreign-keys by default in order to not trigger "false"
        integrity errors. If data in a CSVW Table is known to be ordered appropriately, `False`
        should be passed as `drop_self_referential_fks` keyword parameter to enforce
        self-referential foreign-keys.
    Nr   fnamerB   r   c                     |xs t         j                  | _        |rt        j                  |      nd | _        | j                  ||       d | _        y )Nr   )r   name_translatorrB   pathlibPathr   init_schema_connection)r0   r   r   rB   r   s        r   __init__zDatabase.__init__i  sF     #>h&>&>,1W\\%(t
7PQr   c                 r    || _         | j                   rt        | j                   |      | _        yg | _        y)zLInititialize the db schema, possibly ignoring self-referential foreign keys.r   N)r   r   r   )r0   r   r   s      r   r   zDatabase.init_schemau  s3    MQWW GG/HJZ\ 	r   r.   c                 V    | j                   D ci c]  }|j                  | c}S c c}w r   )r   r   )r0   r   s     r   tdictzDatabase.tdict{  s&    #';;/;a	;///s   &r,   r-   c                     |xs | S )af  
        A callable with this signature can be passed into DB creation to control the names
        of the schema objects.

        :param table: CSVW name of the table before translation
        :param column: CSVW name of a column of `table` before translation
        :return: Translated table name if `column is None` else translated column name
        r   )r,   r-   s     r   r   zDatabase.name_translator  s     r   c                     | j                   r;t        j                  t        j                  t        | j                                     S | j                  st        j                  d      | _        | j                  S )z,DB connection to be used as context manager.z:memory:)r   
contextlibclosingsqlite3connectr   r   rd   s    r   
connectionzDatabase.connection  sQ    ::%%gooc$**o&FGG&z:Dr   tnamert   c                 p    |rt        | j                  ||            S t        | j                  |            S )z,Translate and then quote a db schema object.)r>   rB   )r0   r   rt   s      r   _qtzDatabase._qt  s/    $..677dnnU+,,r   c                 Z   |d| d}nd}t        j                  | j                  |j                        }d ||j                  d   j                         d ||j                  d   j                         d |        d	| d
 ||j                  d   j                         
}|j                  |      }|j                         D 	
ci c]N  }|d   t        |d   j                         |d   j                  d            D 	
cg c]  \  }	}
||	|
fn|	 c}
}	P c}
}	}S c c}
}	w c c}
}	}w )zDSelect data from an association table, grouped by first foreign key.zWHERE context = ''ry   zSELECT r   z,        group_concat(r   z>, ' '),        group_concat(COALESCE(context, ''), '||') FROM  z
 GROUP BY    z||)	r   r   r   r   r   rR   rU   zipsplit)r0   rA   r,   r   context_sqlqtrO   rV   rrK   vs              r   select_many_to_manyzDatabase.select_many_to_many  sD   -gYa8KKtxx4EMM!,1123 4&&(q)9)>)>&?%@ AtfAk]*Ra8H8M8M5N4OQ ZZ_ NP[[]\MZ aD"1Q4::<1D1ABDBtq! %_Aq6!3BD DMZ\ 	\D\s   ;D&D D& D&c                     | j                   D ]d  }| j                  |      |k(  s| j                   |   j                  D ]1  }| j                  ||j                        |k(  s#|j                  c c S  f y)ze
        :return: separator for the column specified by db schema names `tname` and `cname`.
        N)r   rB   r   r   r\   )r0   r   rt   r   r   s        r   r\   zDatabase.separator  sb     JJD~~d#u,::d+33C~~dCHH5>"}}, 4 
 r   c                 X    | j                  ||      }|r|xs dj                  |      S |S )z7Split a value if a separator is defined for the column.ry   )r\   r   )r0   r   rt   valueseps        r   split_valuezDatabase.split_value  s.    nnUE*+.""3'9E9r   c                    t        j                  t              }| j                         5 }| j                  j
                  D ]  }t        | j                  |   || j                        }|j                  j                  j                         D ]P  \  }}| j                  |||      j                         D ]'  \  }}||j                  |   | j                  ||      <   ) R t        || j                  |            \  }	}
|
D ]>  }|| j                  |         j                  |j!                  t#        |	|                   @  	 ddd       |S # 1 sw Y   |S xY w)z
        :return: A `dict` where keys are SQL table names corresponding to CSVW tables and values         are lists of rows, represented as dicts where keys are the SQL column names.
        N)r   defaultdictrT   r   r   r   TableReadSpecr   rB   r,   r   itemsr   
referencesrX   rn   read_rowr   )r0   resconnr   r   r   r   pkr   rL   rD   rP   s               r   r   zDatabase.read  s    
 %%d+__$**
 %TZZ%6t~~N#zz66<<>GC!%!9!9$C!H!N!N!PAJK+DNN5#,FG "Q  ? $D$..*?@
dCu-.55dmmCcN6ST   +  
  
s   DEEc                 
    ||fS )a  
        Context for association tables is created calling this method.

        Note: If a custom value for the `context` column is created by overwriting this method,
        `select_many_to_many` must be adapted accordingly, to make sure the custom
        context is retrieved when reading the data from the db.

        :param table:
        :param column:
        :param fkey:
        :return: a pair (foreign key, context)
        r   )r0   rM   r-   fkeys       r   association_table_contextz"Database.association_table_context  s     V|r   Fc                 ^     | j                   d|||d| j                  j                         S )z5Write the data from the contained tablegroup to a db.force
_exists_ok_skip_extrar   )writer   r   )r0   _forcer  r  s       r   write_from_tgzDatabase.write_from_tg  s7    tzz !# gglln	 	r   c                 |   g g }}|j                   D ci c]  }|j                  | }}t        |      D ]  \  }	}
|j                  r*t	        |j                        dk(  r|
|j                  d      nd }g }|
j                         D ]  \  }}||j                  v r|sJ t        |j                  |   j                  g|j                  |   j                   D cg c]  }|j                   c}z         }||   j                  |xs g D cg c]+  }t        |gt        | j                  |||            z         - c}       ||vr|rt        d| d      ||   t        |t              rNj                  dk(  r)j                  xs dj                  fd|D              }n5t!        j"                  |      }n|j$                  j'                  |      nd }|	dk(  r|j)                  j                         |j)                  |        |j)                  t        |              ||fS c c}w c c}w c c}w )Nr   r   zunspecified column z found in datar%   ;c              3   \   K   | ]#  }j                   j                  |      xs d  % yw)ry   N)r]   r   )r;   vvr   s     r   r<   z%Database._get_rows.<locals>.<genexpr>  s-      <KHI" 3 3B 7 =2 =s   ),)r   r   	enumerater   r   r   r   tupleextendrT   r   r   
isinstancer[   r\   r=   jsondumpsr]   r   rn   )r0   r   r   refsr  rD   rC   rs   rL   r   rP   r   r   rK   r   atkeyr	  r   s                    @r   	_get_rowszDatabase._get_rows  s!   d#$99-9a	9-&FAs*+--C<NRS<SQ]]1%&Y]BF		1&I2!1>>!#4#9#9":34>>!3D3L3L"M3La1663L"M#N OE K&&#$77(-#*B rdT$*H*HAr*R%SST#*(- . }&$(+>qc)PQQq'C!!T*==H4!$!5# ; ; <KHI<K !KA !%

1A67mCKK//2AvCHH-MM!$7 $8 KKf&? '@ TzC . #N(-s   H/H4
0H9r   c          
      0   | j                   rA| j                   j                         r'|st        d      | j                   j                          | j	                         5 }| j
                  D ]-  }|j                  |j                  | j                               / |j                  d       |j                          t        j                  t              }| j
                  D ]X  }|j                  |vr| j                  |||j                     ||      \  }	}
t        || j                  |j                  |
g|	  Z |j!                         D ]$  \  }}	t        || j                  |d   |dd g|	  & |j                          ddd       y# 1 sw Y   yxY w)z
        Creates a db file with the core schema.

        :param force: If `True` an existing db file will be overwritten.
        z3db file already exists, use force=True to overwrite)rB   zPRAGMA foreign_keys = ON;r   r   N)r   existsr   unlinkr   r   rR   rO   rB   commitr   r   rT   r   r  rI   r   )r0   r   r  r  r   rA   r,   r  r   rD   rC   r  s               r   r  zDatabase.write  s<    ::$**++- !VWWJJ__"

599t~~9>? % JJ23IIK**40D[[66&!^^AuQVV}dKP
dr4>>1664?$?	 !  $zz|tr4>>58U12YFF  , IIK# s   D%FF)NNTr   r   )FFF)&r   r   r   r   r   r   r   r   r   r   r+   r}   r   r   propertydictr   r   staticmethodr   r   
Connectionr   r   r   r   r  r   r\   rT   r   r   r   r   r   r  r  r  r   r   r   r   r   V  s   * 9=488<
 
  E',,"345
    01	
 
 (0~
 ]j ]T ] 0tCN+ 0 0 
s 
HSM 
S 
 
 E'"4"4j6H6H"HI  - -Xc] -c -\c5sTWxZ]I]C^>^9_ \ 	s 	3 	8C= 	: :S :E$s)SRVBV<W :
d3[%<%< ==> . #J #u% r   r   c                   $   e Zd ZU dZeed<   eed<   eed<    ej                  e
      Ze
eeeef   f   ed<    ej                  e
      Ze
eef   ed<    ej                  d       Ze
ed	<   d
 Zdeeeef      dej(                  eef   fdZy)r   z1Bundles data informing the reading of table rows.r,   r   rB   r   
converters
separatorsc                  4    t        j                  t              S r   )r   r   r  r   r   r   <lambda>zTableReadSpec.<lambda>5  s    AXAXY]A^r   r   c                 p   | j                   j                  D ]  }|j                  t        v rt        |j                     j                  }nt
        |j                     j                  }|j                  |f| j                  | j                  | j                  |j                        <   |j                  s|j                  dk(  r>|j                  | j                  | j                  | j                  |j                        <   d| j                  | j                  | j                  |j                        <    y )Nr%   r  )r,   r   r[   ra   r   r
   rc   r   r  rB   r\   r  )r0   r   convs      r   re   zTableReadSpec.__post_init__7  s    ::%%C}}(.66 /99DGHHdCSDOODNN499chh?@}}==H,KN==DOODNN499chh$GHKQDOODNN499chh$GH &r   rP   r.   c                 X   t        j                         }|D ]  \  }}|| j                  v r|d||<   |sg ||<   $| j                  |   dk(  rt        j                  |      ||<   O|xs dj                  | j                  |         D cg c]  } | j                  |   d   |       c}||<   | | j                  |   d   |      nd||<    | j                  j                  rXt        | j                  j                        dk(  r6|| j                  | j                  | j                  j                  d            nd}|j                  | j                  j                  D ci c]  }|g  c}       |j                  | j                  j                  |i              |S c c}w c c}w )zRead a table according to spec.Nr  ry   r   r   )r   r   r  r  loadsr   r  r,   r   r   rB   r   updater   r   rm   )r0   rP   rW   rK   r   v_r   s          r   r   zTableReadSpec.read_rowE  s   ##%DAqDOO#9AaDAaD__Q'61::a=AaDABbPTP_P_`aPb@cd@c"1DOOA.q1"5@cdAaD34=,tq)!,Q/d!  zz%%#djj.D.D*E*J t~~dii)?)?)BCDPT 		!8!89!8A!R%!89:	$$R,- e
 :s   
F"$
F'N)r   r   r   r   r   r    r   r+   r   r   r  r  r  r   r  r   re   r	   r   r   r   r   r   r   r   r   r   -  s    ;
I2C+2C2CTX2YJS%X../Y!2!2!24!HJS#XH({((9^_J_RHU38_5 +:Q:QRUWZRZ:[ r   r   r   )0r   r  typingr   r   r   r   r   r(   r   r   r   r   r   collections.abcr   r	   r   r_   csvw.datatypesr
   csvw.metadatar   r   utilsr   r   	dataclassr   intr}   floatDecimalra   r+   r4   r   r>   r  rT   rI   r  rX   rZ   r   r   r   r   r   r   r   <module>r/     s  6  ; ;       .   $ .  * * * Vni iwt}=fgenggoo.FGx 
x 
33 33 3 %*w!! & # 	
 D>B%w!! %# %%S	88K2L % =b =b =b@ qZ qZ qZj 8<$"t $"&.tn$"@DY$"NT Tn * * *r   