
    Vh                        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mZmZ d dlmZ d dlmZ d Zd Zd	 Zd
 Zd Z G d de      Z G d de      Zy)    )annotationsN)partial)to_bytesto_text)AnsibleJSONEncoder)	iteritemsc                    t        j                  |      }| j                  dt        |      z         | j                  |       y)z;Write a length+newline-prefixed pickled object to a stream.s   %d
N)pickledumpswritelen)streamobjsrcs      O/home/dcms/DCMS/lib/python3.12/site-packages/ansible/module_utils/connection.pywrite_to_streamr   -   s1    
,,s
C
LL3s8#$
LL    c                h    t        j                  dt        |            }| j                  ||z         S )N!Q)structpackr   sendall)sdata
packed_lens      r   	send_datar   5   s*    T3t9-J99Z$&''r   c                j   d}t        d      }t        |      |k  r4| j                  |t        |      z
        }|sy ||z  }t        |      |k  r4t        j                  d|d |       d   }||d  }t        |      |k  r4| j                  |t        |      z
        }|sy ||z  }t        |      |k  r4|S )N    r   r   )r   r   recvr   unpack)r   
header_lenr   ddata_lens        r   	recv_datar%   :   s    JB<D
d)j
 FF:D	)*		 d)j
 
 }}T4#45a8H
D
d)h
FF8c$i'(		 d)h

 Kr   c                    t        | j                        }	 |j                  |      }d|dfS # t        $ r4}t	        |dd      }t	        |d|      }|dt        |d      fcY d }~S d }~ww xY w)Ncode   errr   surrogate_then_replaceerrorsr   )
Connection_socket_pathexec_commandConnectionErrorgetattrr   )modulecommand
connectionoutexcr'   messages          r   r/   r/   L   s{    F//0JK%%g.
 c2:	  KsFA&#uc*R1IJJJKs   - 	A*)A%A*%A*c                Z    t        t        j                               }d| |d}||f|d<   |S )Nz2.0)jsonrpcmethodidparams)struuiduuid4)method_argskwargsreqidreqs        r   request_builderrE   W   s0    

Ewe
<C6NCMJr   c                       e Zd Z fdZ xZS )r0   c                l    t         t        |   |       t        |      D ]  \  }}t	        | ||        y )N)superr0   __init__r   setattr)selfr7   rA   rB   kv	__class__s         r   rI   zConnectionError.__init__a   s6    ot-g6f% 	 DAqD!Q	 r   )__name__
__module____qualname__rI   __classcell__)rN   s   @r   r0   r0   _   s       r   r0   c                  *    e Zd Zd Zd Zd Zd Zd Zy)r-   c                ,    |t        d      || _        y )Nzsocket_path must be a value)AssertionErrorsocket_path)rK   rV   s     r   rI   zConnection.__init__i   s     !>??&r   c                    	 | j                   |   S # t        $ rP |j                  d      r&t        d| j                  j
                  d|d      t        | j                  |      cY S w xY w)N_'z' object has no attribute ')__dict__KeyError
startswithAttributeErrorrN   rO   r   __rpc__)rK   names     r   __getattr__zConnection.__getattr__n   s]    	/==&& 	/s#$DNNLcLcei%jkk4<<..	/s    AA*)A*c           
         t        |g|i |}|d   }t        j                  j                  | j                        st        d| j                  z        	 t        j                  |t        d      }	 | j                  |      }	 t        j                   |      }
|
d   |k7  rt        d      d|
v r&t/        j                   t1        |
d   d            |
d<   |
S # t        $ r}t        dt        |      z        d }~ww xY w# t        j                  $ r=}	t        d| j                  z  t        |	d      t        j                         	      d }	~	ww xY w# t"        $ r |j%                  d
      rt        dj'                  ||            |D cg c]  }t)        |       nc c}w c}t+        |      D cg c]  \  }}dj'                  ||       nc c}}w c}}z   }dj-                  |      }t        dj'                  |||            w xY w)Nr;   zsocket path %s does not exist or cannot be found. See Troubleshooting socket path issues in the Network Debug and Troubleshooting GuideT)clsvault_to_textzFailed to encode some variables as JSON for communication with the persistent connection helper. The original exception was: %szuunable to connect to socket %s. See Troubleshooting socket path issues in the Network Debug and Troubleshooting Guider*   r+   r)   	exception
set_optionz;Unable to decode JSON from response to {0}. Received '{1}'.z	{0}={1!r}z, z@Unable to decode JSON from response to {0}({1}). Received '{2}'.zinvalid json-rpc id receivedresult_typeresultsurrogateescape)rE   ospathexistsrV   r0   jsonr   r   	TypeErrorr   sendsocketerror	traceback
format_excloads
ValueErrorr\   formatreprr   joinr
   r   )rK   r_   rA   rB   rD   rC   r   r6   r5   eresponseargrL   rM   r<   s                  r   _exec_jsonrpczConnection._exec_jsonrpcv   s   d4T4V4D	ww~~d../!MOSO_O_` 
	::c'9ND	))D/C	zz#H D>U"!"@AAH$!'hx7IRc.d!eHXE  	!13:3<@ 	 || 	!ACGCSCSTA&>?9K_K_Ka 	  
	|,%QXXY]_bc  ,00Cd3i00YbciYj3kQUQRTUK4F4Fq!4L3k3kkFYYv&F!RYYZ^`fhkl 
	sT   C 4D E 	C?#C::C?E8EE9HF! H6G
;Hc                     | j                   |g|i |}d|v rF|j                  d      }|j                  d      xs |d   }|d   }t        t        |d      |      |d   S )	a  Executes the json-rpc and returns the output received
           from remote device.
           :name: rpc method to be executed over connection plugin that implements jsonrpc 2.0
           :args: Ordered list of params passed as arguments to rpc method
           :kwargs: Dict of valid key, value pairs passed as arguments to rpc method

           For usage refer the respective connection plugin docs.
        rq   r   r7   r'   r*   r+   )r'   rh   )r|   getr0   r   )rK   r_   rA   rB   rz   r)   msgr'   s           r   r^   zConnection.__rpc__   sv     &4%%d<T<V<h,,w'C''&/3S^Cv;D!'#6N"OVZ[[!!r   c                   	 t        j                   t         j                  t         j                        }|j                  | j                         t        |t        |             t        |      }|j                          t        |d      S # t         j                  $ rM}j                          t        d| j                  z  t        |d      t        j                               d }~ww xY w)Nzmunable to connect to socket %s. See the socket path issue category in Network Debug and Troubleshooting Guider*   r+   rd   surrogate_or_strict)rp   AF_UNIXSOCK_STREAMconnectrV   r   r   r%   rq   closer0   r   rr   rs   )rK   r   sfrz   ry   s        r   ro   zConnection.send   s    	v~~v/A/ABBJJt''(b(4.) }H 	
x(=>> || 	HHJ!:<@<L<LMA&>?9K_K_Ka 	s   A-B C,AC''C,N)rO   rP   rQ   rI   r`   r|   r^   ro    r   r   r-   r-   g   s    '
//b"(?r   r-   )
__future__r   rj   rm   r
   rp   r   rr   r>   	functoolsr   +ansible.module_utils.common.text.convertersr   r    ansible.module_utils.common.jsonr   ansible.module_utils.sixr   r   r   r%   r/   rE   	Exceptionr0   objectr-   r   r   r   <module>r      s^   : # 	        I ? .(
$ i  f? f?r   