
    Vh                    j    d dl mZ d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mZ  G d d	e
      Zy)
    )annotationsaB  
    name: tree
    type: notification
    requirements:
      - invoked in the command line
    short_description: Save host events to files
    version_added: "2.0"
    options:
        directory:
            version_added: '2.11'
            description: directory that will contain the per host JSON files. Also set by the C(--tree) option when using adhoc.
            ini:
                - section: callback_tree
                  key: directory
            env:
                - name: ANSIBLE_CALLBACK_TREE_DIR
            default: "~/.ansible/tree"
            type: path
    description:
        - "This callback is used by the Ansible (adhoc) command line option C(-t|--tree)."
        - This produces a JSON dump of events in a directory, a file for each host, the directory used MUST be passed as a command line option.
N)TREE_DIR)to_bytesto_text)CallbackBase)makedirs_safeunfrackpathc                  T     e Zd ZdZdZdZdZdZd fd	Zd Z	d Z
d	 Zdd
Zd Z xZS )CallbackModulez]
    This callback puts results into a host specific file in a directory in json format.
    g       @	aggregatetreeTc                    t         t        |   |||       t        rt	        t              | _        y| j                  d      | _        y)z override to set self.tree )	task_keysvar_optionsdirect	directoryN)superr   set_optionsr   r	   r   
get_option)selfr   r   r   	__class__s       M/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/callback/tree.pyr   zCallbackModule.set_options0   s<     	nd/)Q\ek/l#H-DI4DI    c           	     P   t        |      }	 t        | j                         	 t        t        j                  j                  | j                  |            }t        |d      5 }|j                  |       ddd       y# t        t        f$ rG}| j
                  j                  dt        | j                        dt        |             Y d}~d}~ww xY w# 1 sw Y   yxY w# t        t        f$ r4}| j
                  j                  d|dt        |             Y d}~yd}~ww xY w)z' write something into treedir/hostname z5Unable to access or create the configured directory (z): Nzwb+zUnable to write to z	's file: )r   r   r   OSErrorIOError_displaywarningr   ospathjoinopenwrite)r   hostnamebufer    fds         r   write_tree_filezCallbackModule.write_tree_file;   s    sm	F$))$	`BGGLLH=>DdE" b  ! 	FMM!!elmqmvmvew  zA  BC  zD  #E  F  F	F
 ! 	`MM!!SZ[\S]"^__	`sL   A= ?C" "C4C" =C=CCCC" C" "D%1*D  D%c                    | j                  |j                  j                         | j                  |j                               y N)r(   _hostget_name_dump_results_resultr   results     r   result_to_treezCallbackModule.result_to_treeK   s.    V\\224d6H6H6XYr   c                &    | j                  |       y r*   r1   r/   s     r   v2_runner_on_okzCallbackModule.v2_runner_on_okN       F#r   c                &    | j                  |       y r*   r3   )r   r0   ignore_errorss      r   v2_runner_on_failedz"CallbackModule.v2_runner_on_failedQ   r5   r   c                &    | j                  |       y r*   r3   r/   s     r   v2_runner_on_unreachablez'CallbackModule.v2_runner_on_unreachableT   r5   r   )NNN)F)__name__
__module____qualname____doc__CALLBACK_VERSIONCALLBACK_TYPECALLBACK_NAMECALLBACK_NEEDS_ENABLEDr   r(   r1   r4   r8   r:   __classcell__)r   s   @r   r   r   &   s@     MM!	5` Z$$$r   r   )
__future__r   DOCUMENTATIONr   ansible.constantsr   +ansible.module_utils.common.text.convertersr   r   ansible.plugins.callbackr   ansible.utils.pathr   r	   r    r   r   <module>rK      s.   
 #. 
 & I 1 9/$\ /$r   