o
    i,F                     @  sr  U d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	Z	ddl
Z
ddlmZmZmZmZmZmZmZmZmZmZmZmZ dd	lmZ erMdd
lmZ edZedZdZG dd deZej Z!d2ddZ"d3ddZ#e	j$e	j%e	j&e	j'e	j(dZ)d4ddZ*d5d d!Z+d6d#d$Z,d6d%d&Z-e,e-d'Z.d(e/d)< ed7d+d,Z0G d-d. d.eeef Z1G d/d0 d0Z2e2 Z3d0e/d1< dS )8zIProvide access to settings for globally used Azure configuration values.
    )annotations)
namedtuple)Enum)cacheN)TypeOptionalCallableUnionDictAnyTypeVarTupleGenericMappingListTYPE_CHECKING   )AzureClouds)AbstractSpanValidInputType	ValueType)settingsSettingsc                   @  s   e Zd ZdZdS )_Unsetr   N)__name__
__module____qualname__token r   r   J/home/app/Keep/.python/lib/python3.10/site-packages/azure/core/settings.pyr   =   s    r   valueUnion[str, bool]returnboolc                 C  s<   t | tr| S |  }|dv rdS |dv rdS td| )a  Convert a string to True or False

    If a boolean is passed in, it is returned as-is. Otherwise the function
    maps the following strings, ignoring case:

    * "yes", "1", "on" -> True
    " "no", "0", "off" -> False

    :param value: the value to convert
    :type value: str or bool
    :returns: A boolean value matching the intent of the input
    :rtype: bool
    :raises ValueError: If conversion to bool fails

    )yes1ontrueTrueT)no0offfalseFalseFz"Cannot convert {} to boolean value)
isinstancer#   lower
ValueErrorformat)r    valr   r   r   convert_boolD   s   
r3   Optional[Union[str, bool]]c                 C  s    | du rt  r
dS dS t| S )a  Convert tracing value to bool with regard to tracing implementation.

    :param value: the value to convert
    :type value: str or bool or None
    :returns: A boolean value matching the intent of the input
    :rtype: bool
    :raises ValueError: If conversion to bool fails
    NTF)r   tracing_implementationr3   )r    r   r   r   convert_tracing_enabled^   s
   	r6   )CRITICALERRORWARNINGINFODEBUGUnion[str, int]intc                 C  s>   t | tr| S |  }t|}|std| dt|S )a  Convert a string to a Python logging level

    If a log level is passed in, it is returned as-is. Otherwise the function
    understands the following strings, ignoring case:

    * "critical"
    * "error"
    * "warning"
    * "info"
    * "debug"

    :param value: the value to convert
    :type value: str or int
    :returns: A log level as an int. See the logging module for details.
    :rtype: int
    :raises ValueError: If conversion to log level fails

    z4Cannot convert {} to log level, valid values are: {}, )r.   r=   upper_levelsgetr0   r1   join)r    r2   levelr   r   r   convert_loggingy   s   

rD   Union[str, AzureClouds]r   c                 C  s^   t | tr| S t | tr(dd tD }| |v r||  S td| d| td| )zConvert a string to an Azure Cloud

    :param value: the value to convert
    :type value: string
    :returns: An AzureClouds enum value
    :rtype: AzureClouds
    :raises ValueError: If conversion to AzureClouds fails

    c                 S  s   i | ]}|j |qS r   )name).0cloudr   r   r   
<dictcomp>   s    z'convert_azure_cloud.<locals>.<dictcomp>z6Cannot convert {} to Azure Cloud, valid values are: {}r>   z Cannot convert {} to Azure Cloud)r.   r   strr0   r1   rB   keys)r    Zazure_cloudsr   r   r   convert_azure_cloud   s   


rL   Optional[Type[AbstractSpan]]c                  C  (   z	ddl m}  | W S  ty   Y dS w )zReturns the OpenCensusSpan if the opencensus tracing plugin is installed else returns None.

    :rtype: type[AbstractSpan] or None
    :returns: OpenCensusSpan type or None
    r   OpenCensusSpanN)Z&azure.core.tracing.ext.opencensus_spanrP   ImportErrorrO   r   r   r   _get_opencensus_span      rR   c                  C  rN   )zReturns the OpenTelemetrySpan if the opentelemetry tracing plugin is installed else returns None.

    :rtype: type[AbstractSpan] or None
    :returns: OpenTelemetrySpan type or None
    r   OpenTelemetrySpanN)Z)azure.core.tracing.ext.opentelemetry_spanrU   rQ   rT   r   r   r   _get_opentelemetry_span   rS   rV   )Z
opencensusZopentelemetryz5Dict[str, Callable[[], Optional[Type[AbstractSpan]]]]_tracing_implementation_dict(Optional[Union[str, Type[AbstractSpan]]]c                 C  sZ   | du rdS t | ts| S |  } t| dd }| }|tu r+td| dt|S )ao  Convert a string to AbstractSpan

    If a AbstractSpan is passed in, it is returned as-is. Otherwise the function
    understands the following strings, ignoring case:

    * "opencensus"
    * "opentelemetry"

    :param value: the value to convert
    :type value: string
    :returns: AbstractSpan
    :raises ValueError: If conversion to AbstractSpan fails

    Nc                   S  s   t S N)_unsetr   r   r   r   <lambda>       z&convert_tracing_impl.<locals>.<lambda>z7Cannot convert {} to AbstractSpan, valid values are: {}r>   )	r.   rJ   r/   rW   rA   rZ   r0   r1   rB   )r    Zget_wrapper_classZwrapper_classr   r   r   convert_tracing_impl   s   

r]   c                   @  s   e Zd ZdZddedfd*ddZd+ddZd,d-ddZd,d.ddZd/d d!Z	d0d"d#Z
d1d$d%Zed2d&d'Zed3d(d)ZdS )4PrioritizedSettinga  Return a value for a global setting according to configuration precedence.

    The following methods are searched in order for the setting:

    4. immediate values
    3. previously user-set value
    2. environment variable
    1. system setting
    0. implicit default

    If a value cannot be determined, a RuntimeError is raised.

    The ``env_var`` argument specifies the name of an environment to check for
    setting values, e.g. ``"AZURE_LOG_LEVEL"``.
    If a ``convert`` function is provided, the result will be converted before being used.

    The optional ``system_hook`` can be used to specify a function that will
    attempt to look up a value for the setting from system-wide configurations.
    If a ``convert`` function is provided, the hook result will be converted before being used.

    The optional ``default`` argument specified an implicit default value for
    the setting that is returned if no other methods provide a value. If a ``convert`` function is provided,
    ``default`` will be converted before being used.

    A ``convert`` argument may be provided to convert values before they are
    returned. For instance to concert log levels in environment variables
    to ``logging`` module values. If a ``convert`` function is provided, it must support
    str as valid input type.

    :param str name: the name of the setting
    :param str env_var: the name of an environment variable to check for the setting
    :param callable system_hook: a function that will attempt to look up a value for the setting
    :param default: an implicit default value for the setting
    :type default: any
    :param callable convert: a function to convert values before they are returned
    NrF   rJ   env_varOptional[str]system_hook&Optional[Callable[[], ValidInputType]]defaultUnion[ValidInputType, _Unset]convert;Optional[Callable[[Union[ValidInputType, str]], ValueType]]c                 C  s8   || _ || _|| _|| _dd }|r|n|| _t| _d S )Nc                 S     | S rY   r   )xr   r   r   r[   (  r\   z-PrioritizedSetting.__init__.<locals>.<lambda>)_name_env_var_system_hook_default_convertrZ   _user_value)selfrF   r_   ra   rc   re   Znoop_convertr   r   r   __init__  s   	
zPrioritizedSetting.__init__r"   c                 C  s
   d| j  S )NzPrioritizedSetting(%r))ri   ro   r   r   r   __repr__,     
zPrioritizedSetting.__repr__r    Optional[ValidInputType]r   c                 C  s   |dur	|  |S t| jts|  | jS | jr'| jtjv r'|  tj| j S | jr1|  |  S t| jts=|  | jS t	d| j
 )a   Return the setting value according to the standard precedence.

        :param value: value
        :type value: str or int or float or None
        :returns: the value of the setting
        :rtype: str or int or float
        :raises RuntimeError: if no value can be determined
        Nz(No configured value found for setting %r)rm   r.   rn   r   rj   osenvironrk   rl   RuntimeErrorri   ro   r    r   r   r   __call__/  s   
zPrioritizedSetting.__call__instancer   ownerOptional[Any]-PrioritizedSetting[ValidInputType, ValueType]c                 C  rg   rY   r   )ro   rz   r{   r   r   r   __get__O  s   zPrioritizedSetting.__get__r   Nonec                 C  s   |  | d S rY   )	set_value)ro   rz   r    r   r   r   __set__R  s   zPrioritizedSetting.__set__c                 C  s
   || _ dS )a  Specify a value for this setting programmatically.

        A value set this way takes precedence over all other methods except
        immediate values.

        :param value: a user-set value for this setting
        :type value: str or int or float
        N)rn   rx   r   r   r   r   U  s   
	zPrioritizedSetting.set_valuec                 C  s
   t | _dS )z>Unset the previous user value such that the priority is reset.N)rZ   rn   rq   r   r   r   unset_value`     
zPrioritizedSetting.unset_valuec                 C     | j S rY   )rj   rq   r   r   r   r_   d     zPrioritizedSetting.env_varc                 C  r   rY   )rl   rq   r   r   r   rc   h  r   zPrioritizedSetting.default)
rF   rJ   r_   r`   ra   rb   rc   rd   re   rf   )r"   rJ   rY   )r    rt   r"   r   )rz   r   r{   r|   r"   r}   )rz   r   r    r   r"   r   )r    r   r"   r   r"   r   )r"   r`   )r"   rd   )r   r   r   __doc__rZ   rp   rr   ry   r~   r   r   r   propertyr_   rc   r   r   r   r   r^      s"    (
 


r^   c                   @  s   e Zd ZU dZd&ddZed'ddZejd(d
dZed)ddZed)ddZ	d*ddZ
d+ddZeddeejdZded< eddeddZded< ed d!eddZd"ed < ed#d$eejdZd%ed#< dS ),r   a]
  Settings for globally used Azure configuration values.

    You probably don't want to create an instance of this class, but call the singleton instance:

    .. code-block:: python

        from azure.core.settings import settings
        settings.log_level = log_level = logging.DEBUG

    The following methods are searched in order for a setting:

    4. immediate values
    3. previously user-set value
    2. environment variable
    1. system setting
    0. implicit default

    An implicit default is (optionally) defined by the setting attribute itself.

    A system setting value can be obtained from registries or other OS configuration
    for settings that support that method.

    An environment variable value is obtained from ``os.environ``

    User-set values many be specified by assigning to the attribute:

    .. code-block:: python

        settings.log_level = log_level = logging.DEBUG

    Immediate values are (optionally) provided when the setting is retrieved:

    .. code-block:: python

        settings.log_level(logging.DEBUG())

    Immediate values are most often useful to provide from optional arguments
    to client functions. If the argument value is not None, it will be returned
    as-is. Otherwise, the setting searches other methods according to the
    precedence rules.

    Immutable configuration snapshots can be created with the following methods:

    * settings.defaults returns the base defaultsvalues , ignoring any environment or system
      or user settings

    * settings.current returns the current computation of settings including prioritization
      of configuration sources, unless defaults_only is set to True (in which case the result
      is identical to settings.defaults)

    * settings.config can be called with specific values to override what settings.current
      would provide

    .. code-block:: python

        # return current settings with log level overridden
        settings.config(log_level=logging.DEBUG)

    :cvar log_level: a log level to use across all Azure client SDKs (AZURE_LOG_LEVEL)
    :type log_level: PrioritizedSetting
    :cvar tracing_enabled: Whether tracing should be enabled across Azure SDKs (AZURE_TRACING_ENABLED)
    :type tracing_enabled: PrioritizedSetting
    :cvar tracing_implementation: The tracing implementation to use (AZURE_SDK_TRACING_IMPLEMENTATION)
    :type tracing_implementation: PrioritizedSetting

    :Example:

    >>> import logging
    >>> from azure.core.settings import settings
    >>> settings.log_level = logging.DEBUG
    >>> settings.log_level()
    10

    >>> settings.log_level(logging.WARN)
    30

    r"   r   c                 C  s
   d| _ d S )NFZ_defaults_onlyrq   r   r   r   rp     rs   zSettings.__init__r#   c                 C  r   )zWhether to ignore environment and system settings and return only base default values.

        :rtype: bool
        :returns: Whether to ignore environment and system settings and return only base default values.
        r   rq   r   r   r   defaults_only  s   zSettings.defaults_onlyr    c                 C  s
   || _ d S rY   r   rx   r   r   r   r     r   Tuple[Any, ...]c                 C  s    dd | j j D }| |S )zReturn implicit default values for all settings, ignoring environment and system.

        :rtype: namedtuple
        :returns: The implicit default values for all settings
        c                 S  s"   i | ]\}}t |tr||jqS r   )r.   r^   rc   rG   kvr   r   r   rI        " z%Settings.defaults.<locals>.<dictcomp>)	__class____dict__items_config)ro   propsr   r   r   defaults  s   
zSettings.defaultsc                 C  s   | j r| jS |  S )zReturn the current values for all settings.

        :rtype: namedtuple
        :returns: The current values for all settings
        )r   r   configrq   r   r   r   current  s   zSettings.currentkwargsr   c                 K  s*   dd | j j D }|| | |S )a  Return the currently computed settings, with values overridden by parameter values.

        :rtype: namedtuple
        :returns: The current values for all settings, with values overridden by parameter values

        Examples:

        .. code-block:: python

           # return current settings with log level overridden
           settings.config(log_level=logging.DEBUG)

        c                 S  s"   i | ]\}}t |tr|| qS r   )r.   r^   r   r   r   r   rI     r   z#Settings.config.<locals>.<dictcomp>)r   r   r   updater   )ro   r   r   r   r   r   r     s   

zSettings.configr   Mapping[str, Any]c                 C  s$   t | }td|}|di |S )NConfigr   )listrK   r   )ro   r   rK   r   r   r   r   r     s   
zSettings._config	log_levelZAZURE_LOG_LEVEL)r_   re   rc   z(PrioritizedSetting[Union[str, int], int]tracing_enabledZAZURE_TRACING_ENABLEDNz4PrioritizedSetting[Optional[Union[str, bool]], bool]r5   Z AZURE_SDK_TRACING_IMPLEMENTATIONzZPrioritizedSetting[Optional[Union[str, Type[AbstractSpan]]], Optional[Type[AbstractSpan]]]azure_cloudZAZURE_CLOUDz8PrioritizedSetting[Union[str, AzureClouds], AzureClouds]r   )r"   r#   )r    r#   r"   r   )r"   r   )r   r   r"   r   )r   r   r"   r   )r   r   r   r   rp   r   r   setterr   r   r   r   r^   rD   loggingr:   r   __annotations__r6   r   r]   r5   rL   r   ZAZURE_PUBLIC_CLOUDr   r   r   r   r   r   m  sL   
 
N	


	
	r   r   )r    r!   r"   r#   )r    r4   r"   r#   )r    r<   r"   r=   )r    rE   r"   r   )r"   rM   )r    rX   r"   rM   )4r   
__future__r   collectionsr   enumr   	functoolsr   r   ru   typingr   r   r   r	   r
   r   r   r   r   r   r   r   Z_azure_cloudsr   Zazure.core.tracingr   r   r   __all__r   r   rZ   r3   r6   r7   r8   r9   r:   r;   r@   rD   rL   rR   rV   rW   r   r]   r^   r   r   r   r   r   r   <module>   sL   8


	


!x ,