
    ?g.                         d dl Z d dlZd dlZd dlmZ d dlmZ  ej                  d      Z ej                  d      Z		 ddZ
	 	 	 	 	 ddZ	 ddZ	 dd	Z G d
 de      Zy)    N)client)DynamicClientz(.)([A-Z][a-z]+)z([a-z0-9])([A-Z])c           	      j   |st        d      t        j                  j                  |      st        d      t        j                  |      D cg c]_  }t        j                  j                  t        j                  j                  ||            r t        j                  j                  ||      a }}|st        d      g }g }	|D ]%  }
	 t        | |
f|||d|}|	j                  |       ' |rt        |      |	S c c}w # t        $ r%}|j                  |j                         Y d}~ed}~ww xY w)a  
    Perform an action from files from a directory. Pass True for verbose to
    print confirmation information.

    Input:
    k8s_client: an ApiClient object, initialized with the client args.
    yaml_dir: string. Contains the path to directory.
    verbose: If True, print confirmation from the create action.
        Default is False.
    namespace: string. Contains the namespace to create all
        resources inside. The namespace must preexist otherwise
        the resource creation will fail. If the API object in
        the yaml file already contains a namespace definition
        this parameter has no effect.
    apply: bool. If True, use server-side apply for creating resources.

    Available parameters for creating <kind>:
    :param async_req bool
    :param bool include_uninitialized: If true, partially initialized
        resources are included in the response.
    :param str pretty: If 'true', then the output is pretty printed.
    :param str dry_run: When present, indicates that modifications
        should not be persisted. An invalid or unrecognized dryRun
        directive will result in an error response and no further
        processing of the request.
        Valid values are: - All: all dry run stages will be processed

    Returns:
        The list containing the created kubernetes API objects.

    Raises:
        FailToCreateError which holds list of `client.rest.ApiException`
        instances for each object that failed to create.
    z$`yaml_dir` argument must be providedz/`yaml_dir` argument must be a path to directoryz`yaml_dir` contains no files)verbose	namespaceapplyN)
ValueErrorospathisdirlistdirisfilejoincreate_from_yamlappendFailToCreateErrorextendapi_exceptions)
k8s_clientyaml_dirr   r   r   kwargsifilesfailuresk8s_objects_allfilek8s_objectsfailures                V/var/www/openai/venv/lib/python3.12/site-packages/kubernetes/utils/create_from_yaml.pycreate_from_directoryr       s/   L ?@@WW]]8$JKK H%%A77>>"'',,x34 	Xq!% 
 
 788HO	4*  # K "";/  ))5, ! 	4OOG2233	4s   A$C?"D	D2D--D2c                 R    |f fd	} G d dt         j                  j                        }|r
|}	 ||	      S |rRt        t        j
                  j                  |            5 }
t        j                  |
|      }	 ||	|      cddd       S t        d      # 1 sw Y   yxY w)a1  
    Perform an action from a yaml file. Pass True for verbose to
    print confirmation information.
    Input:
    yaml_file: string. Contains the path to yaml file.
    k8s_client: an ApiClient object, initialized with the client args.
    yaml_objects: List[dict]. Optional list of YAML objects; used instead
        of reading the `yaml_file`. Default is None.
    verbose: If True, print confirmation from the create action.
        Default is False.
    namespace: string. Contains the namespace to create all
        resources inside. The namespace must preexist otherwise
        the resource creation will fail. If the API object in
        the yaml file already contains a namespace definition
        this parameter has no effect.
    apply: bool. If True, use server-side apply for creating resources.

    Available parameters for creating <kind>:
    :param async_req bool
    :param bool include_uninitialized: If true, partially initialized
        resources are included in the response.
    :param str pretty: If 'true', then the output is pretty printed.
    :param str dry_run: When present, indicates that modifications
        should not be persisted. An invalid or unrecognized dryRun
        directive will result in an error response and no further
        processing of the request.
        Valid values are: - All: all dry run stages will be processed

    Returns:
        The created kubernetes API objects.

    Raises:
        FailToCreateError which holds list of `client.rest.ApiException`
        instances for each object that failed to create.
    c                     g }g }| D ](  }|	 t        |
f	|d}|j                  |       * |rt        |      |S # t        $ r%}|j                  |j                         Y d }~cd }~ww xY w)Nr   r   )create_from_dictr   r   r   r   )objectsr   r   r   yml_documentcreatedr   r   r   r   r   s          r   create_withz%create_from_yaml.<locals>.create_with   s    #L#8*  (  ""7+ $ #H--	 % 8 6 6778s   "A	A0A++A0c                       e Zd Zej                  j
                  j                  j                         Zdev rej                  d       yy) create_from_yaml.<locals>.Loader=N)	__name__
__module____qualname__yamlloader
SafeLoaderyaml_implicit_resolverscopypop     r   Loaderr*      s=    "&++"8"8"P"P"U"U"W))#'', *r6   r7   )r7   Nz?One of `yaml_file` or `yaml_objects` arguments must be provided)	r/   r0   r1   openr
   r   abspathload_allr	   )r   	yaml_fileyaml_objectsr   r   r   r   r(   r7   yml_document_allfs   `  `` `    r   r   r   c   s    Z $)  ,-'' -
 '+,,	"''//),-#}}Qv>/7 .- M
 	
	 .-s   ( BB&c                    g }g }d|d   v rU|d   j                  dd      }|d   D ]7  }	|dk7  r|d   |	d<   ||	d<   	 t        | |	|f||d|}
|j                  |
       9 n#	 t        | ||f||d|}
|j                  |
       |rt        |      |S # t        j                  j
                  $ r}|j                  |       Y d}~d}~ww xY w# t        j                  j
                  $ r}|j                  |       Y d}~}d}~ww xY w)a  
    Perform an action from a dictionary containing valid kubernetes
    API object (i.e. List, Service, etc).

    Input:
    k8s_client: an ApiClient object, initialized with the client args.
    data: a dictionary holding valid kubernetes objects
    verbose: If True, print confirmation from the create action.
        Default is False.
    namespace: string. Contains the namespace to create all
        resources inside. The namespace must preexist otherwise
        the resource creation will fail. If the API object in
        the yaml file already contains a namespace definition
        this parameter has no effect.
    apply: bool. If True, use server-side apply for creating resources.

    Returns:
        The created kubernetes API objects.

    Raises:
        FailToCreateError which holds list of `client.rest.ApiException`
        instances for each object that failed to create.
    Listkind items
apiVersionr#   N)replacecreate_from_yaml_single_itemr   r   restApiExceptionr   )r   datar   r   r   r   r   r   rA   
yml_objectr'   api_exceptions               r   r$   r$      sH   6 NKf F|##FB/w-J rz+/+=
<(%)
6"56 (  ""7+ ((	12D'5>eOUG w'
 //! ;;++ 5%%m445 {{'' 	1!!-00	1s/   "B""C C0CCD+DDc                    |d   }|du rt        |       j                  j                  |d   |      } |j                  d|dd|}|rOdj	                  |      }t        |d      r'|d	j	                  t        |j                              z  }t        |       |S |d   j                  d
      \  }	}
}|dk(  r|	}d}	dj                  |	j                  dd            }	dj                  d |	j                  d      D              }	dj	                  |	|j                               } t        t        |      |       }t         j#                  d|      }t$        j#                  d|      j'                         }t        |dj	                  |            r8d|d   v r|d   d   }||d<    t        |dj	                  |            dd|i|}n5|j)                  dd         t        |dj	                  |            dd|i|}|rOdj	                  |      }t        |d      r'|d	j	                  t        |j                              z  }t        |       |S )NrA   TrD   )api_versionrA   zpython-client)bodyfield_managerz{0} created.statusz status='{0}'/rB   corez.k8s.io   c              3   <   K   | ]  }|j                           y wN)
capitalize).0words     r   	<genexpr>z/create_from_yaml_single_item.<locals>.<genexpr>  s     C2B$DOO%2Bs   .z	{0}{1}Apiz\1_\2zcreate_namespaced_{0}r   metadatarN   z
create_{0}r5   )r   	resourcesgetserver_side_applyformathasattrstrrP   print	partitionr   rsplitsplitrV   getattrr   UPPER_FOLLOWED_BY_LOWER_REsub!LOWER_OR_NUM_FOLLOWED_BY_UPPER_RElowerr4   )r   rJ   r   r   r   rA   apply_clientrespmsggroup_versionfcn_to_callk8s_apir   s                  r   rF   rF      sd    fD}$Z0::>>"<0t ? 
 .|-- 
?
>D
  ''-CtX&--c$++.>??#J"<0::3?E1g"} GGELLA./E GGC%++c2BCCE$$UG,>,>,@AK*gfk*:6G%))(D9D,004@FFHDw/66t<= *Z00":.{;I"+F;Eww 7 > >t DE 

%
 	

;%:ww 3 3D 9: 

%
 ##D)4"?))#dkk*:;;Cc
Kr6   c                       e Zd ZdZd Zd Zy)r   zc
    An exception class for handling error if an error occurred when
    handling a yaml file.
    c                     || _         y rU   )r   )selfr   s     r   __init__zFailToCreateError.__init__;  s
    ,r6   c                 ~    d}| j                   D ]+  }|dj                  |j                  |j                        z  }- |S )NrB   zError from server ({0}): {1})r   r_   reasonrN   )ru   rm   rK   s      r   __str__zFailToCreateError.__str__>  sG    !00M188$$m&8&8 C 1 
r6   N)r,   r-   r.   __doc__rv   ry   r5   r6   r   r   r   5  s    
-r6   r   )NFdefaultF)NNFr{   F)Fr{   F)FF)r
   rer/   
kubernetesr   kubernetes.dynamic.clientr   compilerg   ri   r    r   r$   rF   	Exceptionr   r5   r6   r   <module>r      s     
 	   3'RZZ(:; $.BJJ/B$C ! JOET 
R
l AFBL 275p	 r6   