
    Vh                     r    d dl mZmZmZ eZdZdZdZd dl	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)    )absolute_importdivisionprint_functiona  
name: shelvefile
author: Alejandro Guirao (!UNKNOWN) <lekumberri@gmail.com>
short_description: read keys from Python shelve file
description:
  - Read keys from Python shelve file.
options:
  _terms:
    description: Sets of key value pairs of parameters.
    type: list
    elements: str
  key:
    description: Key to query.
    type: str
    required: true
  file:
    description: Path to shelve file.
    type: path
    required: true
z
---
- name: Retrieve a string value corresponding to a key inside a Python shelve file
  ansible.builtin.debug:
    msg: "{{ lookup('community.general.shelvefile', 'file=path_to_some_shelve_file.db key=key_to_retrieve') }}"
zZ
_list:
  description: Value(s) of key(s) in shelve file(s).
  type: list
  elements: str
N)AnsibleErrorAnsibleAssertionError)
LookupBase)to_bytesto_textc                       e Zd Zd ZddZy)LookupModulec                     t        j                  t        |            }|j                  |d      }|j	                          |S )z<
        Read the value of "key" from a shelve file
        N)shelveopenr	   getclose)selfshelve_filenamekeydress        o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/shelvefile.pyread_shelvezLookupModule.read_shelve4   s5     KK12eeC		
    Nc                    t        |t              s|g}g }|D ]  }d d d}|j                         }	 |D ]-  }|j                  d      \  }	}
|	|vrt        |	 d      |
||	<   / 	 |d   }| j                  |d|d         }|rB| j                  ||      }|t        d| d|       |j                  t        |              |S t        d	|d           |S # t        t
        f$ r}t        |      d }~ww xY w)
N)filer   =z not in paramvalsr   filesr   zKey z not found in shelve file z(Could not locate shelve file in lookup: )
isinstancelistsplitr   
ValueErrorAssertionErrorr   find_file_in_search_pathr   appendr
   )r   terms	variableskwargsretterm	paramvalsparamsparamnamevalueer   
shelvefiler   s                  r   runzLookupModule.run=   sA   %&GE 	cD!%d3IZZ\F	&# ,E"'++c"2KD%9,3tf<M4NOO&+IdO	, E"C 66y'9U[K\]J&&z37;&cU2LZL'YZZ

73<( 
 #%MiX^N_M`#abb9	c< 
' / &"1o%&s   2CC6&C11C6)N)__name__
__module____qualname__r   r1    r   r   r   r   2   s    $r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.errorsr   r   ansible.plugins.lookupr   +ansible.module_utils.common.text.convertersr	   r
   r   r5   r   r   <module>r?      sB    C B*
  > - I/: /r   