
    E%j                    2   d Z ddlmZ ddlZddlZddlmZ ddlmZ  ee	      j                         j                  Zedz  Zedz  Zg dZg d	Zd
dddd
dddd
dddddddddddd	ZddZddZd dZd!dZefd"dZefd#dZd$dZedk(  r e        yy)%a  Offline DLR Development Lifecycle extractor.

Reads one compact, verified source-snippet row per quarter and emits canonical
metric rows for the DLR Development Lifecycle total row. This is not a live PDF
parser; it is an offline parser seeded from audited primary-source snippets.
    )annotationsN)Decimal)Pathz&dlr_development_lifecycle_manifest.csvz"dlr_development_lifecycle_rows.csv)companyperiodmetric_name	raw_valuenormalized_valueunitvalue_qualifierownership_basisforecast_or_actual
is_derived
source_urlsource_locationsource_snippetsnippet_support_status
confidencecaveatfuture_land_mwfuture_shell_mwconstruction_underway_mwpreleased_percentage0development_lifecycle_total_investment_100_usd_mMWforecast_pipeline_estimatezhLand MW is stored separately from shell MW; expected capacity may differ from actual capacity developed.)r   r   r   z+Shell MW is stored separately from land MW.actual_development_snapshotz$Actual table snapshot, not forecast.percentz1Pre-leased percentage applies to construction MW.USD_millions mixed_actual_forecast_investmentz@Source reports dollars in thousands; normalized to USD millions.c                x    t        | j                  dd      j                  dd      j                  dd            S )N, $%)r   replace)values    e/home/dev/research_loop/runs/next3-task1-dlr-extractor/artifacts/extract_dlr_development_lifecycle.pyparse_numberr*   P   s2    5==b)11#r:BB3KLL    c                    t        | j                         d      }d|v r |j                  d      j                  d      }|S )Nf.0)format	normalizerstrip)r(   texts     r)   decimal_to_textr4   T   s:    %//#S)D
d{{{3&&s+Kr+   c                    t        j                  d      }|j                  | j                               }|st	        d|       |j                         S )Nzq^Total\s+(?P<land>[\d,]+)\s+(?P<shell>[\d,]+)\s+\$[\d,]+\s+\$[\d,]+\s+(?P<underway>[\d,]+)\s+(?P<preleased>\d+%)$z1Unparseable Development Lifecycle total snippet: )recompilematchstrip
ValueError	groupdict)snippetpatternr8   s      r)   parse_capacity_snippetr>   [   sN    jj	 G MM'--/*ELWKXYY??r+   c                    t        j                  d|       }t        |      dk  rt        d|       |d   }t	        |      t        d      z  }|t        |      fS )Nz
\$([\d,]+)   z+Expected at least three investment values: 1000)r6   findalllenr:   r*   r   r4   )r<   money_valuesraw_thousandsnormalized_millionss       r)   parse_investment_snippetrH   k   sa    ::mW5L
<1FwkRSS $M&}5G/*=>>>r+   c                   g }| j                  dd      5 }t        j                  |      }|D ]  }t        |d         }t	        |d         \  }}|d   t        t        |d               f|d   t        t        |d               f|d   t        t        |d               f|d	   t        t        |d	               f||fd
}t        D ]~  }	t        |	   }
||	   \  }}|	dk(  r|d   n|d   }|j                  i d|d   d|d   d|	d|d|d|
d   ddddd|
d   ddd|d   d|d   d|d|d   d|d   d|
d            	 d d d        |S # 1 sw Y   |S xY w)Nr$   utf-8newlineencoding'development_lifecycle_total_row_snippet(development_lifecycle_investment_snippetlandshellunderway	preleasedr   r   r   r   r   r	   r
   r   r   exactr   total_100_sharer   r   falser   r   r   r   r   r   )
opencsv
DictReaderr>   rH   r4   r*   METRIC_ORDERMETRIC_METADATAappend)manifest_pathrowshandlereadersourceparsedinvestment_rawinvestment_normalizedvaluesr   metadatar	   r
   r   s                 r)   canonical_rowsrg   t   sd   !#D			B		9 3V' 1	F+F3\,]^F4LAB51N1 $*&>?<PVW]P^C_3`"a$*7O_\RXY`RaEb5c#d:&#L
1C$DE-
 ;'#L1D$EF)
 #)EF"  , *;7.4[.A+	+ #&XX EF IJ 
 !6)#4 &"2 &{ $Y	
 +,<  0 *7 *+< -h7K.L %g %f\&: *62C+D ). 1&9Q2R %f\&:  !(8"4!-1	3h Ki3h Ks   D7EE"c                    |j                  ddd      5 }t        j                  |t              }|j	                          |j                  |        d d d        y # 1 sw Y   y xY w)Nwr$   rJ   rK   )
fieldnames)rW   rX   
DictWriterCANONICAL_FIELDSwriteheader	writerows)r^   output_pathr_   writers       r)   
write_rowsrq      sY    			#rG		< 3CD  s   =AA$c                 *    t        t                      y )N)rq   rg    r+   r)   mainrt      s    ~ r+   __main__)r(   strreturnr   )r(   r   rw   rv   )r<   rv   rw   zdict[str, str])r<   rv   rw   ztuple[str, str])r]   r   rw   list[dict[str, str]])r^   rx   ro   r   rw   None)rw   ry   )__doc__
__future__r   rX   r6   decimalr   pathlibr   __file__resolveparentBASE_DIRDEFAULT_MANIFESTDEFAULT_OUTPUTrl   rZ   r[   r*   r4   r>   rH   rg   rq   rt   __name__rs   r+   r)   <module>r      s    # 
 	   >!!#**FF @@ & :5	 :? ;8! ;E @T91@M ? *: 6r @N ! zF r+   