Current File : //usr/lib/python3/dist-packages/lazr/restfulclient/__pycache__/_browser.cpython-312.pyc |
�
}�;e$L � �h � d Z eZg d�ZddlZddlZddlZddlZddlZddl Z ddl
Z
ddlmZ ddl
mZ ddlmZ ddlmZ ddlmZmZ dd lmZ ddlmZ ddlmZ dd
lm Z ddl!m"Z"m#Z# ddl$m%Z% e&e'u re(Z)e*Z+ne'Z)e'Z+ ejX d� Z- ejX d� Z. ejX d� Z/d� Z0d� Z1ejd jg d� rdZ4nddlm5Z4 G d� de� Z6 G d� de7� Z8 G d� de8� Z9 G d� d� Z:y# e$ r dd
lmZ ej2 ZY ��w xY w# e$ r ddlmZ Y ��w xY w)z�Browser object to make requests of lazr.restful web services.
The `Browser` class does some massage of HTTP requests and responses,
and handles custom caches. It is not part of the public
lazr.restfulclient API. (But maybe it should be?)
)�Browser�RestfulHttp�#ssl_certificate_validation_disabled� N)�md5)�BytesIO)�dumps)�sleep)�Http�urlnorm)�proxy_info_from_environment)� ProxyInfo)� urlencode)�Application)�DatetimeJSONEncoder)� HTTPError� error_for)�URIs ^\w+://z^\w+://s [?/:|]+c � � t | t � r| j d� }n| }t j |� rDt | t � r#| j d� } | j d� } n| j d� } t | t � r| j d� } t | � j � }t j d| � } t j d| � } t j }|dz
dz
}t | � |kD r| d| } dj! | j d� |f� S # t $ r Y ��w xY w) z�Return a filename suitable for the cache.
Strips dangerous and common characters to create a filename we
can use to store the cache in.
�utf-8�idna� � ,� � N�,)�
isinstance�bytes�decode�re_url_scheme_s�match�encode�UnicodeError�unicode_typer � hexdigest�
re_url_scheme�sub�re_slashr �maximum_cache_filename_length�len�join)�filename�filename_match�filemd5�maximum_filename_length�maximum_length_before_md5_sums �=/usr/lib/python3/dist-packages/lazr/restfulclient/_browser.py�safenamer1 W s � �
��h��&�%�_�_�W�5�N�%�N�� � ��0��(�E�*�#�?�?�7�3��#�?�?�6�2��#�?�?�6�2�� �(�L�)��?�?�7�+���(�m�%�%�'�G�� � ��h�/�H��|�|�D�(�+�H� *�G�G��$;�b�$@�1�$D�!�
�8�}�4�4��:�:�;���8�8�X�_�_�W�-�w�7�8�8��5 �
��
�s �A=D2 �2 D>�=D>c �T � t t j j dd� � S )z�Whether the user has disabled SSL certificate connection.
Some testing servers have broken certificates. Rather than raising an
error, we allow an environment variable,
``LP_DISABLE_SSL_CERTIFICATE_VALIDATION`` to disable the check.
�%LP_DISABLE_SSL_CERTIFICATE_VALIDATIONF)�bool�os�environ�get� r r0 r r � s � � ��
�
���F��N�O�Or z"/etc/ssl/certs/ca-certificates.crt)�CA_CERTSc �@ � � e Zd ZdZdZdddef� fd� Z� fd�Zd� Z� xZ S )r z�An Http subclass with some custom behavior.
This Http client uses the TE header instead of the Accept-Encoding
header to ask for compressed representations. It also knows how to
react when its cache is a MultipleRepresentationCache.
� Nc � �� t � }t t | � ||||t �� || _ | j
�| j
j
| � y y )N)�"disable_ssl_certificate_validation�ca_certs)r �superr �__init__�SYSTEM_CA_CERTS�
authorizer�authorizeSession)�selfrB �cache�timeout�
proxy_info�
cert_disabled� __class__s �r0 r@ zRestfulHttp.__init__� sZ �� � <�=�
�
�k�4�)����/<�$� *�
� %����?�?�&��O�O�,�,�T�2� 'r c
� �� d|v r|d= | j �| j j ||||� t t | � ||||||||| � S )z4Use the authorizer to authorize an outgoing request.�
authorization)rB �authorizeRequestr? r �_request)rD �conn�host�absolute_uri�request_uri�method�body�headers�redirections�cachekeyrI s �r0 rM zRestfulHttp._request� sm �� � �g�%� ��(��?�?�&��O�O�,�,��f�d�G�
� �[�$�0����������
�
r c �p � t | j t � r| j j ||� S y)�+Retrieve a cached value for an HTTP header.N)r rE �MultipleRepresentationCache�_getCachedHeader)rD �uri�headers r0 rZ zRestfulHttp._getCachedHeader� s+ � ��d�j�j�"=�>��:�:�.�.�s�F�;�;�r )
�__name__�
__module__�__qualname__�__doc__r( r r@ rM rZ �
__classcell__�rI s @r0 r r � s. �� �� %(�!� ���.�3�&#
�Jr r c �6 � e Zd ZdZdZefd�Zd� Zd� Zd� Z d� Z
y) �AtomicFileCachez�A FileCache that can be shared by multiple processes.
Based on a patch found at
<http://code.google.com/p/httplib2/issues/detail?id=125>.
z.tempc � � t j j |� | _ || _ t j
| j � y# t $ r(}|j t j k7 r� Y d}~yd}~ww xY w)aU Construct an ``AtomicFileCache``.
:param cache: The directory to use as a cache.
:param safe: A function that takes a key and returns a name that's
safe to use as a filename. The key must never return a string
that begins with ``TEMPFILE_PREFIX``. By default uses
``safename``.
N) r5 �path�normpath�
_cache_dir�_get_safe_name�makedirs�OSError�errno�EEXIST)rD rE �safe�es r0 r@ zAtomicFileCache.__init__� s_ � � �'�'�*�*�5�1���"��� ��K�K����(��� ��w�w�%�,�,�&�� '�� �s �A
�
A>�A9�9A>c �� � | j |� }|j | j � rt d| j z � �t j
j
| j |� S )z0Return the path on disk where ``key`` is stored.z Cache key cannot start with '%s')ri �
startswith�TEMPFILE_PREFIX�
ValueErrorr5 rf r* rh )rD �key�safe_keys r0 �
_get_key_pathzAtomicFileCache._get_key_path� s^ � ��&�&�s�+�����t�3�3�4� �2�T�5I�5I�I��
� �w�w�|�|�D�O�O�X�6�6r c � � | j |� } t |d� } |j � |j � S # |j � w xY w# t t
f$ r(}|j t j k7 r� Y d}~yd}~ww xY w)a� Get the value of ``key`` if set.
This behaves slightly differently to ``FileCache`` in that if
``set()`` fails to store a key, this ``get()`` will behave as if that
key were never set whereas ``FileCache`` returns the empty string.
:param key: The key to retrieve. Must be either bytes or unicode
text.
:return: The value of ``key`` if set, None otherwise.
�rbN)rv �open�read�close�IOErrorrk rl �ENOENT)rD rt �cache_full_path�fro s r0 r7 zAtomicFileCache.get sr � � �,�,�S�1�� ��_�d�+�A�
��v�v�x���� ����� ����!� ��w�w�%�,�,�&�� '�� �s- �A �A �A � A�A �B�$B�Bc � � t j | j | j �� \ }}t j
|d� }|j
|� |j � | j |� }t j dk( r4t j j |� rt j |� t j ||� y)z�Set ``key`` to ``value``.
:param key: The key to set. Must be either bytes or unicode text.
:param value: The value to set ``key`` to. Must be bytes.
)�prefix�dir�wb�win32N)�tempfile�mkstemprr rh r5 �fdopen�writer{ rv �sys�platformrf �exists�unlink�rename)rD rt �value�handle� path_namer r~ s r0 �setzAtomicFileCache.set s� � � %�,�,��'�'�T�_�_�
��� �
�I�I�f�d�#�� ����� ��� ��,�,�S�1���<�<�7�"�r�w�w�~�~�o�'F��I�I�o�&�
� � �)�_�-r c � � | j |� } t j |� y# t $ r(}|j t j
k7 r� Y d}~yd}~ww xY w)z�Delete ``key`` from the cache.
If ``key`` has not already been set then has no effect.
:param key: The key to delete. Must be either bytes or unicode text.
N)rv r5 �removerk rl r} )rD rt r~ ro s r0 �deletezAtomicFileCache.delete. sM � � �,�,�S�1�� ��I�I�o�&��� ��w�w�%�,�,�&�� '�� �s �) � A�A�AN)r] r^ r_ r` rr r1 r@ rv r7 r� r� r8 r r0 rd rd � s* � �� �O�#+� �"
7��,.�&r rd c �. � � e Zd ZdZ� fd�Zd� Zd� Z� xZS )rY a> A cache that can hold different representations of the same resource.
If a resource has two representations with two media types,
FileCache will only store the most recently fetched
representation. This cache can keep track of multiple
representations of the same resource.
This class works on the assumption that outside calling code sets
an instance's request_media_type attribute to the value of the
'Accept' header before initiating the request.
This class is very much not thread-safe, but FileCache isn't
thread-safe anyway.
c �P �� t t | � || j � d| _ y)z>Tell FileCache to call append_media_type when generating keys.N)r? rY r@ �append_media_type�request_media_type)rD rE rI s �r0 r@ z$MultipleRepresentationCache.__init__M s( �� �
�)�4�9��4�)�)�
� #'��r c �T � | j �|dz | j z }t |� S )z�Append the request media type to the cache key.
This ensures that representations of the same resource will be
cached separately, so long as they're served as different
media types.
�-)r� r1 )rD rt s r0 r� z-MultipleRepresentationCache.append_media_typeT s. � � �"�"�.���)�d�5�5�5�C���}�r c � � t |� \ }}}}| j |� }|dz }t |t � s|j d� }|�?t |� D ]1 } | j
|� s�| t |� d j � c S y)rX �:r N) r r7 r r r! r rq r) �strip)
rD r[ r\ �scheme� authorityrQ rV �cached_value�header_start�lines
r0 rZ z,MultipleRepresentationCache._getCachedHeader_ s� � �5<�S�\�2���K���x�x��)����|���,��.�'�.�.�w�7�L��#���-�
=���?�?�<�0���L� 1� 3�4�:�:�<�<�
=� r )r] r^ r_ r` r@ r� rZ ra rb s @r0 rY rY = s �� �
�'� �r rY c �t � e Zd ZdZ e� ZdZddddefd�Zd� Z d
d�Z dd�Z
d� Zd � Zdd
�Z
d� Zdd�Zy)r z6A class for making calls to lazr.restful web services.� Nc � � |�8t j � }t j t j
|� t
|t � rt |� }|j ||||� | _
|| _ || _ y)z�Initialize, possibly creating a cache.
If no cache is provided, a temporary directory will be used as
a cache. The temporary directory will be automatically removed
when the Python process exits.
N)
r� �mkdtemp�atexit�register�shutil�rmtreer � str_typesrY �httpFactory�_connection�
user_agent�max_retries)rD �service_root�credentialsrE rF rG r� r� s r0 r@ zBrowser.__init__s sj � � �=��$�$�&�E��O�O�F�M�M�5�1��e�Y�'�/��6�E�'�3�3�����
��� %���&��r c � � t d| j dz � D ]b }| j j ||||�� \ }}|j dv r,|| j k rt d|dz
z � }t
|� �_ ||fS fS )Nr r �rR rS rT )i� i� � )�ranger� r� �request�status�intr ) rD �urlrR rS rT �retry_count�response�content� sleep_fors r0 �_request_and_retryzBrowser._request_and_retry� s� � � ��D�$4�$4�q�$8�9�
�K� $� 0� 0� 8� 8��F��w� !9� !��H�g� ���:�-��$�"2�"2�2� ��k�A�o� 6�7� ��i� ���� � �
� �� � r c �� � |dk( rt d� �d|i}| j �| j |d<