
    Vh                     |    d dl mZmZmZ eZdZdZdZd dl	m
Z
 d dlmZ d dlmZmZmZmZmZ d Zed	k(  r e        y
y
)    )absolute_importdivisionprint_functiona  
module: read_csv
short_description: Read a CSV file
description:
  - Read a CSV file and return a list or a dictionary, containing one dictionary per row.
author:
  - Dag Wieers (@dagwieers)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  path:
    description:
      - The CSV filename to read data from.
    type: path
    required: true
    aliases: [filename]
  key:
    description:
      - The column name used as a key for the resulting dictionary.
      - If O(key) is unset, the module returns a list of dictionaries, where each dictionary is a row in the CSV file.
    type: str
  dialect:
    description:
      - The CSV dialect to use when parsing the CSV file.
      - Possible values include V(excel), V(excel-tab) or V(unix).
    type: str
    default: excel
  fieldnames:
    description:
      - A list of field names for every column.
      - This is needed if the CSV does not have a header.
    type: list
    elements: str
  unique:
    description:
      - Whether the O(key) used is expected to be unique.
    type: bool
    default: true
  delimiter:
    description:
      - A one-character string used to separate fields.
      - When using this parameter, you change the default value used by O(dialect).
      - The default value depends on the dialect used.
    type: str
  skipinitialspace:
    description:
      - Whether to ignore any whitespaces immediately following the delimiter.
      - When using this parameter, you change the default value used by O(dialect).
      - The default value depends on the dialect used.
    type: bool
  strict:
    description:
      - Whether to raise an exception on bad CSV input.
      - When using this parameter, you change the default value used by O(dialect).
      - The default value depends on the dialect used.
    type: bool
seealso:
  - plugin: ansible.builtin.csvfile
    plugin_type: lookup
    description: Can be used to do selective lookups in CSV files from Jinja.
a*  
# Example CSV file with header
#
#   name,uid,gid
#   dag,500,500
#   jeroen,501,500

# Read a CSV file and access user 'dag'
- name: Read users from CSV file and return a dictionary
  community.general.read_csv:
    path: users.csv
    key: name
  register: users
  delegate_to: localhost

- ansible.builtin.debug:
    msg: 'User {{ users.dict.dag.name }} has UID {{ users.dict.dag.uid }} and GID {{ users.dict.dag.gid }}'

# Read a CSV file and access the first item
- name: Read users from CSV file and return a list
  community.general.read_csv:
    path: users.csv
  register: users
  delegate_to: localhost

- ansible.builtin.debug:
    msg: 'User {{ users.list.1.name }} has UID {{ users.list.1.uid }} and GID {{ users.list.1.gid }}'

# Example CSV file without header and semi-colon delimiter
#
#   dag;500;500
#   jeroen;501;500

# Read a CSV file without headers
- name: Read users from CSV file and return a list
  community.general.read_csv:
    path: users.csv
    fieldnames: name,uid,gid
    delimiter: ';'
  register: users
  delegate_to: localhost
a  
dict:
  description: The CSV content as a dictionary.
  returned: success
  type: dict
  sample:
    dag:
      name: dag
      uid: 500
      gid: 500
    jeroen:
      name: jeroen
      uid: 501
      gid: 500
list:
  description: The CSV content as a list.
  returned: success
  type: list
  sample:
    - name: dag
      uid: 500
      gid: 500
    - name: jeroen
      uid: 501
      gid: 500
)AnsibleModule)	to_native)initialize_dialectread_csvCSVErrorDialectNotAvailableErrorCustomDialectFailureErrorc                     t        t        t        dddg      t        dd      t        dd	      t        d
d      t        dd      t        d      t        d      t        d            d      } | j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   }| j                  d   | j                  d   | j                  d   d}	 t        |fi |}	 t        |d      5 }|j                         }	d d d        t        	||      }
|r?||
j                  vr1| j                  d|ddj                  |
j                               t               }t               }|	 |
D ]  }|j!                  |        n6	 |
D ]/  }|r#||   |v r| j                  d|d||   d        ||||   <   1 	 | j%                  ||!       y # t        t
        f$ r&}| j                  t        |             Y d }~d }~ww xY w# 1 sw Y   
xY w# t        t        f$ r)}| j                  dt        |      z         Y d }~@d }~ww xY w# t"        $ r(}| j                  dt        |      z         Y d }~d }~ww xY w# t"        $ r(}| j                  dt        |      z         Y d }~d }~ww xY w)"NpathTfilename)typerequiredaliasesstrexcel)r   defaultF)r   no_loglist)r   elementsbool)r   )r   dialectkey
fieldnamesunique	delimiterskipinitialspacestrict)argument_specsupports_check_moder   r   r   r   r   r   r    )r   r   r    )msgrbzUnable to open file: %szKey 'z*' was not found in the CSV header fields: z, zUnable to process file: %sz' is not unique for value '')dictr   )r   r&   paramsr   r   r   	fail_jsonr   openreadIOErrorOSErrorr	   r   joinr   appendr
   	exit_json)moduler   r   r   r   r   dialect_paramsefdatareader	data_dict	data_listrows                 n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/read_csv.pymainr:      s   6D:,GeW5%.%8VT2&!v.V$	
 !F == DmmI&G
--
C|,J]]8$F ]];/"MM*<=--)N+$W??G$ 	668D	
 dGZ0F
s&+++VY[_[d[dekevev[wxyII
{	N &  %&
	N *c#h)3$$TWY\]`Ya)b$c&)	#c(#* ))4A &'?@ +Yq\**+	 	W G61EFFG  	N!=	!!LMM	N  	N!=	!!LMM	Nsx   /G) <H. H!H. I)  4J )H8HH!H+&H. .I&=I!!I&)	J2JJ	K&K		K__main__N)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNansible.module_utils.basicr   +ansible.module_utils.common.text.convertersr   >ansible_collections.community.general.plugins.module_utils.csvr   r	   r
   r   r   r:   __name__     r9   <module>rG      sY    A @AF)V
6 5 Ag g
=5@ zF rF   