
    U>qj                     t    d dl mZmZ d dlZd dlmZmZmZ ddl	m
Z
 ddlmZ ddlmZ e
 dZ G d	 d
e      Zy)    )ABCabstractmethodN)DictListOptional   )_QUERY1_URL_)YfData)Tickerz/v1/financec                   F   e Zd ZdZddedefdZedefd       Zedefd       Zedefd	       Z	ede
fd
       Zedefd       Zedeej                      fd       Zedeeeef      fd       ZdefdZddZdefdZdeej                      fdZedd       ZddZy)Domainz
    Abstract base class representing a domain entity in financial data, with key attributes 
    and methods for fetching and parsing data. Derived classes must implement the `_fetch_and_parse()` method.
    Nkeyregionc                     || _         || _        |j                         j                         | _        t        |      | _        d| _        d| _        d| _	        d| _
        d| _        y)a  
        Initializes the Domain object with a key, session, and region.

        Args:
            key (str): Unique key identifying the domain entity.
            session (Optional[requests.Session]): Session object for HTTP requests. Defaults to None.
            region (str): Yahoo region (ISO 3166-1 alpha-2 country code, e.g.
                "US", "GB", "FR", "DE", "JP"). Determines the regional scope
                of returned data such as ``top_companies``. Defaults to "US".
        )sessionN)_keyr   stripupper_regionr
   _data_name_symbol	_overview_top_companies_research_reports)selfr   r   r   s       U/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/yfinance/domain/domain.py__init__zDomain.__init__   sY     	"LLN002#G4
$(
&*)-7;AE    returnc                     | j                   S )z
        Retrieves the key of the domain entity.

        Returns:
            str: The unique key of the domain entity.
        )r   r   s    r   r   z
Domain.key'   s     yyr   c                 P    | j                  | j                         | j                  S )z|
        Retrieves the name of the domain entity.

        Returns:
            str: The name of the domain entity.
        )_ensure_fetchedr   r"   s    r   namezDomain.name1   s      	TZZ(zzr   c                 P    | j                  | j                         | j                  S )z
        Retrieves the symbol of the domain entity.

        Returns:
            str: The symbol representing the domain entity.
        )r$   r   r"   s    r   symbolzDomain.symbol<   s      	T\\*||r   c                 b    | j                  | j                         t        | j                        S )z
        Retrieves a Ticker object based on the domain entity's symbol.

        Returns:
            Ticker: A Ticker object associated with the domain entity.
        )r$   r   r   r"   s    r   tickerzDomain.tickerG   s%     	T\\*dll##r   c                 P    | j                  | j                         | j                  S )z
        Retrieves the overview information of the domain entity.

        Returns:
            Dict: A dictionary containing an overview of the domain entity.
        )r$   r   r"   s    r   overviewzDomain.overviewR   s      	T^^,~~r   c                 P    | j                  | j                         | j                  S )z
        Retrieves the top companies within the domain entity.

        Returns:
            pandas.DataFrame: A DataFrame containing the top companies in the domain.
        )r$   r   r"   s    r   top_companieszDomain.top_companies]   s$     	T001"""r   c                 P    | j                  | j                         | j                  S )z
        Retrieves research reports related to the domain entity.

        Returns:
            List[Dict[str, str]]: A list of research reports, where each report is a dictionary with metadata.
        )r$   r   r"   s    r   research_reportszDomain.research_reportsh   s$     	T334%%%r   c                 b    ddd| j                   d}| j                  j                  ||      }|S )z
        Fetches data from the given query URL.

        Args:
            query_url (str): The URL used for the data query.

        Returns:
            Dict: The JSON response data from the request.
        truezen-US)	formattedwithReturnslangr   )params)r   r   get_raw_json)r   	query_urlparams_dictresults       r   _fetchzDomain._fetchs   s7     %+67^b^j^jk((;(Gr   c                     |j                  d      | _        |j                  d      | _        | j                  |j                  di             | _        | j                  |j                  di             | _        |j                  d      | _        y)z
        Parses and assigns common data fields such as name, symbol, overview, and top companies.

        Args:
            data (Dict): The raw data received from the API.
        r%   r'   r+   topCompaniesresearchReportsN)getr   r   _parse_overviewr   _parse_top_companiesr   r   )r   datas     r   _parse_and_assign_commonzDomain._parse_and_assign_common   sr     XXf%
xx)--dhhz2.FG"77QS8TU!%*;!<r   c           
      V   |j                  dd      |j                  di       j                  dd      |j                  dd      |j                  dd      |j                  dd      |j                  di       j                  dd      |j                  d	i       j                  dd      d
S )z
        Parses the overview data for the domain entity.

        Args:
            overview (Dict): The raw overview data.

        Returns:
            Dict: A dictionary containing parsed overview information.
        companiesCountN	marketCaprawmessageBoardIddescriptionindustriesCountmarketWeightemployeeCount)companies_count
market_capmessage_board_idrH   industries_countmarket_weightemployee_count)r>   )r   r+   s     r   r?   zDomain._parse_overview   s      (||,<dC",,{B7;;E4H (-=t D#<<t< (-> E%\\."=AA%N&ll?B?CCE4P
 	
r   c           
      &   g d}|D cg c]U  }|j                  d      |j                  d      |j                  d      |j                  di       j                  dd      fW }}|syt        j                  ||      j                  d      S c c}w )	a)  
        Parses the top companies data and converts it into a pandas DataFrame.

        Args:
            top_companies (Dict): The raw top companies data.

        Returns:
            Optional[pandas.DataFrame]: A DataFrame containing top company data, or None if no data is available.
        )r'   r%   ratingzmarket weightr'   r%   rS   rJ   rF   N)columns)r>   _pd	DataFrame	set_index)r   r-   top_companies_columnctop_companies_valuess        r   r@   zDomain._parse_top_companies   s      M Ta b OP "#x !f !h !nR 8 < <U4 H!J  b  b
 $}}1;OPZZ[cdd bs   ABc                     t        d      )z
        Abstract method for fetching and parsing domain-specific data. 
        Must be implemented by derived classes.
        z>_fetch_and_parse() needs to be implemented by children classes)NotImplementedErrorr"   s    r   _fetch_and_parsezDomain._fetch_and_parse   s     ""bccr   c                 *    || j                          yy)z
        Ensures that the given attribute is fetched by calling `_fetch_and_parse()` if the attribute is None.

        Args:
            attribute: The attribute to check and potentially fetch.
        N)r]   )r   	attributes     r   r$   zDomain._ensure_fetched   s     !!# r   )NUS)r    N)__name__
__module____qualname____doc__strr   propertyr   r%   r'   r   r)   r   r+   r   rU   rV   r-   r   r/   r:   rB   r?   r@   r   r]   r$    r   r   r   r      s?   
FC Fs F, S   c      $ $ $ $   #x6 # # &$tCH~"6 & &4 =
4 
(eXcmm5L e* d d$r   r   )abcr   r   pandasrU   typingr   r   r   constr	   rA   r
   r)   r   _QUERY_URL_r   rg   r   r   <module>rm      s4    #  ' '    k*|$S |$r   