
    Vh0                       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mZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ d dlmZ d dlmZ  e       Z G d de      Z G d de      Z G d de
      Z y)    )annotationsN)abstractmethod)MutableMapping)	constants)AnsibleError)S_IRWU_RG_RO)to_bytesto_text)AnsiblePlugin)cache_loader)resource_from_fqcr)Displayc                       e Zd ZeZ fdZed        Zed        Zed        Z	ed        Z
ed        Zed        Zed        Z xZS )	BaseCacheModulec                P    t         t        |           | j                  ||       y )N)var_optionsdirect)superr   __init__set_optionsselfargskwargs	__class__s      N/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/cache/__init__.pyr   zBaseCacheModule.__init__.   s#    ot-/T&9    c                     y N r   keys     r   getzBaseCacheModule.get2       r   c                     y r   r    r   r"   values      r   setzBaseCacheModule.set6   r$   r   c                     y r   r    r   s    r   keyszBaseCacheModule.keys:   r$   r   c                     y r   r    r!   s     r   containszBaseCacheModule.contains>   r$   r   c                     y r   r    r!   s     r   deletezBaseCacheModule.deleteB   r$   r   c                     y r   r    r*   s    r   flushzBaseCacheModule.flushF   r$   r   c                     y r   r    r*   s    r   copyzBaseCacheModule.copyJ   r$   r   )__name__
__module____qualname__display_displayr   r   r#   r(   r+   r-   r/   r1   r3   __classcell__r   s   @r   r   r   )   s     H:              r   r   c                       e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zed        Zed        Z xZS )BaseFileCacheModulez8
    A caching module backed by file based storage.
    c                   	 t        t        | 
  |i | | j                  | j	                  d            | _        t        | j	                  d            | _        t        | j                        | _        i | _        | j!                          y # t        $ rE | j                  t        j                        | _        t        t        j                        | _        Y w xY w)N_uri_timeout)r   r<   r   _get_cache_connection
get_option
_cache_dirfloatr?   KeyErrorCCACHE_PLUGIN_CONNECTIONCACHE_PLUGIN_TIMEOUTr   r5   plugin_name_cachevalidate_cache_connectionr   s      r   r   zBaseFileCacheModule.__init__S   s    	:%t5tFvF"889PQDO!$//*"=>DM .doo>&&(  	:"889R9RSDO!!"8"89DM	:s   AB ACCc                    |r=	 t         j                  j                  t         j                  j                  |            S y # t        $ r Y y w xY wr   )ospath
expanduser
expandvars	TypeError)r   sources     r   r@   z)BaseFileCacheModule._get_cache_connection`   sG    ww))"''*<*<V*DEE   s   ;A 	AAc                d   | j                   st        d| j                  z        t        j                  j                  | j                         s!	 t        j                  | j                          y t        j                  t        j                  t        j                  fD ]H  }t        j                  | j                   |      r$t        d| j                  d| j                   d       y # t        t        f$ r6}t        d| j                  d| j                   dt        |            d }~ww xY w)Nzwerror, '%s' cache plugin requires the 'fact_caching_connection' config option to be set (to a writeable directory path)
error in 'z0' cache plugin while trying to create cache dir  : z' cache, configured path (z=) does not have necessary permissions (rwx), disabling plugin)rB   r   rH   rL   rM   existsmakedirsOSErrorIOErrorr	   R_OKW_OKX_OKaccess)r   exs      r   rJ   z-BaseFileCacheModule.validate_cache_connectiong   s     KMQM]M] ^ _ _ ww~~doo.]DOO, ggrww0 <yy!4&(($//(; < << W% ]"lpl|l|  C  N  N  PX  YZ  P[  $\  ]  ]]s   C* *D/91D**D/c                x    | j                  d      }|r| j                  d||}|S | j                  d|}|S )N_prefix/)rA   rB   )r   r"   prefix	cachefiles       r   _get_cache_file_namez(BaseFileCacheModule._get_cache_file_namew   sA    +%)__fcBI  $(??C8Ir   c                   || j                   vrN| j                  |      s|dk(  rt        | j                  |      }	 | j	                  |      }|| j                   |<   | j                   j                  |      S # t
        $ rV}t        j                  d| j                  d|dt        |      d       | j                  |       t        d|z        d}~wt        t        f$ r<}t        j                  d| j                  d|dt        |             t        d}~wt        $ r}t        d|d	t        |            d}~ww xY w)
z This checks the in memory cache first as the fact was not expired at 'gather time'
        and it would be problematic if the key did expire after some long running tasks and
        user gets 'undefined' error in the same play  rS   z$' cache plugin while trying to read rT   z5. Most likely a corrupt file, so erasing and failing.zThe cache file %s was corrupt, or did not otherwise contain valid data. It has been removed, so you can re-run your command now.Nz$Error while decoding the cache file z: )rI   has_expiredrD   rd   _load
ValueErrorr7   warningrH   r	   r/   r   rW   rX   	Exceptionr#   )r   r"   rc   r'   r]   s        r   r#   zBaseFileCacheModule.get   sG   
 dkk!$r	11#6Il

9-#(C  {{s##  kY]YiYiktv~  @A  wB!C DC " $^`i$j k kW% ]a]m]mox  {C  DE  {F  !G  H l"S\^fgh^i#jkkls0    A8 8	EACE$7DE'EEc                   || j                   |<   | j                  |      }t        j                  t        j
                  j                  |            \  }}	 	 | j                  ||       	 t	        j                  ||       t	        j                  |t                	 t	        j"                  |       y # t        t        f$ r;}t        j                  d| j                  d|dt        |             Y d }~d }~ww xY w# t        t        f$ r>}t        j                  d| j                  d|d|dt        |             Y d }~d }~ww xY w# t        $ r Y y w xY w# 	 t	        j"                  |       w # t        $ r Y w w xY wxY w)N)dirrS   z)' cache plugin while trying to write to 'z' : )modez%' cache plugin while trying to move 'z' to ')rI   rd   tempfilemkstemprL   rM   dirname_dumprW   rX   r7   rj   rH   r	   renamechmodr   unlink)r   r"   r'   rc   tmpfile_handletmpfile_pathr]   s          r   r(   zBaseFileCacheModule.set   sx    C--c2	'/'7'7BGGOOI<V'W$	Q

5,/`		,	26		,' W% Qcgcscs  vB  DL  MN  DO  !P  Q  QQ
 W% `gkgwgw  zF  HQ  S[  \]  S^  !_  `  ``
  		,' s   B4 ,1D E 4C>1C94E  9C>>E  E4E	E  	EE  	EE F"E87F8	FFFFc                   | j                   dk(  ry| j                  |      }	 t        j                  |      }t        j                         |j                  z
  | j                   k  ry|| j                  v r| j                  |= y# t        t
        f$ r]}|j                  t        j                  k(  rY d }~yt        j                  d| j                  d|dt        |             Y d }~yd }~ww xY w)Nr   FrS   $' cache plugin while trying to stat rT   T)r?   rd   rL   statrW   rX   errnoENOENTr7   rj   rH   r	   timest_mtimerI   )r   r"   rc   str]   s        r   rg   zBaseFileCacheModule.has_expired   s    ==A--c2		#B 99;$5$++C  ! 	ww%,,&]a]m]mox  {C  DE  {F  !G  H	s   B C/C*41C**C/c                    | j                  d      }t        |      }g }t        j                  | j                        D ]M  }|j                  d      s|j                  |      s&||d  }| j                  |      r=|j                  |       O |S )Nr`   .)rA   lenrL   listdirrB   
startswithrg   append)r   rb   prefix_lengthr+   ks        r   r+   zBaseFileCacheModule.keys   s     +FDOO, 	A||C V(<-.!A##A&A	 r   c                r   | j                  |      }|| j                  v ry| j                  |      ry	 t        j                  |       y# t
        t        f$ r]}|j                  t        j                  k(  rY d }~yt        j                  d| j                  d|dt        |             Y d }~y d }~ww xY w)NTFrS   ry   rT   )rd   rI   rg   rL   rz   rW   rX   r{   r|   r7   rj   rH   r	   )r   r"   rc   r]   s       r   r-   zBaseFileCacheModule.contains   s    --c2	$++C 	HGGI! 	Hww%,,&]a]m]mox  {C  DE  {F  !G  H  H		Hs   A
 
B6B1;1B11B6c                    	 | j                   |= 	 t        j                  | j	                  |             y # t        $ r Y 1w xY w# t
        t        f$ r Y y w xY wr   )rI   rD   rL   removerd   rW   rX   r!   s     r   r/   zBaseFileCacheModule.delete   s[    	C 	IId//45  		 ! 		s    5 $A 	A AAAc                ^    i | _         | j                         D ]  }| j                  |        y r   )rI   r+   r/   r!   s     r   r1   zBaseFileCacheModule.flush   s*    99; 	CKK	r   c                l    t               }| j                         D ]  }| j                  |      ||<    |S r   )dictr+   r#   )r   retr"   s      r   r3   zBaseFileCacheModule.copy   s4    f99; 	%Cxx}CH	%
r   c                     y)a  
        Read data from a filepath and return it as a value

        :arg filepath: The filepath to read from.
        :returns: The value stored in the filepath

        This method reads from the file on disk and takes care of any parsing
        and transformation of the data before returning it.  The value
        returned should be what Ansible would expect if it were uncached data.

        .. note:: Filehandles have advantages but calling code doesn't know
            whether this file is text or binary, should be decoded, or accessed via
            a library function.  Therefore the API uses a filepath and opens
            the file inside of the method.
        Nr    )r   filepaths     r   rh   zBaseFileCacheModule._load   s    " 	r   c                     y)z
        Write data to a filepath

        :arg value: The value to store
        :arg filepath: The filepath to store it at
        Nr    )r   r'   r   s      r   rr   zBaseFileCacheModule._dump  s     	r   )r4   r5   r6   __doc__r   r@   rJ   rd   r#   r(   rg   r+   r-   r/   r1   r3   r   rh   rr   r9   r:   s   @r   r<   r<   O   sm    )< $8,,"H"
  $  r   r<   c                      e Zd ZdZddZd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZddZd Zd Zd Zd Zd Zd Zd Zd Zy)CachePluginAdjudicatorzC
    Intermediary between a cache dictionary and a CacheModule
    c                    i | _         i | _        t        j                  |fi || _        | j                  st        d|z        || _        y )Nz%Unable to load the cache plugin (%s).)rI   
_retrievedr   r#   _pluginr   _plugin_name)r   rH   r   s      r   r   zCachePluginAdjudicator.__init__   sG    #''>v>||FTUU'r   c                X    | j                   | j                  k7  r| j                          y y r   )r   rI   	set_cacher*   s    r   update_cache_if_changedz.CachePluginAdjudicator.update_cache_if_changed*  s!    ??dkk)NN *r   c                    | j                   j                         D ]+  }| j                  j                  || j                   |          - t	        j
                  | j                         | _        y r   )rI   r+   r   r(   r3   deepcopyr   )r   top_level_cache_keys     r   r   z CachePluginAdjudicator.set_cache.  sT    #';;#3#3#5 	TLL0$++>Q2RS	T--4r   c                    | j                   j                         D ]*  }| j                   j                  |      | j                  |<   , y r   )r   r+   r#   rI   r!   s     r   load_whole_cachez'CachePluginAdjudicator.load_whole_cache3  s;    <<$$& 	5C#||//4DKK	5r   c                ,    t        | j                        S r   )r
   rI   r*   s    r   __repr__zCachePluginAdjudicator.__repr__7  s    t{{##r   c                4    t        | j                               S r   )iterr+   r*   s    r   __iter__zCachePluginAdjudicator.__iter__:  s    DIIK  r   c                4    t        | j                               S r   )r   r+   r*   s    r   __len__zCachePluginAdjudicator.__len__=  s    499;r   c                    d}t        || j                  v|| j                  v| j                  dk7  | j                  j                  |      g      rd}|S )NFmemoryT)allrI   r   r   r   r-   )r   r"   loads      r   _do_load_keyz#CachePluginAdjudicator._do_load_key@  sX    t{{"t&)LL!!#&	
  Dr   c                    | j                  |      rE	 | j                  j                  |      | j                  |<   | j                  |   | j                  |<   | j                  |   S # t
        $ r Y w xY wr   r   r   r#   rI   r   rD   r!   s     r   __getitem__z"CachePluginAdjudicator.__getitem__K  sn    S!8#'<<#3#3C#8C  (,{{3'7${{3	  s   (A& &	A21A2Nc                   | j                  |      rE	 | j                  j                  |      | j                  |<   | j                  |   | j                  |<   | j                  j                  ||      S # t
        $ r
}Y d }~*d }~ww xY wr   r   )r   r"   defaultr]   s       r   r#   zCachePluginAdjudicator.getU  su    S!8#'<<#3#3C#8C  (,{{3'7${{sG,,	  s   (A3 3	BBc                6    | j                   j                         S r   )rI   itemsr*   s    r   r   zCachePluginAdjudicator.items_  s    {{  ""r   c                6    | j                   j                         S r   )rI   valuesr*   s    r   r   zCachePluginAdjudicator.valuesb  s    {{!!##r   c                6    | j                   j                         S r   )rI   r+   r*   s    r   r+   zCachePluginAdjudicator.keyse  s    {{!!r   c                z    |r| j                   j                  ||d         S | j                   j                  |      S )Nr   )rI   pop)r   r"   r   s      r   r   zCachePluginAdjudicator.poph  s1    ;;??3Q00{{s##r   c                    | j                   |= y r   rI   r!   s     r   __delitem__z"CachePluginAdjudicator.__delitem__m  s    KKr   c                "    || j                   |<   y r   r   r&   s      r   __setitem__z"CachePluginAdjudicator.__setitem__p  s     Cr   c                F    | j                   j                          i | _        y r   )r   r1   rI   r*   s    r   r1   zCachePluginAdjudicator.flushs  s    r   c                :    | j                   j                  |       y r   )rI   update)r   r'   s     r   r   zCachePluginAdjudicator.updatew  s    5!r   )r   r   )r4   r5   r6   r   r   r   r   r   r   r   r   r   r   r#   r   r   r+   r   r   r   r1   r   r    r   r   r   r     sa    (5
5$! 	 -#$"$
!"r   r   )!
__future__r   r3   r{   rL   ro   r}   abcr   collections.abcr   ansibler   rE   ansible.errorsr    ansible.module_utils.common.filer   +ansible.module_utils.common.text.convertersr	   r
   ansible.pluginsr   ansible.plugins.loaderr   ansible.utils.collection_loaderr   ansible.utils.displayr   r7   r   r<   r   r    r   r   <module>r      sh   $ #   	    * " ' 9 I ) / > )
)#m #LJ/ JZ\"^ \"r   