
    j                    x   U d Z ddlmZ ddlZddlmZ ddlmZ ddlZ	 ej
        e          Zi dg ddg d	d
g ddg ddg ddg ddg ddg ddg ddg ddg dddgdg dd g d!d"g d#d$g d%d&g d'g d(g d)g d*d+Zd,ed-<   h d.Zd?d5Zd@d6ZdAd8ZdBd9ZdCd<Z G d= d>          ZdS )DuO   LSEG Workspace data loader — reads bond data from downloaded Excel/CSV files.    )annotationsN)Path)Optionalisin)ISIN
IdentifierzBond IDCUSIPRICname)zInstrument Namez	Bond NameNameDescriptionzSecurity NameBondissuer)zIssuer NameIssuerzCompany NamezIssuer Short Namecoupon)CouponzCoupon Ratez
Coupon (%)zCoupon Rate (%)maturity_date)zMaturity DateMaturityzMaturity (Date)zMat Dateytm)YTMzYield to MaturityYieldzYTM (%)zYield to Maturity (%)ytw)YTWzYield to WorstzYTW (%)oas)OASz
OAS SpreadzOption Adjusted Spreadz	OAS (bps)zOAS Spread (bps)z_spread)zZ-SpreadzZ SpreadZSpreadzZ Spread (bps)zZ-Spread (bps)g_spread)zG-SpreadzG SpreadGSpreadzG Spread (bps)modified_duration)zModified DurationzMod DurationDurationzMod. DurationModDuration	convexity	Convexityrating_moodys)zMoody's RatingzMoody'szMoodys RatingMoodyszMoody's Long-Term	rating_sp)z
S&P RatingzS&Pz	SP RatingzS and P RatingzStandard & Poor'szS&P Long-Termrating_fitch)zFitch RatingFitchzFitch Long-Termprice)Pricez	Bid PricezClean Pricez	Mid Pricez
Last Pricez	Ask Pricecountry)CountryzCountry of RiskzCountry of DomicilezIssuer Country)SectorIndustryzIndustry SectorzGICS SectorzBloomberg Sector)CurrencyCCYCcy)z
Issue SizezAmount OutstandingzFace AmountNotionalzAmt Outstandingsectorcurrency
issue_sizezdict[str, list[str]]_COL_MAP>   n/a#n/an/a.#value! nannone-dfpd.DataFramealiases	list[str]returnOptional[str]c                    d | j         D             }|D ]A}|| j         v r|c S |                                |v r||                                         c S Bd S )Nc                8    i | ]}|                                 |S  )lower).0cs     /app/app/providers/lseg.py
<dictcomp>z_find_col.<locals>.<dictcomp>(   s"    3331!''))Q333    )columnsrJ   )rA   rC   
cols_loweraliass       rM   	_find_colrS   '   sr    33
333J - -BJLLL;;==J&&ekkmm,,,, '4rO   c                    i }t                                           D ]"\  }}t          | |          }|r||k    r|||<   #|                     |          } t           D ]}|| j        vrd | |<   | S )N)rP   )r8   itemsrS   renamerP   )rA   rV   norm_keyrC   founds        rM   _normalize_dfrY   1   s    F%^^-- % %'"g&& 	%Uh&&$F5M	6	"	"B    2:%%BxLIrO   Optional[float]c                >   | d S t          | t                    rt          j        |           rd S 	 t          t	          |                               dd                              dd                                                    S # t          t          f$ r Y d S w xY w)N,r=   %)	
isinstancefloatpdisnastrreplacestrip
ValueError	TypeError)vals    rM   	_to_floatrh   >   s    
{t#u "'#,, tSXX%%c2..66sB??EEGGHHH	"   tts   AB BBc                    | d S t          |                                           }|                                t          v rd n|S N)rb   rd   rJ   _NULL_STRINGS)rg   ss     rM   _to_strrm   I   s=    
{tCA7799--4414rO   pathr   c                    | j                                         }|dv rt          j        | d          S |dk    rt          j        |           S t          d|           )N>   .xls.xlsxopenpyxl)engine.csvzUnsupported file type: )suffixrJ   r`   
read_excelread_csvre   )rn   ru   s     rM   
_read_filerx   P   sh    [  F"""}T*5555{4   
7v77
8
88rO   c                  (    e Zd ZdZd
dZd ZddZd	S )LsegDataLoaderzBReads and normalizes bond data from LSEG Workspace exported files.	data_pathrb   c                .    t          |          | _        d S rj   )r   r{   )selfr{   s     rM   __init__zLsegDataLoader.__init__\   s    irO   c              #  D  K   | j                                         s"t                              d| j                    d S t	          | j                                                   D ];}|j                                        dv r|j        	                    d          s|V  <d S )Nz!LSEG data path does not exist: %s>   rt   rp   rq   ~)
r{   existsloggerwarningsortediterdirru   rJ   r   
startswith)r}   ps     rM   _iter_fileszLsegDataLoader._iter_files_   s      ~$$&& 	NN>OOOF..0011 	 	Ax~~#<<<QVEVEVWZE[E[<	 	rO   rE   
list[dict]c           
     0   g }|                                  D ]}	 t          |          }t          |          }|                    |           t                              dt          |          |j                   e# t          $ r+}t          	                    d|j        |           Y d}~d}~ww xY w|sg S t          j        |d          }||d                                                  }|                    dgd          }g }|                                D ]\  }}|                    i dt          |d                   d	t          |d	                   d
t          |d
                   dt!          |d                   dt          |d                   dt!          |d                   dt!          |d                   dt!          |d                   dt!          |d                   dt!          |d                   dt!          |d                   dt!          |d                   dt          |d                   dt          |d                   dt          |d                   dt!          |d                   dt          |d                   t          |d                   t          |d                   t!          |d                   d           t                              dt          |                     |S )zCReturn a list of normalized bond dicts from all files in data_path.zLSEG: loaded %d rows from %su   LSEG: skipping %s — %sNT)ignore_indexr   last)subsetkeepr   r   r   r   r   r   r   r   r   r    r#   r%   r'   r(   r*   r,   r5   r6   r7   r4   zLSEG: %d unique bonds loaded)r   rx   rY   appendr   infolenr   	Exceptionr   r`   concatnotnadrop_duplicatesiterrowsrm   rh   )	r}   framesrn   rA   exccombinedrecords_rows	            rM   
load_bondszLsegDataLoader.load_bondsg   s[   %'$$&& 	K 	KDK%%"2&&b!!!:CGGTYOOOO K K K949cJJJJJJJJK  	I9V$777HV,22445++F8&+II'')) 	 	FAsNN WS[%9%9WS[%9%9 WS]%;%; Ys8}%=%=	
  WS-A%B%B Ys5z%:%: Ys5z%:%: Ys5z%:%: Ys:%?%? Ys:%?%? $Ys3F/G%H%H Ys;/?%@%@  WS-A%B%B WS-=%>%> WS-@%A%A  Ys7|%<%<!" WS^%<%<#$ &-S]%;%;%,S_%=%=%.s</@%A%A)      . 	2CLLAAAs   A!A<<
B1!B,,B1N)r{   rb   )rE   r   )__name__
__module____qualname____doc__r~   r   r   rI   rO   rM   rz   rz   Y   sQ        LL) ) ) )  - - - - - -rO   rz   )rA   rB   rC   rD   rE   rF   )rA   rB   rE   rB   )rE   rZ   )rE   rF   )rn   r   rE   rB   )r   
__future__r   loggingpathlibr   typingr   pandasr`   	getLoggerr   r   r8   __annotations__rk   rS   rY   rh   rm   rx   rz   rI   rO   rM   <module>r      s   U U U " " " " " "                 		8	$	$"
JJJ"
iii" WWW" SSS	"
 UUU" 
bbb" 
===" 
iii" ```" NNN" jjj" +" fff" sss" EEE"  fff!"" ```#"$ fee333kkk)" " "    . KJJ   
 
 
 
   5 5 5 59 9 9 9; ; ; ; ; ; ; ; ; ;rO   