
    ivq             	       l   S r  SSKrSSKrSSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKJrJr  SSKJrJr  SSKJr  SSKJr  SSKr " S S5      r " S	 S
5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r  " S S\5      r! " S S\5      r" " S S \5      r# " S! S"\5      r$ " S# S$\5      r% " S% S&\5      r& " S' S(5      r' " S) S*5      r( " S+ S,5      r)S- r*S. r+S/ r,S0 r-S\S1 jr.S\S2 jr/S3 r0S]S4 jr1S5 r2S6 r3S7 r4S8 r5\'" 5       r6\6Rn                  r7S9 r8S: r9S; r:\;S<:X  GaM  SS=K<J=r=  \=" 5       r>\>R                  S>S?S@SASBSC9  \>R                  SDSESFSGSHSISJ9  \>R                  SKSLSMSGSHSNSJ9  \>R                  SOSPSQSGSHSRSJ9  \>R                  SSSTSU\R                  R                  SV5      SWSX9  \>R                  5       u  rCrD\'" \CR                  SY9rF\D(       a`  \D HY  rG\FRo                  \G\CR                  \CR                  \CR                  \CR                  SZ9rL\LSH:X  d  MF  \CR                  (       d  MY    g   g\FRo                  \CR                  \CR                  \CR                  \CR                  S[9  gg)^aH	  
The NLTK corpus and module downloader.  This module defines several
interfaces which can be used to download corpora, models, and other
data packages that can be used with NLTK.

Downloading Packages
====================
If called with no arguments, ``download()`` will display an interactive
interface which can be used to download and install new packages.
If Tkinter is available, then a graphical interface will be shown,
otherwise a simple text interface will be provided.

Individual packages can be downloaded by calling the ``download()``
function with a single argument, giving the package identifier for the
package that should be downloaded:

    >>> download('treebank') # doctest: +SKIP
    [nltk_data] Downloading package 'treebank'...
    [nltk_data]   Unzipping corpora/treebank.zip.

NLTK also provides a number of "package collections", consisting of
a group of related packages.  To download all packages in a
colleciton, simply call ``download()`` with the collection's
identifier:

    >>> download('all-corpora') # doctest: +SKIP
    [nltk_data] Downloading package 'abc'...
    [nltk_data]   Unzipping corpora/abc.zip.
    [nltk_data] Downloading package 'alpino'...
    [nltk_data]   Unzipping corpora/alpino.zip.
      ...
    [nltk_data] Downloading package 'words'...
    [nltk_data]   Unzipping corpora/words.zip.

Download Directory
==================
By default, packages are installed in either a system-wide directory
(if Python has sufficient access to write to it); or in the current
user's home directory.  However, the ``download_dir`` argument may be
used to specify a different installation target, if desired.

See ``Downloader.default_download_dir()`` for more a detailed
description of how the default download directory is chosen.

NLTK Download Server
====================
Before downloading any packages, the corpus and module downloader
contacts the NLTK download server, to retrieve an index file
describing the available packages.  By default, this index file is
loaded from ``https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml``.
If necessary, it is possible to create a new ``Downloader`` object,
specifying a different URL for the package index file.

Usage::

    python nltk/downloader.py [-d DATADIR] [-q] [-f] [-k] PACKAGE_IDS

or::

    python -m nltk.downloader [-d DATADIR] [-q] [-f] [-k] PACKAGE_IDS
    N)md5sha256)	HTTPErrorURLError)urlopen)ElementTreec                   T    \ rS rSrSr           S	S jr\S 5       rS rS r	Sr
g)
Package   a  
A directory entry for a downloadable package.  These entries are
extracted from the XML index file that is downloaded by
``Downloader``.  Each package consists of a single file; but if
that file is a zip file, then it can be automatically decompressed
when the package is installed.
Nc                 
   Xl          U=(       d    UU l         X@l         X l         [	        U5      U l         [	        U5      U l         Xpl         Xl         Xl	         Xl
         Xl         Xl         [        R                  R                  UR!                  S5      S   5      S   n[        R                  R#                  XAU-   5      U l         ['        [	        U5      5      U l         U R*                  R-                  U5        g )N/   )idnamesubdirurlintsizeunzipped_sizechecksumsvn_revision	copyrightcontactlicenseauthorospathsplitextsplitjoinfilenameboolunzip__dict__update)selfr   r   r   r   r   r   r   r   r   r   r   r   r$   kwexts                   I/home/james-whalen/.local/lib/python3.13/site-packages/nltk/downloader.py__init__Package.__init__   s    " 3JB	-	4 EI	: /	 !4(<"0	, 3%ggsyy~b1215V#X6	3 #e*%
	 	R     c                     [        U [        5      (       a  [        R                  " U 5      n U R                   H(  n[        U R                  U   5      U R                  U'   M*     [        S0 U R                  D6$ )N )
isinstancestrr   parseattribr
   )xmlkeys     r*   fromxmlPackage.fromxml	  sY    c3##C(C::C!#**S/2CJJsO $$$r-   c                 4    U R                   UR                   :  $ Nr   r'   others     r*   __lt__Package.__lt__      ww!!r-   c                      SU R                   -  $ )Nz<Package %s>r:   r'   s    r*   __repr__Package.__repr__  s    ''r-   )r   r   r   r   r"   r   r   r   r   r   r   r$   r   r   )N NNNNUnknownrE   rE   rE   T__name__
__module____qualname____firstlineno____doc__r+   staticmethodr6   r=   rB   __static_attributes__r/   r-   r*   r
   r
      sO     C!J % %"(r-   r
   c                   >    \ rS rSrSrS	S jr\S 5       rS rS r	Sr
g)

Collectioni  z
A directory entry for a collection of downloadable packages.
These entries are extracted from the XML index file that is
downloaded by ``Downloader``.
Nc                     Xl          U=(       d    UU l         X l         S U l         U R                  R                  U5        g r9   )r   r   childrenpackagesr%   r&   )r'   r   rQ   r   r(   s        r*   r+   Collection.__init__  sD    6JB	0 	, 	3 	R r-   c                 ^   [        U [        5      (       a  [        R                  " U 5      n U R                   H(  n[        U R                  U   5      U R                  U'   M*     U R                  S5       Vs/ s H  o"R                  S5      PM     nn[        SSU0U R                  D6$ s  snf )NitemrefrQ   r/   )r0   r1   r   r2   r3   findallgetrO   )r4   r5   childrQ   s       r*   r6   Collection.fromxml1  s    c3##C(C::C!#**S/2CJJsO 25++f2EF2EIIe$2EF:8:szz:: Gs   7B*c                 4    U R                   UR                   :  $ r9   r:   r;   s     r*   r=   Collection.__lt__:  r?   r-   c                      SU R                   -  $ )Nz<Collection %s>r:   rA   s    r*   rB   Collection.__repr__=  s     477**r-   )rQ   r   r   rR   r9   rF   r/   r-   r*   rO   rO     s*    !$ ; ;"+r-   rO   c                       \ rS rSrSrSrg)DownloaderMessageiF  zOA status message object, used by ``incr_download`` to
communicate its progress.r/   N)rG   rH   rI   rJ   rK   rM   r/   r-   r*   r`   r`   F  s    !r-   r`   c                       \ rS rSrSrS rSrg)StartCollectionMessageiK  z<Data server has started working on a collection of packages.c                     Xl         g r9   
collectionr'   re   s     r*   r+   StartCollectionMessage.__init__N      $r-   rd   NrG   rH   rI   rJ   rK   r+   rM   r/   r-   r*   rb   rb   K  s
    F%r-   rb   c                       \ rS rSrSrS rSrg)FinishCollectionMessageiR  z=Data server has finished working on a collection of packages.c                     Xl         g r9   rd   rf   s     r*   r+    FinishCollectionMessage.__init__U  rh   r-   rd   Nri   r/   r-   r*   rk   rk   R  s
    G%r-   rk   c                       \ rS rSrSrS rSrg)StartPackageMessageiY  z-Data server has started working on a package.c                     Xl         g r9   packager'   rr   s     r*   r+   StartPackageMessage.__init__\      r-   rq   Nri   r/   r-   r*   ro   ro   Y  
    7r-   ro   c                       \ rS rSrSrS rSrg)FinishPackageMessagei`  z.Data server has finished working on a package.c                     Xl         g r9   rq   rs   s     r*   r+   FinishPackageMessage.__init__c  ru   r-   rq   Nri   r/   r-   r*   rx   rx   `  
    8r-   rx   c                       \ rS rSrSrS rSrg)StartDownloadMessageig  z.Data server has started downloading a package.c                     Xl         g r9   rq   rs   s     r*   r+   StartDownloadMessage.__init__j  ru   r-   rq   Nri   r/   r-   r*   r}   r}   g  r{   r-   r}   c                       \ rS rSrSrS rSrg)FinishDownloadMessagein  z/Data server has finished downloading a package.c                     Xl         g r9   rq   rs   s     r*   r+   FinishDownloadMessage.__init__q  ru   r-   rq   Nri   r/   r-   r*   r   r   n  
    9r-   r   c                       \ rS rSrSrS rSrg)StartUnzipMessageiu  z,Data server has started unzipping a package.c                     Xl         g r9   rq   rs   s     r*   r+   StartUnzipMessage.__init__x  ru   r-   rq   Nri   r/   r-   r*   r   r   u  s
    6r-   r   c                       \ rS rSrSrS rSrg)FinishUnzipMessagei|  z-Data server has finished unzipping a package.c                     Xl         g r9   rq   rs   s     r*   r+   FinishUnzipMessage.__init__  ru   r-   rq   Nri   r/   r-   r*   r   r   |  rv   r-   r   c                       \ rS rSrSrS rSrg)UpToDateMessagei  z/The package download file is already up-to-datec                     Xl         g r9   rq   rs   s     r*   r+   UpToDateMessage.__init__  ru   r-   rq   Nri   r/   r-   r*   r   r     r   r-   r   c                       \ rS rSrSrS rSrg)StaleMessagei  z3The package download file is out-of-date or corruptc                     Xl         g r9   rq   rs   s     r*   r+   StaleMessage.__init__  ru   r-   rq   Nri   r/   r-   r*   r   r     s
    =r-   r   c                       \ rS rSrSrS rSrg)ErrorMessagei  z Data server encountered an errorc                 h    Xl         [        U[        5      (       a  [        U5      U l        g X l        g r9   )rr   r0   	Exceptionr1   message)r'   rr   r   s      r*   r+   ErrorMessage.__init__  s$    gy))w<DL"Lr-   )r   rr   Nri   r/   r-   r*   r   r     s
    *#r-   r   c                       \ rS rSrSrS rSrg)ProgressMessagei  z4Indicates how much progress the data server has madec                     Xl         g r9   progress)r'   r   s     r*   r+   ProgressMessage.__init__  s     r-   r   Nri   r/   r-   r*   r   r     s
    >!r-   r   c                       \ rS rSrSrS rSrg)SelectDownloadDirMessagei  z:Indicates what download directory the data server is usingc                     Xl         g r9   download_dirr'   r   s     r*   r+   !SelectDownloadDirMessage.__init__  s    (r-   r   Nri   r/   r-   r*   r   r     s
    D)r-   r   c                   `   \ rS rSrSrSr Sr Sr Sr Sr	 Sr
 S*S
 jr      S+S jrS rS rS rS rS rS,S jrS rS rS rS	S	SSSSS\R.                  4S jrS-S jrS-S jrS-S jrS-S jrS rS.S jrS-S jrS  r S! r!S" r"S# r#S$ r$\%" \#\$5      r&S% r'S& r(S' r)\%" \(\)5      r*S( r+S)r,g	)/
Downloaderi  zm
A class used to access the NLTK data server, which can be used to
download corpora and other data packages.
i  zChttps://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml	installedznot installedzout of datepartialNc                     U=(       d    U R                   U l         0 U l         0 U l         X l         S U l         S U l         0 U l         S U l         U R                  c  U R                  5       U l        g g r9   )
DEFAULT_URL_url_collections	_packages_download_dir_index_index_timestamp_status_cache_errorsdefault_download_dir)r'   server_index_urlr   s      r*   r+   Downloader.__init__  s    $8(8(8	7E?)I@ $	L  	
 R %!%!:!:!<D &r-   TFc                 6   SnUc  U R                   n[        SU-  5        U(       ae  [        SS[        U R                  5      -   -  5        [        SU R                  -  5        [        SS[        U R                  5      -   -  5        US-  nS=p/ n
U(       a  U
R	                  S5        U(       a  U
R	                  S	5        U
 GH  n[        S
UR                  5       -  5        US-  n[        [        X5      " 5       [        S9 GHF  nU R                  X5      nXR                  :X  a	  U(       a  M-  XR                  :X  a  SnXR                  :X  a  Sn	U R                  SU R                  SU R                  SU R                  S0U   n[        R                  " SUR                   =(       d    UR"                  -   SSS9SS  n[        SR%                  XR"                  R'                  SS5      U5      5        U[        UR)                  S5      5      -  nU(       d  GM  US:  d  GM"  [+        S5      nUR-                  5       S;   a      g SnGMI     [        5         GM     SnU(       a  US-  nU	(       a  US-  n[        [        R                  " US -   SS!S"95        g )#Nr   z!Using default data directory (%s)=   z Data server index for <%s>   FrR   collectionsz%s:r   r5   T*-P ---------------------------K                              subsequent_indent   z  [{}] {} {}   .
zHit Enter to continue: )xqz([*] marks installed packagesz+; [-] marks out-of-date or corrupt packagesz+; [P] marks partially installed collections)L   )r   width)r   printlenr   append
capitalizesortedgetattrr1   status	INSTALLEDSTALEPARTIALNOT_INSTALLEDtextwrapfillr   r   formatljustr    inputlower)r'   r   show_packagesshow_collectionsheadermore_promptskip_installedlinesstaler   
categoriescategoryinfor   prefixr   
user_inputmsgs                     r*   listDownloader.list  sA    --L5DE#c$))n,-./$));<#c$))n,-.QJE
j)m,"H%(--//0QJEwt68cBT8^^+ZZ' E\\)"GNNCJJLL#&&	
   }}		 4TWW5rX# n++FGGMM"c4JDQRTZZ-..;52:!&'@!AJ!'')Z7E/ C0 G7 #8 .@@C@@ChmmC#IBGHr-   c                 V    U R                  5         U R                  R                  5       $ r9   )_update_indexr   valuesrA   s    r*   rR   Downloader.packages9  s!    ~~$$&&r-   c                     U R                  5         U R                  R                  5        VVs/ s H  u  pUR                  S:X  d  M  UPM     snn$ s  snnf Ncorporar   r   itemsr   r'   r   pkgs      r*   r   Downloader.corpora=  B    %)^^%9%9%;W%;	szzY?V%;WWW   AAc                     U R                  5         U R                  R                  5        VVs/ s H  u  pUR                  S:w  d  M  UPM     snn$ s  snnf r   r   r   s      r*   modelsDownloader.modelsA  r   r   c                 V    U R                  5         U R                  R                  5       $ r9   )r   r   r   rA   s    r*   r   Downloader.collectionsE  s#      ''))r-   c                 R    [        U[        5      (       a  U R                  U5      $ U$ r9   )r0   r1   r   )r'   
info_or_ids     r*   _info_or_idDownloader._info_or_idM  s$    j#&&99Z((r-   c              #     #    Uc  U R                   n[        U5      v   [        U[        [        45      (       a  U R                  XU5       S h  vN   g  U R                  U5      n[        U[        5      (       a?  [        U5      v   U R                  UR                  X#5       S h  vN   [        U5      v   g U R                  XBU5       S h  vN   g  N! [        [        4 a  n[        S SU SU 35      v    S nAg S nAff = f N` N:7f)NzError loading : )r   r   r0   r   tuple_download_listr  OSError
ValueErrorr   rO   rb   incr_downloadrQ   rk   _download_package)r'   r  r   forcer   es         r*   r  Downloader.incr_download\  s     --L*<88 j4-00**:UKKK	##J/D dJ''(..))$--MMM)$// --d%HHH% L $ 	t~j\A3%GHH	 N
 Is[   ADCDC 'AD(D	)'DDDD(D<DDDDc                 X    [        U[        5      (       a  g[        UR                  5      $ )Nr   )r0   r
   r   rR   )r'   rU   s     r*   _num_packagesDownloader._num_packagesx  s!    dG$$t}}%%r-   c              #   8  ^ #    [        [        U5      5       H  n T R                  X   5      X'   M     [        U 4S jU 5       5      nSn[        U5       H  u  pH[        U[        5      (       a  SU-  n	O[        UR                  5      U-  n	T R                  XU5       H:  n
[        U
[        5      (       a  [        XzR                  U	-  -   5      v   M6  U
v   M<     USU	-  -  nM     g ! [        [        4 a  n[        X   U5      v    S nA  g S nAff = f7f)Nc              3   F   >#    U  H  nTR                  U5      v   M     g 7fr9   )r  ).0rU   r'   s     r*   	<genexpr>,Downloader._download_list.<locals>.<genexpr>  s     F4--d33s   !r   g      ?d   )ranger   r  r  r  r   sum	enumerater0   r
   rR   r  r   r   )r'   r   r   r  ir  num_packagesr   rU   deltar   s   `          r*   r  Downloader._download_list~  s     s5z"A++EH5 # FFF 'GA$((l*DMM*\9))$eDc?33)(\\E5I*IJJI	 E e#H ( Z( "58Q//s.   DC,B;D,D<DDDDc              #   8  #    [        U5      v   [        S5      v   U R                  X5      nU(       d7  X@R                  :X  a(  [	        U5      v   [        S5      v   [        U5      v   g U R                  R                  UR                  S 5        [        R                  R                  X!R                  5      n[        R                  R                  U5      (       a2  X@R                  :X  a  [        U5      v   [        R                   " U5        [        R"                  " USS9  [        R"                  " [        R                  R                  X!R$                  5      SS9  ['        U5      v   [        S5      v    [)        UR*                  5      n[-        US5       n[/        SUR0                  S-  5      n[2        R4                  " 5        HX  n	UR7                  S5      n
UR9                  U
5        U
(       d    O.U	S	-  S:X  d  M9  [        [;        S
SSX-  -  -   5      5      v   MZ     S S S 5        UR=                  5         [C        U5      v   [        S
5      v   UR                  RE                  S5      (       a  [        R                  R                  X!R$                  5      nURF                  (       dK  [        R                  R                  [        R                  R                  XR                  5      5      (       a5  [I        U5      v   [K        X\SS9 H  nXl&        Uv   M     [O        U5      v   [        U5      v   g ! , (       d  f       GN!= f! [>         a8  n[A        USUR                  < SUR*                  < SU< 35      v    S nAg S nAff = f7f)Nr   r  T)exist_ok   wbr    @     P   r   zError downloading z from <>:
  .zipF)verbose)(ro   r   r   r   r   rx   r   popr   r   r   r!   r"   existsr   r   removemakedirsr   r}   r   r   openmaxr   	itertoolscountreadwritemincloser  r   r   endswithr$   r   _unzip_iterrr   r   )r'   r   r   r  r   filepathinfileoutfile
num_blocksblocksr  zipdirr   s                 r*   r  Downloader._download_package  s    !$''a   T0>>1!$''!#&&&t,, 	tww- 77<<mm<77>>(###"4((IIh 	L40
BGGLL{{;dK #4((a  	TXX&Fh% DII$;<
&__.EI.AMM!$qyA~-c"a"@R:S6S.TUU / & LLN $D))b!! ==!!&))WW\\,<F zzRWW^^BGGLL,IJJ'--&xGC"&KI H ).."4((A &%  	>BggtxxQRS  	sP   E:N=!M A$M#M)M DN
MM 
N.NNNN[nltk_data] c	           
      \  ^^ [         R                  " [        US9mUc  Ub  X l        U R	                  5         gSUU4S jjn	U R                  XU5       GHT  n
[        U
[        5      (       a  U	" U
R                  5        U(       a  [        U
R                  5      eU(       a    gSU l
        U(       da  T" S5        [        5       R                  5       nUS;   a3  U R                  U
R                  R                  UUUTUU5      (       d    gOUS;   a    gU(       a  M  [        U
[         5      (       a/  U	" SU
R"                  R                  -  5        TS	-  mT" T5        GM  [        U
[$        5      (       ab  T" T5        TS S
 mU R                  (       a"  U	" SU
R"                  R                  -  5        GMa  U	" SU
R"                  R                  -  5        GM  [        U
[&        5      (       a(  U	" SU
R                  R                  < SU< S35        GM  [        U
[(        5      (       a#  U	" SU
R                  R                  -  S5        GM  [        U
[*        5      (       a#  U	" SU
R                  R,                  -  S5        GM0  [        U
[.        5      (       d  GMH  U
R0                  nGMW     g)N)fileTc                 P   > T" [         R                  " U TU-   TU-   S-   S95        g )N    )initial_indentr   )r   r   )r@  prefix2r   print_tos     r*   show!Downloader.download.<locals>.show  s/    MM'-'7*07*:W*Dr-   Fz(Error installing package. Retry? [n/y/e])yY)r  EDownloading collection %rz   | z$Downloaded collection %r with errorszDone downloading collection %szDownloading package z to z...z!Package %s is already up-to-date!  zUnzipping %s.rD   )	functoolsr   r   r   _interactive_downloadr  r0   r   r   r  r   r   stripdownloadrr   r   rb   re   rk   ro   r   r   r"   r   r   )r'   r  r   quietr  r   halt_on_errorraise_on_errorprint_error_torK  r   choicerJ  s        `      @r*   rW  Downloader.download  s7    $$U@ '%1"&&(  ))*EJc<00%%(55$$#'DL  !KL!&!Z/#'== # , % % & - .$ $ (-$ $z1#( u!#'=>>83>>;L;LLM') (#C)@AA (!'<<  F"%.."3"3!4
 !!ACNNDUDU!UV $C)<=="{{~~|= $C99@3;;>>QSWX $C):;;_s{{/C/CCTJ $C)ABB'*'7'7y Kz r-   c                 >    U R                  X5      U R                  :H  $ r9   )r   r   r'   r  r   s      r*   is_staleDownloader.is_stale9  s    {{:4

BBr-   c                 >    U R                  X5      U R                  :H  $ r9   )r   r   r_  s      r*   is_installedDownloader.is_installed<  s    {{:4FFr-   c                 x    Uc  U R                   R                  5         g U R                   R                  US 5        g r9   )r   clearr-  r'   r   s     r*   clear_status_cacheDownloader.clear_status_cache?  s/    :$$&""2t,r-   c                 >   Uc  U R                   nU R                  U5      n[        U[        5      (       a  UR                   Vs/ s H  o@R                  UR                  5      PM     nnU R                  U;   a  U R                  $ U R                  U;   a  U R                  $ U R                  U;   a  U R                  U;   a  U R                  $ U R                  U;   a  U R                  $ U R                  $ [        R                  R                  X#R                  5      nX R                   :w  a  U R                  X65      $ UR                  U R                   ;  a(  U R                  X65      U R                   UR                  '   U R                   UR                     $ s  snf )z
Return a constant describing the status of the given package
or collection.  Status can be one of ``INSTALLED``,
``NOT_INSTALLED``, ``STALE``, or ``PARTIAL``.
)r   r  r0   rO   rR   r   r   r   r   r   r   r   r   r!   r"   _pkg_statusr   )r'   r  r   r   r   
pkg_statusr;  s          r*   r   Downloader.statusE  sK    --L
+ dJ''9=G#++cff-JGzzZ'zz!+||#:-$2D2D
2R||###z1)))~~% ww||L--@H111''7777$"4"44262B2B42RD&&tww/))$''22) Hs   $Fc                     [         R                  R                  U5      (       d  U R                  $  [         R                  " U5      nUR                  [        UR                  5      :w  a  U R                  $ [        U5      UR                  :w  a  U R                  $ UR                  S5      (       a  US S n[         R                  R                  U5      (       d  U R                  $ [         R                  R                  U5      (       d  U R                  $ [        S [         R                   " U5       5       5      nXQR"                  :w  a  U R                  $ U R                  $ ! [
         a    U R                  s $ f = f)Nr+  rQ  c              3      #    U  HO  u  pnU  HB  n[         R                  " [         R                  R                  X5      5      R                  v   MD     MQ     g 7fr9   )r   statr   r!   st_size)r  d_filesfs        r*   r  )Downloader._pkg_status.<locals>.<genexpr>  sD       #4KA%A Q*+33 4#4s   AA)r   r   r.  r   rp  r  rq  r   r   r   md5_hexdigestr   r9  r   isdirr  walkr   )r'   r   r;  filestatunzipdirr   s         r*   rk  Downloader._pkg_statusg  s,   ww~~h''%%%	&wwx(H s499~-:: "dmm3:: V$$}H77>>(++~~%77==**zz!  #%778#4  M
  2 22zz! ~~7  	&%%%	&s   E$ $E=<E=c                     U R                  5         U R                  5        H4  nU R                  U5      U R                  :X  d  M$  U R	                  X1US9  M6     g)z1
Re-download any packages whose status is STALE.
)rX  r   N)rh  rR   r   r   rW  )r'   rX  r   r   s       r*   r&   Downloader.update  sE     	!==?C{{34::-cv> #r-   c                 2   U R                   b4  Uc1  [        R                  " 5       U R                  -
  U R                  :  d  gU=(       d    U R                  U l        [
        R                  R                  [        R                  " [        U R                  5      5      R                  5       5      U l         [        R                  " 5       U l        U R                   R                  S5       Vs/ s H  n[        R                  U5      PM     nnU Vs0 s H  o"R                  U_M     snU l        U R                   R                  S5       Vs/ s H  n["        R                  U5      PM     nnU Vs0 s H  oDR                  U_M     snU l        U R$                  R'                  5        H  n[)        UR*                  5       H  u  pxXR                   ;   a  U R                   U   UR*                  U'   M2  XR$                  ;   a  U R$                  U   UR*                  U'   M_  [-        SR/                  U5      5        UR*                  U	 M     M     U R$                  R'                  5        H}  n0 nU/n	U	 HZ  n
[1        U
["        5      (       a  U	R3                  U
R*                  5        M5  [1        U
[        5      (       a  XU
R                  '   MZ  M\     UR'                  5       Ul        M     U R6                  R9                  5         gs  snf s  snf s  snf s  snf )zA helper function that ensures that self._index is
up-to-date.  If the index is older than self.INDEX_TIMEOUT,
then download it again.Npackages/packagecollections/collectionz.removing collection member with no package: {})r   timer   INDEX_TIMEOUTr   nltk	internalsElementWrapperr   r2   r   getrootrW   r
   r6   r   r   rO   r   r   r  rQ   r   r   r0   extendrR   r   rf  )r'   r   prR   cr   re   r  child_idqueuerY   s              r*   r   Downloader._update_index  s}    KKyy{T222T5G5GG $499	 nn33gdii0199;
 !%		 150C0CDV0WX0W1GOOA&0WX+348a$$'84 ,0;;+>+>?W+X
+XaJq!+X 	 
 /::kTT1Wk: ++224J()<)<=~~--1^^H-EJ''*!2!22-1->->x-HJ''*HOO$
 #++A.  > 5 ++224JHLEeZ00LL0w//).UXX&  #+//"3J 5 	  "M Y4
 ;s   ,LL
L1Lc                 :    U R                  5         U R                  $ )z
Return the XML index describing the packages available from
the data server.  If necessary, this index will be downloaded
from the data server.
)r   r   rA   s    r*   indexDownloader.index  s     	{{r-   c                     U R                  5         XR                  ;   a  U R                  U   $ XR                  ;   a  U R                  U   $ [        SU-  5      e)zCReturn the ``Package`` or ``Collection`` record for the
given item.Package %r not found in index)r   r   r   r  rg  s     r*   r   Downloader.info  sW     	>>"%%"""$$R((82=>>r-   c                 *   U R                  5         U R                  R                  S5       H  nUR                  S5      U:X  d  M  Us  $    U R                  R                  S5       H  nUR                  S5      U:X  d  M  Us  $    [	        SU-  5      e)z-Return the XML info record for the given itemr  r   r  r  )r   r   rW   rX   r  )r'   r   rr   re   s       r*   xmlinfoDownloader.xmlinfo  s    {{**+=>G{{4 B& ? ++--.FGJ~~d#r)!! H 82=>>r-   c                     U R                   $ )z)The URL for the data server's index file.)r   rA   s    r*   _get_urlDownloader._get_url  s    yyr-   c                 X    U R                   n U R                  U5        g!   X l         e = f)zm
Set a new URL for the data server. If we're unable to contact
the given url, then the original url is kept.
N)r   r   )r'   r   original_urls      r*   _set_urlDownloader._set_url  s/    
 yy	s#	$Is     	)c                 "   S[         R                  ;   a  g[        R                  R                   HQ  n[         R                  R                  U5      (       d  M)  [        R                  R                  U5      (       d  MO  Us  $    [        R                  S:X  a(  S[         R                  ;   a  [         R                  S   nO0[         R                  R                  S5      nUS:X  a  [        S5      e[         R                  R                  US5      $ )aw  
Return the directory to which packages will be downloaded by
default.  This value can be overridden using the constructor,
or on a case-by-case basis using the ``download_dir`` argument when
calling ``download()``.

On Windows, the default download directory is
``PYTHONHOME/lib/nltk``, where *PYTHONHOME* is the
directory containing Python, e.g. ``C:\Python25``.

On all other platforms, the default directory is the first of
the following which exists or which can be created with write
permission: ``/usr/share/nltk_data``, ``/usr/local/share/nltk_data``,
``/usr/lib/nltk_data``, ``/usr/local/lib/nltk_data``, ``~/nltk_data``.
APPENGINE_RUNTIMENwin32APPDATAz~/z+Could not find a default download directory	nltk_data)r   environr  datar   r.  r  is_writablesysplatform
expanduserr  r!   )r'   nltkdirhomedirs      r*   r   Downloader.default_download_dir
  s    " "**, yy~~Gww~~g&&4>>+E+Eg+N+N &
 <<7"yBJJ'>jj+G gg((.G$ !NOO ww||G[11r-   c                     U R                   $ )z
The default directory to which packages will be downloaded.
This defaults to the value returned by ``default_download_dir()``.
To override this default on a case-by-case basis, use the
``download_dir`` argument when calling ``download()``.
)r   rA   s    r*   _get_download_dirDownloader._get_download_dir1  s     !!!r-   c                 D    Xl         U R                  R                  5         g r9   )r   r   rf  r   s     r*   _set_download_dirDownloader._set_download_dir:  s    )  "r-   c                    [         R                  R                  SS5      R                  5       S:X  a  [	        U 5      R                  5         g  SS Kn [        U 5      R                  5         g ! [         a    [	        U 5      R                  5          g f = f! UR                   a    [	        U 5      R                  5          g f = f)N'NLTK_DOWNLOADER_FORCE_INTERACTIVE_SHELLfalsetruer   )r   r  rX   r   DownloaderShellruntkinterImportErrorDownloaderGUImainloopTclError)r'   r  s     r*   rU   Downloader._interactive_downloadE  s     JJNNDgNTTV D!%%'	
	($((*  	D!%%'	  	(D!%%'	(s$   A- B -#BB)CC)r   r   r   r   r   r   r   r   )NN)NTTTFF)NFr9   )FrC  )-rG   rH   rI   rJ   rK   r  r   r   r   r   r   r+   r   rR   r   r   r   r  r  r  r  r  r  stderrrW  r`  rc  rh  r   rk  r&   r   r  r   r  r  r  propertyr   r   r  r  r   rU  rM   r/   r-   r*   r   r     s7    M XK! I$#ME"GE=N :Ix'XX*I8&$2@)H zz^@CG- 3D"H?<#|?	?
 8X
&C%2N"#
 -/@AL(r-   r   c                   D    \ rS rSrS rS rS rS rS rS r	S r
S	 rS
rg)r  i]  c                     Xl         g r9   _ds)r'   
dataservers     r*   r+   DownloaderShell.__init__^  s    r-   c                     [        S5        S[        S U 5       5      -
  [        U5      S-
  -  S-  n[        SUR                  U5      -   5        [        S5        g )NzK---------------------------------------------------------------------------D   c              3   8   #    U  H  n[        U5      v   M     g 7fr9   )r   )r  os     r*   r  ;DownloaderShell._simple_interactive_menu.<locals>.<genexpr>c  s     01A   r   r   rG  )r   r  r   r!   )r'   optionsspcs      r*   _simple_interactive_menu(DownloaderShell._simple_interactive_menua  sN    hC0000c'lQ6FG#Mfsxx(()hr-   c                 &   [        S5         U R                  SSSSSS5        [        S	5      R                  5       nU(       d  [        5         MC  UR	                  5       R                  5       S
   nUR                  5       SS  n US:X  a:  [        5         U R                  R                  U R                  R                  SSS9  OsUS:X  a  U R                  5         O\US:X  a  U R                  5         OEUS;   a  g US:X  a  U R                  U5        O&US:X  a  U R                  5         O[        SU-  5         [        5         GM7  ! [         a  n[        SU-  5         S nAN*S nAf[         a"  n[        SUR                  -  5         S nANTS nAff = f)NNLTK DownloaderTzd) Downloadzl) Listz
 u) Updatez	c) Configzh) Helpzq) QuitzDownloader> r   r   lF)r   r   hr  )r   r   rr  uzCommand %r unrecognizedzError reading from server: %szError connecting to server: %s)r   r  r   rV  r   r    r  r   r   _simple_interactive_help_simple_interactive_config_simple_interactive_download_simple_interactive_updater   r   reason)r'   r   commandargsr  s        r*   r  DownloaderShell.rung  si    )) ~.446J &&(..03G##%ab)DCc>GHHMM$(("7"7SWMX^113^335
*^55d;^3353j@A GK >  ;59:: C6ABBCsH   ?E E E 2E 9E E (E 
FE!!F.FFc                 n   U(       a%  U H  n U R                   R                  USS9  M      g  [	        5         [	        S5        [        S5      nUR                  5       S:X  a2  U R                   R                  U R                   R                  SSSS9  Mg  UR                  5       S	;   a  g U(       a3  UR                  5        H  n U R                   R                  USS9  M      g M  ! [        [        4 a  n[	        U5         S nAM  S nAff = f! [        [        4 a  n[	        U5         S nAMo  S nAff = f)
NrG  r   Tz*Download which package (l=list; x=cancel)?  Identifier> r  F)r   r   r   r   r   rD   )
r  rW  r  r  r   r   r   r   r   r    )r'   r  argr  r   r   s         r*   r  ,DownloaderShell._simple_interactive_download  s   HH%%c&%9  BC"#34
##%,HHMM--$$('+	 "  %%'>9(..0% HH--b-@ 1
 )   , !HH* !(4 %!!HH%s/   C%D%D5DDD4D//D4c           	      :    / nS=p#[        [        U R                  S5      " 5       [        S9 H_  nU R                  R	                  U5      U R                  R
                  :X  d  M8  UR                  UR                  UR                  45        Ma     [        5         U(       a  [        S5        U HL  u  pV[        R                  " SU-   SSS9S	S  n[        S
R                  UR                  SS5      U5      5        MN     [        5         [        S5      nUR                  5       S:X  a'  U H   u  pV U R                  R!                  USS9  M"     g UR                  5       S;   a  g O[        S5        g GMr  ! ["        [$        4 a  n	[        U	5         S n	A	Mk  S n	A	ff = f)NFrR   r   z/Will update following packages (o=ok; x=cancel)r   r   r   r   r   z  [ ] {} {}r   r   r  r  rG  r  r  zNothing to update.)r   r   r  r1   r   r   r   r   r   r   r   r   r   r   r   r   rW  r  r  )
r'   stale_packagesr   r   r   pidpnamer   r   r  s
             r*   r  *DownloaderShell._simple_interactive_update  sk   N##Ewtxx<>CH88??4(DHHNN:"))477DII*>? I GGH"0JC#== E*B(cD -..syyS/A4HI	 #1
 "#34
##%,&4
% HH--c&-A '5
 %%'>9 : *+; , !(4 %!!HH%s   0E44FFFc                 p    [        5         [        S5        [        S5        [        S5        [        S5        g )Nz	Commands:zH  d) Download a package or collection     u) Update out of date packagesz1  l) List packages & collections          h) Helpz1  c) View & Modify Configuration          q) Quit)r   rA   s    r*   r  (DownloaderShell._simple_interactive_help  s.    kV	
 	ABABr-   c                    [        5         [        S5        [        SU R                  R                  -  5        [        S[        U R                  R	                  5       5      -  5        [        S[        U R                  R                  5       5      -  5        [        5         [        S5        [        SU R                  R                  -  5        g )NzData Server:z  - URL: <%s>z$  - %d Package Collections Availablez$  - %d Individual Packages AvailablezLocal Machine:z  - Data directory: %s)r   r  r   r   r   rR   r   rA   s    r*   _show_configDownloaderShell._show_config  s    no,-4s488;O;O;Q7RRS4s488;L;L;N7OOP&)>)>>?r-   c                    U R                  5          [        5         U R                  SSSS5        [        S5      R	                  5       R                  5       nUS:X  a  U R                  5         OUS:X  ao  [        S5      R	                  5       nUS	;   a  [        S
5        O[        R                  R                  U5      (       a  X R                  l
        Ot[        SU-  5        OeUS:X  aX  [        S5      R	                  5       nUS	;   a  [        S
5        O4UR                  S5      (       d  SU-   n X0R                  l        OUS:X  a  g GM9  ! [         a  n[        SU< SU 35         S nAN$S nAff = f)Nzs) Show Configzu) Set Server URLzd) Set Data Dirzm) Main MenuzConfig> r@  rr  z  New Directory> )rD   r   r   XQz  Cancelled!z)Directory %r not found!  Create it first.r  z  New URL> )zhttp://https://r  zError reading <r*  m)r  r   r  r   rV  r   r   r   rx  r  r   
startswithr   r   )r'   r   
new_dl_dirnew_urlr  s        r*   r  *DownloaderShell._simple_interactive_config  sN   G)) "57H. z*00288:JS !!#s""#67==?
!99.)WW]]:..,6HH)E
RSs".44666.)"--.EFF",w"6F'. s"9 2 % F{&DEEFs   0E 
E1E,,E1r  N)rG   rH   rI   rJ   r+   r  r  r  r  r  r  r  rM   r/   r-   r*   r  r  ]  s.    'R<@C@r-   r  c                      \ rS rSrSr/ SQr SSSSS.r SSS	S
S
SS.r Sr / SQr	 \ H  r
\
\;   a  M   e   \ H  r
\
\;   a  M   e   \	 H  r
\
\;   a  M   e   Sr\R                  S\R                  S\R                  S\R                   S0rSrSrSrSrSrSDS jrS rS rS rS rS rS rSES  jrS! rS" r S# r!S$ r"S% r#S& r$S' r%S(r&Sr'S) r(S* r)S+ r*S
r+S, r,S- r-S. r.S/ r/S0 r0S1 r1S2 r2S3 r3S4 r4S5 r5S6 r6\7Rp                  " S75      r9S8 r:S9 r;Sr<S: r=S; r>S< r?S= r@S> rA " S? S@\BR                  5      rDSArESB rFSCrGg)Fr  i  zI
Graphical interface for downloading packages from the NLTK data
server.
)rD   
IdentifierNameSizeStatusUnzipped Size	CopyrightContactLicenseAuthorSubdirChecksumr   r%  )rD   r  r  r  r   r   -   
      )rD   r  r  r  r  r     )rD   r  r  r  r  )z#000z#ccc)z#afaz#080)z#ffaz#880)z#faaz#800)#fffz#888)r  z#45c)#aaaz#67a)z#f00r  zhelvetica -16 boldc                    SS K nSSKJn  Xl        X l        [
        R                  " 5       U l        / U l        / U l	        SU l
        0 U l        / U l        SU l        U R                  S5        UR                  5       =oPl        UR#                  S5        UR%                  S5        UR'                  U R(                  S   S9  UR+                  S	U R,                  5        UR+                  S
U R,                  5        SU l        0 U l        U R3                  5         U R5                  5          U R7                  5         U R?                  5         U RA                  5         U RB                  RE                  S5        U RB                  R+                  SU RF                  5        g ! [8         a  nU" SU5         S nANzS nAf[:         a  nU" SUR<                  5         S nANS nAff = f)Nr   	showerrorFzNLTK Downloader Started!z+50+50r  r   )
backgroundz<Control-q>z<Control-x>Error reading from serverError connecting to serverz	<Destroy>)$r  tkinter.messageboxr
  r  _use_threads	threadingLock_download_lock_download_msg_queue_download_abort_queue_downloading_afterid_log_messages_log_indent_logTktopgeometrytitle	configure_BACKDROP_COLORbinddestroy
_destroyed_column_vars_init_widgets
_init_menu_fill_tabler   r   r  
_show_info_select_columns_tableselect_destroy)r'   r  use_threadsr  r
  r  r  s          r*   r+   DownloaderGUI.__init__\  s    	0' (nn.#% %'"!   		,- !%hX		#$!5!5a!89 	-- 		> 	1 	dmm4  	61155 	>2AHH==	>s$   'F 
G#	F11G>GGc                     U R                   R                  SR                  [        R                  " 5       SU R
                  -  U5      5        g )Nz{} {}{}z | )r  r   r   r  ctimer  r'   r   s     r*   r  DownloaderGUI._log  s9    !!TZZ\543C3C+CSI	
r-   c           	      d  ^  SS K nSSKJn  UR                  T R                  SSSSS9nUR                  SSS	S
9  UR                  SSS9  UR                  SSS9  UR                  USS9R                  SSS9  UR                  U5      nUR                  SSSS9  UR                  U5      nUR                  SSSS9  UR                  U5      nUR                  SSSS9  UR                  USS9R                  SSS9  UR                  U5      nUR                  SSSS9  UR                  USS9R                  SSS9  UR                  T R                  SST R                  S   S9nUR                  SSS9  ST R                  S'   ST R                  S'   / SQT l
        0 T l        [        T R                  5       Hq  u  pUR                  XJT R                  S9nUR                  SU	S-   S-  S-  S9  UR                  S T R                   5        UT R                  U
R#                  5       '   Ms     T R$                   Vs/ s H  nT R&                  R)                  US5      PM!     nnU" UT R$                  USS!T R*                  S"9T l        T R,                  R/                  ST R0                  S   S#9  [        T R$                  5       HD  u  pT R2                  R)                  UT R4                  5      nT R,                  R/                  XS$9  MF     T R,                  R                  SS	S%9  T R,                  R7                  5         T R,                  R9                  S&T R:                  5        T R,                  R                  S'T R<                  5        T R,                  R                  S(T R:                  5        T R,                  R                  S)T R>                  5        T R,                  R                  S*T R@                  5        T R,                  R                  S+T RB                  5        UR                  SSS9  S,S-T RD                  4S.S/T RF                  4/n0 T l$        [        U5       H  u  n	u  nnnUR                  X{S09R                  SU	S1S9  URK                  US2S3S4S4S59nUU4T RH                  U'   UR                  S(T RL                  5        UR                  S U4U 4S6 jj5        UR                  SU	S7S9  M     T R                  R                  S T RL                  5        URO                  US8T R:                  SS99T l(        T RP                  R                  SS:9  URO                  US;T RR                  SS99T l*        T RT                  R                  S<S:9  UR                  US=T R                  S   T R                  S   S>9T l+        URY                  US?S!T RZ                  S   S@SSA9T l.        T R_                  5         T R\                  R                  S<S:9  T RV                  R                  SS:9  g s  snf )BNr   )Tableraisedr(     )reliefborderpadxpadyr  Tboth)sidexpandr   r   )weight)height)columnrownews)r?  r@  stickyr      r%     )r8  r9  r  bottomr   )sider   r7  highlightthickness)CollectionsCorporaModelszAll Packages)textfontleftr  )rF  r8  z
<Button-1>   )column_weightsrG  listbox_heightreprfunc)
foregroundr   )r<  r   z<Double-Button-1>z<space>z<Return>z<Left>z<Right>z<Control-a>r   zServer Index:r   zDownload Directory:)rK  r  couriergroovez#007aff)rL  r6  disabledforegroundrR  c                 &   > TR                  U5      $ r9   
_info_edit)r  r5   r'   s     r*   <lambda>-DownloaderGUI._init_widgets.<locals>.<lambda>  s    8Lr-   ewDownload)rK  r  r   )rF  RefreshrightrD   )rK  rR  r     sunken)r   r>  r  r6  r7  )0r  nltk.draw.tabler3  Framer  packgrid_rowconfiguregrid_columnconfiguregridr  
_tab_names_tabsr  Label	_TAB_FONTr   _select_tabr   COLUMNSCOLUMN_WEIGHTSrX   _table_reprfuncr)  columnconfig_MARK_COLORCOLUMN_WIDTHSDEFAULT_COLUMN_WIDTHfocusbind_to_listboxes	_download_table_mark	_prev_tab	_next_tab	_mark_allr  r  _infoEntry
_info_saveButton_download_button_refresh_refresh_button_progresslabelCanvas_PROGRESS_COLOR_progressbar_init_progressbar)r'   r  r3  f1tabframe
tableframebuttonframe	infoframeprogressframer  tablabelr?  rO  r   r   r5   callbackentrys   `                  r*   r$  DownloaderGUI._init_widgets  s    	) ]]488HQQQ]O
E$V4
Qq)
!,b#((q(9==$QAf5]]2&
qa7mmB'q8b#((q(9MM"%	aQv6b#((q(9HH111E1Ea1H & 
 	s3)*%& O
0FAMM(4>>MJEJJF1q5A+);J<JJ|T%5%56&+DJJsyy{#	 1 LP<<X<$--11&!<<XLL) ))
 	  t/?/?/B C"4<<0IA&&**643L3LMEKK$$Q$4 1 	62%%&94>>JD$4$45T^^44>>2DNN37 	&&q&3 OT]]32D4J4JK
 
)24%A%UHMM)M055QAc5RMM#,$ " E  %h/DJJsOJJz4??3JJ|3%LMJJaQtJ4 *9  	lDOO4 !(j$.. !/ !
 	"""/&~~ia  .  
 	!!w!/ &mm++A.++A.	 , 
 $NN++A. + 
 	 G,  f -W Ys   ;&X-c                   ^  SS K nUR                  T R                  5      nUR                  USS9nUR                  SST R                  SS9  UR                  5         UR                  SSU 4S jS	9  UR                  S
SU 4S jS	9  UR                  5         UR                  SST R                  S	9  UR                  5         UR                  SST R                  SS9  UR                  SSUS9  UR                  USS9nT R                  R                  SS   H|  nUR                  T R                  5      nUT R                  ;  d   eUT R                  U'   UT R                  ;   a  UR                  S5        UR                  USUT R                   S9  M~     UR                  SSUS9  UR                  USS9nT R                  R                  SS   H  nUR                  SU-  U4U 4S jjS9  M     UR                  5         T R                  R                  SS   H  nUR                  SU-  U4U 4S jjS9  M     UR                  SSUS9  UR                  USS9nUR                  SST R"                  S	9  UR                  SST R$                  SS9  UR                  SSUS9  T R                  R'                  S T R$                  5        T R                  R)                  US!9  g )"Nr   )tearoffr]  Return)r  	underliner  acceleratorzChange Server Index   c                  &   > T R                  S5      $ )Nr   rX  rA   s   r*   rZ  *DownloaderGUI._init_menu.<locals>.<lambda>  s    DOOE2r-   )r  r  r  zChange Download Directoryc                  &   > T R                  S5      $ )Nr   rX  rA   s   r*   rZ  r  "  s    DOON;r-   zShow Logr%  Exitr   zCtrl-xFile)r  r  menur(  )r  r  variabler  Viewz
Sort by %sc                 <   > TR                   R                  U S5      $ )N	ascendingr)  sort_byr  r'   s    r*   rZ  r  B  s    $++*=*=a*Mr-   )r  r  zReverse sort by %sc                 <   > TR                   R                  U S5      $ )N
descendingr  r  s    r*   rZ  r  I  s    $++*=*=a*Nr-   SortAboutInstructionsF1Helpz<F1>)r  )r  Menur  add_commandrv  add_separator	_show_logr!  add_cascader)  column_namesIntVarr#  INITIAL_COLUMNSsetadd_checkbuttonr(  abouthelpr   config)	r'   r  menubarfilemenuviewmenur?  varsortmenuhelpmenus	   `        r*   r%  DownloaderGUI._init_menu  s   ,,txx(<<<34>>x 	 	
 	 '2 	 	

 	-; 	 	

 	 :DNNS At|| 	 	
 	&AHE
 <<<3kk..qr2F..*C!2!2222(+Df%---
$$CAUAU %  3 	&AHE
 <<<3kk..qr2F  "V+"(M !  3
 	 kk..qr2F  *V3"(N !  3
 	&AHE<<<37aL Atyyd 	 	
 	&AHEfdii(W%r-   c                     U R                   R                  5        HR  u  pUR                  5       (       a  U R                  R	                  U5        M7  U R                  R                  U5        MT     g r9   )r#  r   rX   r)  show_columnhide_column)r'   r?  r  s      r*   r(  DownloaderGUI._select_columnsW  sL    ,,224KFwwyy''/''/	 5r-   c                 &   SSK Jn  U R                  R                  5          U R	                  5         U R                  R                  S5        g ! [
         a  nU" SU5         S nAN4S nAf[         a  nU" SUR                  5         S nANYS nAff = fNr   r	  r  r  )
r  r
  r  rh  r&  r   r   r  r)  r*  )r'   r
  r  s      r*   r  DownloaderGUI._refresh^  su    0##%	>
 	1	  	61155 	>2AHH==	>s#   A 
B	A&&B3BBc                 z    U R                  5         U R                  U   u  p#SUS'   SUS'   UR                  5         g )Nnormalstatera  r6  )r}  r{  rt  )r'   info_keyr  r  s       r*   rY  DownloaderGUI._info_editj  s8     JJx0!g"hr-   Nc                 $   U R                   nU R                  R                  5        HV  u  p4US   S:X  a  M  Ub#  UR                  UL a  UR                  S:w  a  UnM6  SUS'   SUS'   U" UR                  5       5        MX     UR                  5         g )Nr  disabledr  rU  r6  )r)  r{  r   widgetkeysymrX   rt  )r'   r  rt  r  r  s        r*   r}  DownloaderGUI._info_saveq  s~    #zz002OEW~+}U!2qxx87K!+g"*h%  3 	r-   c                    U R                   R                  U   R                  S5      (       a>  [        U[        5      (       a  SU-  $ US:  a  SUS-  -  $ US:  a  SUS-  -  $ S	US
-  -  $ US;   a  [	        U5      $ SU-  $ )Nr  z  %si   z	  %.1f KBg      @i   @z	  %.1f MBg      0Az	  %.1f GBg      A)r   rD   )r)  r  r9  r0   r1   )r'   r@  colvals       r*   ro  DownloaderGUI._table_reprfunc~  s    ;;##C(11&99#s##|#w"cIo66w"cIo66"cIo66'>s8OC<r-   c                     SSK Jn  XR                  R                  :X  a  g  XR                  l        U R	                  5         U R                  5         g ! [
         a  nU" S[        U5      5         S nAN2S nAff = f)Nr   r	  zError Setting Server Index)r  r
  r  r   r&  r  r1   r'  )r'   r   r
  r  s       r*   r  DownloaderGUI._set_url  s`    0((,,	<HHL 	  	<2CF;;	<s    A 
A9A44A9c                 2   SSK Jn  U R                  R                  U:X  a  g XR                  l         U R	                  5         U R                  5         g ! [
         a  nU" SU5         S nAN)S nAf[         a  nU" SUR                  5         S nANNS nAff = fr  )	r  r
  r  r   r&  r   r   r  r'  )r'   r   r
  r  s       r*   r  DownloaderGUI._set_download_dir  s~    088  L0 !-	>
 		  	61155 	>2AHH==	>s#   A 
B	A,,B9BBc                    [        SU R                  R                  5        U R                  R	                  5        H  u  pSUS'   UR                  SS5        M     U R                  S   S   R                  SU R                  R                  5        U R                  S   S   R                  SU R                  R                  5        U R                  R	                  5        H
  u  pSUS'   M     g )	Nzshowing infor  r  r   endr   r   r  )r   r  r   r{  r   deleteinsertr   )r'   r  cbs      r*   r'  DownloaderGUI._show_info  s    ndhhll+**,IE%E'NLLE" - 	

5!##Atxx||4

>"1%,,Q0E0EF**,IE'E'N -r-   c                    SSK Jn  [        U R                  5       Hc  u  p4UR	                  5       U R
                  :X  d  M%  US:  d  M-  U R                  US-
     R	                  5       U l         U R                  5       s  $    g ! [         a  nU" SU5         S nAM  S nAf[         a  nU" SUR                  5         S nAM  S nAff = fNr   r	  r   r  r  )
r  r
  r  rh  r   _tabr&  r   r   r  r'   r  r
  r  r  s        r*   rx  DownloaderGUI._prev_tab  s    00FAyy{dii'AE OOAE288:	F++--	 1
 ! >91== F:AHHEEFs$   /B
C	BC*CCc                    SSK Jn  [        U R                  5       Hy  u  p4UR	                  5       U R
                  :X  d  M%  U[        U R                  5      S-
  :  d  MC  U R                  US-      R	                  5       U l         U R                  5       s  $    g ! [         a  nU" SU5         S nAM  S nAf[         a  nU" SUR                  5         S nAM  S nAff = fr  )r  r
  r  rh  r   r  r   ri  r&  r   r   r  r  s        r*   ry  DownloaderGUI._next_tab  s    00FAyy{dii'ATZZ11D,E OOAE288:	F++--	 1
 ! >91== F:AHHEEFs$   B
C$	B33C CCc                     SSK Jn  UR                  S   R                  5       U l         U R                  5         g ! [         a  nU" SU5         S nAg S nAf[         a  nU" SUR                  5         S nAg S nAff = f)Nr   r	  rK  r  r  )	r  r
  r  r   r  r&  r   r   r  )r'   eventr
  r  s       r*   rl  DownloaderGUI._select_tab  sh    0LL(..0		> 	61155 	>2AHH==	>s!   ; 
A=	AA= A88A=r   c                 ~   U R                   R                  5       nU R                   R                  5         U R                  S:X  a  U R                  R                  5       nOU R                  S:X  a  U R                  R                  5       nOjU R                  S:X  a  U R                  R                  5       nO?U R                  S:X  a  U R                  R                  5       nO SU R                  -  5       eU Vs/ s H  o0R                  U5      PM     nnU R                   R                  U5        U R                  R                  5        Hj  u  pVXPR                  :X  a,  UR                  U R                  S   U R                  S   S9  M@  UR                  U R                  S   U R                  S   S9  Ml     U R                   R!                  S	S
S9  U R#                  5         U R                   R%                  U5        U R&                  R(                  " SU R                   R*                  R,                  /U R                   R.                  R1                  5       Q76   U R&                  R(                  " SU R                   R*                  R,                  /U R                   R.                  R1                  5       Q76   g s  snf )Nzall packagesr   r   r   r   zbad tab value %rr   rR  r  r  r  )order   i,  )r)  selected_rowrf  r  r  rR   r   r   r   _package_to_columnsr  ri  r   r  _FRONT_TAB_COLOR_BACK_TAB_COLORr  _color_tabler*  r  after
_scrollbarr  _mlbyview)r'   r  r   rU   rowsr  r  s          r*   r&  DownloaderGUI._fill_table  s   {{//199&HH%%'EYY)#HH$$&EYY("HHOO%EYY-'HH((*E4(499441;@A54((.5A4  ****,JCii#44Q7#44Q7   
 #33A6#33A6    - 	L<<( 	sDKK2266R9I9I9O9O9QRsDKK2266R9I9I9O9O9QR5 Bs   9J:c                     [        [        U R                  5      5       H=  nU R                  R	                  U R                  US4   5      nX R                  US4'   M?     U R                  5         g )Nr  r  )r  r   r)  r  r   r  )r'   row_numr   s      r*   _update_table_status"DownloaderGUI._update_table_status  sY    S-.GXX__T[[,1F%GHF-3KK)* / 	r-   c                    U R                   (       a  U R                  " U6 $ [        [        U R                  5      5       Vs/ s H+  nU R                  US4   S:w  d  M  U R                  US4   PM-     nnU R                  R                  5       nU(       d  Ub  U R                  US4   /nU R                  R                  X0R                  R                  5      nSU l	        U R                  XS5        g s  snf )Nr   rD   r  )r  _download_threadedr  r   r)  r  r  r  r   r  _download_cb)r'   r  r@  marked	selectiondownload_iters         r*   rv  DownloaderGUI._download  s     **A.. S-.
.{{36"b( +DKK\)*. 	 

 KK,,.	)/kk)\"9:;F..vxx7L7LM-0
s   C3C3c                 $  ^   [        U5      nU 4S jn[        U[        5      (       a  T R                  UR                  5        GO5[        U[        5      (       aV  U" UR                  5        UR                  b%  T R                  UR                  R                  5        T R                  S 5        g [        U[        5      (       a6  U" SUR                   R                  -  5        T =R"                  S-  sl        GO[        U[$        5      (       a!  U" SUR                  R                  -  5        GOI[        U[&        5      (       a!  U" SUR                  R                  -  5        GO[        U[(        5      (       a   U" S	UR                  R                  -  5        O[        U[*        5      (       a   U" S
UR                  R,                  -  5        O[        U[.        5      (       aZ  T =R"                  S-  sl        U" SUR                   R                  -  5        T R1                  UR                   R                  5        O:[        U[2        5      (       a%  T R1                  UR                  R                  5        T R                  R	                  T R4                  T R6                  X5      nUT R                  S'   g ! [         aI    T R                  5         T R                  R	                  ST R
                  S5      nUT R                  S'    g f = f)Nr  r   r  c                 F   > U TR                   S'   TR                  U 5        g NrK  r  r  r@  r'   s    r*   rK  (DownloaderGUI._download_cb.<locals>.show,      *+D'IIaLr-   zDownloading collection %sr   zDownloading package %sPackage %s is up-to-date!Finished downloading %r.Unzipping %s#Finished downloading collection %r.)nextStopIterationr  r  r  _show_progressr  r0   r   r   r   r   rr   _selectr   rb   re   r  ro   r   r   r   r"   rk   _clear_markrx   	_DL_DELAYr  )r'   r  idsr   afteridrK  s   `     r*   r  DownloaderGUI._download_cb"  s5   	}%C	 c?++-\**{{&S[[^^,%344,s~~/@/@@A!011)CKKNN:;_--,s{{~~=> 233+ckknn<=.//#++"6"667455!69J9JJKS^^../122S[[^^,((..1B1BMW(/n%O  	%%'hhnnR)<)<a@G,3DMM.)	s   J< <ALLc                     [        [        U R                  5      5       H5  nU R                  US4   U:X  d  M  U R                  R                  U5          g    g )Nr  )r  r   r)  r*  r'   r   r@  s      r*   r  DownloaderGUI._selectN  sE    T[[)*C{{3,-3""3' +r-   c           	      <   [        [        U R                  5      5       Hz  nU R                  U R                  US4      u  p#Su  pEU R                  R	                  UUUUUS9  U R                  R                  USU R                  S   U R                  S   S9  M|     g )Nr  )blackwhite)rR  selectforegroundr  selectbackgroundr   r   r  )r  r   r)  
_ROW_COLOR	rowconfigitemconfigurerq  )r'   r@  bgsbgfgsfgs         r*   r  DownloaderGUI._color_tableT  s    T[[)*Coodkk#x-&@AGB(GBKK!!!$!$ "  KK%%Q4#3#3A#64CSCSTUCV &  +r-   c                     [        [        U R                  5      5       H+  nU R                  US4   U:X  d  M  SU R                  US4'   M-     g )Nr  rD   r   r  r   r)  r  s      r*   r  DownloaderGUI._clear_marke  sB    T[[)*C{{3,-3&(CF# +r-   c                 p    [        [        U R                  5      5       H  nSU R                  US4'   M     g )Nr  r   r(  )r'   r  r@  s      r*   rz  DownloaderGUI._mark_allj  s,    T[[)*C"%DKKQ +r-   c                     U R                   R                  5       nUS:  a9  U R                   U   S   S:w  a  SU R                   US4'   OSU R                   US4'   U R                   R                  SS9  g )Nr   rD   r  r   )r!  )r)  r  r*  )r'   r  r   s      r*   rw  DownloaderGUI._table_markn  sj    KK,,.	>{{9%a(B.,.IqL),/IqL)#r-   c                 n    SSK Jn  SR                  U R                  5      nU" U R                  SU5        g )Nr   ShowTextr   zNLTK Downloader Log)nltk.draw.utilr0  r!   r  r  )r'   r0  rK  s      r*   r  DownloaderGUI._show_logw  s*    +yy++,0$7r-   c                    / n[        U R                  5       H  u  p4US:X  a  UR                  S5        M  US:X  a  UR                  UR                  5        MA  US:X  a,  UR                  U R                  R                  U5      5        Ms  UR                  5       R                  SS5      nUR                  [        XS5      5        M     U$ )zl
Given a package, return a list of values describing that
package, one for each column in ``self.COLUMNS``.
r   rD   r  r  r   rs  zn/a)	r  rm  r   r   r  r   r   replacer   )r'   r   r@  column_indexcolumn_nameattrs         r*   r  !DownloaderGUI._package_to_columns}  s    
 )24<<)@%Lq 

2,

366"(

488??3/0"((*223<

73e45 *A 
r-   c                 j    U R                   (       a  g U R                  R                  5         SU l         g )NT)r"  r  r!  )r'   r  s     r*   r!  DownloaderGUI.destroy  s#    ??r-   c                 .   U R                   b<  U R                  R                  5        H  nU R                   R                  U5        M      U R                  (       a!  U R
                  (       a  U R                  5         U R                  R                  5         g r9   )	r  r  r   after_cancelr  r  _abort_downloadr#  rf  )r'   r  r  s      r*   r+  DownloaderGUI._destroy  sh    88==//1%%g. 2 !2!2  "
 	!r-   c                 <    U R                   R                  " U0 UD6  g r9   )r  r  )r'   r  kwargss      r*   r  DownloaderGUI.mainloop  s    4*6*r-   an      This tool can be used to download a variety of corpora and models
    that can be used with NLTK.  Each corpus or model is distributed
    in a single zip file, known as a "package file."  You can
    download packages individually, or you can download pre-defined
    collections of packages.

    When you download a package, it will be saved to the "download
    directory."  A default download directory is chosen when you run

    the downloader; but you may also select a different download
    directory.  On Windows, the default download directory is


    "package."

    The NLTK downloader can be used to download a variety of corpora,
    models, and other data packages.

    Keyboard shortcuts::
      [return]	 Download
      [up]	 Select previous package
      [down]	 Select next package
      [left]	 Select previous tab
      [right]	 Select next tab
    c                     SSK Jn   U" U R                  SU R                  R	                  5       SSS9  g !   U" U R                  SU R                  R	                  5       SS9   g = f)Nr   r/  zHelp: NLTK Downloaderr   fixed)r   rL  rS  )r1  r0  r  HELPrV  )r'   r  r0  s      r*   r  DownloaderGUI.help  sX    +		U'		!	UTXX6		8IQSTs	   ,5 -A$c                     SSK Jn  SnSn SSKJn  U" X4S9R	                  5         g ! [
         a    U" U R                  XC5         g f = f)Nr   r/  z'NLTK Downloader
Written by Edward LoperzAbout: NLTK Downloader)Message)r   r  )r1  r0  r  rG  rK  r  r  )r'   r  r0  ABOUTTITLErG  s         r*   r  DownloaderGUI.about  sE    +?(	-2E/446 	-TXXu,	-s   ' AAc                    U R                   n[        US   5      [        US   5      p2[        S[        US   5      S-  U R                  -  5       H]  nUR	                  X@R                  -  S-   SX@R                  -  U-
  S-
  US-   U R                  SS[        US	-  S
-
  5      S-  -   -  S9  M_     UR                  S5        UR                  SSS9  UR                  SUR                  SSSSU R                  S   S95        g )Nr   r>  r   r(  r   iz	#%02x0000r)  rD  r   r  )r   r   gradienthiddenr  redbox)r   )r  r   r  _gradient_widthcreate_lineabs
addtag_all
itemconfigaddtag_withtagcreate_rectangler  )r'   r  r   r>  r  s        r*   r  DownloaderGUI._init_progressbar  s   AgJQx[)9vq3qz?Q.43G3GGHAMM(((2-(((61B6** BQUQY")<$<=   I 	
Z 	Zx0 	
a((Aq!$:N:Nq:Q(R	
r-   c                     U R                   nUc&  UR                  SSSSS5        UR                  SSS9  g [        US   5      [        US   5      pCU[        U5      -  S-  S	-   nUR                  SSSXTS	-   5        g )
NrO  r   rL  rM  rN  r   r>  r  r   )r  coordsrT  r   )r'   percentr  r   r>  r   s         r*   r  DownloaderGUI._show_progress   s|    ?HHXq!Q*LL8L4'
OS8-=6#e*$+a/AHHXq!Q
3r-   c                    U R                   nU R                  (       d  UR                  SSS9  g UR                  SSS9  UR                  S5      u  p#pEUS::  a$  UR	                  SU R
                  S-  S-
  S5        OUR	                  SS	S5        U R                  R                  S
U R                  5      nX`R                  S'   g )NrL  rM  rN  r  irD  rC  r   rQ  r`  _progress_alive)
r  r  rT  bboxmoverP  r  r  r]  r  )r'   r  x1y1x2y2r  s          r*   r]  DownloaderGUI._progress_alive
  s      LL8L4LL8L4VVJ/NBBTzzD$8$81$<#A1Ez2q)hhnnS$*>*>?G/6MM+,r-   c                    U R                   (       a  U R                  5         g SU R                  S'   [        [	        U R
                  5      5       Vs/ s H+  nU R
                  US4   S:w  d  M  U R
                  US4   PM-     nnU R
                  R                  5       nU(       d  Ub  U R
                  US4   /n[        U R                  R                  U R                  R                  5      nU R                  / :X  d   eU R                  / :X  d   eU R                  UUU R                  U R                  U R                  5      R                  5         SU l        SU l         U R#                  5         U R%                  5         g s  snf )NCancelrK  r   rD   r  T)r  r=  r  r  r   r)  r  r   r  r   r   r  r  _DownloadThreadr  startr  _monitor_message_queuer]  )r'   r  r@  r  r   dss         r*   r   DownloaderGUI._download_threaded  si      " )1f% S-.
.{{36"b( +DKK\)*. 	 

 KK,,.	)/kk)\"9:;F
 dhh&;&;< ''2---))R///$$&&	
 %'  ##% 	A
s   F/Fc                     U R                   (       aP  U R                  R                  5         U R                  R	                  S5        U R                  R                  5         g g )Nabort)r  r  acquirer  r   releaserA   s    r*   r=  DownloaderGUI._abort_downloadH  sH    '')&&--g6'') r-   c                        \ rS rSrS rS rSrg)DownloaderGUI._DownloadThreadiN  c                 ~    Xl         X l        X0l        X@l        XPl        [
        R                  R                  U 5        g r9   )data_serverr   lockmessage_queuerm  r  Threadr+   )r'   rt  r   ru  rv  rm  s         r*   r+   &DownloaderGUI._DownloadThread.__init__O  s1    *JI!.J%%d+r-   c                 (   U R                   R                  U R                  5       H  nU R                  R	                  5         U R
                  R                  U5        U R                  (       a7  U R
                  R                  S5        U R                  R                  5           g U R                  R                  5         M     U R                  R	                  5         U R
                  R                  S5        U R                  R                  5         g )Nabortedfinished)	rt  r  r   ru  rn  rv  r   rm  ro  r0  s     r*   r  !DownloaderGUI._DownloadThread.runW  s    ''55djjA		!!#""))#.::&&--i8II%%'		!!# B II%%j1IIr-   )rm  rt  r   ru  rv  N)rG   rH   rI   rJ   r+   r  rM   r/   r-   r*   rg  rr  N  s    	,	 r-   rg  r  c                   ^  U 4S jnT R                   R                  5       (       d  g T R                   GH  nUS:X  d  US:X  a  T R                  5         ST l        ST R
                  S'   T R                  S S 2	 T R                  S S 2	 T R                   R                  5         US:X  a  U" S5        T R                  S 5          g T R                  R                  ST R                  S 5      nUT R                  S	'     g [        U[        5      (       a  T R                  UR                  5        M  [        U[        5      (       a^  U" UR                   5        UR"                  b%  T R%                  UR"                  R&                  5        T R                  S 5        ST l          g [        U[(        5      (       a7  U" S
UR*                  R&                  -  5        T =R,                  S-  sl        GM  [        U[.        5      (       aQ  T R0                  R3                  UR"                  R&                  5        U" SUR"                  R&                  -  5        GM  [        U[4        5      (       a"  U" SUR"                  R&                  -  5        GMU  [        U[6        5      (       a"  U" SUR"                  R&                  -  5        GM  [        U[8        5      (       a"  U" SUR"                  R:                  -  5        GM  [        U[<        5      (       a"  U" SUR"                  R&                  -  5        GM  [        U[>        5      (       a\  T =R,                  S-  sl        U" SUR*                  R&                  -  5        T RA                  UR*                  R&                  5        GMk  [        U[B        5      (       d  GM  T R                  5         T RA                  UR"                  R&                  5        GM     T R                  (       a  ST RD                  S'   T R                  S S 2	 T R                   R                  5         T R                  R                  T RF                  T RH                  5      nUT R                  S	'   g )Nc                 F   > U TR                   S'   TR                  U 5        g r  r  r  s    r*   rK  2DownloaderGUI._monitor_message_queue.<locals>.showh  r	  r-   r{  rz  Fr]  rK  zDownload aborted!r  ri  rP  r   zDownloading package %rr
  r  r  zFinished installing %sr  zAborting download...)%r  rn  r  r  r  r  r  ro  r  r  r  r  r0   r   r   r   r   rr   r  r   rb   re   r  ro   r  rh  r   r   r   r"   r   rk   r  rx   r  _MONITOR_QUEUE_DELAYri  )r'   rK  r   r  s   `   r*   ri  $DownloaderGUI._monitor_message_queueg  sQ   	
 ""**,,++Cj C9$4))+$)!0:%%f-,,Q/..q1##++-)#,-''-  #hhnnS$2E2EtLG>EDMM":; C11##CLL1C..S[[!;;*LL0##D)$)!C!78803>>3D3DDE  A%  C!455++CKKNN;->?C1103;;>>AB C!677/#++..@AC!233^ckk&:&::;C!344->?C!899  A% :S^^=N=NNO  !2!23C!566))+  0g ,p %%*@D' $$Q'##% ((..!:!:D<W<WX29./r-   )r  r#  r"  r  r  r  r  r  r  r{  r  r  r  r  r  r  rh  r)  ri  r  r  Tr9   )HrG   rH   rI   rJ   rK   rm  rn  rr  rs  r  r  r  r   r   r   r   r   r  rq  r  r  r  rk  r+   r  r$  r%  r(  r  rY  r}  ro  r  r  r'  rx  ry  rl  r  _rowsr&  r  rv  r  r  r  r  r  rz  rw  r  r  r!  r+  r  r   dedentrD  r  r  rP  r  r  r]  r  r=  r  rw  rg  r  ri  rM   r/   r-   r*   r  r    s   GD QQ?NC M<   COA G|| G|| G||  'O 	.,*  "2	J #K (&O&O$I45l
s.jE&N0
  
"(FF	> DE'SR1& I*0X")
&$8,"+ ??	D:U
-  O
(47$*X* )**  . I:r-   r  c                     [        U [        5      (       a!  [        U S5       n[        U5      sSSS5        $ [        U 5      $ ! , (       d  f       N= f)zn
Calculate and return the MD5 checksum for a given file.
``file`` may either be a filename or an open stream.
rbN)r0   r1   r1  _md5_hexdigestrE  r<  s     r*   rw  rw    sA    
 $$!&) $    A
Ac                     [        5       n U R                  S5      nU(       d   UR                  5       $ UR                  U5        M<  Nr'  )r   r5  r&   	hexdigest)fp
md5_digestr?  s      r*   r  r    sD    J
	"!! 	% 	 r-   c                     [        U [        5      (       a!  [        U S5       n[        U5      sSSS5        $ [        U 5      $ ! , (       d  f       N= f)zr
Calculate and return the SHA-256 checksum for a given file.
``file`` may either be a filename or an open stream.
r  N)r0   r1   r1  _sha256_hexdigestr  s     r*   sha256_hexdigestr    sA    
 $$$V, T"" r  c                     [        5       n U R                  S5      nU(       d   UR                  5       $ UR                  U5        M<  r  )r   r5  r&   r  )r  sha256_digestr?  s      r*   r  r    sD    HM
	"""$$ 	U#	 r-   c                 j    [        XU5       H#  n[        U[        5      (       d  M  [        U5      e   g)zP
Extract the contents of the zip file ``filename`` into the
directory ``root``.
N)r:  r0   r   r   )r"   rootr,  r   s       r*   r$   r$     s-    
 xw7g|,,G$$ 8r-   c              #     #    U(       a`  [         R                  R                  S[        R                  R                  U 5      S   -  5        [         R                  R                  5          [        R                  " U 5      nUR                  U5        U(       a  [        5         g g ! [        R                   a    [        U S5      v    g [         a  n[        X5      v    S nAg S nAff = f7f)Nr  r   zError with downloaded zip file)r  stdoutr6  r   r   r    flushzipfileZipFile
BadZipFiler   r   
extractallr   )r"   r  r,  zfr  s        r*   r:  r:    s     

"''--*A!*DDE

__X& MM$   8%EFF 8''s<   A(C,+B% $C,%"C)C,		C)C$C,$C))C,c                 v   / n[        [        R                  R                  U S5      5       GHJ  u  p4n[        R                  " UR
                  5      nU SU S[        R                  R                  UR
                  5      S    3n[        S UR                  5        5       5      nUR                  SSU-  5        UR                  SSUR                  -  5        UR                  SS[        UR
                  5      -  5        UR                  S	S[        UR
                  5      -  5        UR                  S
U5        UR                  S5      (       d  UR                  SU5        UR                  U5        GMM     [        [!        [        R                  R                  U S5      5      5      n	[        5       n
X)-    HU  nUR                  S5      U
;   a  [#        SUR                  S5      -  5      eU
R%                  UR                  S5      5        MW     [&        R(                  " S5      nUR                  [&        R(                  " S5      5        US   R+                  [-        US S95        UR                  [&        R(                  " S5      5        US   R+                  [-        U	S S95        [/        U5        U$ )a  
Create a new data.xml index file, by combining the xml description
files for various packages and collections.  ``root`` should be the
path to a directory containing the package xml and zip files; and
the collection xml files.  The ``root`` directory is expected to
have the following subdirectories::

  root/
    packages/ .................. subdirectory for packages
      corpora/ ................. zip & xml files for corpora
      grammars/ ................ zip & xml files for grammars
      taggers/ ................. zip & xml files for taggers
      tokenizers/ .............. zip & xml files for tokenizers
      etc.
    collections/ ............... xml files for collections

For each package, there should be two files: ``package.zip``
(where *package* is the package name)
which contains the package itself as a compressed zip file; and
``package.xml``, which is an xml description of the package.  The
zipfile ``package.zip`` should expand to a single subdirectory
named ``package/``.  The base filename ``package`` must match
the identifier given in the package's xml file.

For each collection, there should be a single file ``collection.zip``
describing the collection, where *collection* is the name of the collection.

All identifiers (for both packages and collections) must be unique.
rR   r   r   c              3   8   #    U  H  oR                   v   M     g 7fr9   )	file_size)r  zf_infos     r*   r  build_index.<locals>.<genexpr>-	  s     K]'--]r  r   z%sr   r   sha256_checksumr   r   r   r   zDuplicate UID: %sr  r   c                 $    U R                  S5      $ Nr   rX   rq   s    r*   rZ  build_index.<locals>.<lambda>I	  s    7;;t;Lr-   r   c                 $    U R                  S5      $ r  r  rd   s    r*   rZ  r  K	  s    PTAUr-   )_find_packagesr   r   r!   rp  r"   r    r  infolistr  rq  rw  r  rX   r   r   _find_collectionsr  addr   Elementr  r   _indent_xml)r  base_urlrR   pkg_xmlr  r   zipstatr   r   r   uidsrU   top_elts                r*   build_indexr  
	  s   > H-bggll4.LMV''"++&
!F81RWW]]2;;%?%B$CDKR[[]KK 	OTM%9:FD7??23J}R[['A AB%t.>r{{.K'KLHf%{{5!!KKs# 	 !  N& (dM)JKLK 5D&88D>T!0488D>ABB$  ' !!+.GNN;&&z23AJfX+LMNNN;&&}56AJf[.UVWNr-   c                    [        U 5      S:  a  U R                  =(       d    SR                  5       S-   U-   S-   U l        U  H  n[        X!S-   5        M     U SS  H4  nUR                  =(       d    SR                  5       S-   U-   S-   Ul        M6     U S   R                  =(       d    SR                  5       S-   U-   U S   l        gg)z
Helper for ``build_index()``: Given an XML ``ElementTree``, modify it
(and its descendents) ``text`` and ``tail`` attributes to generate
an indented tree, where each nested element is indented by 2
spaces with respect to its parent.
r   rD   r   rR  Nr   )r   rK  rV  r  tail)r4   r   rY   s      r*   r  r  Q	  s     3x!|HHN))+d2V;dBE}- "XE***113d:VCdJEJ B*113d:VCB r-   c                   ^ [         R                  R                  [         R                  R                  U5      S   5      S   mU R	                  S5      T:w  a*  [        SR                  U R	                  S5      T5      5      e[        U4S jUR                  5        5       5      (       a  [        ST< ST< S35      eg	)
zf
Helper for ``build_index()``: Perform some checks to make sure that
the given package is consistent.
r   r   r   z&package identifier mismatch ({} vs {})c              3   l   >#    U  H)  oT:g  =(       a    UR                  TS -   5      (       + v   M+     g7fr   Nr  r  r   uids     r*   r  !_check_package.<locals>.<genexpr>n	  s+     
VCK:c	 :::s   14Zipfile ..zip does not expand to a single subdirectory r   N)	r   r   r   r    rX   r  r   r  namelist)r  zipfilenamer  r  s      @r*   _check_packager  a	  s     ''

277==5a8
9!
<C{{4C4;;GKK<MsS
 	

 
V
VVV"%s,
 	
 Wr-   c                    [         R                  " SSSU /[         R                  [         R                  S9nUR                  5       u  p#UR                  S:w  d  U(       d  U(       dG  [        S[        R                  R                  U 5      S   < S[        R                  " U5      < 35      eUR                  5       S	   $ )
z
Helper for ``build_index()``: Calculate the subversion revision
number for a given file (by using ``subprocess`` to run ``svn``).
svnr   z-v)r  r  r   z#Error determining svn_revision for r   r	  r(  )
subprocessPopenPIPEcommunicate
returncoder  r   r   r    r   r   )r"   r  r  r  s       r*   _svn_revisionr  v	  s    
 		$)	A
 }}V||qF&ww}}X&q)8==+@B
 	
 <<>!r-   c              #     #    [         R                  " U 5       Hl  u  pnU H`  nUR                  S5      (       d  M  [         R                  R	                  X5      n[
        R                  " U5      R                  5       v   Mb     Mn     g7f)z
Helper for ``build_index()``: Yield a list of ElementTree.Element
objects, each holding the xml for a single package collection.
.xmlN)r   ry  r9  r   r!   r   r2   r  )r  dirname_subdirsrt  r"   xmlfiles         r*   r  r  	  se     
 %'GGDM 5H  (('',,w9!''088::  %2s   6B
AB
c           
   #     ^#    SSK Jn  / n[        R                  " U 5       GH  u  p4nSR	                  U" X5      5      nU GH  nUR                  S5      (       Ga  [        R                  R	                  X75      nUSS S-   n	 [        R                  " U	5      n
 [        R                  " U5      R                  5       n[        R                  R                  USS 5      S
   mUR                  S5      T:w  a#  [        SUR                  S5      < ST< S35      e[!        U4S jU
R#                  5        5       5      (       a  [        ST< ST< S35      eXU4v   GM   UR                  S5      (       d  GM9  [        R                  R%                  U5      S   n[        R                  R	                  X=S-   5      n[        R                  R'                  U5      (       a  GM  [(        R*                  " U SUS-    SU S3SS9  GM      UR-                  S5        GM     g! [         a  n[        SU	< S	U 35      UeSnAff = f! [         a  n[        SU< S	U 35      UeSnAff = f! [         a     GMZ  f = f7f)ab  
Helper for ``build_index()``: Yield a list of tuples
``(pkg_xml, zf, subdir)``, where:
  - ``pkg_xml`` is an ``ElementTree.Element`` holding the xml for a
    package
  - ``zf`` is a ``zipfile.ZipFile`` for the package's contents.
  - ``subdir`` is the subdirectory (relative to ``root``) where
    the package was found (e.g. 'corpora' or 'grammars').
r   )
_path_fromr   r  NrQ  r+  zError reading file z!
r   r   zpackage identifier mismatch (z vs r   c              3   n   >#    U  H*  nUT:g  =(       a    UR                  TS -   5      (       + v   M,     g7fr  r  r  s     r*   r  !_find_packages.<locals>.<genexpr>	  s4       - S[Cs)C%CC -s   25r  r  z exists, but z' cannot be found! This could mean that z can not be downloaded.r(  )
stacklevelz.svn)nltk.corpus.reader.utilr  r   ry  r!   r9  r   r  r  r   r  r   r2   r  r    rX   r  r  r   r.  warningswarnr/  )r  r  rR   r  subdirsrt  relpathr"   xmlfilenamer  r  r  r  resourcenamer  s                 @r*   r  r  	  sT     3 H#%774=%((:d45H  (( ggll7=)#2.7Y 5BY)//<DDFG
 ggmmK$45a8;;t$+$$+KK$5s<    "   %58#? 
 7**""6**!ww//9!< ggll764IJww~~k22MM#*M,2G1H I00<~=TV#$O \	NN6"c $1 ! Y$':;/QC%PQWXXY ! Y$':;/QC%PQWXXYL  		sz   BJ H$$I	 B6J ;A&J &&J I.J $
I.IIJ 	
I+I&&I++J .
I=8J <I==J c                  >    [        [        5      R                  5         g r9   )r  _downloaderr  r/   r-   r*   download_shellr  	  s    K $$&r-   c                  >    [        [        5      R                  5         g r9   )r  r  r  r/   r-   r*   download_guir  	  s    +'')r-   c                  ,    [         R                  5         g r9   )r  r&   r/   r-   r*   r&   r&   	  s    r-   __main__)OptionParserz-dz--dirdirz!download package to directory DIRDIR)destr  metavarz-qz--quietrX  
store_trueFzwork quietly)r  actiondefaultr  z-fz--forcer  z"download even if already installedz-ez--exit-on-errorrY  zexit if an error occursz-uz--urlr   NLTK_DOWNLOAD_URLzdownload server index url)r  r  r  )r   )r  r   rX  r  rY  )r   rX  r  rY  r  rS  )MrK   rT  r3  r   shutilr  r  r   r  r  r  r  hashlibr   r   urllib.errorr   r   urllib.requestr   	xml.etreer   r  r
   rO   r`   rb   rk   ro   rx   r}   r   r   r   r   r   r   r   r   r   r  r  rw  r  r  r  r$   r:  r  r  r  r  r  r  r  rW  r  r  r&   rG   optparser  parser
add_optionr  rX   
parse_argsr  r  r   
downloaderpkg_idr  rX  r  rY  rvr/   r-   r*   <module>r     s.  <~Yt   	   
       , " ! Z( Z(z&+ &+\! !
%. %%/ %+ , , - ) * ' $ #$ #!' !)0 )k
( k
(\b bJn: n:n "#%%4DND 
*&	;AV l'* z%^F
0      1   &   

23(   '')OWdW-E-EFJF$$!$[[mmmm%33 % B U{w444  	 ----!//	 	 	
w r-   