
    Vh                         d dl mZ dZdZd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mZ d d
lmZmZ d dlmZ d Z G d de      Zy)    )annotationsa  
    name: ini
    author: Yannig Perre (!UNKNOWN) <yannig.perre(at)gmail.com>
    version_added: "2.0"
    short_description: read data from an ini file
    description:
      - "The ini lookup reads the contents of a file in INI format C(key1=value1).
        This plugin retrieves the value on the right side after the equal sign C('=') of a given section C([section])."
      - "You can also read a property file which - in this case - does not contain section."
    options:
      _terms:
        description: The key(s) to look up.
        required: True
      type:
        description: Type of the file. 'properties' refers to the Java properties files.
        default: 'ini'
        choices: ['ini', 'properties']
      file:
        description: Name of the file to load.
        default: 'ansible.ini'
      section:
        default: global
        description: Section where to lookup the key.
      re:
        default: False
        type: boolean
        description: Flag to indicate if the key supplied is a regexp.
      encoding:
        default: utf-8
        description:  Text encoding to use.
      default:
        description: Return value if the key is not in the ini file.
        default: ''
      case_sensitive:
        description:
          Whether key names read from O(file) should be case sensitive. This prevents
          duplicate key errors if keys only differ in case.
        default: False
        version_added: '2.12'
      allow_no_value:
        description:
        - Read an ini file which contains key without value and without '=' symbol.
        type: bool
        default: False
        aliases: ['allow_none']
        version_added: '2.12'
      interpolation:
        description:
          Allows for interpolation of values, see https://docs.python.org/3/library/configparser.html#configparser.BasicInterpolation
        type: bool
        default: True
        version_added: '2.18'
    seealso:
      - ref: playbook_task_paths
        description: Search paths used for relative files.
a  
- ansible.builtin.debug: msg="User in integration is {{ lookup('ansible.builtin.ini', 'user', section='integration', file='users.ini') }}"

- ansible.builtin.debug: msg="User in production  is {{ lookup('ansible.builtin.ini', 'user', section='production',  file='users.ini') }}"

- ansible.builtin.debug: msg="user.name is {{ lookup('ansible.builtin.ini', 'user.name', type='properties', file='user.properties') }}"

- ansible.builtin.debug:
    msg: "{{ item }}"
  loop: "{{ q('ansible.builtin.ini', '.*', section='section1', file='test.ini', re=True) }}"

- name: Read an ini file with allow_no_value
  ansible.builtin.debug:
    msg: "{{ lookup('ansible.builtin.ini', 'user', file='mysql.ini', section='mysqld', allow_no_value=True) }}"
z`
_raw:
  description:
    - value(s) of the key(s) in the ini file
  type: list
  elements: str
N)StringIO)defaultdict)MutableSequence)AnsibleLookupErrorAnsibleOptionsError)to_text	to_native)
LookupBasec                @   |j                         }t        d       }g }d}t        | j                               D ]K  \  }}d|v r|D ]  }d|z  |v s|} |dk(  s||   s|||<   |j	                  |       <||xx   d|z   z  cc<   M |D 	cg c]  }	||	   	 c}	S c c}	w )z.Safely split parameter term to preserve spacesc                      y)N  r       J/home/dcms/DCMS/lib/python3.12/site-packages/ansible/plugins/lookup/ini.py<lambda>z_parse_params.<locals>.<lambda>i   s    r   key=z%s=r    )keysr   	enumeratesplitappend)
term	paramvals
valid_keysparamsr   thiskeyidpphrasekxs
             r   _parse_paramsr#   d   s     !J$F DG . ,V &=  AI&(G 
 !86'?$F7OKK  7OsV|+O,"  $$!F1I$$$s   Bc                      e Zd Zd ZddZy)LookupModulec                   |rE| j                   j                  |      D cg c]  \  }}t        j                  ||      s|  c}}S d }	 | j                   j	                  ||      }|S c c}}w # t
        j                  $ r |cY S w xY wN)cpitemsrematchgetconfigparserNoOptionError)selfr   sectiondflt	is_regexpr!   vvalues           r   	get_valuezLookupModule.get_value   s{    "&''--"8M$!QBHHS!<LAMM	GGKK-E  N
 )) 	K	s   A) A)A/ /BBNc                2   | j                  ||       | j                         }t        j                  |j	                  d|j	                  d            |j	                  d      rt        j
                         nd       | _        |d   rt        | j                  _        g }|D ]  }|}d|v sd|j                         v r| j                          t        ||      }	 d	}	d	}
|D ]K  }d|v r;|j                  d      \  }}||vrt        d
|z        | j                  ||       d}
B||k(  sH|}d}	M |
r| j                         }|	st!        d|z        | j#                  |d|d         }t%               }|d   dk(  r|j'                  d       d|d<   | j(                  j+                  |      \  }}t-        |d|d         }|j'                  |       |j/                  dt0        j2                         	 | j                  j5                  |       	 | j;                  ||d   |d   |d         }|t?        |t@              r|D ]  }|jC                  |        |jC                  |        |S # t        $ r$}t        dd|dt        |      |      d }~ww xY w# t        j6                  $ r-}t        dj9                  |d   t        |                  d }~ww xY w# t        j<                  $ r# t        d j9                  |d   |d   !            w xY w)"N)var_optionsdirectallow_no_value
allow_noneinterpolation)r9   r;   case_sensitiver   r   Fz%s is not a valid option.TzCould not use 'z' from 'z': )orig_exczMNo key to lookup was provided as first term with in string inline options: %sfilesfiletype
propertiesz[java_properties]
java_propertiesr0   surrogate_or_strictencoding)errorsrD   r   z%Duplicate option in '{file}': {error})r?   errordefaultr*   z No section '{section}' in {file})r0   r?   )"set_optionsget_optionsr-   ConfigParserr,   BasicInterpolationr(   r
   optionxformstrip_deprecate_inline_kvr#   r   r   
set_option
ValueErrorr   find_file_in_search_pathr   write_loader_get_file_contentsr	   seekosSEEK_SET	read_fileDuplicateOptionErrorformatr5   NoSectionError
isinstancer   r   )r/   terms	variableskwargsr   retr   r   r   updated_keyupdated_optionsparamnamer4   epathconfigcontents	show_datadoevarr3   s                         r   runzLookupModule.run   se   Yv>$$&	++$==)99==;VW?H}}_?],99;cg
 %&"+DGG =	$DCd{cTZZ\1))+&tY7}"'K&+O!' 
/%<*/++c*:KD%#94&89TW[9[&\ \ OOD%8.2O D["'C*.K
/ '$($4$4$6	 #-.}  AE  /E  F  F
 00GYvEVWD ZF L034'8	)$ #',,"A"A$"GHix0EPYZdPefHLL"KK2;;'G!!&)JnnS)I*>	)@TV_`dVef c?3  &

1& JJsO{=	$| 
O " },SXZ`bklmbn-oz{||}4  44 G()P)W)W]fgm]nv  AD  wE)W  *F  G  GG
  .. J()K)R)R[den[ov  AG  wH)R  *I  J  JJsC   AI-)I-0JK -	J6JJK0(KK 6Lr'   )__name__
__module____qualname__r5   rl   r   r   r   r%   r%      s    
Kr   r%   )
__future__r   DOCUMENTATIONEXAMPLESRETURNr-   rV   r*   ior   collectionsr   collections.abcr   ansible.errorsr   r   +ansible.module_utils.common.text.convertersr	   r
   ansible.plugins.lookupr   r#   r%   r   r   r   <module>rz      sP    #7r 
  	 	  # + B J -%<Y: Yr   