o
    ɶdA4                     @   s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlZzd dlmZ W n eyC   d dlmZ Y nw d dlmZ d dlZeeZeeZdZdZdZd	Zd
ZdZde Zde Zzd dlmZ W n ey   d dl ZY nw e! Z"dd Z#dd Z$dd Z%dd Z&G dd de!Z'e'dddZ(e'dddZ)dd Z*dd Z+ee*ee+iZ,d d! Z-d"d# Z.efd$d%Z/d&d' Z0d(d) Z1d*d+ Z2d,d- Z3d.d/ Z4d0d1 Z5d2d3 Z6d4d5 Z7dS )6    )absolute_importN)six)urlparse)SEEK_SETZinstance_principalZ(delegation_token_with_instance_principalZresource_principalZdelegation_token_fileZauthentication_typei   d      c                 C   s   t | tjr| S | tu rdS t | tjtjfr0| js tj	| } t | tjr,| j
ddS | 
 S t | tjr:| 
 S t | tjrJdd t| D S t | tjrWdd | D S t| ds^| S i }t| jD ]}t| |t}|turxt|||< qf|S )	zHelper to flatten models into dicts for rendering.

    The following conversions are applied:

    * datetime.date, datetime.datetime, datetime.time
      are converted into ISO8601 UTC strings
    NT)sepc                 S   s   i | ]	\}}|t |qS  to_dict).0kvr
   r
   ,usr/lib/python3.10/site-packages/oci/util.py
<dictcomp>I   s    zto_dict.<locals>.<dictcomp>c                 S   s   g | ]}t |qS r
   r   )r   r   r
   r
   r   
<listcomp>K   s    zto_dict.<locals>.<listcomp>swagger_types)
isinstancer   Zstring_typesNONE_SENTINELdatetimetimetzinfopytzutclocalize	isoformatdateabcMappingZ	iteritemsIterablehasattrZiterkeysr   getattrmissing_attrr   )objas_dictkeyvaluer
   r
   r   r   0   s2   	
r   c                 C   s   t | }tj|dddS )z9Returns a string of the model flattened as a dict, sorted   T)indent	sort_keys)r   jsondumps)modelr%   r
   r
   r   formatted_flat_dictY   s   r.   c                 C   s   | d u p| t u p| |v S N)r   )Zvalue_to_testZallowed_valuesr
   r
   r   #value_allowed_none_or_none_sentinelc   s   r0   c                 C   sV   t jt j| }t|d}| }W d   n1 sw   Y  t|dS )a  
    Takes a file path and returns a Base64-encoded string which can be provided as the value of the ``user_data`` key
    in the ``metadata`` dictionary when launching an instance(see :py:class:`~oci.core.models.LaunchInstanceDetails`
    for more information).

    :param str file_path:
      The path to the file whose contents will be Base64-encoded

    :return: The Base64-encoded string
    :rtype: str
    rbNzutf-8)	ospath
expandvars
expanduseropenreadbase64	b64encodedecode)	file_path	full_pathffile_contentsr
   r
   r   )file_content_as_launch_instance_user_datag   s
   
r?   c                       sD   e Zd ZdZi Zd fdd	ZdddZdd Zd	d
 ZeZ	  Z
S )Sentinela  Named singletons for clear docstrings.
    Also used to differentiate an explicit param of None from a lack of argument.

    .. code-block:: pycon

        >>> missing = Sentinel("Missing", False)
        >>> also_missing = Sentinel("Missing", False)
        >>> assert missing is also_missing
        >>> repr(missing)
        <Missing>
        >>> assert bool(missing) is False
    Tc                    sP   t j|d }|d u rtt | |  }t j|< |S |j|ur&td||S )Nz;Tried to get existing Sentinel {!r} with wrong truthy value)r@   _symbolsgetsuper__new__truthy
ValueErrorformat)clsnamerE   sentinel	__class__r
   r   rD      s   
zSentinel.__new__c                 C   s   || _ || _d S r/   rI   rE   )selfrI   rE   r
   r
   r   __init__   s   
zSentinel.__init__c                 C   s   d | jS )Nz<{}>)rG   rI   rN   r
   r
   r   __repr__   s   zSentinel.__repr__c                 C   s   | j S r/   )rE   rP   r
   r
   r   __bool__   s   zSentinel.__bool__)T)__name__
__module____qualname____doc__rA   rD   rO   rQ   rR   Z__nonzero____classcell__r
   r
   rK   r   r@   {   s    
r@   NoneFrM   ZWaitResourceNotFoundc                 C   s   ddl m} i }| t}|d u rtjdttj	
|}tj	|s/tjd|t|dd}|  }W d    n1 sFw   Y  |d u rUtjd||d< |d	i |S )
Nr   )'InstancePrincipalsDelegationTokenSignerzERROR: {} was not provided.z,ERROR: delegation token file not found at {}r)modez)ERROR: delegation_token was not provided.delegation_tokenr
   )oci.auth.signersrY   rB    DELEGATION_TOKEN_FILE_FIELD_NAMEoci
exceptionsInvalidConfigrG   r2   r3   r5   isfiler6   r7   strip)configrY   Zsigner_kwargsZdelegation_token_file_pathZ#expanded_delegation_token_file_pathr=   r\   r
   r
   r   4_get_signer_from_delegation_token_instance_principal   s   
re   c                 C   s   ddl m} | S )Nr   )get_resource_principal_signer)r]   rf   )rd   rf   r
   r
   r   #_get_signer_from_resource_principal   s   rg   c                 C   s   t | }t|}|| S r/   )#get_authentication_type_from_config AUTH_TYPE_TO_SIGNER_FUNCTION_MAPrB   )rd   	auth_typeZsigner_functionr
   r
   r   #get_signer_from_authentication_type   s   
rk   c                 C   sZ   |  t}|d u rtdt|tkr$t| v rtS tj	dtttj	d|)Nz{} not providedzAThe authentication type {} requires config values for the keys {}z+The authentication type {} is not supported)
rB   AUTHENTICATION_TYPE_FIELD_NAMErF   rG   1INSTANCE_PRINCIPAL_AUTHENTICATION_TYPE_VALUE_NAMEr^   <DELEGATION_TOKEN_WITH_INSTANCE_PRINCIPAL_AUTHENTICATION_TYPEr_   r`   ra   )rd   rj   r
   r
   r   rh      s   
rh   c              	   C   s   t d zKd}t}d}d}d}|rKt| dr| j|}nt| dr)| |}ntdt|dkr5d	}||7 }|t|7 }|rI||krItd
|s||dW S  t	t
fy]   tdw )NzReading the stream to calculate its content-length. Process may freeze for very big streams. Consider passing in content length for big objectsTr        bufferr7   zuStream object does not contain a 'read' property. Cannot auto calculate content length, please pass in content lengthFz{Stream size is greater than the buffer_limit, please pass in a bigger buffer_limit or pass in content length to the request)content_lengthbyte_contentzRStream object's content length cannot be calculated, please pass in content length)loggerwarningDEFAULT_PART_SIZEr!   rq   r7   	TypeErrorlenBufferErrorIOErrorOSError)streamZbuffer_limitZkeep_readingZ	part_sizerr   contentrs   r
   r
   r   ,back_up_body_calculate_stream_content_length   s0   


r~   c                 C   s:   t | dst | dst | dst | drdS td dS )N__len__rx   filenotellTzRequest did not contain content-length and stream object does not contain fileno or tell property. Stream object will be read to calculate content-lengthF)r!   rt   ru   )or
   r
   r   (is_content_length_calculable_by_req_util   s   (
r   c                 C   s   t | }|jd |j S )a  
    Takes a Service Endpoint with base-path embedded and extracts the Service Endpoint from it.

    :param str endpoint_with_base_path:
    Service Endpoint with base-path embedded

    :return: The Service Endpoint without base-path.
    :rtype: str
    z://)r   schemenetloc)Zendpoint_with_base_pathZparsed_endpointr
   r
   r   extract_service_endpoint  s   
r   c                 K   s2   | j }|dkr|d}|rt|drdS dS dS )NZcall_apibodyr7   TF)rS   rB   r!   )Zfunc_refZfunc_kwargs	func_namer   r
   r
   r   %should_record_body_position_for_retry  s   
r   c              	   C   sp   d}t | dd d ur+z	|  }W ||fS  ttfy*   d}d }td Y ||fS w d}d }td ||fS )NTr   FzVUnable to record body position for rewinding. This request will not be retried/rewound)r"   r   rz   r{   rt   ru   )r   Zis_body_rewindablebody_positionr
   r
   r   record_body_position_for_rewind!  s   

r   c              	   C   sT   t | dd d ur#z	| |t W dS  ttfy"   td Y dS w td dS )NseekzUUnable to reset body position for rewinding. This request will not be retried/rewoundFT)r"   r   r   rz   r{   rt   ru   )r   r   r
   r
   r   rewind_body4  s   

r   c              	   C   s   d}z.	 d}t |dr|t}nt |dr|jt}t|dkr%W |S |t|7 }| | q ttfyC   t	d d}Y |S w )Nr   Tro   r7   rq   z&Unable to read stream body for signing)
r!   r7   rv   rq   rx   updaterz   r{   rt   ru   )Zsigning_algorithmr   Z
bytes_readchunkr
   r
   r   read_stream_for_signingB  s&   




r   c                 C   s    t dd| }t dd| S )Nz(.)([A-Z][a-z]+)z\1_\2z([a-z0-9])([A-Z]))resublower)rI   s1r
   r
   r   camel_to_snakeU  s   r   c                 C   s&   i }|   D ]
\}}||t|< q|S r/   )itemsr   )
dictionaryZoutdictr   r   r
   r
   r   camel_to_snake_keysZ  s   r   )8
__future__r   r8   r   r+   r   r2   os.pathr   Zoci._vendorr   Zoci.exceptionsr_   urllib.parser   ImportErrorior   logging	getLoggerrS   rt   rm   rn   Z&RESOURCE_PRINCIPAL_AUTHENTICATION_TYPEr^   rl   ZMEBIBYTEDEFAULT_BUFFER_SIZErv   collections.abcr   collectionsobjectr#   r   r.   r0   r?   r@   r   ZWAIT_RESOURCE_NOT_FOUNDre   rg   ri   rk   rh   r~   r   r   r   r   r   r   r   r   r
   r
   r
   r   <module>   sp   

)
%		