
    d!                        d Z ddlmZmZmZm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ej                  dk\  rddlmZmZ ddlmZ ddlmZ nddlZddlmZmZ ddlZddZddZddZd	 Zdd
ZddZej8                  fdZedk(  r e        yy)a2  
wxget.py -- wx Version of wget utility for platform that don't have it already.

Usage:
    wxget URL [DEST_DIR]

Where URL is a file URL and the optional DEST_DIR is a destination directory to
download to, (default is to prompt the user).
The --trusted option can be used to suppress certificate checks.
    )divisionabsolute_importprint_functionunicode_literalsN)   )	HTTPErrorURLErrorc                     | rd}nd}d}t        j                         j                  d      d   }|j                  d      d   }|dk7  rt        d      ||||fz  S )	z" Get the URL for the docs or demo.demodocszEhttps://extras.wxpython.org/wxPython4/extras/%s/wxPython-%s-%s.tar.gz r   .4z#wx Versions before 4 not supported!)wxversionsplit
ValueError)r   pkgbase_urlvermajors        0/usr/lib/python3/dist-packages/wx/tools/wxget.pyget_docs_demo_urlr   /   se    VH
**,

S
!!
$CIIcN1E|>??sCo%%    c                    t        j                         }|st        j                         }nIt         j                  j                  |      st        j                  |       t        j
                  |       t         j                  j                  t        j                  |       d         }|sd}|st        j                  dd||t        j                  t        j                  z        5 }|j                         t        j                  k(  r1t         j                  j                  |j!                               \  }}nd} ddd       nVt         j                  j                  |      st        j                  |       n!t         j                  j#                  |      sd} |r t         j                  j%                  ||      }t        j
                  |       | |fS # 1 sw Y   DxY w)z Get the file save location.   zdownloaded.fileNzSave As ...)message
defaultDirdefaultFilestyle)osgetcwdpathexistsmakedirschdirbasenameurlparseurlsplitr   
FileDialogFD_SAVEFD_OVERWRITE_PROMPT	ShowModalID_OKr   GetPathisdirjoin)urldest_dirforceold_dirfilenamedlgs         r   get_save_pathr8   <   sL   iikG99;ww~~h'KK!
ww 1 1# 6q 9:H$]]-H 

R5K5K(K
 	 }}"((*%'WW]]3;;=%A"(	 	 ww~~h'KK!x(C77<<(3HHW?%	 	s   )AGG!c                     d}	 d| d|g}|r|j                  d       t        ddj                  |             t        j                  |      }|dk(  }|S # t
        $ r t        d       Y |S w xY w)	z Try to download via wget.Fwgetz-Oz--no-check-certificatez
Trying:
  r   r   z2wget did not work or not installed - trying urllib)appendprintr1   
subprocess
check_call	Exception)r2   r6   trustedresultcmds        r   download_wgetrC   ^   s~    F	DsD(+JJ/0mSXXc]+&&s+1 M  DBCMDs   AA A,+A,c                    t        d|        d}	 t        j                  |       }t        |d      5 }d}|j                         }t        |d      r|j                  n|j                  } |d	      }	d}
|	rt        |	d
         }
dj                  | |
      }t        j                  t        j                   z  t        j"                  z  }|
r t        j$                  d|d|
|z  z   |      }nt        j$                  d||      }d
}|r|j'                  |      }|s+|j)                  ||z  d       t        j*                  d       n|t-        |      z  }|j/                  |       dj                  |      }|
r|dj                  |dz  |
z        z  }|j)                  ||z  ||z         \  }}t        j*                  d       |r|j1                          ddd       t2        j4                  j7                  |      xr" t3        j8                  |      j:                  d
kD  }|S # t        t        t
        j                  f$ r}t        d|z         Y d}~yd}~ww xY w# 1 sw Y   xY w)z Try to download via urllib.z&Trying to Download via urllib from:
  Tz	Error: %sNFwbi    
getheaderszContent-Lengthr   zDownloading: {0}
Bytes: {1}
Downloading   )maximumr    )r    zmessage+
DONE!   z{0:16}z   [{0:6.2f}%]d   P   )r<   urllib2urlopenr   r	   sslCertificateErroropeninfohasattrrF   get_allintformatr   PD_APP_MODALPD_CAN_ABORTPD_AUTO_HIDEProgressDialogreadUpdate
MilliSleeplenwriteDestroyr!   r#   r$   statst_size)r2   r6   
keep_goingurl_reserroutfileblock_szmeta	meta_funcmeta_length	file_sizer   dstyleprogressfile_size_dlread_bufferstatus
dummy_skiprA   s                      r   download_urllibrr   m   s(   	
3S9J//#& 
h	 !||~'.t\'BDOO	 01	KN+I299#yI2??2R__D((129h3F1FfVH ((vNH!,,x0K 8:KLc"C,,LMM+&__\2F*11,2Dy2PQQ'/|x7O7>v~(G$ZMM"   	C!F WW^^H%G"''(*;*C*Ca*GFMO x!5!56 kC ! !s)   H FIII<III c                 D   t         j                  j                  |      d   }t        |      dk(  rd}t	        d| d|       dd| d|dd	d
g}|r|j                  d       |r6dj                  | j                  d      dd       }|j                  d|g       |r@t         j                  j                  |      r!t	        d|       t        j                  |       t	        ddj                  |             	 t	        d       t        j                  |      }t	        |       t         j                  j                  |      xr" t        j                  |      j                  dkD  }|S # t        t        j                  f$ r"}t	        d       t	        |       d}Y d}~{d}~ww xY w)z Try to download via pip.r   r   z&Trying to use pip to download From:
  zTo:
  pipdownloadz--destz	--no-depsz--exists-actioniz--no-cache-dir/Nr   z--trusted-hostzDelete ExistingzRunning pipr   z?
Abusing pip so expect possible error(s) in the next few lines.z!Download via pip may have Failed!)r!   r#   r   r^   r<   r;   r1   extendr$   unlinkr=   r>   FileNotFoundErrorCalledProcessErrorra   rb   )	r2   r6   r4   r@   download_dircmdshostrA   Errors	            r   download_pipr      sY   77==*1-L
<A	
3S)XN:sHlKs$D$%xx		#r*+%t,-)*
		(	-#((4.)PQ&&t,f
 WW^^H%G"''(*;*C*Ca*GFM z<<= 12es   4+E$ $F=FFc                    t        | ||      \  } }d}d}| y| rt        | ||      }|st        | |      }|st        | |||      }|s| j	                  d      }dj                  ddd	d
|d   ddj                  |dd       d|g      }t        |d       t        j                  |dt        j                  t        j                  z  t        j                  z         y|S )z
    Download and save a file specified by url to dest directory, with force will
    operate silently and overwrite any existing file.
    TFNzAborted!rw   
z6

ERROR in Web Access! - You may be behind a firewall!z4----------------------------------------------------zAYou should be able to bybass this by using a browser to download:	z
from:	z%
then copying the download file to:
	zWDOWNLOAD ERROR!)captionr    zFAILURE or Abort!)r8   rC   rr   r   r   r1   r<   r   
MessageBoxOKCENTRE
ICON_ERROR)	r2   destr4   r@   r6   rc   success	split_urlmsgs	            r   download_filer      s    
 "#tU3MCJG
{
Xw7%c84G"3%AG		#I))JSbM388IcrN#;XG	 C #tMM#'9 "bii =?&Or   c                    t        j                         }d}d}d}d}d}|| v rd}| j                  |       || v rd}| j                  |       t        |       dkD  r| d   }nd}t        |       dkD  r| d   }nwt	        t
               t        j                  t
        d	z   d
t         j                  t         j                  z        }|t         j                  k(  rt	        d       t               }nd}|rt        ||||      }	t	        |	       yy)z Entry point for wxget.r   z--forcez	--trustedFTr   NrH   z

RUN TEST?wxgetzTesting with wxDemo)r2   r   r4   r@   )r   Appremover^   r<   __doc__r   YES_NOCENTERYESr   r   )
argsAPPr3   
force_flagtrusted_flagr4   r@   r2   yes_noFILENAMEs
             r   mainr      s    
&&(CHJLEGTJtJ
4y1}7
4y1}1ggw6!yy24RVV'(#%CC
 SxugVh r   __main__)F)FF)NFF)r   
__future__r   r   r   r   sysr!   r   r=   rO   rt   version_infourllib.errorr   r	   urllib.requestrequestrM   urllib.parseparser(   r   r8   rC   rr   r   r   argvr   __name__ r   r   <module>r      s   &	 U T 
 	 	  
 
t2$#-& D-^8B hh "H zF r   