
    ce	n                       U d Z ddlmZ g dZddlZddlZddlZddl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d	
          Z G d de	j        e                   Z G d de	j        e                   Z G d de	j                  Zd9dZd:dZd;dZd<d=dZd<d>d"Z G d# d$ej                  Z G d% d&eej                   Z! G d' d(ej                  Z" G d) d*ej                  Z# e	j        d+d,-          Z$e	j%        eee	j&        f         Z' G d. d,ej(                  Z) e	j        d/d0-          Z* G d1 d0ej(                  Z+ G d2 d3ej(                  Z, G d4 d5ej(                  Z-da.d6e/d7<   d?d8Z0dS )@aF  
Manager for downloading resources from the web.

This manager might be used to download regular files, however - it is designed particularly for the resources, that
should be downloaded once per application launch and used continuously.

Manager is optimized to detect repeated downloads and reuse previous download results.
    )annotations)KeepMediumStatusDownloadGroupManagermanagerN)QtCore)logs)workersT_coT)	covariantc                      e Zd ZdZddZdS )	ProcessDownloadz@Common interface for functions to post-process download results.download
'Download'returnr   c                    dS )z&Prepare a `result` of some `download`.N selfr   s     Ilib/python3.11/site-packages/anaconda_navigator/utils/download_manager.py__call__zProcessDownload.__call__%             N)r   r   r   r   __name__
__module____qualname____doc__r   r   r   r   r   r   "   s.        JJ5 5 5 5 5 5r   r   c                      e Zd ZdZddZdS )	ProcessGroupzFCommon interface for functions to post-process group download results.group'Group'r   r   c                    dS )z#Collect a `result` of some `group`.Nr   )r   r$   s     r   r   zProcessGroup.__call__,   r   r   N)r$   r%   r   r   r   r   r   r   r#   r#   )   s.        PP2 2 2 2 2 2r   r#   c                  P    e Zd ZU dZded<   ded<   ded<   ded<   ded<   ded<   d	S )

GroupStatsz#Tracking stats of a group download.ztyping.Set['Download']canceleddonefailed	requestedstarted	succeededN)r   r   r    r!   __annotations__r   r   r   r(   r(   0   s`         --$$$$    """"%%%%####%%%%%%r   r(   r   boolc                 F    ddl m}  | j                            ddd          S )zOTop-level function that injects "cancel if required" action into all downloads.r   configmainoffline_modeF)anaconda_navigatorr3   CONFgetr2   s    r   inject_cancelr9   ;   s,    ))))));??6>5999r   typing.Mapping[str, typing.Any]c                     ddl m}  ddl m} ddl m} |                                                                |                    |                                                     d                    ddS )	zWTop-level function that injects additional :mod:`requests` arguments into all requests.r   )
client_api)	conda_api)utilsF)set_conda_ssl   )proxiesverifytimeout)	anaconda_navigator.apir<   r=   r>   CondaAPIload_proxy_confignormalize_certificate	ClientAPIget_ssl)r<   r=   r>   s      r   inject_kwargsrJ   B   s    111111000000,,,,,, %%''99;;--j.B.B.D.D.L.L[`.L.a.abb  r   sourcestyping.Iterable[str]strc                p    | D ]%}t           j                            |          r|c S &t          d          )z8Find first file from `sources` that should be available.zno usable source found)ospathisfile
ValueError)rK   sources     r   coalesce_sourcerT   O   sF       7>>&!! 	MMM	
-
.
..r   FrS   destinations	overwriteNonec                .   |D ]}t           j                            |          }|| k    r(|st           j                            |          sHt          j        t           j                            |          d           t          j        | |           dS )z
    Copy content from `source` file into multiple `destinations`.

    By default - if any destination already exists, it would stay untouched. To change this behavior, set `overwrite` to
    :code:`True`.
    Texist_okN)rO   rP   abspathrQ   makedirsdirnameshutilcopy2)rS   rU   rV   destinations       r   populatera   X   s     $ . .gook22&   	.RW^^K88 	.K44tDDDDL---. .r   contentbytesc                p   |D ]}t           j                            |          }|st           j                            |          spt          j        t           j                            |          d           t          |d          5 }|                    |            ddd           n# 1 swxY w Y   dS )z
    Write binary `content` into multiple `destinations`.

    By default - if any destination already exists, it would stay untouched. To change this behavior, set `overwrite` to
    :code:`True`.
    TrY   wbN)rO   rP   r[   rQ   r\   r]   openwrite)rb   rU   rV   r`   streams        r   rg   rg   i   s     $ & &gook22 	&RW^^K88 	&K44tDDDDk4(( &FW%%%& & & & & & & & & & & & & & && &s   B**B.	1B.	c                  l    e Zd ZdZ ej                    Z ej                    Z ej                    ZdS )r   aP  
    Options of keeping files being downloaded, if they already exist.

    :code:`NEVER` - would overwrite file in any case, no matter it exists or not. This is the default behavior.

    :code:`FRESH` - file would stay untouched it already exists and of the expected size.

    :code:`ALWAYS` - do not overwrite file in any case.
    N)	r   r   r    r!   enumautoNEVERFRESHALWAYSr   r   r   r   r   z   sA          DIKKEDIKKETY[[FFFr   r   c                      e Zd ZdZdZdZdZdS )r   ah  
    Intermediate source to store download results in.

    :code:`MEMORY` - download all content directly into the RAM. If no download destination specified - this option
    would be used automatically, regardless of the selected medium.

    :code:`RANDOM` - use random temporary file to store intermediate download results. After download is finished - this
    file is copied into all requested destinations. This is the default behavior.

    :code:`TARGET` - write content directly into the target file. This might be an insecure option if there are clients
    that expect complete file to be available.
    z:memory:z:random:z:target:N)r   r   r    r!   MEMORYRANDOMTARGETr   r   r   r   r      s)          FFFFFr   r   c                  l    e Zd ZdZ ej                    Z ej                    Z ej                    ZdS )Requirea
  
    Control how many downloads should succeed in a :class:`Group` to treat it as a success.

    :code:`ALL` - all downloads should succeed.

    :code:`ANY` - at least one download should succeed.

    :code:`NONE` - any outcome would be treated as a success.
    N)	r   r   r    r!   rj   rk   ALLANYNONEr   r   r   rt   rt      sA          $)++C
$)++C49;;DDDr   rt   c                      e Zd ZdZ ej                    Z ej                    Z ej                    Z ej                    Z	 ej                    Z
dS )r   a?  
    Possible statuses of a download.

    :code:`PENDING` - download is not yet started.

    :code:`STARTED` - download is already in progress.

    :code:`SUCCEEDED` - download finished successfully (with a download result).

    :code:`FAILED` - download failed. There might be no download result in such case.
    N)r   r   r    r!   rj   rk   PENDINGSTARTED	SUCCEEDEDCANCELEDFAILEDr   r   r   r   r      s[        
 
 dikkGdikkG	Ity{{HTY[[FFFr   r   	DownloadTr   )boundc                  :    e Zd ZdZ ej        e          Z ej        e          Z ej        e          Z	 ej        e          Z
 ej        e          Zd8 fdZed9d            Zed:d
            Zed;d            Zed<d            Zed=d            Zed>d            Zed?d            Zd@dAdZdBdZdCdZdDd!ZdEd$Zej        	 	 dFdGd+            Zej        dHd.            ZdId0ZdJd1ZdJd2ZdJd3ZdJd4Z dJd5Z!e"j#        dJd6            Z$dJd7Z% xZ&S )Kr   zA single download request.urlrM   r   rW   c                    t                                                       d _        d _        t	                       _        t          j         _        t          j
         _        d _        d _        d _        t          j         _        d _        | _         j                                         _         j        j        j                             fd            j        j        j                             fd            j        j        j                             fd            j        j        j                             fd            j        j        j                             fd           dS )	z+Initialize new :class:`~Download` instance.NGETr   c                 ,                                      S N_startr   s   r   <lambda>z#Download.__init__.<locals>.<lambda>   s    t{{}} r   c                ,                                     S r   
_succeeded_r   s    r   r   z#Download.__init__.<locals>.<lambda>   s    T__=N=N r   c                ,                                     S r   	_canceledr   s    r   r   z#Download.__init__.<locals>.<lambda>   s    DNN<L<L r   c                ,                                     S r   _failedr   s    r   r   z#Download.__init__.<locals>.<lambda>   s    $,,.. r   c                ,                                     S r   _doner   s    r   r   z#Download.__init__.<locals>.<lambda>   s    

 r   )super__init___content_dataset_filesr   rl   _keep_existingr   rq   _medium_method_process_resultr   ry   _status_status_code_url_runworker_workersignals	sig_startconnectsig_succeededsig_canceled
sig_failedsig_done)r   r   	__class__s   ` r   r   zDownload.__init__   sL   04!%
'*uu$(J28-!:>#'%~!"	+/9+;+;+=+=&../D/D/D/DEEE*223N3N3N3NOOO)112L2L2L2LMMM'//0H0H0H0HIII%--.D.D.D.DEEEEEr   DownloadIdentityc                *    | j         | j        | j        fS )z
        Identity of the download source.

        This is used to detect requests to the same resources - such requests should have same
        :attr:`~Download._identity`.
        )r   r   r   r   s    r   	_identityzDownload._identity   s     |TY
22r   rc   c                    | j         | j         S |                     d          5 }|                                cddd           S # 1 swxY w Y   dS )a@  
        Downloaded content.

        It is unnecessary to store the content in memory in order to use this property. Content might also be stored on
        a disk - it would be fetched from it automatically.

        In case you want to read a content from a disk directly - use :meth:`Download.read` instead.
        Nrb)r   rf   read)r   rh   s     r   rb   zDownload.content   s     =$=  YYt__ 	!;;==	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!s   AA	A	typing.Tuple[str, ...]c                *    t          | j                  S )z0List of files into which content was downloaded.)tupler   r   s    r   fileszDownload.files  s     T[!!!r   
typing.Anyc                    | j         S )z
        Output of the download.

        This would be set only if download is successful, and :meth:`~Download.process` function provided. Otherwise -
        it is :code:`None`.
        r   r   s    r   resultzDownload.result       |r   r   c                    | j         S )zh
        Current status of a download.

        Refer to :class:`Status` for available options.
        r   r   s    r   statuszDownload.status       |r   intc                    | j         S )z*HTTP status code of the resource response.)r   r   s    r   status_codezDownload.status_code  s       r   c                *    t          | j                  S )z?Any file from :attr:`~Download.files` that should be available.)rT   r   r   s    r   usable_filezDownload.usable_file  s     t{+++r   Nr   r~   methoddatac                j    | j         t          j        k    rt          d          || _        || _        | S )z
        Attach data to the resource request.

        It contains of a `method` and a `data` payload. E.g. :code:`.attach('POST', b'{"message": "test"}')`.

        This method returns the self instance, which is useful for chaining calls.
        3can not change properties after download is started)r   r   ry   rR   r   r   )r   r   r   s      r   attachzDownload.attach&  s5     <6>))RSSS
r   r   c                   | j         t          j        k    rt          d          | j         t          j        k    rs| j        0t          | j        | j                            |          d           n<t          t          | j                  | j                            |          d           | j                            t          t          j        j        |                     | S )z
        Add `files` into which resource should be downloaded.

        This method returns the self instance, which is useful for chaining calls.
        zdownloaded generated no outputNTrV   )r   r   r}   rR   r{   r   rg   r   
differencera   rT   updatemaprO   rP   r[   )r   r   s     r   intozDownload.into5  s     <6=((=>>><6+++}(dmT[%;%;E%B%BdSSSSS55t{7M7Me7T7T`deeee3rw66777r   keep_existingr   c                \    | j         t          j        k    rt          d          || _        | S )z
        Set how existing files should be treated.

        For available options, refer to :class:`~Keep`.

        This method returns the self instance, which is useful for chaining calls.
        r   )r   r   ry   rR   r   )r   r   s     r   keepzDownload.keepG  s/     <6>))RSSS+r   process"'typing.Optional[ProcessDownload]'c                \    | j         t          j        k    rt          d          || _        | S )z
        Set a post-process action.

        It would populate :attr:`~Download.result` if request succeeds.

        This method returns the self instance, which is useful for chaining calls.
        r   )r   r   ry   rR   r   r   r   s     r   r   zDownload.processU  s.     <6>))RSSSr   mediumtyping.Union[Medium, str]c                \    | j         t          j        k    rt          d          || _        | S )z
        Set a medium to use to store intermediate download results.

        It might be a path to any file or a particular :class:`Medium` value.

        This method returns the self instance, which is useful for chaining calls.
        r   )r   r   ry   rR   r   )r   r   s     r   viazDownload.viac  s.     <6>))RSSSr   rtmodetyping.Literal['r', 'rt']encodingtyping.Optional[str]typing.TextIOc                    dS Open resource for reading.Nr   r   r   r   s      r   rf   zDownload.opens  r   r   typing.Literal['rb']typing.BinaryIOc                    dS r   r   )r   r   s     r   rf   zDownload.open{  r   r   rc                @    d|vr|pd}t          | j        ||          S )r   bzutf-8)r   )rf   r   r   s      r   rf   zDownload.open  s-    d??*7HD$dX>>>>r   c                    t           j                            dt          |                      t          j        | _        | j                            |            dS )Process canceled download.zdownload %016d: canceledN)	r   loggerdebugidr   r|   r   r   emitr   s    r   r   zDownload._canceled  sG    4bhh???t$$$$$r   c                    t           j                            dt          |                      | j                            |            dS )zProcess completed download.zdownload %016d: doneN)r   r   r   r   r   r   r   s    r   r   zDownload._done  s=    0"T((;;;4     r   c                    t           j                            dt          |                      t          j        | _        | j                            |            dS )Process failed download.zdownload %016d: failedN)	r   r   r   r   r   r}   r   r   r   r   s    r   r   zDownload._failed  sF    2BtHH===}T"""""r   c                    t           j                            dt          |                      t          j        | _        | j                            |            dS )Process started download.zdownload %016d: startedN)	r   r   r   r   r   rz   r   r   r   r   s    r   r   zDownload._start  sF    3RXX>>>~D!!!!!r   c                    t           j                            dt          |                      t          j        | _        | j                            |            dS )Process succeeded download.zdownload %016d: succeededN)	r   r   r   r   r   r{   r   r   r   r   s    r   r   zDownload._succeeded  sH    5r$xx@@@'%%%%%r   c                   t           j                            dt          |           | j                   t                      rAt           j                            dt          |                      t          j        d          d}t          j	        t                    5  t          | j                  }ddd           n# 1 swxY w Y   || j        t          j        ur| j        t          j        u }|s9| j        dk    r-t           j                            dt          |                      	 t%          j        d| j        fi t)                      }t           j                            |           |                                 	 t1          |j                            d	d
                    t6          j                            |          k    }n\# t<          t>          t          f$ r Y nCw xY w# t@          $ r2 t           j                            dt          |           d           Y nw xY w|rZt           j                            dt          |                      tC          || j        d           | "                                 dS t           j                            dt          |                      tG          | j                  o| j$        tJ          j&        u}t%          j        | j        | j        f| j'        |dt)                      }t           j                            |           |j(        | _)        |                                 |r| j$        tJ          j*        u r| j+        d         }d}nz| j$        tJ          j,        u rJt6          j        -                    t]          j/                    ta          j1                    j2                  }d}ntg          | j$                  }|| j        v}t           j                            dt          |           |           t7          j4        t6          j        5                    |          d           d|j6        _7        tq          |d          5 }ts          j:        |j6        |           ddd           n# 1 swxY w Y   t           j                            dt          |                      tC          || j        d           |rEt          j	        t<                    5  t7          j;        |           ddd           n# 1 swxY w Y   nt           j                            dt          |                      |j<        | _=        t           j                            dt          |                      t}          | j=        | j                   | "                                 dS )zPerform download.zdownload %016d: downloading %sz,download %016d: canceled due to offline modez3unable to fetch resources with offline mode enabledNr   zdownload %016d: fetching HEADHEAD)responsezContent-Lengthz$download %016d: failed to fetch HEADT)exc_infoz(download %016d: keeping existing contentFr   z$download %016d: requesting a content)r   rh   r   z)download %016d: streaming content into %srY   re   z-download %016d: copying into additional filesz,download %016d: fetching content into memoryz*download %016d: writing content into files)?r   r   r   r   r   r9   r   TaskCanceledError
contextlibsuppressrR   rT   r   r   r   rl   rn   r   requestsrequestrJ   http_loggerhttpraise_for_statusr   headersr8   rO   rP   getsizeOSError	TypeErrorBaseExceptionra   	_finalizer0   r   r   rp   r   r   r   rr   r   rq   jointempfile
gettempdiruuiduuid4hexrM   r\   r]   rawdecode_contentrf   r^   copyfileobjremoverb   r   rg   )r   rS   r   r   	streamingr`   	temporaryrh   s           r   r   zDownload._run  s    	:BtHHdiPPP?? 	cKLbQUhhWWW+,abbb'+ ,, 	2 	2$T[11F	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 T%8
%J%J,;D t|u44!!"A2d88LLL
'/	UU]__UUH$))8)<<<--///"8#3#7#78H"#M#MNNRTRYRaRabhRiRii#Y
;    % g g gK%%&LbQUhhae%fffffg  !!"LbQUhhWWW>>>>   @"T((KKKt{++SV]1R	#DL$)r$*U^rrbobqbqrrx000$0!!### "	. |v},,"jm!		.. gll8+>+@+@$*,,BRSS 		!$,//'t{:	KI2d88U`aaaK44tDDDD*.HL' k4(( 9F"8<8889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 KMrRVxxXXX[$+>>>> +(11 + +Ik***+ + + + + + + + + + + + + + + KLbQUhhWWW$,DMKJBtHHUUU$----s\   B??CC1AG1 
A	G G.-G.19H-,H-.RRR?T  T$'T$c                    t           j                            dt          |                      | j        |                     |           | _        dS dS )z,Apply post-processing actions to a download.z!download %016d: processing outputN)r   r   r   r   r   r   r   s    r   r  zDownload._finalize  sH    =r$xxHHH=$==..DLLL %$r   )r   rM   r   rW   r   r   )r   rc   )r   r   r   r   r   r   )r   r   )r   rM   r   )r   r~   r   rM   r   r   r   r~   )r   r~   r   rM   r   r~   )r   r~   r   r   r   r~   )r   r~   r   r   r   r~   )r   r~   r   r   r   r~   )r   N)r   r   r   r   r   r   )r   r   r   r   )r   Nr   rW   )'r   r   r    r!   r   Signalobjectr   r   r   r   r   r   propertyr   rb   r   r   r   r   r   r   r   r   r   r   typingoverloadrf   r   r   r   r   r   r   Taskr   r  __classcell__r   s   @r   r   r      s       $$f%%I!FM&))M 6=((Lv&&Jv}V$$HF F F F F F. 3 3 3 X3 ! ! ! X!  " " " X"    X    X ! ! ! X! , , , X,       $           _ /3-1) ) ) ) _) _) ) ) _)? ? ? ?% % % %! ! ! !
# # # #" " " "& & & & \O O O \Ob/ / / / / / / /r   GroupTr   c                  L    e Zd ZdZ ej        e          Z ej        e          Z ej        e          Z	 ej        e          Z
 ej        e          Zd fdZedd            Zedd
            Zedd            Zd dZd!dZd"dZd"dZd"dZd"dZd"dZd#dZ xZS )$r   zy
    Group of downloads.

    Allows managing multiple downloads simultaneously instead of each download separately.
    	downloadsr   r   rW   c                    t                                                       | _        d _        t          j         _        d _        t                      t                      t                      t          |          t                      t                      d _	        t          j         _         j        D ]}|j                             fd           |j                             fd           |j                             fd           |j                             fd           |j                             fd           |j        t          j        k    r                     |           |j        t          j        k    r|j        t          j        k    r                     |           nr|j        t          j        k    r                     |           nG|j        t          j        k    r                     |           nt9          d|j        j                                        |           dS )	z(Initialize new :class:`~Group` instance.N)r*   r)   r+   r,   r-   r.   c                .                         |           S r   r   itemr   s    r   r   z Group.__init__.<locals>.<lambda>(  s    DKK4E4E r   c                .                         |           S r   r   r-  s    r   r   z Group.__init__.<locals>.<lambda>)  s    8M8M r   c                .                         |           S r   r   r-  s    r   r   z Group.__init__.<locals>.<lambda>*  s    t~~d7K7K r   c                .                         |           S r   r   r-  s    r   r   z Group.__init__.<locals>.<lambda>+  s    T\\$5G5G r   c                .                         |           S r   r   r-  s    r   r   z Group.__init__.<locals>.<lambda>,  s    4::d3C3C r   zunexpected download status: )r   r   
_downloadsr   rt   ru   _requirer   r   _statsr   ry   r   r   r   r   r   r   r   r   r   rz   r{   r   r|   r   r}   r   rR   namer   )r   r*  r   r   s   `  r   r   zGroup.__init__  s   7@7;!(#'EEeeYuu%
 %
  &~  	! 	!H&&'E'E'E'EFFF"**+M+M+M+MNNN!))*K*K*K*KLLL''(G(G(G(GHHH%%&C&C&C&CDDD&.00KK!!!&.00&"222))))FO33x((((FM11X&&&& !V@T!V!VWWWJJx    +	! 	!r   typing.Tuple[Download, ...]c                    | j         S )zDownloads in this group.)r3  r   s    r   r*  zGroup.downloads=  s     r   r   c                    | j         S )z
        Output of the group.

        This would be set only if the whole group would be successful (see :meth:`~Group.require`), and
        :meth:`~Group.process` is provided. Otherwise - it is :code:`None`.
        r   r   s    r   r   zGroup.resultB  r   r   r   c                    | j         S )zn
        Current status of a group download.

        Refer to :class:`Status` for available options.
        r   r   s    r   r   zGroup.statusL  r   r   r   r(  requirert   c                    || _         | S )z
        Set a requirement to treat this group download a successful one.

        Refer to :class:`~Require` for available options.

        This method returns the self instance, which is useful for chaining calls.
        )r4  )r   r;  s     r   r;  zGroup.requireW        r   r   'ProcessGroup'c                    || _         | S )z
        Set a post-process action.

        It would populate :attr:`~Group.result` if request succeeds.

        This method returns the self instance, which is useful for chaining calls.
        )r   r   s     r   r   zGroup.processb  r=  r   r   c                   | j         d         sX| j        t          j        u rEt          j        | _        | j                            |            | j	                            |            | j         d         
                    |           dS )r   r)   N)r5  r4  rt   ru   r   r|   r   r   r   r   addr   s     r   r   zGroup._canceledo  sv    J' 	%dmw{.J.J!?DL""4(((Mt$$$J##H-----r   c                d   | j         d                             |           | j        t          j        us| j         d         | j         d         k    rdS | j        t          j        u s| j         d         r@|                                  t          j	        | _        | j
                            |            nd| j         d         r,t          j        | _        | j                            |            n+t          j        | _        | j                            |            | j                            |            dS )zProcess finished download.r*   r,   Nr.   r)   )r5  rA  r   r   rz   r4  rt   rw   r  r{   r   r   r|   r   r}   r   r   r   s     r   r   zGroup._donex  s   F)))L..DK4G4;WbKc4c4cFMW\))dk+.F)NN!+DL##D))))[$ 	'!?DL""4((((!=DLO  &&&4     r   c                   | j         d         sX| j        t          j        u rEt          j        | _        | j                            |            | j	                            |            | j         d         
                    |           dS )r   r+   N)r5  r4  rt   ru   r   r}   r   r   r   r   rA  r   s     r   r   zGroup._failed  su    H% 	%DMW[,H,H!=DLO  &&&Mt$$$H!!(+++++r   c                    | j         d         s+t          j        | _        | j                            |            | j         d                             |           dS )r   r-   N)r5  r   rz   r   r   r   rA  r   s     r   r   zGroup._start  sQ    {9% 	&!>DLN%%%I""8,,,,,r   c                F    | j         d                             |           dS )r   r.   N)r5  rA  r   s     r   r   zGroup._succeeded  s#    K $$X.....r   c                L    | j         |                      |           | _        dS dS )z)Apply post-processing actions to a group.N)r   r   r   s    r   r  zGroup._finalize  s)    =$==..DLLL %$r   )r*  r   r   rW   )r   r7  r  r  )r   r(  r;  rt   r   r(  )r   r(  r   r>  r   r(  r   r   r   rW   r  )r   r   r    r!   r   r   r!  r   r   r   r   r   r   r"  r*  r   r   r;  r   r   r   r   r   r   r  r&  r'  s   @r   r   r     s         f%%I!FM&))M 6=((Lv&&Jv}V$$H(! (! (! (! (! (!T    X    X    X	 	 	 		 	 	 	. . . .! ! ! !&, , , ,- - - -/ / / // / / / / / / /r   c                  T     e Zd ZdZd fdZedd            Zdd	Zdd
ZddZ	 xZ
S )ManagerItemzdInternal collection item, that allows sharing download results between :class:`~Download` instances.r   r   r   rW   c                     t                                                       | _        g  _         j        j                             fd           dS )z.Initialize new :class:`~ManagerItem` instance.c                ,                                     S r   r   r   s    r   r   z&ManagerItem.__init__.<locals>.<lambda>  s    $**,, r   N)r   r   	_download	_childrenr   r   )r   r   r   s   ` r   r   zManagerItem.__init__  sQ    #+02''(>(>(>(>?????r   r   c                    | j         j        S )zo
        Identity of the download source.

        Used for detecting downloads with the same sources.
        )rL  r   r   s    r   identityzManagerItem.identity  s     ~''r   c                D   t           j                            dt          |          t          | j                             | j                            |           | j        j        t          j	        t          j
        t          j        fv r|                                  dS dS )zHRegister another `download` which should use results of the current one.z+download %016d: attaching download to %016dN)r   r   r   r   rL  rM  appendr   r   r{   r|   r}   r   r   s     r   r   zManagerItem.attach  s~    GHWYZ^ZhWiWijjjh'''> V%5v$VVVJJLLLLL WVr   c                   t           j                            dt          |          t          | j                             |j        t          j        k    rt          d          |	                                 | j        j
        |_
        | j        j        t          j        u rt           j                            dt          |                     |j        t          j        u s|j        s| j        j        |_        	  | j        j        |j          |                                 |                                 n# t          $ r |                                 Y nzw xY w| j        j        t          j        u r|                                 nI| j        j        t          j        u r|                                 nt          d| j        j                   |                                 dS )zCApply results of the internal :class:`~Download` onto a `download`.z*download %016d: copying results from %016dz8download should not be started to fetch previous resultsz download %016d: applying contentz+unexpected status of the managed download: N)r   r   r   r   rL  r   r   ry   rR   r   r   r{   r   r   rp   r   rb   r   r   r  r   r   r|   r   r}   r   r   s     r   	_apply_tozManagerItem._apply_to  s    	F8VXY]YgVhVhiiiv~--WXXX $ ;>!V%555K@"X,,OOO FM118?1$(N$:!&##X_55 ""$$$##%%%%	  # # #  """""# ^#v66    ^#v}44 c4>Kaccddds   ;D8 8EEc                ~    | j         r5|                     | j                                                    | j         3dS dS )z5Process the finish of the wrapped :class:`~Download`.N)rM  rS  popr   s    r   r   zManagerItem._done  sL    n 	1NN4>--//000 n 	1 	1 	1 	1 	1r   rG  r  r  )r   r   r    r!   r   r"  rO  r   rS  r   r&  r'  s   @r   rI  rI    s        nn@ @ @ @ @ @ ( ( ( X(   " " " "H1 1 1 1 1 1 1 1r   rI  c                  ,     e Zd ZdZd fdZd	dZ xZS )
r	   zCentral download manager.r   rW   c                V    t                                                       i | _        dS )z*Initialize new :class:`~Manager` instance.N)r   r   _Manager__history)r   r   s    r   r   zManager.__init__  s$    EGr   itemstyping.Union[Download, Group]c                |   g }|D ]G}t          |t                    r|                    |           -|                    |j                   H|D ]l}|j        }	 | j        |         }|                    |           -# t          $ r3 t          |          | j        |<   |j
                                         Y iw xY wdS )zStart downloads.N)
isinstancer   rQ  extendr*  r   rX  r   KeyErrorrI  r   start)r   rY  queuer.  r   rO  parents          r   executezManager.execute  s    ')  	- 	-D$)) -T""""T^,,,,  	( 	(H)1);H(&*nX&>
 h''''	  ) ) )+6x+@+@x( &&((((()		( 	(s   A<<:B98B9r  )rY  rZ  r   rW   )r   r   r    r!   r   rb  r&  r'  s   @r   r	   r	     s]        ##H H H H H H( ( ( ( ( ( ( (r   r	   ztyping.Optional[Manager]MANAGERc                 :    t           t                      a t           S )z4Prepare singleton instance of the :class:`~Manager`.)rc  r	   r   r   r   r
   r
     s     ))Nr   )r   r0   )r   r:   )rK   rL   r   rM   )F)rS   rM   rU   rL   rV   r0   r   rW   )rb   rc   rU   rL   rV   r0   r   rW   )r   r	   )1r!   
__future__r   __all__r  rj   rO   r^   r  r#  r  qtpyr   r  anaconda_navigator.utilsr   r   TypeVarr   Protocolr   r#   	TypedDictr(   r9   rJ   rT   ra   rg   IntEnumr   rM   Enumr   rt   r   r~   TupleAnyr   QObjectr   r(  r   rI  r	   rc  r/   r
   r   r   r   <module>rq     s     # " " " " "
Q
Q
Q      				            ) ) ) ) ) ) , , , , , , v~f---5 5 5 5 5fod+ 5 5 52 2 2 2 26?4( 2 2 2& & & & &! & & &: : : :
 
 
 
/ / / /. . . . ."& & & & &"    4<        S$)   &    dl        T\   ( FN;j999	 <S&* 45 y/ y/ y/ y/ y/v~ y/ y/ y/x	 
	0	0	0_/ _/ _/ _/ _/FN _/ _/ _/DD1 D1 D1 D1 D1&. D1 D1 D1N( ( ( ( (fn ( ( (@ %) ( ( ( (     r   