
    Vhs%                    `   d dl mZ dZdZd dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZmZmZ d dlmZmZmZ d d	lmZmZ d d
lmZmZ d dlmZ d dlmZ d dlmZm Z  dZ!	 d dl"Z"dZ!dZ$	 d dl%Z%dZ$dZ&	 d dl'Z'dZ& e       Z)e!r8 e*e"d      r/ G d de"jV                        Z, ee"jZ                   e,             Z.nd Z.e!re"j^                  Z0e"jb                  Z2ne&re'j^                  Z0e'jd                  Z2d Z3 G d de      Z4y# e#$ r Y w xY w# e#$ r Y w xY w# e#$ r 	 d dl(Z'dZ&n# e#$ r Y nw xY wY w xY w)    )annotationsaP  
    name: toml
    version_added: "2.8"
    short_description: Uses a specific TOML file as an inventory source.
    description:
        - TOML based inventory format
        - File MUST have a valid '.toml' file extension
    notes:
        - >
          Requires one of the following python libraries: 'toml', 'tomli', or 'tomllib'
a  # fmt: toml
# Example 1
[all.vars]
has_java = false

[web]
children = [
    "apache",
    "nginx"
]
vars = { http_port = 8080, myvar = 23 }

[web.hosts]
host1 = {}
host2 = { ansible_port = 222 }

[apache.hosts]
tomcat1 = {}
tomcat2 = { myvar = 34 }
tomcat3 = { mysecret = "03#pa33w0rd" }

[nginx.hosts]
jenkins1 = {}

[nginx.vars]
has_java = true

# Example 2
[all.vars]
has_java = false

[web]
children = [
    "apache",
    "nginx"
]

[web.vars]
http_port = 8080
myvar = 23

[web.hosts.host1]
[web.hosts.host2]
ansible_port = 222

[apache.hosts.tomcat1]

[apache.hosts.tomcat2]
myvar = 34

[apache.hosts.tomcat3]
mysecret = "03#pa33w0rd"

[nginx.hosts.jenkins1]

[nginx.vars]
has_java = true

# Example 3
[ungrouped.hosts]
host1 = {}
host2 = { ansible_host = "127.0.0.1", ansible_port = 44 }
host3 = { ansible_host = "127.0.0.1", ansible_port = 45 }

[g1.hosts]
host4 = {}

[g2.hosts]
host4 = {}
N)MutableMappingMutableSequence)partial)AnsibleFileNotFoundAnsibleParserErrorAnsibleRuntimeError)to_bytes	to_nativeto_text)string_types	text_type)AnsibleSequenceAnsibleUnicode)BaseFileInventoryPlugin)Display)AnsibleUnsafeBytesAnsibleUnsafeTextFTTomlEncoderc                       e Zd Z fdZ xZS )AnsibleTomlEncoderc                z   t        t        | 
  |i | | j                  j	                  t
        | j                  j                  t              t        | j                  j                  t              t        | j                  j                  t              t        | j                  j                  t              i       y N)superr   __init__
dump_funcsupdater   getlistr   strr   r   )selfargskwargs	__class__s      N/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/inventory/toml.pyr   zAnsibleTomlEncoder.__init__   s    $d4dEfEOO""!4!4T!: 3 3C 8"DOO$7$7$<!4??#6#6s#;	$     )__name__
__module____qualname__r   __classcell__r$   s   @r%   r   r      s    	 	r&   r   )encoderc                    t         rt        j                  t        |             S t        rt        j                  t        |             S t        d      )NzTThe python "toml" or "tomli-w" library is required when using the TOML output format)HAS_TOMLtomldumpsconvert_yaml_objects_to_native
HAS_TOMLIWtomli_wr	   )datas    r%   
toml_dumpsr5      sB    ::<TBCC==!?!EFF!b
 	
r&   c                    t        | t              r t        d | j                         D              S t        | t              r| D cg c]  }t	        |       c}S t        | t
              rt        |       S | S c c}w )a  Older versions of the ``toml`` python library, and tomllib, don't have
    a pluggable way to tell the encoder about custom types, so we need to
    ensure objects that we pass are native types.

    Used with:
      - ``toml<0.10.0`` where ``toml.TomlEncoder`` is missing
      - ``tomli`` or ``tomllib``

    This function recurses an object and ensures we cast any of the types from
    ``ansible.parsing.yaml.objects`` into their native types, effectively cleansing
    the data before we hand it over to the toml library.

    This function doesn't directly check for the types from ``ansible.parsing.yaml.objects``
    but instead checks for the types those objects inherit from, to offer more flexibility.
    c              3  <   K   | ]  \  }}|t        |      f  y wr   )r1   ).0kvs      r%   	<genexpr>z1convert_yaml_objects_to_native.<locals>.<genexpr>   s     Stq!Q6q9:Ss   )
isinstancedictitemsr   r1   r   )objr:   s     r%   r1   r1      se      #tSsyy{SSS	C	;>?a.q1??	C	#~
	 @s   A7c                  :     e Zd ZdZd Zd Zd fd	Z fdZ xZS )InventoryModuler/   c           	        |/t        |t              s| j                  j                  d|z         y | j                  j                  |      }|y |j                         D ]f  \  }}|dk(  rat        |t              st        d|dt        |      d      |j                         D ]"  \  }}| j                  j                  |||       $ m|dk(  rat        |t              st        d|dt        |      d      |D ]0  }| j                  |i        | j                  j                  ||       2 |d	k(  rmt        |t              st        d
|dt        |      d      |j                         D ]-  \  }}| j                  |      \  }	}
| j                  |	|||
       / E| j                  j                  d|d|d       i y )Nz5Skipping '%s' as this is not a valid group definitionvarszInvalid "vars" entry for "z!" group, requires a dict, found "z
" instead.childrenzInvalid "children" entry for "z!" group, requires a list, found "hostszInvalid "hosts" entry for "zSkipping unexpected key "z" in group "z0", only "vars", "children" and "hosts" are valid)r<   r   displaywarning	inventory	add_groupr>   r   typeset_variabler   _parse_group	add_child_expand_hostpattern_populate_host_vars)r!   group
group_datakeyr4   varvaluesubgrouphost_patternrE   ports              r%   rL   zInventoryModule._parse_group   s   !*Z*PLL  !X[`!`a((/#))+ !	ICf}!$7,T
,  #'**, CJCNN//sEBC 
"!$8,T
,  !% >H%%h3NN,,UH=> !$7,T
,  ,0::< H'L%"&":":<"HKE4,,UE5$GH $$%!=!	r&   c           	     \   |rt        |t              st        dt        |      z        t	        | j
                  j                  |            }| j
                  j                  |      st        d|      	 | j
                  j                  |      \  }}t        t        |d            S # t        $ r!}t        d|dt        |      |      d }~wt        t        f$ r!}t        d	|d
t        |      |      d }~wt        $ r!}t        d|d
t        |      |      d }~ww xY w)NzInvalid filename: '%s'z Unable to retrieve file contents)	file_namesurrogate_or_strict)errorszTOML file (z) is invalid: )orig_excz1An error occurred while trying to read the file 'z': z5An unexpected error occurred while parsing the file ')r<   r   r   r   r
   loader	path_dwimpath_existsr   _get_file_contents
toml_loadsr   TOMLDecodeErrorIOErrorOSError	Exception)r!   rY   b_file_nameb_dataprivatees         r%   
_load_filezInventoryModule._load_file   s   
9l C$%=	)@T%TUUt{{44Y?@{{&&{3%&HT]^^	 $ > >y IVWgf5JKLL 	$3<ilK  ! 	$NWYbcdYef   	$R[]fgh]ij 	s0   73B+ +	D+4CD+"C>>D+
D&&D+c                d   t         st        st        d      t        t        |   |||       | j                          	 | j                  |      }|st        d      |j                  d      rt        d      |D ]  }| j                  |||           y# t        $ r}t        |      d}~ww xY w)z parses the inventory file zHThe TOML inventory plugin requires the python "toml", or "tomli" libraryNzParsed empty TOML filepluginz2Plugin configuration TOML file, not TOML inventory)HAS_TOMLLIBr.   r   r   rA   parseset_optionsrj   re   r   rL   )	r!   rH   r]   pathcacher4   ri   
group_namer$   s	           r%   rn   zInventoryModule.parse  s    8 %Z  	ot*9fdC	(??4(D $%=>>XXh$%YZZ 	<Jj$z*:;	<  	($Q''	(s   B 	B/B**B/c                |    t         t        |   |      r(t        j                  j                  |      \  }}|dk(  ryy)Nz.tomlTF)r   rA   verify_fileosrp   splitext)r!   rp   rY   extr$   s       r%   rt   zInventoryModule.verify_file$  s7    $3D9WW--d3NIsg~r&   )T)	r'   r(   r)   NAMErL   rj   rn   rt   r*   r+   s   @r%   rA   rA      s"    D*X6<2 r&   rA   )5
__future__r   DOCUMENTATIONEXAMPLESru   typingtcollections.abcr   r   	functoolsr   ansible.errorsr   r   r	   +ansible.module_utils.common.text.convertersr
   r   r   ansible.module_utils.sixr   r   ansible.parsing.yaml.objectsr   r   ansible.plugins.inventoryr   ansible.utils.displayr   ansible.utils.unsafe_proxyr   r   r.   r/   ImportErrorr2   r3   rm   tomllibtomlirF   hasattrr   r   r0   r5   loadsra   TomlDecodeErrorrb   r1   rA    r&   r%   <module>r      s\   #
EN 
  ;  W W T T < H = ) L	H 
	J K ) m,	T-- 	 -?-ABJ
 J**OJ--O4h- hm  		  		   	sZ   C< %D .D <DDDDD-DD-D'$D-&D''D-,D-