U
    EZhW                     @   s*  d Z ddlZddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZ ddlmZ ddlmZ eeZee d	 Zd
ZddiZdZdddddddZdd Zdd ZG dd dZG dd de	ZG dd dZzddlm Z  W n e!k
r   d d! Z Y nX G d"d# d#Z"d$d% Z#dS )&a  
NOTE: All classes and functions in this module are considered private and are
subject to abrupt breaking changes. Please do not use them directly.

To modify the User-Agent header sent by botocore, use one of these
configuration options:
* The ``AWS_SDK_UA_APP_ID`` environment variable.
* The ``sdk_ua_app_id`` setting in the shared AWS config file.
* The ``user_agent_appid`` field in the :py:class:`botocore.config.Config`.
* The ``user_agent_extra`` field in the :py:class:`botocore.config.Config`.

    N)copy)ascii_lettersdigits)
NamedTupleOptional)__version__)HAS_CRT)get_contextz!$%&'*+-.^_`|~,)windowslinuxmacosZandroidiosZwatchosZtvosotherdarwinr   ZBotocoreBCGMNS)ZWAITERZ	PAGINATORZS3_TRANSFERZPROTOCOL_RPC_V2_CBORZENDPOINT_OVERRIDEZSIGV4A_SIGNINGc                 C   s0   t  }|dkrdS t|  }r,|j| dS )zAdds metric value to the current context object's ``features`` set.

    :type feature_id: str
    :param feature_id: The name of the feature to register. Value must be a key
        in the ``_USERAGENT_FEATURE_MAPPINGS`` dict.
    N)r	   _USERAGENT_FEATURE_MAPPINGSgetfeaturesadd)Z
feature_idctxval r   Y/home/aprabhat/apps/x.techxrdev.in/venv/lib/python3.8/site-packages/botocore/useragent.pyregister_feature_idC   s
    r   c                    s   d  fdd| D S )a~  Replaces all not allowed characters in the string with a dash ("-").

    Allowed characters are ASCII alphanumerics and ``!$%&'*+-.^_`|~,``. If
    ``allow_hash`` is ``True``, "#"``" is also allowed.

    :type raw_str: str
    :param raw_str: The input string to be sanitized.

    :type allow_hash: bool
    :param allow_hash: Whether "#" is considered an allowed character.
     c                 3   s*   | ]"}|t ks r|d kr|ndV  qdS )#-N)_USERAGENT_ALLOWED_CHARACTERS).0c
allow_hashr   r   	<genexpr>b   s   z7sanitize_user_agent_string_component.<locals>.<genexpr>)join)Zraw_strr&   r   r%   r   $sanitize_user_agent_string_componentV   s    r)   c                   @   s(   e Zd ZdZeedddZdd ZdS )UserAgentComponentSizeConfigz
    Configures the max size of a built user agent string component and the
    delimiter used to truncate the string if the size is above the max.
    )max_size_in_bytes	delimiterc                 C   s   || _ || _|   d S N)r+   r,   _validate_input)selfr+   r,   r   r   r   __init__p   s    z%UserAgentComponentSizeConfig.__init__c                 C   s    | j dk rtd| j  dd S )N   zInvalid `max_size_in_bytes`: z#. Value must be a positive integer.)r+   
ValueErrorr/   r   r   r   r.   u   s    
z,UserAgentComponentSizeConfig._validate_inputN)__name__
__module____qualname____doc__intstrr0   r.   r   r   r   r   r*   j   s   r*   c                   @   sR   e Zd ZU dZeed< eed< dZee ed< dZee	 ed< dd Z
d	d
 ZdS )UserAgentComponenta  
    Component of a Botocore User-Agent header string in the standard format.

    Each component consists of a prefix, a name, a value, and a size_config.
    In the string representation these are combined in the format
    ``prefix/name#value``.

    ``size_config`` configures the max size and truncation strategy for the
    built user agent string component.

    This class is considered private and is subject to abrupt breaking changes.
    prefixnameNvaluesize_configc                 C   s   t | jdd}t | jdd}| jdks0| jdkr@| d| }n"t | jdd}| d| d| }| jdk	r| || jj| jj}|S )zACreate string like 'prefix/name#value' from a UserAgentComponent.Tr%   FNr   /r    )r)   r;   r<   r=   r>   _truncate_stringr+   r,   )r/   Zclean_prefixZ
clean_nameZclean_stringZclean_valuer   r   r   	to_string   s,       
zUserAgentComponent.to_stringc              	   C   s^   |}t |d|kr4||}|  ||}q|dkrZtd| d| d| d |S )zx
        Pop ``delimiter``-separated values until encoded string is less than or
        equal to ``max_size``.
        zutf-8r   zUser agent component `z` could not be truncated to `z` bytes with delimiter `zL` without losing all contents. Value will be omitted from user agent string.)lenencodesplitpopr(   loggerdebug)r/   stringmax_sizer,   origpartsr   r   r   r@      s    
z#UserAgentComponent._truncate_string)r4   r5   r6   r7   r9   __annotations__r=   r   r>   r*   rA   r@   r   r   r   r   r:   }   s   
r:   c                   @   s    e Zd ZdZdd Zdd ZdS )RawStringUserAgentComponentz
    UserAgentComponent interface wrapper around ``str``.

    Use for User-Agent header components that are not constructed from
    prefix+name+value but instead are provided as strings. No sanitization is
    performed.
    c                 C   s
   || _ d S r-   _value)r/   r=   r   r   r   r0      s    z$RawStringUserAgentComponent.__init__c                 C   s   | j S r-   rN   r3   r   r   r   rA      s    z%RawStringUserAgentComponent.to_stringN)r4   r5   r6   r7   r0   rA   r   r   r   r   rM      s   rM   )modify_componentsc                 C   s   | S r-   r   )
componentsr   r   r   rP      s    rP   c                   @   s   e Zd ZdZd%ddZedd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zd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 )&UserAgentStringaU  
    Generator for AWS SDK User-Agent header strings.

    The User-Agent header format contains information from session, client, and
    request context. ``UserAgentString`` provides methods for collecting the
    information and ``to_string`` for assembling it into the standardized
    string format.

    Example usage:

        ua_session = UserAgentString.from_environment()
        ua_session.set_session_config(...)
        ua_client = ua_session.with_client_config(Config(...))
        ua_string = ua_request.to_string()

    For testing or when information from all sources is available at the same
    time, the methods can be chained:

        ua_string = (
            UserAgentString
            .from_environment()
            .set_session_config(...)
            .with_client_config(Config(...))
            .to_string()
        )

    Nc                 C   sL   || _ || _|| _|| _|| _|| _|| _d| _d| _d| _	d| _
d| _dS )a  
        :type platform_name: str
        :param platform_name: Name of the operating system or equivalent
            platform name. Should be sourced from :py:meth:`platform.system`.
        :type platform_version: str
        :param platform_version: Version of the operating system or equivalent
            platform name. Should be sourced from :py:meth:`platform.version`.
        :type platform_machine: str
        :param platform_version: Processor architecture or machine type. For
        example "x86_64". Should be sourced from :py:meth:`platform.machine`.
        :type python_version: str
        :param python_version: Version of the python implementation as str.
            Should be sourced from :py:meth:`platform.python_version`.
        :type python_implementation: str
        :param python_implementation: Name of the python implementation.
            Should be sourced from :py:meth:`platform.python_implementation`.
        :type execution_env: str
        :param execution_env: The value of the AWS execution environment.
            Should be sourced from the ``AWS_EXECUTION_ENV` environment
            variable.
        :type crt_version: str
        :param crt_version: Version string of awscrt package, if installed.
        N)_platform_name_platform_version_platform_machine_python_version_python_implementation_execution_env_crt_version_session_user_agent_name_session_user_agent_version_session_user_agent_extra_client_config_client_features)r/   platform_nameplatform_versionplatform_machinepython_versionpython_implementationexecution_envcrt_versionr   r   r   r0      s    !zUserAgentString.__init__c              	   C   sD   d }t rt pd}| t t t t t tj	
d|dS )NUnknownZAWS_EXECUTION_ENV)r_   r`   ra   rb   rc   rd   re   )r   _get_crt_versionplatformsystemreleasemachinerb   rc   osenvironr   )clsre   r   r   r   from_environment&  s    

z UserAgentString.from_environmentc                 C   s   || _ || _|| _| S )ax  
        Set the user agent configuration values that apply at session level.

        :param user_agent_name: The user agent name configured in the
            :py:class:`botocore.session.Session` object. For backwards
            compatibility, this will always be at the beginning of the
            User-Agent string, together with ``user_agent_version``.
        :param user_agent_version: The user agent version configured in the
            :py:class:`botocore.session.Session` object.
        :param user_agent_extra: The user agent "extra" configured in the
            :py:class:`botocore.session.Session` object.
        )rZ   r[   r\   )r/   Zsession_user_agent_nameZsession_user_agent_versionZsession_user_agent_extrar   r   r   set_session_config5  s    z"UserAgentString.set_session_configc                 C   s
   || _ dS )z
        Persist client-specific features registered before or during client
        creation.

        :type features: Set[str]
        :param features: A set of client-specific features.
        N)r^   )r/   r   r   r   r   set_client_featuresL  s    z#UserAgentString.set_client_featuresc                 C   s   t | }||_|S )z
        Create a copy with all original values and client-specific values.

        :type client_config: botocore.config.Config
        :param client_config: The client configuration object.
        )r   r]   )r/   Zclient_configcpr   r   r   with_client_configV  s    z"UserAgentString.with_client_configc                 C   s   d}| j r(t| j dr | j j}n| j j}|dk	r:| |S |  tdf|  |  | 	 | 
 |  |  |  |  
}t|}ddd |D S )zN
        Build User-Agent header string from the object's properties.
        N_supplied_user_agentzua/2.1 c                 S   s   g | ]}|  r|  qS r   )rA   )r#   compr   r   r   
<listcomp>  s      z-UserAgentString.to_string.<locals>.<listcomp>)r]   hasattrrt   
user_agent_build_legacy_ua_string_build_sdk_metadatarM   _build_os_metadata_build_architecture_metadata_build_language_metadata_build_execution_env_metadata_build_feature_metadata_build_config_metadata_build_app_id_build_extrarP   r(   r/   Zconfig_ua_overriderQ   r   r   r   rA   a  s.    

zUserAgentString.to_stringc                 C   sx   g }| j rF| jrF| j tks$| jtkrF|t| j | jtdttg n|ttt | jdk	rt|tdd| j |S )at  
        Build the SDK name and version component of the User-Agent header.

        For backwards-compatibility both session-level and client-level config
        of custom tool names are honored. If this removes the Botocore
        information from the start of the string, Botocore's name and version
        are included as a separate field with "md" prefix.
        mdNawscrt)rZ   r[   _USERAGENT_SDK_NAMEbotocore_versionextendr:   appendrY   )r/   Zsdk_mdr   r   r   r{     s:    	  
z#UserAgentString._build_sdk_metadatac                 C   sz   | j dkrtddgS | j  }|tkr.|}n|tkr@t| }nd}|dk	r\td|| jgS tddtd| j | jgS dS )a"  
        Build the OS/platform components of the User-Agent header string.

        For recognized platform names that match or map to an entry in the list
        of standardized OS names, a single component with prefix "os" is
        returned. Otherwise, one component "os/other" is returned and a second
        with prefix "md" and the raw platform name.

        String representations of example return values:
         * ``os/macos#10.13.6``
         * ``os/linux``
         * ``os/other``
         * ``os/other md/foobar#1.2.3``
        Nrl   r   r   )rS   r:   lower_USERAGENT_ALLOWED_OS_NAMES!_USERAGENT_PLATFORM_NAME_MAPPINGSrT   )r/   Zplt_name_lowerZ	os_familyr   r   r   r|     s$    


  z"UserAgentString._build_os_metadatac                 C   s   | j rtdd| j  gS g S )z
        Build architecture component of the User-Agent header string.

        Returns the machine type with prefix "md" and name "arch", if one is
        available. Common values include "x86_64", "arm64", "i386".
        r   arch)rU   r:   r   r3   r   r   r   r}     s      z,UserAgentString._build_architecture_metadatac                 C   s.   t dd| jg}| jr*|t dd| j |S )a  
        Build the language components of the User-Agent header string.

        Returns the Python version in a component with prefix "lang" and name
        "python". The Python implementation (e.g. CPython, PyPy) is returned as
        separate metadata component with prefix "md" and name "pyimpl".

        String representation of an example return value:
        ``lang/python#3.10.4 md/pyimpl#CPython``
        langpythonr   Zpyimpl)r:   rV   rW   r   )r/   Zlang_mdr   r   r   r~     s    z(UserAgentString._build_language_metadatac                 C   s   | j rtd| j gS g S dS )z
        Build the execution environment component of the User-Agent header.

        Returns a single component prefixed with "exec-env", usually sourced
        from the environment variable AWS_EXECUTION_ENV.
        zexec-envN)rX   r:   r3   r   r   r   r     s    z-UserAgentString._build_execution_env_metadatac                 C   sX   t  }|dkrt n|j}| jp$t }||}|s8g S tdd}tdd||dgS )z
        Build the features component of the User-Agent header string.

        Returns a single component with prefix "m" followed by a list of
        comma-separated metric values.
        Ni   ,m)r>   )r	   setr   r^   unionr*   r:   r(   )r/   r   Zcontext_featuresZclient_featuresr   r>   r   r   r   r      s    

  z'UserAgentString._build_feature_metadatac                 C   sJ   | j r| j jsg S | j jd}tdd|g}| j jrF|tdd |S )a  
        Build the configuration components of the User-Agent header string.

        Returns a list of components with prefix "cfg" followed by the config
        setting name and its value. Tracked configuration settings may be
        added or removed in future versions.
        modecfgz
retry-modezendpoint-discovery)r]   retriesr   r:   Zendpoint_discovery_enabledr   )r/   Z
retry_modeZcfg_mdr   r   r   r     s    z&UserAgentString._build_config_metadatac                 C   s&   | j r| j jrtd| j jgS g S dS )a  
        Build app component of the User-Agent header string.

        Returns a single component with prefix "app" and value sourced from the
        ``user_agent_appid`` field in :py:class:`botocore.config.Config` or
        the ``sdk_ua_app_id`` setting in the shared configuration file, or the
        ``AWS_SDK_UA_APP_ID`` environment variable. These are the recommended
        ways for apps built with Botocore to insert their identifer into the
        User-Agent header.
        ZappN)r]   Zuser_agent_appidr:   r3   r   r   r   r   $  s    zUserAgentString._build_app_idc                 C   s>   g }| j r|t| j  | jr:| jjr:|t| jj |S )a  User agent string components based on legacy "extra" settings.

        Creates components from the session-level and client-level
        ``user_agent_extra`` setting, if present. Both are passed through
        verbatim and should be appended at the end of the string.

        Preferred ways to inject application-specific information into
        botocore's User-Agent header string are the ``user_agent_appid` field
        in :py:class:`botocore.config.Config`. The ``AWS_SDK_UA_APP_ID``
        environment variable and the ``sdk_ua_app_id`` configuration file
        setting are alternative ways to set the ``user_agent_appid`` config.
        )r\   r   rM   r]   user_agent_extra)r/   extrar   r   r   r   6  s    zUserAgentString._build_extrac                 C   s8   |g}| j r|| j  | jjr.|| jj d|S )Nru   )r\   r   r]   r   r(   r   r   r   r   rz   P  s    z'UserAgentString._build_legacy_ua_stringc                 K   s&   |   }|jdr"|jd| d S )Nz
User-Agent)rA   headersr   replace_header)r/   Zoperation_namerequestkwargsZ	ua_stringr   r   r   &rebuild_and_replace_user_agent_handlerX  s    z6UserAgentString.rebuild_and_replace_user_agent_handler)N)r4   r5   r6   r7   r0   classmethodro   rp   rq   rs   rA   r{   r|   r}   r~   r   r   r   r   r   rz   r   r   r   r   r   rR      s(   $ 
3

!)&rR   c                  C   s,   zddl } | jW S  tk
r&   Y dS X dS )z\
    This function is considered private and is subject to abrupt breaking
    changes.
    r   N)r   r   AttributeError)r   r   r   r   rg   `  s
    rg   )$r7   loggingrl   rh   r   rH   r   r   typingr   r   Zbotocorer   r   Zbotocore.compatr   Zbotocore.contextr	   	getLoggerr4   rF   r"   r   r   r   r   r   r)   r*   r:   rM   Z!botocore.customizations.useragentrP   ImportErrorrR   rg   r   r   r   r   <module>   sF   


?   