
    Vhy                         d dl mZmZmZ e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Zd dlmZ d dlmZ  ej&                  d	      Zd
 ZddZddZ G d de      Zy)    )absolute_importdivisionprint_functiona!  
name: collection_version
author: Felix Fontein (@felixfontein)
version_added: "4.0.0"
short_description: Retrieves the version of an installed collection
description:
  - This lookup allows to query the version of an installed collection, and to determine whether a collection is installed
    at all.
  - By default it returns V(none) for non-existing collections and V(*) for collections without a version number. The latter
    should only happen in development environments, or when installing a collection from git which has no version in its C(galaxy.yml).
    This behavior can be adjusted by providing other values with O(result_not_found) and O(result_no_version).
options:
  _terms:
    description:
      - The collections to look for.
      - For example V(community.general).
    type: list
    elements: str
    required: true
  result_not_found:
    description:
      - The value to return when the collection could not be found.
      - By default, V(none) is returned.
    type: string
    default: ~
  result_no_version:
    description:
      - The value to return when the collection has no version number.
      - This can happen for collections installed from git which do not have a version number in C(galaxy.yml).
      - By default, V(*) is returned.
    type: string
    default: '*'
z
- name: Check version of community.general
  ansible.builtin.debug:
    msg: "community.general version {{ lookup('community.general.collection_version', 'community.general') }}"
a  
_raw:
  description:
    - The version number of the collections listed as input.
    - If a collection can not be found, it will return the value provided in O(result_not_found). By default, this is V(none).
    - If a collection can be found, but the version not identified, it will return the value provided in O(result_no_version).
      By default, this is V(*). This can happen for collections installed from git which do not have a version number in V(galaxy.yml).
  type: list
  elements: str
N)import_module)AnsibleLookupError)
LookupBasez^[A-Za-z0-9_]+\.[A-Za-z0-9_]+$c                     t        | d      5 }t        j                  |      }d d d        dd   d   iS # 1 sw Y   xY w)Nrbversioncollection_info)openjsonload)manifest_pathfmetas      w/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/lookup/collection_version.pyload_collection_meta_manifestr   I   sL    	mT	" ayy| 	4)*95  s   5>c                     t        | d      5 }t        j                  |      }d d d        dj                  d      xs |iS # 1 sw Y    xY w)Nr
   r   )r   yaml	safe_loadget)galaxy_path
no_versionr   r   s       r   load_collection_meta_galaxyr   Q   sP    	k4	  !A~~a ! 	488I&4* ! !s   AAc                    t         j                  j                  | j                        }t         j                  j	                  |d      }t         j                  j                  |      rt        |      S t         j                  j	                  |d      }t         j                  j                  |      rt        ||      S i S )NzMANIFEST.jsonz
galaxy.ymlr   )ospathdirname__file__joinexistsr   r   )collection_pkgr   r   r   r   s        r   load_collection_metar%   Y   s    77??>223D GGLL7M	ww~~m$,];; '',,t\2K	ww~~k"*;:NNI    c                       e Zd ZddZy)LookupModuleNc                    g }| j                  ||       | j                  d      }| j                  d      }|D ]d  }t        j                  |      st	        d| d      	 t        d|       }	 t        ||      }	|j                  |	j                  d
|             f |S # t        $ r |j                  |       Y w xY w# t        $ r}
t	        d| d	|
       d }
~
ww xY w)N)var_optionsdirectresult_not_foundresult_no_version"z" is not a FQCNzansible_collections.r   z!Error while loading metadata for z: r   )set_options
get_optionFQCN_REmatchr   r   ImportErrorappendr%   	Exceptionr   )selfterms	variableskwargsresult	not_foundr   termr$   dataexcs              r   runzLookupModule.runj   s    Yv>OO$67	__%89
 	;D==&(1TF/)BCC!.1EdV/L!M\+NzR MM$((9j9:!	;$   i(  \(+LTFRTUXTY)Z[[\s*   "B#1C#C ?C 	C"CC")N)__name__
__module____qualname__r?    r&   r   r(   r(   i   s    r&   r(   )*)
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr   r   re	importlibr   r   ansible.errorsr   ansible.plugins.lookupr   compiler1   r   r   r%   r(   rC   r&   r   <module>rP      sn   
 C B D	
  	 	 #  - - "**6
7 : r&   