
    Vh}                        d Z ddlmZ ddlZej
                  r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d	lmZ  e       Z G d
 d      Zy)z8A facade for interfacing with multiple Galaxy instances.    )annotationsN)CollectionVersionMetadata)ConcreteArtifactsManager)	CandidateRequirement)	GalaxyAPIGalaxyError)to_text)Displayc                  Z    e Zd ZdZdddZed        ZddZddZddZ	ddZ
ddZdd	Zy
)MultiGalaxyAPIProxyz<A proxy that abstracts talking to multiple Galaxy instances.c                .    || _         || _        || _        y)z Initialize the target APIs list.N)_apis_concrete_art_mgr_offline)selfapisconcrete_artifacts_managerofflines       Z/home/dcms/DCMS/lib/python3.12/site-packages/ansible/galaxy/collection/galaxy_api_proxy.py__init__zMultiGalaxyAPIProxy.__init__   s    
!;    c                    | j                   S N)r   r   s    r   is_offline_mode_requestedz-MultiGalaxyAPIProxy.is_offline_mode_requested$   s    }}r   c                2    | j                   rt        d      y )NzAThe calling code is not supposed to be invoked in 'offline' mode.)r   NotImplementedErrorr   s    r   *_assert_that_offline_mode_is_not_requestedz>MultiGalaxyAPIProxy._assert_that_offline_mode_is_not_requested(   s    ))%&ijj *r   c              #    K   | j                   ryd}d}t        |j                  t              r|j                  fn| j                  }|D ]8  }	 |j                  |j                  |j                        }d}|D ]  }||f 
 : |s||yy# t        $ r}|}Y d}~Sd}~wt        $ rP}	t        j                  dj                  |j                  |j                  t        |	                   |	}Y d}	~	d}	~	ww xY ww)zHelper for get_collection_versions.

        Yield api, version pairs for all APIs,
        and reraise the last error if no valid API was found.
        NFTzSkipping Galaxy server {server!s}. Got an unexpected error when getting available versions of collection {fqcn!s}: {err!s}serverfqcnerr)r   
isinstancesrcr   r   get_collection_versions	namespacenamer	   	Exceptiondisplaywarningformat
api_serverr$   r
   )
r   requirement	found_api
last_errorapi_lookup_orderapiversionsversionapi_errunknown_errs
             r   _get_collection_versionsz,MultiGalaxyAPIProxy._get_collection_versions,   s     ==	'+
 +//95 __ 	 $ 	'C'66{7L7LkN^N^_  !	' 'Gw,&''	', Z3 4y'  %$
 )I F"~~(--#K0  		 )
)sC   A
C;&B3C;	C8BC;C8(AC3.C;3C88C;c                   |j                   r(| j                  j                  |      |j                  fhS t	        |j                  t
              r|j                  fn| j                  }t        d | j                  |      D              S )z8Get a set of unique versions for FQCN on Galaxy servers.c              3  *   K   | ]  \  }}||f  y wr    ).0r4   r6   s      r   	<genexpr>z>MultiGalaxyAPIProxy.get_collection_versions.<locals>.<genexpr>g   s       
W cN
s   )	is_concrete_artifactr   get_direct_collection_versionr'   r&   r   r   setr9   )r   r0   r3   s      r   r(   z+MultiGalaxyAPIProxy.get_collection_versionsW   s    ++ **11+>OO  +//95 __ 	
  
 $ = =!
 
 	
r   c                   | j                          t        |j                  t              r|j                  fn| j                  }|D ]  }	 |j                  |j                  |j                  |j                        }| j                  j                  ||j                  |j                  |j                  |j                  |j                         |c S  # t         $ r}|}Y d}~d}~wt"        $ rP}|}t$        j'                  dj)                  |j*                  |j,                  t/        |                   Y d}~d}~ww xY w)z2Retrieve collection metadata of a given candidate.Nr!   r"   )r   r&   r'   r   r   get_collection_version_metadatar)   r*   verr   save_collection_sourcedownload_urlartifact_sha256tokensignatures_url
signaturesr	   r+   r,   r-   r.   r/   r$   r
   )r   collection_candidater3   r4   version_metadatar7   last_errr8   s           r   rC   z3MultiGalaxyAPIProxy.get_collection_version_metadatan   s9   779 .22I> "%%( 	 $ !	(C (#&#F#F(22(--(,,$ . &&==($11$44II$33$// ('C!	(F 9  #"  'I F"~~166#K0  		 	s%   
1C	E C$$E 0AD;;E c                    |j                   r| j                  j                  |      S | j                  |      j                  S )z6Retrieve collection dependencies of a given candidate.)r?   r   "get_direct_collection_dependenciesrC   dependencies)r   rK   s     r   get_collection_dependenciesz/MultiGalaxyAPIProxy.get_collection_dependencies   sD      44!!22"$$ ++,@AL	
r   c                   | j                          |j                  }|j                  }|j                  }d }t	        |j
                  t              r|j
                  fn| j                  }|D ]  }	 |j                  |||      c S  |r|g S # t        $ r}|}Y d }~0d }~wt        $ rP}	|	}t        j                  dj                  |j                  |j                  t!        |	                   Y d }	~	d }	~	ww xY w)Nr!   r"   )r   r)   r*   rD   r&   r'   r   r   get_collection_signaturesr	   r+   r,   r-   r.   r/   r$   r
   )
r   rK   r)   r*   r6   rM   r3   r4   r7   r8   s
             r   get_signaturesz"MultiGalaxyAPIProxy.get_signatures   s    779(22	#((&**%) .22I> "%%( 	 $ 	C44YgNN	$ N	%  #" &I F"~~166#K0  		 	s%   0B	C8BC8(AC33C8N)F)r   zt.Iterable[GalaxyAPI]r   r   r   boolreturnNone)rV   rW   )r0   r   rV   z!t.Iterator[tuple[GalaxyAPI, str]])r0   r   rV   z!t.Iterable[tuple[str, GalaxyAPI]])rK   r   rV   r   )rK   r   rV   zdict[str, str])rK   r   rV   z	list[str])__name__
__module____qualname____doc__r   propertyr   r   r9   r(   rC   rQ   rT   r<   r   r   r   r      s?    F   k)V
./b
 "r   r   )r[   
__future__r   typingtTYPE_CHECKINGansible.galaxy.apir   3ansible.galaxy.collection.concrete_artifact_managerr   0ansible.galaxy.dependency_resolution.dataclassesr   r   r   r	   +ansible.module_utils.common.text.convertersr
   ansible.utils.displayr   r,   r   r<   r   r   <module>rf      sD    ? " ??< 6 ? ) )v vr   