o
    0=j­„  ã                   @   sl  d Z ddlZddlZddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZ ddlZddlZddlZddlmZmZmZ ddlZejejd� e e¡ZdZdZed	 ZG d
d„ deƒZG dd„ deƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZ G dd„ dƒZ!G dd„ dƒZ"d!dedeee#e#f  fdd„Z$		d"dedeee#e#f  dee# fdd„Z%G dd „ d ƒZ&dS )#zxFirebase Remote Config Module.
This module has required APIs for the clients to use Firebase Remote Config with python.
é    N)ÚDictÚOptionalÚLiteralÚUnionÚAny)ÚEnum)ÚAppÚ_http_clientÚ_utils)ÚlevelZ_remoteconfigé
   )ÚdefaultÚremoteÚstaticc                   @   s    e Zd ZdZdZdZdZdZdS )ÚPercentConditionOperatorzFEnum representing the available operators for percent conditions.
    ÚLESS_OR_EQUALÚGREATER_THANÚBETWEENÚUNKNOWNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   © r   r   úfC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\firebase_admin/remote_config.pyr   '   s    r   c                   @   sT   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZdZdS )ÚCustomSignalOperatorzLEnum representing the available operators for custom signal conditions.
    ÚSTRING_CONTAINSÚSTRING_DOES_NOT_CONTAINÚSTRING_EXACTLY_MATCHESÚSTRING_CONTAINS_REGEXÚNUMERIC_LESS_THANÚNUMERIC_LESS_EQUALÚNUMERIC_EQUALÚNUMERIC_NOT_EQUALÚNUMERIC_GREATER_THANÚNUMERIC_GREATER_EQUALÚSEMANTIC_VERSION_LESS_THANÚSEMANTIC_VERSION_LESS_EQUALÚSEMANTIC_VERSION_EQUALÚSEMANTIC_VERSION_NOT_EQUALÚSEMANTIC_VERSION_GREATER_THANÚSEMANTIC_VERSION_GREATER_EQUALr   N)r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r   r   r   r   r   r   /   s&    r   c                   @   sT   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZedd	„ ƒZed
d„ ƒZ	edd„ ƒZ
dS )Ú_ServerTemplateDataz>Parses, validates and encapsulates template data and metadata.c                 C   sª   d|v r|d dur|d | _ ntdƒ‚i | _ d|v r+|d dur'|d | _ntdƒ‚g | _d| _d|v r:|d | _d| _d|v rMt|d tƒrM|d | _t |¡| _	dS )	zîInitializes a new ServerTemplateData instance.

        Args:
            template_data: The data to be parsed for getting the parameters and conditions.

        Raises:
            ValueError: If the template data is not valid.
        Ú
parametersNz2Remote Config parameters must be a non-null objectÚ
conditionsz2Remote Config conditions must be a non-null objectÚ ÚversionÚetag)
Ú_parametersÚ
ValueErrorÚ_conditionsÚ_versionÚ_etagÚ
isinstanceÚstrÚjsonÚdumpsÚ_template_data_json)ÚselfÚtemplate_datar   r   r   Ú__init__F   s"   	

z_ServerTemplateData.__init__c                 C   ó   | j S ©N)r2   ©r<   r   r   r   r-   i   ó   z_ServerTemplateData.parametersc                 C   r?   r@   )r6   rA   r   r   r   r1   m   rB   z_ServerTemplateData.etagc                 C   r?   r@   )r5   rA   r   r   r   r0   q   rB   z_ServerTemplateData.versionc                 C   r?   r@   )r4   rA   r   r   r   r.   u   rB   z_ServerTemplateData.conditionsc                 C   r?   r@   )r;   rA   r   r   r   Útemplate_data_jsony   rB   z&_ServerTemplateData.template_data_jsonN)r   r   r   r   r>   Úpropertyr-   r1   r0   r.   rC   r   r   r   r   r,   D   s    #



r,   c                   @   sv   e Zd ZdZddedeeeef  fdd„Zdd„ Z	dd	eeee
eef f  d
dfdd„Zdefdd„Zdd„ ZdS )ÚServerTemplatezZRepresents a Server Template with implementations for loading and evaluating the template.NÚappÚdefault_configc                 C   sR   t  |tt¡| _d| _i | _t ¡ | _	|dur%|D ]}t
|| ƒ| j|< qdS dS )a  Initializes a ServerTemplate instance.

        Args:
          app: App instance to be used. This is optional and the default app instance will
                be used if not present.
          default_config: The default config to be used in the evaluated config.
        N)r
   Zget_app_serviceÚ_REMOTE_CONFIG_ATTRIBUTEÚ_RemoteConfigServiceÚ_rc_serviceÚ_cacheÚ_stringified_default_configÚ	threadingÚRLockÚ_lockr8   )r<   rF   rG   Úkeyr   r   r   r>   €   s   ÿ
þzServerTemplate.__init__c                 Ã   sD   �| j  ¡ I dH }| j� || _W d  ƒ dS 1 sw   Y  dS )z0Fetches the server template and caches the data.N)rJ   Úget_server_templaterO   rK   )r<   Zrc_server_templater   r   r   Úload—   s
   €"ÿzServerTemplate.loadÚcontextÚreturnÚServerConfigc                 C   sœ   | j stdƒ‚|p
i }i }| j� | j j}| j j}W d  ƒ n1 s#w   Y  | jdur>| j ¡ D ]\}}td|ƒ||< q2t||||ƒ| _	t
| j	 ¡ d�S )a"  Evaluates the cached server template to produce a ServerConfig.

        Args:
          context: A dictionary of values to use for evaluating conditions.

        Returns:
          A ServerConfig object.
        Raises:
            ValueError: If the input arguments are invalid.
        zmNo Remote Config Server template in cache.
                            Call load() before calling evaluate().Nr   )Úconfig_values)rK   r3   rO   r.   r-   rL   ÚitemsÚ_ValueÚ_ConditionEvaluatorZ
_evaluatorrU   Úevaluate)r<   rS   rV   Ztemplate_conditionsZtemplate_parametersrP   Úvaluer   r   r   rZ   �   s    
þ
þzServerTemplate.evaluaterC   c                 C   sD   t  |¡}t|ƒ}| j� || _W d  ƒ dS 1 sw   Y  dS )z¾Updates the cache to store the given template is of type ServerTemplateData.

        Args:
          template_data_json: A json string representing ServerTemplateData to be cached.
        N)r9   Úloadsr,   rO   rK   )r<   rC   Ztemplate_data_mapr=   r   r   r   Úset¼   s
   
"ÿzServerTemplate.setc                 C   sB   | j stdƒ‚| j� | j j}W d  ƒ |S 1 sw   Y  |S )zRProvides the server template in a JSON format to be used for initialization later.zkNo Remote Config Server template in cache.
                            Call load() before calling toJSON().N)rK   r3   rO   rC   )r<   Ztemplate_jsonr   r   r   Úto_jsonÈ   s   

ÿþzServerTemplate.to_json©NNr@   )r   r   r   r   r   r   r   r8   r>   rR   r   ÚintrZ   r]   r^   r   r   r   r   rE   ~   s     (rE   c                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )rU   z.Represents a Remote Config Server Side Config.c                 C   s
   || _ d S r@   )Ú_config_values)r<   rV   r   r   r   r>   Ô   s   
zServerConfig.__init__c                 C   ó   |   |¡ ¡ S )úReturns the value as a boolean.N)Ú
_get_valueÚ
as_boolean©r<   rP   r   r   r   Úget_boolean×   ó   zServerConfig.get_booleanc                 C   rb   )úReturns the value as a string.N)rd   Ú	as_stringrf   r   r   r   Ú
get_stringÛ   rh   zServerConfig.get_stringc                 C   rb   )z Returns the value as an integer.N)rd   Úas_intrf   r   r   r   Úget_intß   rh   zServerConfig.get_intc                 C   rb   )zReturns the value as a float.N)rd   Úas_floatrf   r   r   r   Ú	get_floatã   rh   zServerConfig.get_floatc                 C   rb   ©z Returns the source of the value.N)rd   Ú
get_sourcerf   r   r   r   Úget_value_sourceç   rh   zServerConfig.get_value_sourcec                 C   s   | j  |tdƒ¡S )Nr   )ra   ÚgetrX   rf   r   r   r   rd   ë   s   zServerConfig._get_valueN)r   r   r   r   r>   rg   rk   rm   ro   rr   rd   r   r   r   r   rU   Ò   s    rU   c                   @   s:   e Zd ZdZdd„ Zdd„ Zdd„ Zedefd	d
„ƒZ	dS )rI   zhInternal class that facilitates sending requests to the Firebase Remote
        Config backend API.
    c                 C   sN   d}|j | _|j ¡ }ddtj› �i}|j dtj	¡}tj
||||d�| _dS )zÐInitialize a JsonHttpClient with necessary inputs.

        Args:
            app: App instance to be used for fetching app specific details required
                for initializing the http client.
        z+https://firebaseremoteconfig.googleapis.comzX-FIREBASE-CLIENTzfire-admin-python/ZhttpTimeout)Ú
credentialÚbase_urlÚheadersÚtimeoutN)Z
project_idÚ_project_idrt   Úget_credentialÚfirebase_adminÚ__version__Úoptionsrs   r	   ZDEFAULT_TIMEOUT_SECONDSZJsonHttpClientÚ_client)r<   rF   Zremote_config_base_urlZapp_credentialZ
rc_headersrw   r   r   r   r>   ó   s   
ÿþz_RemoteConfigService.__init__c              
   Ã   sn   �zt  ¡ }| d| jjd|  ¡ ¡I dH \}}W n tjjy+ } z|  	|¡‚d}~ww | 
d¡|d< t|ƒS )z™Requests for a server template and converts the response to an instance of
        ServerTemplateData for storing the template parameters and conditions.Nrs   r1   )ÚasyncioÚget_event_loopÚrun_in_executorr}   Zheaders_and_bodyÚ_get_urlÚrequestsÚ
exceptionsÚRequestExceptionÚ_handle_remote_config_errorrs   r,   )r<   Úlooprv   r=   Úerrorr   r   r   rQ     s   €þ
€ÿz(_RemoteConfigService.get_server_templatec                 C   s   d| j › d�S )zJReturns project prefix for url, in the format of /v1/projects/${projectId}z/v1/projects/z./namespaces/firebase-server/serverRemoteConfigN)rx   rA   r   r   r   r�     rh   z_RemoteConfigService._get_urlr‡   c                 C   s
   t  |¡S )z5Handles errors received from the Cloud Functions API.N)r
   Z#handle_platform_error_from_requests)Úclsr‡   r   r   r   r…     s   
z0_RemoteConfigService._handle_remote_config_errorN)
r   r   r   r   r>   rQ   r�   Úclassmethodr   r…   r   r   r   r   rI   ï   s    rI   c                   @   sÞ   e Zd ZdZdd„ Zdd„ Zdeeef fdd„Z			d!d
e
defdd„Z		d!d
e
defdd„Z		d!d
e
defdd„Zdefdd„Zdede
fdd„Zdefdd„Zdefdd„Zdefdd„Zdefdd„Zdefdd„Zd S )"rY   z_Internal class that facilitates sending requests to the Firebase Remote
    Config backend API.c                 C   s   || _ || _|| _|| _d S r@   )Ú_contextr4   r2   ra   )r<   r.   r-   rS   rV   r   r   r   r>     s   
z_ConditionEvaluator.__init__c           
      C   sö   |   | j| j¡}| jrx| j ¡ D ]g\}}| di ¡}| di ¡}d}|r9| ¡ D ]\}}||v r8|r8|| } q9q(|rG| d¡rGt d|¡ q|rW| d¡}	td|	ƒ| j	|< q|s`t 
d|¡ q| d¡rlt d|¡ qtd| d¡ƒ| j	|< q| j	S )	z]Internal function that evaluates the cached server template to produce
        a ServerConfigZconditionalValuesÚdefaultValueNZuseInAppDefaultz'Using in-app default value for key '%s'r[   r   z#No default value found for key '%s')Úevaluate_conditionsr4   rŠ   r2   rW   rs   ÚloggerÚinforX   ra   Úwarning)
r<   Úevaluated_conditionsrP   Z	parameterZconditional_valuesÚdefault_valueZparameter_value_wrapperZcondition_nameZcondition_evaluationZparameter_valuer   r   r   rZ   %  s6   €

z_ConditionEvaluator.evaluaterT   c                 C   s.   i }|D ]}|   | d¡|¡|| d¡< q|S )a%  Evaluates a list of conditions and returns a dictionary of results.

        Args:
          conditions: A list of NamedCondition objects.
          context: An EvaluationContext object.

        Returns:
          A dictionary that maps condition names to boolean evaluation results.
        Ú	conditionÚnameN)Úevaluate_conditionrs   )r<   r.   rS   r�   r’   r   r   r   rŒ   K  s   

ÿz'_ConditionEvaluator.evaluate_conditionsr   Únesting_levelc                 C   sÔ   |t krt d¡ dS | d¡dur|  | d¡||d ¡S | d¡dur1|  | d¡||d ¡S | d¡dur:dS | d	¡durCdS | d
¡durS|  | d
¡|¡S | d¡durc|  | d¡|¡S t d¡ dS )a  Recursively evaluates a condition.

        Args:
          condition: The condition to evaluate.
          context: An EvaluationContext object.
          nesting_level: The current recursion depth.

        Returns:
          The boolean result of the condition evaluation.
        z+Maximum condition recursion depth exceeded.FZorConditionNé   ZandConditionÚtrueTÚfalseÚpercentZcustomSignalz#Unknown condition type encountered.)ÚMAX_CONDITION_RECURSION_DEPTHr�   r�   rs   Úevaluate_or_conditionÚevaluate_and_conditionÚevaluate_percent_conditionÚ evaluate_custom_signal_condition)r<   r’   rS   r•   r   r   r   r”   \  s*   
ÿÿ
z&_ConditionEvaluator.evaluate_conditionc                 C   s8   |  d¡pg }|D ]}|  |||d ¡}|r dS q	dS )a&  Evaluates an OR condition.

        Args:
          or_condition: The OR condition to evaluate.
          context: An EvaluationContext object.
          nesting_level: The current recursion depth.

        Returns:
          True if any of the subconditions are true, False otherwise.
        r.   r–   TFN©rs   r”   )r<   Zor_conditionrS   r•   Úsub_conditionsÚsub_conditionÚresultr   r   r   r›   |  ó   ÿz)_ConditionEvaluator.evaluate_or_conditionc                 C   s8   |  d¡pg }|D ]}|  |||d ¡}|s dS q	dS )a(  Evaluates an AND condition.

        Args:
          and_condition: The AND condition to evaluate.
          context: An EvaluationContext object.
          nesting_level: The current recursion depth.

        Returns:
          True if all of the subconditions are met; False otherwise.
        r.   r–   FTNrŸ   )r<   Zand_conditionrS   r•   r    r¡   r¢   r   r   r   rœ   �  r£   z*_ConditionEvaluator.evaluate_and_conditionc                 C   s  |  d¡st d¡ dS |  d¡}|  d¡}|  d¡}|  d¡}|s)t d¡ dS |r:|  d	¡p1d
}|  d¡p8d
}nd
}d
}|rC|}	nd
}	|rL|› d�nd}
|
› |  d¡› �}|  |¡}|d }|tjjkrj||	kS |tjjkrt||	kS |tjjkr†||  k oƒ|kS   S t d|¡ dS )zñEvaluates a percent condition.

        Args:
          percent_condition: The percent condition to evaluate.
          context: An EvaluationContext object.

        Returns:
          True if the condition is met, False otherwise.
        Zrandomization_idzEMissing randomization_id in context for evaluating percent condition.FÚseedZpercentOperatorZmicroPercentZmicroPercentRangez/Missing percent operator for percent condition.ZmicroPercentUpperBoundr   ZmicroPercentLowerBoundÚ.r/   i áõzUnknown percent operator: %sN)	rs   r�   r�   Úhash_seeded_randomization_idr   r   r[   r   r   )r<   Zpercent_conditionrS   r¤   Zpercent_operatorZmicro_percentZmicro_percent_rangeZnorm_percent_upper_boundZnorm_percent_lower_boundZnorm_micro_percentZseed_prefixZstring_to_hashÚhash64Zinstance_micro_percentiler   r   r   r�   ¤  s<   







z._ConditionEvaluator.evaluate_percent_conditionÚseeded_randomization_idc                 C   s.   t  ¡ }| | d¡¡ | ¡ }tt|dƒƒS )z¯Hashes a seeded randomization ID.

        Args:
          seeded_randomization_id: The seeded randomization ID to hash.

        Returns:
          The hashed value.
        zutf-8é   N)ÚhashlibÚsha256ÚupdateÚencodeÚ	hexdigestÚabsr`   )r<   r¨   Zhash_objectr§   r   r   r   r¦   Ñ  s   	z0_ConditionEvaluator.hash_seeded_randomization_idc                 C   s²  |  d¡pi }|  d¡pi }|  d¡pi }t|||gƒs#t d¡ dS |s'dS |  |¡p-i }|s8t d|¡ dS |tjjkrG|  ||dd„ ¡S |tj	jkrW|  ||d	d„ ¡ S |tj
jkrf|  ||d
d„ ¡S |tjjkrt|  ||tj¡S |tjjkr†|  ||d |dd„ ¡S |tjjkr˜|  ||d |dd„ ¡S |tjjkrª|  ||d |dd„ ¡S |tjjkr¼|  ||d |dd„ ¡S |tjjkrÎ|  ||d |dd„ ¡S |tjjkrà|  ||d |dd„ ¡S |tjjkrò|  ||d |dd„ ¡S |tjjk�r|  ||d |dd„ ¡S |tjjk�r|  ||d |dd„ ¡S |tjjk�r+|  ||d |dd„ ¡S |tjjk�r>|  ||d |dd„ ¡S |tjjk�rQ|  ||d |dd„ ¡S t d|¡ dS )a  Evaluates a custom signal condition.

        Args:
          custom_signal_condition: The custom signal condition to evaluate.
          context: An EvaluationContext object.

        Returns:
          True if the condition is met, False otherwise.
        ZcustomSignalOperatorZcustomSignalKeyZtargetCustomSignalValueszDMissing operator, key, or target values for custom signal condition.Fz,Custom signal value not found in context: %sc                 S   ó   | |v S r@   r   ©ÚtargetÚactualr   r   r   Ú<lambda>þ  ó    zF_ConditionEvaluator.evaluate_custom_signal_condition.<locals>.<lambda>c                 S   r°   r@   r   r±   r   r   r   r´     rµ   c                 S   s   |   ¡ |  ¡ kS r@   )Ústripr±   r   r   r   r´     s    r   c                 S   ó   | dk S ©Nr   r   ©Úrr   r   r   r´     rµ   c                 S   ó   | dkS r¸   r   r¹   r   r   r   r´     rµ   c                 S   ó   | dkS r¸   r   r¹   r   r   r   r´     rµ   c                 S   ó   | dkS r¸   r   r¹   r   r   r   r´      rµ   c                 S   ó   | dkS r¸   r   r¹   r   r   r   r´   %  rµ   c                 S   ó   | dkS r¸   r   r¹   r   r   r   r´   *  rµ   c                 S   r·   r¸   r   r¹   r   r   r   r´   1  rµ   c                 S   r»   r¸   r   r¹   r   r   r   r´   6  rµ   c                 S   r¼   r¸   r   r¹   r   r   r   r´   ;  rµ   c                 S   r½   r¸   r   r¹   r   r   r   r´   @  rµ   c                 S   r¾   r¸   r   r¹   r   r   r   r´   E  rµ   c                 S   r¿   r¸   r   r¹   r   r   r   r´   J  rµ   z"Unknown custom signal operator: %sN)rs   Úallr�   r�   Údebugr   r   r[   Ú_compare_stringsr   r   r   ÚreÚsearchr    Ú_compare_numbersr!   r"   r#   r$   r%   r&   Ú_compare_semantic_versionsr'   r(   r)   r*   r+   )r<   Zcustom_signal_conditionrS   Zcustom_signal_operatorÚcustom_signal_keyZtarget_custom_signal_valuesZactual_custom_signal_valuer   r   r   rž   ß  sÖ   ÿ
þþþþýýýýýýýýýýýýz4_ConditionEvaluator.evaluate_custom_signal_conditionc                 C   s"   |D ]}||t |ƒƒr dS qdS )aj  Compares the actual string value of a signal against a list of target values.

        Args:
            target_values: A list of target string values.
            actual_value: The actual value to compare, which can be a string or number.
            predicate_fn: A function that takes two string arguments (target and actual)
                            and returns a boolean indicating whether
                            the target matches the actual value.

        Returns:
            bool: True if the predicate function returns True for any target value in the list,
                False otherwise.
        TFN)r8   )r<   Ztarget_valuesÚactual_valueÚpredicate_fnr²   r   r   r   rÂ   N  s
   ÿz$_ConditionEvaluator._compare_stringsc                 C   sX   zt |ƒ}t |ƒ}||k rdn||krdnd}||ƒW S  ty+   t d|¡ Y dS w )Néÿÿÿÿr–   r   z>Invalid numeric value for comparison for custom signal key %s.F)Úfloatr3   r�   r�   )r<   rÇ   Útarget_valuerÈ   rÉ   r²   r³   r¢   r   r   r   rÅ   b  s   
ÿýz$_ConditionEvaluator._compare_numbersc                 C   s   |   |t|ƒt|ƒ|¡S )a§  Compares the actual semantic version value of a signal against a target value.
        Calls the predicate function with -1, 0, 1 if actual is less than, equal to,
        or greater than target.

        Args:
        custom_signal_key: The custom signal for which the evaluation is being performed.
        target_values: A list of target string values.
        actual_value: The actual value to compare, which can be a string or number.
        predicate_fn: A function that takes an integer (-1, 0, or 1) and returns a boolean.

        Returns:
            bool: True if the predicate function returns True for the result of the comparison,
        False otherwise.
        N)Ú_compare_versionsr8   )r<   rÇ   rÌ   rÈ   rÉ   r   r   r   rÆ   m  s   ÿz._ConditionEvaluator._compare_semantic_versionsc           
      C   sî   zfdd„ |  d¡D ƒ}dd„ |  d¡D ƒ}tt|ƒt|ƒƒ}| dg|t|ƒ  ¡ | dg|t|ƒ  ¡ t||ƒD ]&\}}	t|dk |	dk fƒrKt‚||	k rV|dƒ  W S ||	kra|dƒ  W S q;|dƒW S  tyv   t d|¡ Y d	S w )aµ  Compares two semantic version strings.

        Args:
            custom_signal_key: The custom singal for which the evaluation is being performed.
            sem_version_1: The first semantic version string.
            sem_version_2: The second semantic version string.
            predicate_fn: A function that takes an integer and returns a boolean.

        Returns:
            bool: The result of the predicate function.
        c                 S   ó   g | ]}t |ƒ‘qS r   ©r`   ©Ú.0Úpartr   r   r   Ú
<listcomp>Ž  ó    z9_ConditionEvaluator._compare_versions.<locals>.<listcomp>r¥   c                 S   rÎ   r   rÏ   rÐ   r   r   r   rÓ   �  rÔ   r   rÊ   r–   zHInvalid semantic version format for comparison for custom signal key %s.FN)	ÚsplitÚmaxÚlenÚextendÚzipÚanyr3   r�   r�   )
r<   rÇ   Zsem_version_1Zsem_version_2rÉ   Zv1_partsZv2_partsÚ
max_lengthZpart1Zpart2r   r   r   rÍ   €  s,   ÿ
þüz%_ConditionEvaluator._compare_versionsN)r   )r   r   r   r   r>   rZ   r   r8   ÚboolrŒ   r`   r”   r›   rœ   r�   r¦   rž   rÂ   rÅ   rÆ   rÍ   r   r   r   r   rY     sL    &ÿÿ
ÿ"þþ
þþþ
þ
ÿ-
ÿo
ÿÿrY   rF   rG   c                 Ã   s    �t | |d�}| ¡ I dH  |S )a“  Initializes a new ServerTemplate instance and fetches the server template.

    Args:
        app: App instance to be used. This is optional and the default app instance will
            be used if not present.
        default_config: The default config to be used in the evaluated config.

    Returns:
        ServerTemplate: An object having the cached server template to be used for evaluation.
    ©rF   rG   N)Úinit_server_templaterR   )rF   rG   Útemplater   r   r   rQ   ¢  s   €rQ   rC   c                 C   s"   t | |d�}|dur| |¡ |S )aÜ  Initializes a new ServerTemplate instance.

    Args:
        app: App instance to be used. This is optional and the default app instance will
            be used if not present.
        default_config: The default config to be used in the evaluated config.
        template_data_json: An optional template data JSON to be set on initialization.

    Returns:
        ServerTemplate: A new ServerTemplate instance initialized with an optional
        template and config.
    rÝ   N)rE   r]   )rF   rG   rC   rß   r   r   r   rÞ   ±  s   
rÞ   c                   @   s„   e Zd ZdZdZdZdZdZg d¢Zefde	de
fd	d
„Zde
fdd„Zdefdd„Zdefdd„Zdefdd„Zde	fdd„ZdS )rX   z3Represents a value fetched from Remote Config.
    Fr/   r   g        )Ú1r—   ÚtÚyesÚyÚonÚsourcer[   c                 C   s   || _ || _dS )a™  Initializes a Value instance.

        Args:
          source: The source of the value (e.g., 'default', 'remote', 'static').
          "static" indicates the value was defined by a static constant.
          "default" indicates the value was defined by default config.
          "remote" indicates the value was defined by config produced by evaluating a template.
          value: The string value.
        N)rå   r[   )r<   rå   r[   r   r   r   r>   Í  s   

z_Value.__init__rT   c                 C   s   | j dkr| jS t| jƒS )ri   r   N)rå   ÚDEFAULT_VALUE_FOR_STRINGr8   r[   rA   r   r   r   rj   Ú  s   

z_Value.as_stringc                 C   s$   | j dkr| jS t| jƒ ¡ | jv S )rc   r   N)rå   ÚDEFAULT_VALUE_FOR_BOOLEANr8   r[   ÚlowerÚBOOLEAN_TRUTHY_VALUESrA   r   r   r   re   à  s   
z_Value.as_booleanc                 C   ó6   | j dkr| jS zt| jƒW S  ty   | j Y S w ©zReturns the value as a number.r   N)rå   ÚDEFAULT_VALUE_FOR_INTEGERr`   r[   r3   rA   r   r   r   rl   æ  ó   

ÿz_Value.as_intc                 C   rê   rë   )rå   ÚDEFAULT_VALUE_FOR_FLOAT_NUMBERrË   r[   r3   rA   r   r   r   rn   ï  rí   z_Value.as_floatc                 C   r?   rp   )rå   rA   r   r   r   rq   ø  rB   z_Value.get_sourceN)r   r   r   r   rç   ræ   rì   rî   ré   ÚValueSourcer8   r>   rj   rÜ   re   rË   rl   rn   rq   r   r   r   r   rX   Ä  s    		rX   r_   )NNN)'r   r~   r9   ÚloggingrM   Útypingr   r   r   r   r   Úenumr   rÃ   rª   r‚   rz   r   r	   r
   ÚbasicConfigÚINFOÚ	getLoggerr   r�   rH   rš   rï   r   r   r,   rE   rU   rI   rY   r8   rQ   rÞ   rX   r   r   r   r   Ú<module>   sD   
:T-    	ÿ
ÿ