
    6i,F                       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dZ G d de      Zej@                  Z!d dZ"d!dZ#e	jH                  e	jJ                  e	jL                  e	jN                  e	jP                  dZ)d"dZ*d#dZ+d$dZ,d$dZ-e,e-dZ.de/d<   ed%d       Z0 G d deeef         Z1 G d d      Z2 e2       Z3de/d<   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     V/home/azureuser/techstart-app/venv/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   D   sM      %
++-C
00
22
9@@G
HHr!   c                H    | t         j                         ryy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
    TF)r   tracing_implementationr5   )r3   s    r"   convert_tracing_enabledr8   ^   s(     } **,r!   )CRITICALERRORWARNINGINFODEBUGc                    t        | t              r| S | j                         }t        j	                  |      }|s.t        dj                  | dj                  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.   intupper_levelsgetr1   r2   join)r3   r4   levels      r"   convert_loggingrF   y   s]    & % 
++-CKKEOVVW\^b^g^gho^pqrr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: {}r?   z Cannot convert {} to Azure Cloud)r.   r   strnamer1   r2   rD   keys)r3   cloudazure_cloudss      r"   convert_azure_cloudrM      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_spanrP   ImportErrorrO   s    r"   _get_opencensus_spanrS      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_spanrW   rR   rV   s    r"   _get_opentelemetry_spanrY      s&    	
 !  rT   )
opencensusopentelemetryz5Dict[str, Callable[[], Optional[Type[AbstractSpan]]]]_tracing_implementation_dictc                    | yt        | t              s| S | j                         } t        j	                  | d       } |       }|t
        u r.t        dj                  | dj                  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                     t         S N)_unsetr    r!   r"   <lambda>z&convert_tracing_impl.<locals>.<lambda>   s     r!   z7Cannot convert {} to AbstractSpan, valid values are: {}r?   )	r.   rH   r0   r\   rC   r`   r1   r2   rD   )r3   get_wrapper_classwrapper_classs      r"   convert_tracing_implrd      s{      }eS!KKME488OARATMELLtyy!=>
 	

 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 r_   r    )xs    r"   ra   z-PrioritizedSetting.__init__.<locals>.<lambda>(  s    q r!   )_name_env_var_system_hook_default_convertr`   _user_value)selfrI   env_varsystem_hookdefaultconvertnoop_converts          r"   __init__zPrioritizedSetting.__init__  s:     
'-8V]7co:@r!   c                     d| j                   z  S )NzPrioritizedSetting(%r))rj   rp   s    r"   __repr__zPrioritizedSetting.__repr__,  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)rn   r.   ro   r   rk   osenvironrl   rm   RuntimeErrorrj   rp   r3   s     r"   __call__zPrioritizedSetting.__call__/  s     =='' $**F3==!1!122 ==T]]bjj8==DMM!:;; ==!2!2!455 $--0==//E

RSSr!   c                    | S r_   r    )rp   instanceowners      r"   __get__zPrioritizedSetting.__get__O  s    r!   c                &    | j                  |       y r_   )	set_value)rp   r   r3   s      r"   __set__zPrioritizedSetting.__set__R  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)ro   r~   s     r"   r   zPrioritizedSetting.set_valueU  s     !r!   c                    t         | _        y)z>Unset the previous user value such that the priority is reset.N)r`   ro   rx   s    r"   unset_valuezPrioritizedSetting.unset_value`  s
    !r!   c                    | j                   S r_   )rk   rx   s    r"   rq   zPrioritizedSetting.env_vard      }}r!   c                    | j                   S r_   )rm   rx   s    r"   rs   zPrioritizedSetting.defaulth  r   r!   )
rI   rH   rq   Optional[str]rr   z&Optional[Callable[[], ValidInputType]]rs   Union[ValidInputType, _Unset]rt   z;Optional[Callable[[Union[ValidInputType, str]], ValueType]])returnrH   r_   )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__r`   rv   ry   r   r   r   r   r   propertyrq   rs   r    r!   r"   rf   rf      s    #P "&>B17OSAA A <	A
 /A MA"5T@	!"    r!   rf   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

    :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_onlyrx   s    r"   rv   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   rx   s    r"   defaults_onlyzSettings.defaults_only  s     """r!   c                    || _         y r_   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.   rf   rs   _config)rp   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   configrx   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.   rf   updater   )rp   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    )listrJ   r   )rp   r   rJ   r   s       r"   r   zSettings._config  s*    uzz|,Hd+r!   	log_levelAZURE_LOG_LEVEL)rq   rt   rs   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_CLOUDz8PrioritizedSetting[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   rv   r   r   setterr   r   r   r   rf   rF   loggingr<   r   __annotations__r8   r   rd   r7   rM   r   AZURE_PUBLIC_CLOUDr   r    r!   r"   r   r   m  s   L\* # # $ $ # #  #$ ;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   r@   )r3   zUnion[str, AzureClouds]r   r   )r   Optional[Type[AbstractSpan]])r3   z(Optional[Union[str, Type[AbstractSpan]]]r   r   )4r   
__future__r   collectionsr   enumr   	functoolsr   r   r{   typingr   r   r	   r
   r   r   r   r   r   r   r   r   _azure_cloudsr   azure.core.tracingr   r   r   __all__r   r   r`   r5   r8   r9   r:   r;   r<   r=   rB   rF   rM   rS   rY   r\   r   rd   rf   r   r   r    r!   r"   <module>r      s#  4 " "    	    '/)*K 	 #T  
I4&   ]]LL]]<G, " ',W S   Bu!:; uph hV Z( r!   