o
    Rh9                     @   s|   d dl Z d dlZd dlmZ d dlmZ G dd deZG dd deZG dd	 d	eZ	G d
d deZ
G dd deZdS )    N)partial)pformatc                   @   sZ   e Zd ZdZ			dddZdd Zedd	 Zd
d Zedd Z	dddZ
dd ZdS )Resourceza Represents an API resource type, containing the information required to build urls for requests NFc                    s   d |||fv rt d| _| _| _| _| _| _| _| _|	 _	|
p1|r0|d d nd _
| _| _ fdd|pAi  D  _| _d S )Nz7At least prefix, kind, and api_version must be provided c                    s"   i | ]\}}|t  fi |qS  )Subresource.0kvselfr   ]/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/kubernetes/dynamic/resource.py
<dictcomp>,   s    z%Resource.__init__.<locals>.<dictcomp>)
ValueErrorprefixgroupapi_versionkind
namespacedverbsname	preferredclientsingular_nameshort_names
categoriesitemssubresources
extra_args)r   r   r   r   r   r   r   r   r   r   singularName
shortNamesr   r   kwargsr   r   r   __init__   s$   


zResource.__init__c                 C   sV   d| j | j| j| j| j| j| j| j| j| j	| j
dd | j D d}|| j |S )Nr   c                 S   s   i | ]	\}}||  qS r   )to_dict)r
   r   srr   r   r   r   @   s    z$Resource.to_dict.<locals>.<dictcomp>)_typer   r   r   r   r   r   r   r   r!   r"   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   updater    r   dr   r   r   r%   2   s    zResource.to_dictc                 C   s   | j rd| j | jS | jS )N{}/{})r   formatr   r   r   r   r   group_versionE   s   zResource.group_versionc                 C   s   d | jj| j| jS )Nz<{}({}/{})>)r,   	__class____name__r-   r   r   r   r   r   __repr__K      zResource.__repr__c                 C   sH   d | j| j}| j }d ||d ||d ||d ||dS )Nr+   z/{}/{}z/{}/namespaces/{{namespace}}/{}z/{}/{}/{{name}}z(/{}/namespaces/{{namespace}}/{}/{{name}})basenamespaced_basefullnamespaced_full)r,   r   r-   r   lower)r   full_prefixresource_namer   r   r   urlsN   s   




zResource.urlsc                 C   sb   g }i }| j r|r|d ||d< |r|d ||d< n|d | jd| jdi |S )Nr   	namespacer4   r   r2   _r   )r   appendr9   joinr,   )r   r   r:   url_typepath_paramsr   r   r   pathY   s   




zResource.pathc                 C   s&   || j v r
| j | S tt| j|| S N)r   r   getattrr   r   r   r   r   r   __getattr__f   s   

zResource.__getattr__)NNNNFNNFNNNNNNN)r/   
__module____qualname____doc__r$   r%   propertyr-   r0   r9   r@   rD   r   r   r   r   r      s    




r   c                   @   sv   e Zd ZdZdddZdd Zd	d
 Zdd ZdddZdddZ	dd Z
dd Zdd Zdd Zdd Zdd ZdS )ResourceListz" Represents a list of API objects r   v1Nc                 C   s8   || _ || _|| _|pd|| _|| _|| _d | _d S )Nz{}List)r   r   r   r,   r   	base_kindbase_resource_lookup_ResourceList__base_resource)r   r   r   r   rL   r   rM   r   r   r   r$   o   s   
zResourceList.__init__c                 C   s\   | j r| j S | jr| jjjdi | j| _ | j S | jr,| jjj| j| j| jd| _ | j S d S )N)r   r   r   r   )rN   rM   r   	resourcesgetrL   r   r   r   r   r   r   base_resourcex   s   zResourceList.base_resourcec                    sl   |du rt d|d }|d }|d}|st d j|kr)t d j||| fdd	|D d
S )aN   Takes a List body and return a dictionary with the following structure:
            {
                'api_version': str,
                'kind': str,
                'items': [{
                    'resource': Resource,
                    'name': str,
                    'namespace': str,
                }]
            }
        Nz>You must provide a body when calling methods on a ResourceList
apiVersionr   r   zVThe `items` field in the body must be populated when calling methods on a ResourceListzXMethods on a {} must be called with a body containing the same kind. Received {} insteadc                       g | ]}  |qS r   )_item_to_resourcer
   itemr   r   r   
<listcomp>       z4ResourceList._items_to_resources.<locals>.<listcomp>)r   r   r   )r   rP   r   r,   )r   bodyr   r   r   r   r   r   _items_to_resources   s   

z ResourceList._items_to_resourcesc                 C   s`   | di }|  }|s#| d| j}| d| j}| jjj ||d}||| d| ddS )NmetadatarR   r   )r   r   r   r:   )resource
definitionr   r:   )rP   rQ   r   rL   r   rO   )r   rV   r[   r\   r   r   r   r   r   rT      s   zResourceList._item_to_resourcec                    F   |rt d| |}t|} fdd|d D |d< t| |S )NEOperations on ResourceList objects do not support the `name` argumentc                    6   g | ]}|d  j d|d |d pd  qS r\   r   r:   r   r:   r   )rP   r%   rU   r#   r:   r   r   rW          (z$ResourceList.get.<locals>.<listcomp>r   r   rZ   copydeepcopyResourceInstancer   rY   r   r:   r#   resource_listresponser   rc   r   rP         



zResourceList.getc                    r^   )Nr_   c                    r`   ra   )deleter%   rU   rc   r   r   rW      rd   z'ResourceList.delete.<locals>.<listcomp>r   re   ri   r   rc   r   rm      rl   zResourceList.deletec                    s:   |  |}t|} fdd|d D |d< t| |S )Nc                    s0   g | ]}t |d  dd|d i  qS )r\   rY   r]   r   )rB   r%   rU   r#   verbr   r   rW      s    "z,ResourceList.verb_mapper.<locals>.<listcomp>r   )rZ   rf   rg   rh   )r   ro   rY   r#   rj   rk   r   rn   r   verb_mapper   s   



zResourceList.verb_mapperc                 O      | j dg|R i |S )Ncreaterp   r   argsr#   r   r   r   rr         zResourceList.createc                 O   rq   )Nreplacers   rt   r   r   r   rw      rv   zResourceList.replacec                 O   rq   )Npatchrs   rt   r   r   r   rx      rv   zResourceList.patchc                 C   s   d| j | j| j| jdS )NrJ   )r'   r   r   r   rL   )r   r   r   rL   r   r   r   r   r%      s   zResourceList.to_dictc                 C   s   |   rt|   |S d S rA   )rQ   rB   rC   r   r   r   rD      s   zResourceList.__getattr__)r   rK   r   NNrE   )r/   rF   rG   rH   r$   rQ   rZ   rT   rP   rm   rp   rr   rw   rx   r%   rD   r   r   r   r   rJ   l   s    
	

		rJ   c                   @   s>   e Zd ZdZdd ZdddZedd Zd	d
 Zdd Z	dS )r   z Represents a subresource of an API resource. This generally includes operations
        like scale, as well as status objects for an instantiated resource
    c                 K   sz   || _ |j| _|j| _|j| _|d| _|d| _|dd p(| jdd | _|dd| _	|dd | _
|| _d S )	Nr   r   subresource/   r   Fr   )parentr   r   r   popr   r   splitry   r   r   r    )r   r|   r#   r   r   r   r$      s   
zSubresource.__init__Nc                 K   sj   |p
| di  d}| jj|}| jjr | jj| j||}| j||d}| jjjd|fd|i|S )Nr[   r   rb   postrY   )rP   r|   r   serialize_bodyr   ensure_namespacer@   request)r   rY   r   r:   r#   r@   r   r   r   rr      s   zSubresource.createc                 C   s:   d | j| j}d || jj| jd || jj| jdS )Nr+   z/{}/{}/{{name}}/{}z+/{}/namespaces/{{namespace}}/{}/{{name}}/{})r4   r5   )r,   r   r-   r|   r   ry   )r   r7   r   r   r   r9     s   zSubresource.urlsc                 C   s   t t| jj|| S rA   )r   rB   r|   r   rC   r   r   r   rD   
  s   zSubresource.__getattr__c                 C   s*   | j | j| j| j| jd}|| j |S )N)r   r   ry   r   r   )r   r   ry   r   r   r(   r    r)   r   r   r   r%     s   zSubresource.to_dict)NNN)
r/   rF   rG   rH   r$   rr   rI   r9   rD   r%   r   r   r   r   r      s    

r   c                       st   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	 fddZ
 fddZdd Zdd Zdd Z  ZS )rh   z A parsed instance of an API resource. It exists solely to
        ease interaction with API objects by allowing attributes to
        be accessed with '.' notation.
    c                 C   s   || _ |d }|dr9d|v r9|d d d }|d s g |d< |d D ]}d|vr0|d |d< d|vr8||d< q$| || _d| _d S )Nr   Listr   rR   T)r   endswith_ResourceInstance__deserialize
attributes_ResourceInstance__initialised)r   r   instancer   rV   r   r   r   r$     s   
zResourceInstance.__init__c                    sJ   t |trt fdd| D dS t |ttfr# fdd|D S |S )Nc                       i | ]
\}}|  |qS r   r   r	   r   r   r   r   3      z2ResourceInstance.__deserialize.<locals>.<dictcomp>)paramsc                    rS   r   r   rU   r   r   r   rW   7  rX   z2ResourceInstance.__deserialize.<locals>.<listcomp>)
isinstancedictResourceFieldr   listtupler   fieldr   r   r   __deserialize1  s   

zResourceInstance.__deserializec                    sX   t |tr fdd|j D S t |ttfr! fdd|D S t |tr*| S |S )Nc                    r   r   _ResourceInstance__serializer	   r   r   r   r   =  r   z0ResourceInstance.__serialize.<locals>.<dictcomp>c                    rS   r   r   rU   r   r   r   rW   A  rX   z0ResourceInstance.__serialize.<locals>.<listcomp>)r   r   __dict__r   r   r   rh   r%   r   r   r   r   __serialize;  s   


zResourceInstance.__serializec                 C   s   |  | jS rA   )r   r   r   r   r   r   r%   G     zResourceInstance.to_dictc                 C   s   t | S rA   )reprr   r   r   r   to_strJ  s   zResourceInstance.to_strc              	   C   s&   d | jjdt|  dS )NzResourceInstance[{}]:
  {}z  T)r,   r   r   r=   yaml	safe_dumpr%   
splitlinesr   r   r   r   r0   M  s   zResourceInstance.__repr__c                    s&   d| j vrtt| |S t| j|S Nr   )r   superrh   rD   rB   r   rC   r.   r   r   rD   S  s   
zResourceInstance.__getattr__c                    sF   d| j vrtt| ||S || j v rtt| ||S || j|< d S r   )r   r   rh   __setattr__r   r   r   valuer   r   r   r   X  s
   

zResourceInstance.__setattr__c                 C   s
   | j | S rA   r   rC   r   r   r   __getitem__`     
zResourceInstance.__getitem__c                 C      || j |< d S rA   r   r   r   r   r   __setitem__c     zResourceInstance.__setitem__c                 C   s   t t| t| jj  S rA   )dirtyper   r   r   keysr   r   r   r   __dir__f  s   zResourceInstance.__dir__)r/   rF   rG   rH   r$   r   r   r%   r   r0   rD   r   r   r   r   __classcell__r   r   r   r   rh     s    
rh   c                   @   s`   e Zd Z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 )r   z A parsed instance of an API resource attribute. It exists
        solely to ease interaction with API objects by allowing
        attributes to be accessed with '.' notation
    c                 C   s   | j jdi | d S )Nr   )r   r(   )r   r   r   r   r   r$   p  r1   zResourceField.__init__c                 C   s
   t | jS rA   )r   r   r   r   r   r   r0   s  r   zResourceField.__repr__c                 C   s   | j |j kS rA   r   )r   otherr   r   r   __eq__v  r   zResourceField.__eq__c                 C   s   | j |S rA   )r   rP   rC   r   r   r   r   y  r   zResourceField.__getitem__c                 C   s   | j |t| j |d S rA   )r   rP   rB   rC   r   r   r   rD   ~  rv   zResourceField.__getattr__c                 C   r   rA   r   r   r   r   r   r     r   zResourceField.__setattr__c                 C   s   t t| t| j  S rA   )r   r   r   r   r   r   r   r   r   r     s   zResourceField.__dir__c                 c   s$    | j  D ]	\}}||fV  qd S rA   )r   r   )r   r   r   r   r   r   __iter__  s   zResourceField.__iter__c                 C   s
   |  | S rA   _ResourceField__serializer   r   r   r   r%     r   zResourceField.to_dictc                    sF   t |tr fdd|j D S t |ttfr! fdd|D S |S )Nc                    r   r   r   r	   r   r   r   r     r   z-ResourceField.__serialize.<locals>.<dictcomp>c                    rS   r   r   rU   r   r   r   rW     rX   z-ResourceField.__serialize.<locals>.<listcomp>)r   r   r   r   r   r   r   r   r   r   r     s   

zResourceField.__serializeN)r/   rF   rG   rH   r$   r0   r   r   rD   r   r   r   r%   r   r   r   r   r   r   j  s    r   )rf   r   	functoolsr   pprintr   objectr   rJ   r   rh   r   r   r   r   r   <module>   s   V|1Q