
    ڰ[hSW                        d Z dZddlZddlZddlZddlZddlZej                  dk\  Zerddl	m
Z ddlmZ nddlZddlZd Zd Zd Zd Zd	 Zd
 Zd Zd"dZd"dZdad Zd#dZdefdZdZedk(  r eej@                        dk  sdej@                  v sdej@                  v r ejB                  e       dej@                  v r ejB                  dez          esejD                  dk(  r e       e_         ejD                  dk(  r e        ddl#m$Z$  e$       Z%e%jM                  ddd       e%jO                         \  Z(Z)ej@                  d   Z* ee)d   e(jV                        Z, e-d         e-d!e,z         y)$a  
Download utility as an easy way to get file from the net
 
  python -m wget <URL>
  python wget.py <URL>

Downloads: http://pypi.python.org/pypi/wget/
Development: http://bitbucket.org/techtonik/python-wget/

wget.py is not option compatible with Unix wget utility,
to make command line interface intuitive for new people.

Public domain by anatoly techtonik <techtonik@gmail.com>
Also available under the terms of MIT license
Copyright (c) 2010-2015 anatoly techtonik
z3.2    N)   r   c                     ddl m} m}m}m}m} ddlm}m} |j                  j                  }g |_        ||_        |j                  j                  }| | |      g|_         | |      |_         |       }	 |d      }
 ||	 ||
            }|
j                  }t!        t"        j$                        }g }|dkD  r9||z
  }t'        ||      D ]%  }|j)                  ||   j+                  d             ' |S )zUses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
    strings.

    Versions 2.x of Python don't support Unicode in sys.argv on
    Windows, with the underlying Windows API instead replacing multi-byte
    characters with '?'.
    r   )POINTERbyrefcdllc_intwindll)LPCWSTRLPWSTRutf-8)ctypesr   r   r   r   r	   ctypes.wintypesr
   r   kernel32GetCommandLineWargtypesrestypeshell32CommandLineToArgvWvaluelensysargvrangeappendencode)r   r   r   r   r	   r
   r   r   r   cmdargcr   argnumsysnumresultstartis                    S/mounts/lovelace/software/anaconda3/envs/py312/lib/python3.12/site-packages/wget.pywin32_utf8_argvr$   '   s     ;:/mm33O!O%O::#*GEN";!(

C8Dc5;/DZZF]FFzuf% 	3AMM$q'..12	3M    c            
         ! dd l  ddlm} m}m}mm} ddlm}m	}m
m}m}m} t        j                  !!fd j!                   fd       	   | |      d|j"                  f      }	 d      }
 d      }  |       d	|j"                  f      d
d  | || |            d|j"                  f       d      j$                  fd}d }d }t'        t        j(                  d      rt        j(                  j+                         }t'        t        j                  d      rt        j                  j+                         }d}d
}||k(  }||k(  }|r |	|
      } ||      rd}|r |	|      } ||      rd}|s|r  | ||| |      |      d|j"                  f       G fdd      }|r |d |d      t        _        n" |d t        j(                  |d      t        _        |r |d |d      t        _        y  |d t        j                  |d      t        _        y y # t,        $ r} d|d       Y d }~y d }~ww xY w)Nr   )WINFUNCTYPEr	   r   r   r   )BOOLHANDLEDWORDr   r
   LPVOIDc                     j                  t        | t              r| n
t        |              j                  d       y )N
)write
isinstancestrrepr)messageoriginal_stderrs    r#   	_complainz(win32_unicode_console.<locals>._complainS   s.    GS)AgtG}Ud#r%   c                 4    | dk(  rj                  d      S d S )Ncp65001r   )lookup)namecodecss    r#   <lambda>z'win32_unicode_console.<locals>.<lambda>W   s    49;Lw!7 RV r%   GetStdHandleGetFileType   i   GetConsoleModec                 d    | k(  s| y |        z  k7  xs  |                      dk(  S )NTr    )handler*   FILE_TYPE_CHARFILE_TYPE_REMOTEr@   r>   INVALID_HANDLE_VALUEr   s    r#   not_a_consolez,win32_unicode_console.<locals>.not_a_consolec   sJ    -- (,<+<<O C%feEGn=BDr%   fileno   FWriteConsoleWc                   J    e Zd Zd Zd Zd Zd ZfdZ fdZfdZ	y)	,win32_unicode_console.<locals>.UnicodeOutputc                     || _         || _        || _        d| _        d| _        d| _        d| _        || _        | j                          y )NFwr   )		_hConsole_stream_filenoclosed	softspacemodeencodingr8   flush)selfhConsolestreamrI   r8   s        r#   __init__z5win32_unicode_console.<locals>.UnicodeOutput.__init__   sC    %-DN#)DL#)DL"'DK%*DN #DI$+DM $DIJJLr%   c                      y)NFrC   rX   s    r#   isattyz3win32_unicode_console.<locals>.UnicodeOutput.isatty   s     r%   c                     d| _         y )NT)rS   r]   s    r#   closez2win32_unicode_console.<locals>.UnicodeOutput.close   s	    "&DKr%   c                     | j                   S )N)rR   r]   s    r#   rI   z3win32_unicode_console.<locals>.UnicodeOutput.fileno   s    <<'r%   c                     | j                   	 | j                  j                          y y # t        $ r*} | j                  d|d| j                          d }~ww xY w)Nz.flush: z from )rP   rQ   rW   	Exceptionr8   )rX   er4   s     r#   rW   z2win32_unicode_console.<locals>.UnicodeOutput.flush   sS    ~~-" LL..0 .  ) "%		1dll&[\!"s   + 	A%AAc                    	 | j                   kt        s"t        |t              r|j	                  d      }n't        r!t        |t
              r|j	                  d      }| j                  j                  |       y t        s+t        |t              st        |      j                  d      }n't        r!t        |t
              s|j                  d      }t        |      }|r d      } | j                   |t        |d       	|      d       }|dk(  s|j                  dk(  rt        d|d|j                        ||j                  z  }|sy ||j                  d  }|ry y # t        $ r} | j                  d|        d }~ww xY w)Nr   r   i'  zWriteConsoleW returned z, n.value = z.write: )rP   PY3Kr/   unicoder   r0   rQ   r.   decoder   minr   IOErrorrc   r8   )
rX   text	remainingnretvalrd   r*   rK   r4   r   s
         r#   r.   z2win32_unicode_console.<locals>.UnicodeOutput.write   sG   >>1#'JtW,E'+{{7';!%*T3*?'+{{7'; LL..t4#'
40I'*4y'7'7'@!%js.C'+{{7';(+D	I"+$)!H *7t~~tSQZ\aMbdijkdlnr)s#)Q;!''Q,*1_eghgngn2o*p$p )QWW 4	'0$)'+AGGH~ #, % !DIIq"ABs%   A6E" :CE" E" "	F+FFc                     	 |D ]  }| j                  |        y # t        $ r} | j                  d|        d }~ww xY w)Nz.writelines: )r.   rc   r8   )rX   lineslinerd   r4   s       r#   
writelinesz7win32_unicode_console.<locals>.UnicodeOutput.writelines   sG    $) -D JJt,-$ !A"FGs    	A=AN)
__name__
__module____qualname__r[   r^   r`   rI   rW   r.   rr   )r*   rK   r4   r   s   r#   UnicodeOutputrM      s&    	!!'("<r%   rv   z<Unicode console stdout>z<Unicode redirected stdout>z<Unicode console stderr>z<Unicode redirected stderr>z
exception z* while fixing up sys.stdout and sys.stderr)r9   r   r'   r	   r   r   r   r   r(   r)   r*   r   r
   r+   r   stderrregisterr   r   hasattrstdoutrI   rc   )"r'   r	   r   r   r(   r)   r   r
   r+   r;   STD_OUTPUT_HANDLESTD_ERROR_HANDLErH   old_stdout_filenoold_stderr_filenoSTDOUT_FILENOSTDERR_FILENOreal_stdoutreal_stderrhStdouthStderrrv   rd   r*   rE   rF   r@   r>   rG   rK   r4   r   r9   r3   s"                          @@@@@@@@@@@r#   win32_unicode_consoler   K   s;   AALLjjO$ OOVWwS1{651>6??2ST!#J :/k%/0PQ!BT675>BDTV\VeVeCfg$Ry	D 	D ! 3::x( #

 1 1 33::x( #

 1 1 3(M9(M9"#45GW%#"#34GW%#+\KffeWU^U[\^mouo~o~]  AMB BH *7D-Icd
*4=NPmn
*7D-Icd
*4=NPmn
_ &`  SaQRRSs   F:H7 "H7 7	I IIc                 N    t         r| S t        | t              r| S t        | d      S )z/:return: filename decoded from utf-8 to unicoder   )rf   r/   rg   )filenames    r#   
to_unicoder      s*      h(O8W--r%   c                     t         j                  j                  t        j                  |       j                        }t	        |j                  d            dk(  ryt        |      S )z-:return: detected filename as unicode or Nonez 
	.r   N)ospathbasenameurlparser   stripr   )urlfnames     r#   filename_from_urlr      sM     GGX..s3889E
5;;x !Q&er%   c           	         t        |       t        k(  r| j                         } t        |       t        k(  r)t	        | D cg c]  }|j                  dd       c}      } | j                  d      }|sy|j                  d      }t        |      dk(  ry|d   j                         j                         dvry|dd D cg c]$  }|j                         j                  d      s#|& }}t        |      dkD  ry|d   j                  d	      d   j                  d
      }t        j                  j                  |      }|sy|S c c}w c c}w )zDetect filename from Content-Disposition headers if present.
    http://greenbytes.de/tech/tc2231/

    :param: headers as dict, list or string
    :return: filename from content-disposition header or None
    :rJ   zContent-DispositionN;r   )inline
attachmentz	filename==z 	")typer0   
splitlineslistdictsplitgetr   r   lower
startswithr   r   r   )headersxcdispcdtypefnamesr8   s         r#   filename_from_headersr      s!    G}$$&G}9AQ9:KK-.E[[F
6{aay (@@IAqwwy';';K'HaIFI
6{Q!9??3"((0D77D!DK# : Js   E=$E"Ec                 L   d}| j                  dd      \  }}t        j                  |      D cg c]  }|j                  |      s| }}|D cg c]  }|j                  dd      d    }}|D cg c]  }|j	                  |d       }}|D cg c])  }|j                  d      r|j                  d      r|dd + }}|D cg c]$  }t        |      t        d	      k  rt        |      & }}d}|r|t        |      d   z  }d
|||fz  S c c}w c c}w c c}w c c}w c c}w )zsExpands name portion of filename with numeric ' (x)' suffix to
    return filename that doesn't exist already.
    .rJ   r    z ()r?   rA   
0123456789z
%s (%d).%s)	rsplitr   listdirr   replaceendswithsetintsorted)	r   dirnamer8   extr   namessuffixesindexesidxs	            r#   filename_fix_existingr     s1    GQ'ID#

7+B1q||D/AQBEB*/0QQXXc1a 0E0-23		$#3H3!) >All4(QZZ_ !B >H > ( 31!fL 11 A 3G 3
Cvgr""4c*** C03>3s#   DDD2D.D)D!c                     t         j                  dk(  rd} d}d}ddlm}m}m} 	 ddlmmm	 |j                  j                  |      } G fd	d
|       G fdd|       G fdd|      } |       }|j                  j!                  | ||            }	|	dk(  ry|j"                  j$                  dz   S t         j                  dk(  rSddlm}
 ddlm} ddlm}  |ddgdz        }	  |
t0        j2                  j5                         ||       |d   |d   fd   S y# t        $ r ddlmmm Y 
w xY w# t6        $ r Y 3w xY w)zReturn width of available window area. Autodetection works for
       Windows and POSIX platforms. Returns 80 for others

       Code from http://bitbucket.org/techtonik/python-pager
    ntir<   r=   r   )r	   	Structurer   )SHORTWORDr*   )c_shortc_ushortc_ulongc                   "    e Zd ZdW  fdW  fgZy) get_console_width.<locals>.COORDXYNrs   rt   ru   _fields_r   s   r#   COORDr   5  s    esEl3Hr%   r   c                   2    e Zd ZdW  fdW  fdW  fdW  fgZy)%get_console_width.<locals>.SMALL_RECTLeftTopRightBottomNr   r   s   r#   
SMALL_RECTr   8  s&    % %(8U*;=Hr%   r   c                   :    e Zd ZdW  fdW  fdW fdW fdW fgZy)5get_console_width.<locals>.CONSOLE_SCREEN_BUFFER_INFOdwSizedwCursorPositionwAttributessrWindowdwMaximumWindowSizeNr   )r   r*   r   r   s   r#   CONSOLE_SCREEN_BUFFER_INFOr   <  s2    !5)+U3&-#Z0.6	8Hr%   r   rJ   posix)ioctl)
TIOCGWINSZ)arrayH   P   )r   r8   r   r	   r   r   r   r   r   r*   ImportErrorr   r   r   r   r;   GetConsoleScreenBufferInfor   r   fcntlr   termiosr   r   r   rz   rI   rj   )STD_INPUT_HANDLEr{   r|   r	   r   r   console_handler   sbiretr   r   r   winsizer   r*   r   r   r   s                 @@@@@r#   get_console_widthr     sR    
ww$ 	43	F::
  556GH	4I 	4	= 	=	8 	8 	8 )*oo88E#J(!8||!!!##	G	&aS1W%	#**##%z7; 
GAJ'**Q  	FF F	FH  		s#   
D( 4&E (D?>D?	EEc                     |dz
  }t        t        j                  t        |       |z  |z              }dd|z  z   d||z
  z  z   dz   S )a  Return thermometer style progress bar string. `total` argument
    can not be zero. The minimum size of bar returned is 3. Example:

        [..........            ]

    Control and trailing symbols ( and spaces) are not included.
    See `bar_adaptive` for more information.
    r?   [r    ])r   mathfloorfloat)currenttotalwidth
avail_dotsshaded_dotss        r#   bar_thermometerr   Y  sQ     qJdjjw%!7*!DEFK[ 3
;(>#??#EEr%   c                    |r|dk  r+d| z  }t        |      |k  r|S t        d| z        |k  rd| z  S ddt        d|z        dz  dz   d}g d}g }|}|D ]'  }||   |k  s|j                  |       |||   dz   z  }) d	}	|D ]r  }|d
k(  r!|	dd| z  |z  z  j                  |d
         z  }	n=|dk(  r|	t        | ||d   |z         z  }	n!|dk(  r|	| d|j                  |d         z  }	|dd }|sn|	dz  }	t |	S )aw  Return progress bar string for given values in one of three
    styles depending on available width:

        [..  ] downloaded / total
        downloaded / total
        [.. ]

    if total value is unknown or <= 0, show bytes counter using two
    adaptive styles:

        %s / unknown
        %s

    if there is not enough space on the screen, do not display anything

    returned string doesn't include control characters like  used to
    place cursor at the beginning of the line to erase previous content.

    this function leaves one free character at the end of string to
    avoid automatic linefeed on Windows.
    r   z%s / unknownz%sr   r   r?   )percentbarsizerJ   r   r   z%s%%d   r   r   z / Nr   )r   r   rjustr   )
r   r   r   msg	min_widthpriorityselectedavailfieldoutputs
             r#   bar_adaptiver  g  se   0 EAIw&s8eJtg~&'>!  $,!A%I
 *H HE $	5	E	!5!!##$ F 	)	6S7]e34;;Ii<PQQE>/'5)E2B52HIIF?/66y7HII!"h	#  Mr%   c                    t        dt                     }t        j                  dd dk(  r| dk(  rdan	t        |z  at        }nt        | |z  |      } ||||      }|r#t        j
                  j                  d|z          yy)a  callback function for urlretrieve that is called when connection is
    created and when once for each block

    draws adaptive progress bar in terminal/console

    use sys.stdout.write() instead of "print,", because it allows one more
    symbol at the line end without linefeed on Windows

    :param blocks: number of blocks transferred so far
    :param block_size: in bytes
    :param total_size: in bytes, can be -1 if server doesn't return it
    :param bar_function: another callback function to visualize progress
    r   Nr   )r   r   r   r   )ri   r   r   version_info__current_sizerz   r.   )blocks
block_size
total_sizebar_functionr   current_sizeprogresss          r#   callback_progressr    s      &()E
y(Q;Nj(N%6*,j9L*e<H

) r%   c                     t        ddd      }|r	|xs d|d<   | rt        |       xs d|d<   |rt        |      xs d|d<   |d   xs |d   xs	 |d   xs |S )zReturn filename for saving file. If no filename is detected from output
    argument, url or headers, return default (download.wget)
    r   )outr   r   r  r   r   )r   r   r   )r   r  r   defaultr   s        r#   detect_filenamer    sq     RR,E
ybe
(-3e09?Ri<F5+FuU|FwFr%   c                    d}|r#t         j                  j                  |      r|}d}t        | |      }t	        j
                  d|d      \  }}t        j                  |       t        j                  |       fd}r|}nd}t        rOt        t        j                  |             }	t        j                  |	d         |	d<   t        j                  |	      }	n| }	t        j                  |	||      \  }}
t        | ||
      }|r|dz   |z   }t         j                  j!                  |      rt#        |      }t%        j&                  ||       |S )aM  High level function, which downloads URL into tmp file in current
    directory and then renames it to filename autodetected from either URL
    or HTTP headers.

    :param bar: function to track download progress (visualize etc.)
    :param out: output filename or directory
    :return:    filename where URL is downloaded to
    Nz.tmpr   )prefixdirc                 $    t        | ||       y )N)r  )r  )r  r	  r
  r   s      r#   callback_chargedz"download.<locals>.callback_charged  s    &*jsKr%   r?   /)r   r   isdirr  tempfilemkstempr`   unlinkrf   r   r   urlsplitquote
urlunsplituliburlretrieveexistsr   shutilmove)r   r  r   outdirr  fdtmpfiler  callbackbinurlr   r   s     `         r#   downloadr*    s$    F
rww}}S! S#&F$$VFDMRHHRLIIgL #h'',-NN6!9-q	$$V,))&'8DWgsC1HC<(* 
ww~~h(2
KK" Or%   zuusage: wget.py [options] URL

options:
  -o --output FILE|DIR   output filename or directory
  -h --help
  --version
__main__r?   z-hz--helpz	--versionzwget.py win32)OptionParserz-oz--outputr  )destrJ   )r  r   zSaved under %s)r   )NNNzdownload.wget).__doc____version__r   r#  r   r  r   r  rf   urllib.requestrequestr   urllib.parseparser   urllibr$   r   r   r   r   r   r   r   r  r  r  r  r*  usagers   r   r   exitplatformoptparser-  parser
add_option
parse_argsoptionsargsr   r  r   printrC   r%   r#   <module>r@     s  "     
6!!HEST.:+,8vFMd *>G  0f	 z
388}qDCHH,CHH0Dchhk)* CLLG+"$
||w%^F
dJX6'')OWd
((1+CQW^^4H	"I	
X
%&7r%   