ó
    6lj¸#  ã                  óì   • S r SSKJr  SSKrSSKJr  SSKJr  SSKJ	r	J
r
JrJr  SSKrSSKJr  SSKJrJr  SS	KJr  / S
QrSrSrSr " S S\5      rSS jrSS jrSS jrSS jrSS jrSS jrSS jr g)uÅ  What a batch exports: one row per question, as a workbook or as text.

The two writers share the row building and nothing else, because each writes a
different thing. A workbook cell carries its own type and number format, so the
printed figure is a text cell nothing reformats and a percentage arrives as the
fraction under a percent format, displaying at the printed scale. A text file
encodes what it can in the characters: the printed column is quoted, the
numeric column is not, and a percentage keeps its percent sign for the
importer to divide. Both land the same value in the same cell; they need
different bytes to get there.

Every figure is written twice, as printed and as a number. Both are read off
the answer â€” `value` and the derived `number` â€” and nothing here parses a
printed figure. `answers/figures.py` is the one place that does, and the two
sign-flipping defects that motivated that rule were both written by an export
re-deriving what the answer already carried.

A question with no answer is a third kind of row, distinct from a figure and
from a failure: a sentinel in the printed column, an empty numeric column, and
the model's explanation in the note.

Formula injection is handled structurally. Workbook text cells are typed as
text, so a question beginning with `=` is characters. In the CSV the free-text
columns are prefixed, and only those â€” a printed figure is never prefixed,
because that column has to match the filing byte for byte.
é    )ÚannotationsN)ÚDecimal)ÚBytesIO)ÚAnyÚDictÚListÚOptional)Ú	BaseModel)Ú
is_percentÚnorm_printed)ÚRunState)Úquestionz
as printedÚnumberÚunitÚperiodÚpagez	cited idszsource cell textÚstatusÚnotezreview flagÚerrorz#not found#Úreviewz; c                  óÚ   • \ rS rSr% SrS\S'   SrS\S'   SrS\S	'   S
rS\S'   S
r	S\S'   Sr
S\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   SrS\S'   S
rS\S'   SrS\S'   Srg)Ú	ExportRowéH   zCOne question's row, typed for the writers rather than for a reader.Ústrr   Ú ÚprintedNzOptional[Decimal]r   FÚboolÚpercentÚfigurer   r   ÚpagesÚ	cited_idsÚ	cell_textr   r   r   r   © )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   r   r   r   r   r   r    r!   r"   r   r   r   r   Ú__static_attributes__r#   ó    Ú5/home/mande/repo/quber/src/quber/playground/export.pyr   r   H   s‹   ‡ ÙMàƒMØ€GˆSÓØ $€FÐÓ$Ø€GˆTÓð €FˆDÓØ€Dˆ#ƒNØ€FˆCÓØ€Eˆ3ƒOØ€IˆsÓØ€IˆsÓØ€FˆCÓØ€Dˆ#ƒNØ€FˆDÓØ€Eˆ3†Or+   r   c                ó˜   • U R                    Vs/ s H.  n[        UR                  UR                  UR                  5      PM0     sn$ s  snf )N)ÚrowsÚ
_build_rowr   r   Úresponse)ÚstateÚrows     r,   Ú
build_rowsr3   ]   s3   € ØINÏÊÓTÊÀ#ŒJ�s—|‘| S§Y¡Y°·±Ö=ÉÑTÐTùÒTs   �5Ac                óÊ  • Uc  [        X=(       d    SS9$ [        U S9nUR                  S5      =(       d    / nUR                  S5      =(       d    0 nUR                  S5      nUS:X  a¥  UR                  S5      =(       d    SUl        S	Ul        [	        UR                  5      Ul        UR                  S
5      nUb  [        U5      OS Ul        UR                  S5      =(       d    SUl        UR                  S5      =(       d    SUl	        O�US:X  a+  [        Ul        UR                  S5      =(       d    SUl        O_US:X  a   UR                  S5      =(       d    SUl        O9[        R                  " USS9Ul        UR                  S5      =(       d    SUl        / nU H5  n	[        U	R                  S5      5      n
X¨;  d  M$  UR                  U
5        M7     [         R#                  U5      Ul        [         R#                  S U 5       5      Ul        [         R#                  S U 5       5      Ul        [         R#                  S U 5       5      Ul        U Vs/ s H  o»R                  S5      (       d  M  US   PM!     nnU(       a;  [         R#                  UR                  (       a  UR                  /O/ U-   5      Ul        [-        S U 5       5      Ul        U$ s  snf )NÚfailed)r   r   )r   Ú
referencesÚvalueÚshapeÚscalarr   Tr   r   r   ÚunanswerableÚreasonÚproseÚtext)Ú,Ú:)Ú
separatorsr   c              3  óD   #   • U  H  oR                  S S5      v •  M     g7f)Úref_idr   N©Úget©Ú.0Úrs     r,   Ú	<genexpr>Ú_build_row.<locals>.<genexpr>†   s   é € ÐAºD°qŸu™u X¨r×2Ð2ºDùs   ‚ c              3  óZ   #   • U  H!  oR                  S 5      (       d  M  US    v •  M#     g7f)r=   NrC   rE   s     r,   rH   rI   ‰   s   é € ÐH²$¨Q¿%¹%À¿-›y˜q žy²$ùó   ‚+Ÿ+c              3  óZ   #   • U  H!  oR                  S 5      (       d  M  US    v •  M#     g7f)r   NrC   rE   s     r,   rH   rI   Š   s   é € ÐI²¨A¿¹¸x¿›K˜A˜hžK²ùrK   r   c              3  óB   #   • U  H  oR                  S 5      v •  M     g7f)ÚflaggedNrC   rE   s     r,   rH   rI   Ž   s   é € Ð4ªt¨!—U‘U˜9×%Ð%ªtùs   ‚)r   rD   r   r   r   r   r   r   r   r   ÚNOT_IN_DOCUMENTr   ÚjsonÚdumpsr   ÚappendÚ_JOINÚjoinr    r!   r"   r   Úanyr   )r   r   r0   r2   ÚrefsÚpayloadÚkindÚ
serializedÚ
seen_pagesÚrefr   rG   Únotess                r,   r/   r/   a   s/  € àÑÜ (×2C¸8ÑDÐDä
˜XÑ
&€CØ�<‰<˜Ó%×+¨€DØ�l‰l˜7Ó#×) r€GØ�<‰<˜Ó €DàˆxÓØ—k‘k 'Ó*×0¨bˆŒØˆŒ
Ü  §¡Ó-ˆŒð —[‘[ Ó*ˆ
Ø,6Ñ,B”W˜ZÔ(ÈˆŒ
Ø—;‘;˜vÓ&×,¨"ˆŒØ—[‘[ Ó*×0¨bˆ�
Ø	�Ó	Ü%ˆŒØ—;‘;˜xÓ(×.¨Bˆ�Ø	�‹Ø—k‘k &Ó)×/¨Rˆ�ô —j’j °ZÑ@ˆŒØ—;‘;˜vÓ&×,¨"ˆŒà€JÛˆÜ�3—7‘7˜6“?Ó#ˆØÕ!Ø×Ñ˜dÖ#ñ ô —
‘
˜:Ó&€C„IÜ—J‘JÑA¹DÓAÓA€C„Mô —J‘JÑH±$ÓHÓH€C„MÜ—‘ÑI±ÓIÓI€C„JÙ $Ó6¢˜1¯©¨f¯‹YˆQˆvŒY¡€EÐ6ÞÜ—:‘:¨S¯X¯X §¡™z¸2ÀÑFÓGˆŒÜÑ4©tÓ4Ó4€C„JØ€Jùò	 7s   ÉK É:	K c                óÀ   • [        U 5      R                  S5      R                  5       nSU;   a  [        UR	                  S5      S   5      OSnU(       a	  SSU-   S3$ S$ )z‚A percent format with as many decimals as the figure was printed with,
so `6.7 %` shows as 6.7% rather than being padded to 6.70%.Ú%Ú.é   r   z0.Ú0z0%)r   ÚrstripÚstripÚlenÚsplit)r   ÚdigitsÚplacess      r,   Úpercent_formatrh   ’   s\   € ô ˜'Ó"×)Ñ)¨#Ó.×4Ñ4Ó6€FØ*-°«-ŒS�—‘˜cÓ" 1Ñ%Ô&¸Q€FÞ#)ˆR��f‘ˆ~˜QÐÐ3¨tÐ3r+   c                ó˜  • [         R                  " 5       nUR                  nUc   eSUl        UR	                  [
        5        [        [        U 5      SS9 GHF  u  p4UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                  UR                   UR"                  (       a  [$        OSUR&                  S.nUR)                  5        H+  u  pgUS:X  a  M  UR+                  X6US9nSUl        SUl        M-     UR0                  c  MÝ  UR+                  US	UR2                  (       a  UR0                  S
-  OUR0                  S9n	UR2                  (       d  GM,  [5        UR                  5      U	l        GMI     [7        5       n
UR9                  U
5        U
R;                  5       $ )uS  The workbook. Text cells are typed as text â€” nothing reformats the
printed figure and a leading `=` is characters. A percentage's numeric
cell holds the fraction under a percent format, so it computes as a rate
and displays as the document printed it; the writer picks a value and a
format and performs no arithmetic beyond that rescale.Úbatché   )Ústartr   )r`   rk   é   é   é   é   é   é	   é
   é   é   )r2   Úcolumnr7   ÚsÚ@é   éd   )ÚopenpyxlÚWorkbookÚactiveÚtitlerR   ÚCOLUMNSÚ	enumerater3   r   r   r   r   r    r!   r"   r   r   r   ÚREVIEWr   ÚitemsÚcellÚ	data_typeÚnumber_formatr   r   rh   r   ÚsaveÚgetvalue)r1   ÚwbÚwsrG   r2   ÚtextsÚcolr7   rƒ   ÚnumÚouts              r,   Ú
write_xlsxrŽ   š   s[  € ô 
×	Ò	Ó	€BØ	�‰€BØ‰>Ðˆ>Ø€B„HØ‡I�IŒgÔÜœJ uÓ-°QÕ7‰ˆà�|‰|Ø�{‰{Ø�x‰xØ�z‰zØ�y‰yØ�}‰}Ø�}‰}Ø�z‰zØ—‘ØŸ*Ÿ*•¨"Ø—	‘	ñ
ˆð  Ÿ+™+ž-‰JˆCØ˜‹{ñ Ø—7‘7˜q°E�7Ð:ˆDØ ˆDŒNØ!$ˆDÖñ (ð �:‰:Ó!Ø—'‘'˜a¨ÀSÇ[Ç[°·±¸cÒ1AÐVY×V`ÑV`�'ÐaˆCØ�{�{Š{Ü$2°3·;±;Ó$?�×!ñ3 8ô4 ‹)€CØ‡G�GˆC„LØ�<‰<‹>Ðr+   c                ó2   • SU R                  SS5      -   S-   $ )NÚ"z"")Úreplace©rw   s    r,   Ú_quoter“   Ä   s   € Ø�—‘˜3 Ó%Ñ%¨Ñ+Ð+r+   c                ó4   • U SS S;   a  SU -   n [        U 5      $ )zÏA free-text cell: prefixed when it begins with a formula character,
then quoted. The prefix is the injection guard; the quotes are what keeps
the text as typed when the importer treats quoted fields as text.Nr`   )Ú=Ú+Ú-rx   Ú')r“   r’   s    r,   Ú
_free_textr™   È   s&   € ð 	ˆˆ!€uÐ$Ó$Ø�!‰GˆÜ�!‹9Ðr+   c                óX  • SR                  [        5      /n[        U 5       GHq  nUR                  c  SnO6UR                  (       a  UR                   S3nO[        UR                  5      nUR                  SR                  [        UR                  5      UR                  (       a  [        UR                  5      O[        UR                  5      U[        UR                  5      [        UR                  5      [        UR                  5      [        UR                  5      [        UR                   5      [        UR"                  5      [        UR$                  5      UR&                  (       a  [(        OS[        UR*                  5      /5      5        GMt     SR                  U5      S-   $ )uö   The same rows as text. The printed column is quoted and never prefixed;
the numeric column is bare, and a percentage keeps its percent sign so the
importer stores the fraction under a percent format â€” the same cell the
workbook writes directly.r>   r   r^   z
)rT   r   r3   r   r   r   rR   r™   r   r   r“   r   r   r   r    r!   r"   r   r   r   r�   r   )r1   Úlinesr2   r   s       r,   Ú	write_csvrœ   Ñ   s!  € ð
 �X‰X”gÓÐ€EÜ˜%× ˆØ�:‰:ÑØ‰FØ�[�[ØŸ
™
�| 1Ð%‰Fä˜Ÿ™“_ˆFØ�‰Ø�H‰Hä˜sŸ|™|Ó,Ø+.¯:¯:”F˜3Ÿ;™;Ô'¼:ÀcÇkÁkÓ;RØÜ˜sŸx™xÓ(Ü˜sŸz™zÓ*Ü˜3Ÿ9™9Ó%Ü˜3Ÿ=™=Ó)Ü˜sŸ}™}Ó-Ü˜3Ÿ:™:Ó&Ü˜sŸx™xÓ(Ø!ŸjŸj•F¨bÜ˜sŸy™yÓ)ðó÷	
ñ !ð2 �;‰;�uÓ Ñ&Ð&r+   )r1   r   ÚreturnzList[ExportRow])r   r   r   zOptional[str]r0   zOptional[Dict[str, Any]]r�   r   )r   r   r�   r   )r1   r   r�   Úbytes)rw   r   r�   r   )r1   r   r�   r   )!r(   Ú
__future__r   rP   Údecimalr   Úior   Útypingr   r   r   r	   r{   Úpydanticr
   Ú quber.playground.answers.figuresr   r   Úquber.playground.batchr   r   rO   r�   rS   r   r3   r/   rh   rŽ   r“   r™   rœ   r#   r+   r,   Ú<module>r¦      sv   ðñõ6 #ã Ý Ý ß ,Ó ,ã Ý ç EÝ +ò€ð,  €ð 
€à€ô�	ô ô*Uô.ôb4ô'ôT,ôõ'r+   