
    ]j[N                    *   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rdd
lmZ  ed      Z ed      Z e	j<                  e      Z dZ! G d de      Z"e"jF                  Z$d dZ%d!dZ&e	jN                  e	jP                  e	jR                  e	jT                  e	jV                  dZ,d"dZ-d#dZ.d$dZ/d$dZ0e/e0dZ1de2d<   ed%d       Z3 G d deeef         Z4 G d d      Z5 e5       Z6de2d<   y)&zIProvide 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                      e Zd ZdZy)_Unsetr   N)__name__
__module____qualname__token     =/root/env/lib/python3.12/site-packages/azure/core/settings.pyr   r   ?   s    Er!   r   c                    t        | t              r| S | j                         }|dv ry|dv ryt        dj	                  |             )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)
isinstanceboollower
ValueErrorformat)valuevals     r"   convert_boolr5   F   sM      %
++-C
00
22
9@@G
HHr!   c                    | t         j                         ryy	 t        |       S # t        $ r/ t        j                  d|        t         j                         rY yY yw xY w)a  Convert tracing value to bool with regard to tracing implementation.

    If the value cannot be converted to a bool, a warning is logged and the
    default behavior is used (auto-detect based on whether a tracing
    implementation is configured).

    :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
    TFzInvalid value %r for AZURE_TRACING_ENABLED; falling back to default behavior. Valid values are: 'true'/'false', 'yes'/'no', '1'/'0', 'on'/'off' (case-insensitive).)r   tracing_implementationr5   r1   _LOGGERwarning)r3   s    r"   convert_tracing_enabledr:   `   se     } **,
E"" d	

 **,s   
% 3AA)CRITICALERRORWARNINGINFODEBUGc                    t        | t              r| S | j                         }|dk(  rd}t        j	                  |      }|:t
        j                  d| dj                  t                     t        j                  S |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"
    * "verbose" (treated as "debug")

    If the value cannot be converted, a warning is logged and ``logging.INFO``
    is returned.

    :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

    VERBOSEr?   zUInvalid log level %r for AZURE_LOG_LEVEL; falling back to INFO. Valid values are: %s., )
r.   intupper_levelsgetr8   r9   joinloggingr>   )r3   r4   levels      r"   convert_loggingrJ      so    , % 
++-C
iKKE}cIIg	

 ||Lr!   c           	     B   t        | t              r| S t        | t              r_t        D ci c]  }|j                  | }}| |v r||    S t	        dj                  | dj                  |j                                           t	        dj                  |             c c}w )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

    z6Cannot convert {} to Azure Cloud, valid values are: {}rB   z Cannot convert {} to Azure Cloud)r.   r   strnamer1   r2   rG   keys)r3   cloudazure_cloudss      r"   convert_azure_cloudrQ      s     %%%7BCe

E)CCL &&DKKESWS\S\]i]n]n]pSqr
 	
 7>>uE
FF Ds   Bc                 2    	 ddl m}  | S # t        $ r Y yw xY 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)&azure.core.tracing.ext.opencensus_spanrT   ImportErrorrS   s    r"   _get_opencensus_spanrW      s&    	
     
 	c                 2    	 ddl m}  | S # t        $ r Y yw xY w)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))azure.core.tracing.ext.opentelemetry_spanr[   rV   rZ   s    r"   _get_opentelemetry_spanr]      s&    	
 !  rX   )
opencensusopentelemetryz5Dict[str, Callable[[], Optional[Type[AbstractSpan]]]]_tracing_implementation_dictc                    | yt        | t              s| S | j                         } t        j	                  | d       } |       }|t
        u r+t        j                  d| dj                  t                     y|S )a  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"

    If the value cannot be converted, a warning is logged and ``None`` is returned.

    :param value: the value to convert
    :type value: string
    :returns: AbstractSpan

    Nc                     t         S N)_unsetr    r!   r"   <lambda>z&convert_tracing_impl.<locals>.<lambda>  s     r!   z}Invalid tracing implementation %r for AZURE_SDK_TRACING_IMPLEMENTATION; falling back to default (None). Valid values are: %s.rB   )	r.   rL   r0   r`   rF   rd   r8   r9   rG   )r3   get_wrapper_classwrapper_classs      r"   convert_tracing_implrh      su    " }eS!KKME488OARATM$II23		
 r!   c                      e Zd ZdZddedf	 	 	 	 	 	 	 	 	 ddZddZdddZdddZddZ	ddZ
dd	Zedd
       Zedd       Zy)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
    Nc                n    || _         || _        || _        || _        d }|r|n|| _        t
        | _        y )Nc                    | S rc   r    )xs    r"   re   z-PrioritizedSetting.__init__.<locals>.<lambda>D  s    q r!   )_name_env_var_system_hook_default_convertrd   _user_value)selfrM   env_varsystem_hookdefaultconvertnoop_converts          r"   __init__zPrioritizedSetting.__init__7  s:     
'-8V]7co:@r!   c                     d| j                   z  S )NzPrioritizedSetting(%r))rn   rt   s    r"   __repr__zPrioritizedSetting.__repr__H  s    '$**44r!   c                *   || j                  |      S t        | j                  t              s| j                  | j                        S | j                  rH| j                  t
        j                  v r,| j                  t
        j                  | j                           S | j                  r| j                  | j                               S t        | j                  t              s| j                  | j                        S t        d| j                  z        )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
        z(No configured value found for setting %r)rr   r.   rs   r   ro   osenvironrp   rq   RuntimeErrorrn   rt   r3   s     r"   __call__zPrioritizedSetting.__call__K  s     =='' $**F3==!1!122 ==T]]bjj8==DMM!:;; ==!2!2!455 $--0==//E

RSSr!   c                    | S rc   r    )rt   instanceowners      r"   __get__zPrioritizedSetting.__get__k  s    r!   c                &    | j                  |       y rc   )	set_value)rt   r   r3   s      r"   __set__zPrioritizedSetting.__set__n  s    ur!   c                    || _         y)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)rs   r   s     r"   r   zPrioritizedSetting.set_valueq  s     !r!   c                    t         | _        y)z>Unset the previous user value such that the priority is reset.N)rd   rs   r|   s    r"   unset_valuezPrioritizedSetting.unset_value|  s
    !r!   c                    | j                   S rc   )ro   r|   s    r"   ru   zPrioritizedSetting.env_var      }}r!   c                    | j                   S rc   )rq   r|   s    r"   rw   zPrioritizedSetting.default  r   r!   )
rM   rL   ru   Optional[str]rv   z&Optional[Callable[[], ValidInputType]]rw   Union[ValidInputType, _Unset]rx   z;Optional[Callable[[Union[ValidInputType, str]], ValueType]])returnrL   rc   )r3   zOptional[ValidInputType]r   r   )r   r   r   zOptional[Any]r   z-PrioritizedSetting[ValidInputType, ValueType])r   r   r3   r   r   None)r3   r   r   r   r   r   )r   r   )r   r   )r   r   r   __doc__rd   rz   r}   r   r   r   r   r   propertyru   rw   r    r!   r"   rj   rj     s    #P "&>B17OSAA A <	A
 /A MA"5T@	!"    r!   rj   c                  6   e Zd ZU dZddZedd       Zej                  dd       Zedd       Zedd       Z	ddZ
ddZ ed	d
eej                        Zded	<    edded      Zded<    edded      Zded<    eddeej0                        Zded<   y)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
    :cvar azure_cloud: The Azure cloud environment to use (AZURE_SDK_CLOUD_CONF)
    :type azure_cloud: PrioritizedSetting

    The following environment variables are used by the settings:

    * ``AZURE_LOG_LEVEL`` - Logging level. Accepted values: ``CRITICAL``, ``ERROR``, ``WARNING``,
      ``INFO``, ``DEBUG``, ``VERBOSE`` (case-insensitive). ``VERBOSE`` is treated as ``DEBUG``.
      If the value is invalid, a warning is logged and ``INFO`` is used. Default: ``INFO``.
    * ``AZURE_TRACING_ENABLED`` - Enable/disable tracing. Accepted values: ``true``/``false``,
      ``yes``/``no``, ``1``/``0``, ``on``/``off`` (case-insensitive). If the value is invalid,
      a warning is logged and the default is used. Default: auto-detected based on whether a
      tracing implementation is configured.
    * ``AZURE_SDK_TRACING_IMPLEMENTATION`` - Tracing implementation. Accepted values:
      ``opentelemetry``. If the value is invalid, a warning is logged and the default is used.
      Default: None.
    * ``AZURE_SDK_CLOUD_CONF`` - Azure cloud environment. Accepted values:
      ``AZURE_PUBLIC_CLOUD``, ``AZURE_CHINA_CLOUD``, ``AZURE_US_GOVERNMENT``.
      Default: ``AZURE_PUBLIC_CLOUD``.

    :Example:

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

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

    c                    d| _         y )NF_defaults_onlyr|   s    r"   rz   zSettings.__init__  s
    $)r!   c                    | j                   S )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   r|   s    r"   defaults_onlyzSettings.defaults_only  s     """r!   c                    || _         y rc   r   r   s     r"   r   zSettings.defaults_only  s
    #r!   c                    | j                   j                  j                         D ci c]#  \  }}t        |t              s||j
                  % }}}| j                  |      S c c}}w )zReturn implicit default values for all settings, ignoring environment and system.

        :rtype: namedtuple
        :returns: The implicit default values for all settings
        )	__class____dict__itemsr.   rj   rw   _config)rt   kvpropss       r"   defaultszSettings.defaults  sV     -1NN,C,C,I,I,Kq&1azZ[]oOpAIIqq||E"" rs   A$A$c                R    | j                   r| j                  S | j                         S )zReturn the current values for all settings.

        :rtype: namedtuple
        :returns: The current values for all settings
        )r   r   configr|   s    r"   currentzSettings.current  s#     == {{}r!   c                    | j                   j                  j                         D ci c]  \  }}t        |t              s| |         }}}|j                  |       | j                  |      S c c}}w )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)

        )r   r   r   r.   rj   updater   )rt   kwargsr   r   r   s        r"   r   zSettings.config  s`     '+nn&=&=&C&C&EkFQTUWiIjACkkV||E"" ls   A0
A0c                \    t        |j                               }t        d|      } |di |S )NConfigr    )listrN   r   )rt   r   rN   r   s       r"   r   zSettings._config!  s*    uzz|,Hd+r!   	log_levelAZURE_LOG_LEVEL)ru   rx   rw   z(PrioritizedSetting[Union[str, int], int]tracing_enabledAZURE_TRACING_ENABLEDNz4PrioritizedSetting[Optional[Union[str, bool]], bool]r7    AZURE_SDK_TRACING_IMPLEMENTATIONzZPrioritizedSetting[Optional[Union[str, Type[AbstractSpan]]], Optional[Type[AbstractSpan]]]azure_cloudAZURE_SDK_CLOUD_CONFz8PrioritizedSetting[Union[str, AzureClouds], AzureClouds]r   )r   r/   )r3   r/   r   r   )r   Tuple[Any, ...])r   r   r   r   )r   zMapping[str, Any]r   r   )r   r   r   r   rz   r   r   setterr   r   r   r   rj   rJ   rH   r>   r   __annotations__r:   r   rh   r7   rQ   r   AZURE_PUBLIC_CLOUDr   r    r!   r"   r   r     s   ^@* # # $ $ # #  #$ ;M!	;I7  M_''	MOI  	 2$		    M_&#..	MKI r!   r   r   )r3   zUnion[str, bool]r   r/   )r3   zOptional[Union[str, bool]]r   r/   )r3   zUnion[str, int]r   rC   )r3   zUnion[str, AzureClouds]r   r   )r   Optional[Type[AbstractSpan]])r3   z(Optional[Union[str, Type[AbstractSpan]]]r   r   )7r   
__future__r   collectionsr   enumr   	functoolsr   rH   r   typingr   r   r	   r
   r   r   r   r   r   r   r   r   _azure_cloudsr   azure.core.tracingr   r   r   	getLoggerr   r8   __all__r   r   rd   r5   r:   r;   r<   r=   r>   r?   rE   rJ   rQ   rW   r]   r`   r   rh   rj   r   r   r    r!   r"   <module>r      s7  4 " "    	    '/)*K 	
'

H
% #T  
I4@   ]]LL]]%PG, " ',W S  ! !Hu!:; upz zz Z( r!   