
    Vhi-                         d Z ddlmZmZmZ eZddl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 ddlmZ ddddggiiddggddggdZ G d de      Zy)z&
The action plugin file for cli_parse
    )absolute_importdivisionprint_functionN)import_module)AnsibleActionFail)	to_nativeto_text)
Connection)ConnectionError)
ActionBase)check_argspec)DOCUMENTATIONparsermutually_exclusivecommandtemplate_pathtext)argument_specrequired_one_ofr   c                   t     e Zd Zd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ddZ xZS )ActionModulezaction moduleT	CliParserc                 d    t        t        | 
  |i | d | _        d | _        i | _        d | _        y )N)superr   __init__	_playhost_parser_name_result
_task_vars)selfargskwargs	__class__s      j/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/ansible/utils/plugins/action/cli_parse.pyr   zActionModule.__init__,   s4    lD*D;F;     c                 t    dj                  | j                  |      }| j                  j                  |       y)zdOutput text using ansible's display

        :param msg: The message
        :type msg: str
        z<{phost}> [cli_parse] {msg})phostmsgN)formatr   _displayvvvvr    r(   s     r$   _debugzActionModule._debug3   s/     ,22S2Q3r%   c                 d    |j                  d| j                  j                        }t        |      )zxReplace the AnsibleModule fai_json here

        :param msg: The message for the failure
        :type msg: str
        z
(basic.py))replace_taskactionr   r,   s     r$   
_fail_jsonzActionModule._fail_json<   s)     kk,

(9(9:$$r%   c                 x   g }| j                   j                  j                  d      j                  d      }t        |j	                  d            dk7  rd}|j                  |       | j                   j                  j                  d      r|dvr{| j                   j                  j                  d      j                  d      sG| j                   j                  j                  d      j                  d	      sd
}|j                  |       |r.d| j                  d<   dj                  |      | j                  d<   yy)zkCheck additional requirements for the argspec
        that cannot be covered using stnd techniques
        r   name.   zBParser name should be provided as a full name including collectionr   )zansible.utils.jsonzansible.utils.xmlr   r   zUEither parser/command or parser/template_path needs to be provided when parsing text.Tfailed r(   N)r0   r!   getlensplitappendr   join)r    errorsrequested_parserr(   s       r$   _extended_check_argspecz$ActionModule._extended_check_argspecE   s     ::??..x8<<VD%%c*+q0VCMM#::??v&+; D
 ,

 

##H-11)<::??&&x044_Emc"%)DLL""%((6"2DLL r%   c                 
   | j                   j                  j                  d      j                  d      }t        t	        g d|j                  d                  } dj                  di |}	 t        t        |      | j                        } || j                   j                  || j                        }|S # t        $ rA}d| j                  d<   d	j                  t        |      
      | j                  d<   Y d}~yd}~ww xY w)zLoad a parser from the fs

        :param task_vars: The vars provided when the task was run
        :type task_vars: dict
        :return: An instance of class CliParser
        :rtype: CliParser
        r   r4   )corgcnamepluginr5   zQansible_collections.{corg}.{cname}.plugins.sub_plugins.cli_parser.{plugin}_parser)	task_args	task_varsdebugTr7   zError loading parser: {err})errr(   N )r0   r!   r9   dictzipr;   r)   getattrr   PARSER_CLS_NAMEr-   	Exceptionr   r   )r    rF   r?   cref	parserlib	parserclsr   excs           r$   _load_parserzActionModule._load_parser]   s      ::??..x8<<VDC35E5K5KC5PQRngnn 

		i 8$:N:NOI**//#kkF
 M 	%)DLL""?"F"F9UX>"F"ZDLL	s   .A	B8 8	D7C==Dc                 P   | j                   j                  j                  d      r| j                   j                  j                  d      j                  d      sL| j                   j                  j                  d      | j                   j                  j                  d      d<   yyy)zDSet the /parser/command in the task args based on /command if neededr   r   N)r0   r!   r9   r    s    r$   _set_parser_commandz ActionModule._set_parser_commandx   st    ::??y)::??&&x044Y?;?::??;N;Ny;Y

##H-i8 @ *r%   c                     | j                   j                  d      r'| j                   d   | j                  j                  d<   yy)z7Set the /text in the task_args based on the command runstdoutr   N)r   r9   r0   r!   rU   s    r$   	_set_textzActionModule._set_text~   s4    <<H%&*ll8&<DJJOOF# &r%   c                    ddg}d}|D ]  }| j                   j                  |      s|dk(  r^| j                   j                  |d      j                  d      d   }| j                  dj	                  |j                                      | j                   j                  |      }| j                  dj	                  |j                         |	              |j                         S )
zeExtract an os str from the task's vars

        :return: A short OS name
        :rtype: str
        ansible_distributionansible_network_os r5   z/OS set to {os}, derived from ansible_network_os)oszOS set to {os}, using {key})r_   key)r   r9   r;   r-   r)   lower)r    os_varsoper_syshvars       r$   _os_from_task_varszActionModule._os_from_task_vars   s     *+?@ 	eD""4(//#224<BB3GKHKKIPP'~~/ Q   $2248HKK = D DHX^b D cd	e ~~r%   c                 :   | j                   j                  j                  d      j                  d      s| j                   j                  j                  d      j                  d      r5| j                   j                  j                  d      j                  d      }n| j                         }| j                   j                  j                  d      j                  d      j	                  dd      }dj                  |||      }| j                  d	|      }nF| j                   j                  j                  d      j                  d      }| j                  d	|      }| j                  d
j                  |             || j                   j                  d   d<   y)zUpdate the template_path in the task args
        If not provided, generate template name using os and command

        :param template_extension: The parser specific template extension
        :type template extension: str
        r   r   r_   r   r8   _z{os}_{cmd}.{ext})r_   cmdext	templatesz.template_path in task args updated to {source})sourceN)r0   r!   r9   re   r/   r)   _find_needler-   )r    template_extensionrc   cmd_as_fnamefnamerk   s         r$   _update_template_pathz"ActionModule._update_template_path   s;    zz""8,00Azz""8,006::??..x8<<TB224::??..x8<<YGOOPSUXYL&--|Qc-dE&&{E:FZZ__((266GF&&{F;FDKKSYKZ[5;

!/2r%   c           	         d}| j                   j                  j                  d      j                  d      }|r5	 t        |d      5 }	 t	        |j                         d      }	 ddd       |S |S # t        $ r t        d      w xY w# 1 sw Y   |S xY w# t        $ r*}t        dj                  |t        |      	            d}~ww xY w)
zqRetrieve the contents of the parser template

        :return: The parser's contents
        :rtype: str
        Nr   r   rbsurrogate_or_strict)r>   z+Template source files must be utf-8 encodedz/Failed to open template '{tpath}'. Error: {err})tpathrH   )r0   r!   r9   openr	   readUnicodeErrorr   FileNotFoundErrorr)   r   )r    template_contentsr   file_handlerrR   s        r$   _get_template_contentsz#ActionModule._get_template_contents   s     !

++H599/J-. _,_,3(--/#8-)_ !    ( _/0]^^__ !  % 'ELL+%cN M  sG   B BA0$B 0BBBB B 	C%CCc                 t    | j                   j                  dd       | j                   j                  dd       y)zIn the case of an error, remove stdout and stdout_lines
        this allows for easier visibility of the error message.
        In the case of an actual command error, it will be thrown
        in the module
        rX   Nstdout_lines)r   poprU   s    r$   _prune_resultzActionModule._prune_result   s,     	4(.r%   c                 N   | j                   j                  j                  d      }|r| j                  j                  }|rKt        |      }	 |j                  |      }|| j                  d<   |j                         | j                  d<   y| j                  |	      }|d
   r!d| j                  d<   |d   | j                  d<   |d   | j                  d<   |d   | j                  d<   yy# t        $ r2}d| j                  d<   t        |      g| j                  d<   Y d}~yd}~ww xY w)zRun a command on the host
        If socket_path exists, assume it's a network device
        else, run a low level command
        r   )r   rX   r}   Tr7   r(   N)rh   rcstderr)r0   r!   r9   _connectionsocket_pathr
   r   
splitlinesAnsibleConnectionErrorr	   _low_level_execute_command)r    r   r   
connectionresponserR   results          r$   _run_commandzActionModule._run_command   s   
 **//%%i0**66K'4
9)~~g~>H-5DLL*3;3F3F3HDLL0
 88W8E$<-1DLL**0*:DLL')/)9X&/5n/E^,#  . 9-1DLL*+23<.DLL'9s   >C) )	D$2(DD$c           	         t        t        dfdt        i| j                  j                  \  }}}|s|S | j                          | j                  j                  d      r| j                  S || _        |j                  d      | _	        | j                  j                  j                  d      j                  d      | _
        | j                          | j                  j                  d      r| j                  S | j                          | j                          | j                  |      }| j                  j                  d      r| j                          | j                  S t!        |dd      r| j#                  |j$                         t!        |d	d      d
u r| j'                         }nd}	 |j)                  |      }t+        j,                  t+        j.                  |            }|j                  d      rL| j                          | j                  j9                  d
dj;                  |d         d       | j                  S |d   | j                  d<   | j                  j                  j                  d      }
|
r|
|d   i| j                  d<   | j                  S # t0        $ r4}	t3        dj5                  | j                  t7        |	                  d}	~	ww xY w)a  The std execution entry pt for an action plugin

        :param tmp: no longer used
        :type tmp: none
        :param task_vars: The vars provided when the task is run
        :type task_vars: dict
        :return: The results from the parser
        :rtype: dict
        zcli_parse moduleschema_conditionalsr7   inventory_hostnamer   r4   DEFAULT_TEMPLATE_EXTENSIONFPROVIDE_TEMPLATE_CONTENTSTN)ry   z8Unhandled exception from parser '{parser}'. Error: {err})r   rH   r>   r8   )r7   r(   parsedset_factansible_facts)r   r   ARGSPEC_CONDITIONALSr0   r!   r@   r   r9   r   r   r   r   rV   rY   rS   r   rL   rp   r   r{   parsejsonloadsdumpsrN   r   r)   r   updater=   )r    tmprF   validargspec_resultupdated_paramsr   ry   r   rR   r   s              r$   runzActionModule.run   sy    1>1
 !51
 jjoo	1
-~~ !!$$&<<H%<<#"';< JJOO//9==fE<<H%<<  """9-<<H% << 67?&&v'H'HI 66>$F $ ; ; = $	\\4E\FF ZZ

6 23F ::h LL4AQ8R ST ||	 &,H%5DLL"zz**:6H196(;K0L_-||!  	#JQQ,,!# R  	s   +:J 	K$/KK)NN)__name__
__module____qualname____doc___requires_connectionrM   r   r-   r2   r@   rS   rV   rY   re   rp   r{   r   r   r   __classcell__)r#   s   @r$   r   r   %   sU    !O %306Z=
 ,<*!6/F2Hr%   r   )r   
__future__r   r   r   type__metaclass__r   	importlibr   ansible.errorsr   ansible.module_utils._textr   r	   ansible.module_utils.connectionr
   r   r   ansible.plugins.actionr   Nansible_collections.ansible.utils.plugins.module_utils.common.argspec_validater   ;ansible_collections.ansible.utils.plugins.modules.cli_parser   r   r   rI   r%   r$   <module>r      s~    A @   # , 9 6 U - V !5O8T7U VW"F+,%v./ N: Nr%   