
B_Kc        %   @   s  d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d k	 l
 Z
 y d d k l Z Wn d d k l Z n Xy> e Z d d k Td d k Td d k l Z d d k l Z Wn e Z e Z n Xd d k l Z d d k Z e i i d	  Z d
 e f d     YZ d e f d     YZ  d e f d     YZ! d e! f d     YZ" d e! f d     YZ# d e! f d     YZ$ d e! f d     YZ% d e! f d     YZ& d e! f d     YZ' d e! f d     YZ( d e! f d     YZ) d  e! f d!     YZ* d" e! f d#     YZ+ d$ e! f d%     YZ, d& e! f d'     YZ- d( e! f d)     YZ. d* e f d+     YZ/ d, e f d-     YZ0 d. e f d/     YZ1 d0   Z2 e d1  Z3 e d2  Z4 d d k5 Z5 d d k Z d3   Z6 d4 d5  Z7 d6   Z8 d7   Z9 d8   Z: d9   Z; e/   Z< e< i= Z= d:   Z> d;   Z? d<   Z@ eA d= j o_d d> kB lC ZC eC   ZD eD iE d? d@ dA dB dC dD dE dF eD iE dG dH dA dI dJ dK dL e dC dM eD iE dN dO dA dP dJ dK dL e dC dQ eD iE dR dS dA dT dJ dK dL e dC dU eD iF   \ ZG ZH eH od x] eH D]U ZI e= dV eI dW eG iJ dI eG iK dP eG iL dT eG iM  ZN eN e j o eG iM o Pn qGWn, e= dW eG iJ dI eG iK dP eG iL dT eG iM  n d S(X   sN	  
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, L{download() <Downloader.download>}
function 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 C{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 C{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 C{download_dir} argument may be
used to specify a different installation target, if desired.

See L{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 C{<http://nltk.googlecode.com/svn/trunk/nltk_data/index.xml>}.
If necessary, it is possible to create a new L{Downloader} object,
specifying a different URL for the package index file.

Usage::

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

or with py2.5+:

    python -m nltk.downloader [-d DATADIR] [-q] [-f] [-k] PACKAGE_IDS
iN(   t   StringIO(   t   md5(   t   *(   t   Table(   t   ShowText(   t   ElementTreet   urllib2t   Packagec           B   sP   e  Z d  Z e d e e e e d d d d e d  Z e d    Z d   Z RS(   s3  
    A directory entry for a downloadable package.  These entries are
    extracted from the XML index file that is downloaded by
    L{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.
    t    t   Unknownc         K   s   | |  _  | p | |  _ | |  _ | |  _ t |  |  _ t |  |  _ | |  _ | |  _ |	 |  _	 |
 |  _
 | |  _ | |  _ t i i | i d  d  d } t i i | | |  |  _ t t |   |  _ |  i i |  d  S(   Nt   /ii   (   t   idt   namet   subdirt   urlt   intt   sizet   unzipped_sizet   checksumt   svn_revisiont	   copyrightt   contactt   licenset   authort   ost   patht   splitextt   splitt   joint   filenamet   boolt   unzipt   __dict__t   update(   t   selfR   R   R   R   R   R   R   R   R   R   R   R   R   t   kwt   ext(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   __init__   s     									#c         C   s0   t  |  t  o t i |   }  n t |  i   S(   N(   t
   isinstancet
   basestringR   t   parseR   t   attrib(   t   xml(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   fromxml   s    c         C   s   d |  i  S(   Ns   <Package %s>(   R   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   __repr__  s    (	   t   __name__t
   __module__t   __doc__t   Nonet   TrueR%   t   staticmethodR+   R,   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR      s   6t
   Collectionc           B   s2   e  Z d  Z e d  Z e d    Z d   Z RS(   s   
    A directory entry for a collection of downloadable packages.
    These entries are extracted from the XML index file that is
    downloaded by L{Downloader}.
    c         K   s?   | |  _  | p | |  _ | |  _ d  |  _ |  i i |  d  S(   N(   R   R   t   childrenR0   t   packagesR    R!   (   R"   R   R4   R   R#   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%     s
    			c         C   si   t  |  t  o t i |   }  n g  } |  i d  D] } | | i d  q7 ~ } t d | |  i  S(   Nt   itemt   refR4   (   R&   R'   R   R(   t   findallt   getR3   R)   (   R*   t   _[1]t   childR4   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR+      s    3c         C   s   d |  i  S(   Ns   <Collection %s>(   R   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR,   '  s    (   R-   R.   R/   R0   R%   R2   R+   R,   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR3     s   t   DownloaderMessagec           B   s   e  Z d  Z RS(   sU   A status message object, used by L{incr_download} to
       communicate its progress.(   R-   R.   R/   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR<   .  s   t   StartCollectionMessagec           B   s   e  Z d  Z d   Z RS(   s<   Data server has started working on a collection of packages.c         C   s   | |  _  d  S(   N(   t
   collection(   R"   R>   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   3  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR=   1  s   t   FinishCollectionMessagec           B   s   e  Z d  Z d   Z RS(   s=   Data server has finished working on a collection of packages.c         C   s   | |  _  d  S(   N(   R>   (   R"   R>   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   6  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR?   4  s   t   StartPackageMessagec           B   s   e  Z d  Z d   Z RS(   s-   Data server has started working on a package.c         C   s   | |  _  d  S(   N(   t   package(   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   9  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR@   7  s   t   FinishPackageMessagec           B   s   e  Z d  Z d   Z RS(   s.   Data server has finished working on a package.c         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   <  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRB   :  s   t   StartDownloadMessagec           B   s   e  Z d  Z d   Z RS(   s.   Data server has started downloading a package.c         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   ?  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRC   =  s   t   FinishDownloadMessagec           B   s   e  Z d  Z d   Z RS(   s/   Data server has finished downloading a package.c         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   B  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRD   @  s   t   StartUnzipMessagec           B   s   e  Z d  Z d   Z RS(   s,   Data server has started unzipping a package.c         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   E  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRE   C  s   t   FinishUnzipMessagec           B   s   e  Z d  Z d   Z RS(   s-   Data server has finished unzipping a package.c         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   H  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRF   F  s   t   UpToDateMessagec           B   s   e  Z d  Z d   Z RS(   s/   The package download file is already up-to-datec         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   K  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRG   I  s   t   StaleMessagec           B   s   e  Z d  Z d   Z RS(   s3   The package download file is out-of-date or corruptc         C   s   | |  _  d  S(   N(   RA   (   R"   RA   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   N  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRH   L  s   t   ErrorMessagec           B   s   e  Z d  Z d   Z RS(   s    Data server encountered an errorc         C   s9   | |  _  t | t  o t |  |  _ n
 | |  _ d  S(   N(   RA   R&   t	   Exceptiont   strt   message(   R"   RA   RL   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   Q  s    	(   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRI   O  s   t   ProgressMessagec           B   s   e  Z d  Z d   Z RS(   s4   Indicates how much progress the data server has madec         C   s   | |  _  d  S(   N(   t   progress(   R"   RN   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   Z  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRM   X  s   t   SelectDownloadDirMessagec           B   s   e  Z d  Z d   Z RS(   s:   Indicates what download directory the data server is usingc         C   s   | |  _  d  S(   N(   t   download_dir(   R"   RP   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   ]  s    (   R-   R.   R/   R%   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRO   [  s   t
   Downloaderc           B   s  e  Z d  Z d' Z d Z d Z d Z d Z d Z e	 e	 d  Z
 e	 e e e e e d  Z d	   Z d
   Z d   Z d   Z d   Z e	 e d  Z d   Z d   Z d   Z e	 e	 e e d e e d  Z e	 d  Z e	 d  Z e	 d  Z e	 d  Z d   Z e d d  Z e	 d  Z d   Z d   Z  d   Z! d   Z" e# d   e" d  d! Z$ d"   Z% d#   Z& e# d$   e& d  d% Z' d&   Z( RS((   sy   
    A class used to access the NLTK data server, which can be used to
    download corpora and other data packages.
    i<   s8   http://nltk.googlecode.com/svn/trunk/nltk_data/index.xmlt	   installeds   not installeds   out of datet   partialc         C   sy   | p |  i  |  _ h  |  _ h  |  _ | |  _ d  |  _ d  |  _ h  |  _ d  |  _	 |  i d  j o |  i
   |  _ n d  S(   N(   t   DEFAULT_URLt   _urlt   _collectionst	   _packagest   _download_dirR0   t   _indext   _index_timestampt   _status_cachet   _errorst   default_download_dir(   R"   t   server_index_urlRP   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%     s    							c         C   s  d } | d  j o |  i } d | GHn | oF d d t |  i  GHd |  i GHd d t |  i  GH| d 7} n t } }	 g  }
 | o |
 i d  n | o |
 i d  n x|
 D]} d	 | i   GH| d
 7} xrt t |  |     D]X} |  i	 | |  } | |  i
 j o | o q n | |  i j o
 t } n | |  i j o
 t }	 n h  d |  i
 <d |  i <d |  i <d |  i <| } t i d d | i p | i d d d d d } d | | i i d d  | f GH| t | i d   7} | o> | d j o1 t d  } | i   d j o d  Sn d } q q WHq Wd } | o | d 7} n |	 o | d 7} n t i | d d d d d GHd  S(    Ni    s!   Using default data directory (%s)t   =i   s    Data server index for <%s>i   R5   t   collectionss   %s:i   R   t   -t   Pt    i   iK   t   subsequent_indents     [%s] %s %si   t   .s   
s   Hit Enter to continue: t   xt   qs   ([*] marks installed packagess+   ; [-] marks out-of-date or corrupt packagess+   ; [P] marks partially installed collectionst   )t   widthiL   (   Rf   Rg   (   R0   RX   t   lenRU   t   Falset   appendt
   capitalizet   sortedt   getattrt   statust	   INSTALLEDt   STALER1   t   PARTIALt   NOT_INSTALLEDt   textwrapt   fillR   R   t   ljustR   t	   raw_inputt   lower(   R"   RP   t   show_packagest   show_collectionst   headert   more_promptt   skip_installedt   linest   staleRS   t
   categoriest   categoryt   infoRp   t   prefixR   t
   user_inputt   msg(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   list  sL    	
 
 !c         C   s   |  i    |  i i   S(   N(   t   _update_indexRW   t   values(   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR5     s    
c         C   sL   |  i    g  } |  i i   D]' \ } } | i d j o | | q q ~ S(   Nt   corpora(   R   RW   t   itemsR   (   R"   R:   R   t   pkg(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    
 c         C   sL   |  i    g  } |  i i   D]' \ } } | i d j o | | q q ~ S(   NR   (   R   RW   R   R   (   R"   R:   R   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   models  s    
 c         C   s   |  i    |  i i   S(   N(   R   RV   R   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR`     s    
c         C   s)   t  | t  o |  i |  Sn | Sd  S(   N(   R&   R'   R   (   R"   t
   info_or_id(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _info_or_id  s    c      	   c   s0  | d  j o |  i } t |  Vn t | t t f  o- x" |  i | | |  D] } | VqQ Wd  Sn y |  i |  } Wn7 t t	 f j
 o% } t
 d  d | | f  Vd  Sn Xt | t  oB t |  Vx% |  i | i | |  D] } | Vq Wt |  Vn& x" |  i | | |  D] } | VqWd  S(   Ns   Error loading %s: %s(   R0   RX   RO   R&   R   t   tuplet   _download_listR   t   IOErrort
   ValueErrorRI   R3   R=   t   incr_downloadR4   R?   t   _download_package(   R"   R   RP   t   forceR   R   t   e(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s,    	 			 	 c         C   s)   t  | t  o d Sn t | i  Sd  S(   Ni   (   R&   R   Rj   R5   (   R"   R6   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _num_packages  s    c      	   #   sC  xi t  t |   D]U } y   i | |  | | <Wq t t f j
 o } t | | |  Vd  Sq Xq Wt   f d   | D  } d } x t |  D] \ } } t | t	  o d | }	 n t
 t | i   | }	 xL   i | | |  D]5 }
 t |
 t  o t | |
 i |	  Vq |
 Vq W| d |	 7} q Wd  S(   Nc         3   s"   x |  ] }   i  |  Vq Wd  S(   N(   R   (   t   .0R6   (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>+  s    i    g      ?id   (   t   rangeRj   R   R   R   RI   t   sumt	   enumerateR&   R   t   floatR5   R   RM   RN   (   R"   R   RP   R   t   iR   t   num_packagesRN   R6   t   deltaR   (    (   R"   s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   "  s$       	c         c   s-  t  |  Vt d  V|  i | |  } | o9 | |  i j o) t |  Vt d  Vt |  Vd  Sn |  i i | i d   t
 i i | | i  } t
 i i |  o0 | |  i j o t |  Vn t
 i |  n t
 i i |  p t
 i |  n t
 i i t
 i i | | i   p# t
 i t
 i i | | i   n t |  Vt d  Vy t i | i  } t | d  } t d t | i  d  } xq t i   D]c }	 | i d  }
 | i |
  |
 p Pn |	 d d j o$ t t  d	 d d
 |	 |   VqqW| i!   | i!   Wn: t" j
 o. } t# | d | i | i | f  Vd  Sn Xt$ |  Vt d	  V| i i% d  o t
 i i | | i  } | i& p% t
 i i t
 i i | | i   oH t' |  Vx+ t( | | d t) D] } | | _* | VqWt+ |  Vqn t |  Vd  S(   Ni    id   i   t   wbi   i   i   i   iP   iK   s$   Error downloading %r from <%s>:
  %ss   .zipt   verbosei @  i @  (,   R@   RM   Rp   Rq   RG   RB   R[   t   popR   R0   R   R   R   R   t   existsRr   RH   t   removet   mkdirR   RC   R   t   urlopenR   t   opent   maxR   R   t	   itertoolst   countt   readt   writet   mint   closeR   RI   RD   t   endswithR   RE   t   _unzip_iterRk   RA   RF   (   R"   R   RP   R   Rp   t   filepatht   infilet   outfilet
   num_blockst   blockt   sR   t   zipdirR   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   8  sb    %# (
		/ 		s   [nltk_data] c      
      ss  | d  j o, | d  j	 o | |  _ n |  i   t Sn7d   f d  } x!|  i | | |  D]
}	 t |	 t  o | |	 i  | o t |	 i   n | o t	 Sn t |  _
 | pn d GHt   i   }
 |
 d j o4 |  i |	 i i | | |   | |  p t	 Sq'q+|
 d j o t	 Sq+q/n | p5t |	 t  o' | d |	 i i    d	 7    GHqkt |	 t  oI   GH  d
    |  i
 o | d |	 i i  qg| d |	 i i  qkt |	 t  o | d |	 i i | f  qkt |	 t  o | d |	 i i d  qkt |	 t  o | d |	 i i d  qkt |	 t  o |	 i } qkqa qa Wt S(   NR   c            s*   t  i |  d   | d   | d GHd  S(   Nt   initial_indentRd   Rc   i   s       (   Ru   Rv   (   R   t   prefix2(   R   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   show  s    s(   Error installing package. Retry? [n/y/e]t   yt   YR   t   Es   Downloading collection %rs      | is$   Downloaded collection %r with errorss   Done downloading collection %rs   Downloading package %r to %s...s!   Package %s is already up-to-date!s     s   Unzipping %s.(   R   R   (   R   R   (   R0   RX   t   _interactive_downloadR1   R   R&   RI   RL   R   Rk   R\   Rx   t   stript   downloadRA   R   R=   R>   R?   R@   RG   RE   R   RO   RP   (   R"   R   RP   t   quietR   R   t   halt_on_errort   raise_on_errorR   R   t   choice(    (   R   s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   {  s`    
 		
	

c         C   s   |  i  | |  |  i j S(   N(   Rp   Rr   (   R"   R   RP   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   is_stale  s    c         C   s   |  i  | |  |  i j S(   N(   Rp   Rq   (   R"   R   RP   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   is_installed  s    c         C   s5   | d  j o |  i i   n |  i i | d   d  S(   N(   R0   R[   t   clearR   (   R"   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   clear_status_cache  s    c   	      C   ss  | d j o |  i } n |  i |  } t | t  o g  } | i D] } | |  i | i  qG ~ } |  i | j o |  i Sqo|  i	 | j o |  i	 Sqo|  i
 | j o |  i | j o |  i	 Sqo|  i | j o |  i Sqo|  i
 Sn t i i | | i  } | |  i j o |  i | |  } nB | i |  i j o  |  i | |  |  i | i <n |  i | i Sd S(   s   
        Return a constant describing the status of the given package
        or collection.  Status can be one of L{INSTALLED},
        L{NOT_INSTALLED}, L{STALE}, or L{PARTIAL}.
        N(   R0   RX   R   R&   R3   R5   Rp   R   Rr   Rs   Rq   Rt   R   R   R   R   t   _pkg_statusR[   (	   R"   R   RP   R   R:   R   t
   pkg_statusR   Rp   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRp     s*    0	c         C   s.  t  i i |  p |  i Sn y t  i |  } Wn t j
 o |  i Sn X| i t | i  j o |  i	 Sn t
 |  | i j o |  i	 Sn | i d  o | d  } t  i i |  p |  i Sn t  i i |  p |  i	 Sn t d   t  i |  D  } | | i j o |  i	 Sq'n |  i S(   Ns   .zipic         s   sN   xG |  ]@ \ } } } x. | D]& } t  i t  i i | |   i Vq Wq Wd  S(   N(   R   t   statR   R   t   st_size(   R   t   dt   _t   filest   f(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>
  s   	(   R   R   R   Rt   R   t   OSErrorR   R   R   Rr   t   md5_hexdigestR   R   Rq   t   isdirR   t   walkR   (   R"   R   R   t   filestatt   unzipdirR   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s&    
	c         C   s[   |  i    xJ |  i   D]< } |  i |  |  i j o |  i | d | d | q q Wd S(   sA   
        Re-download any packages whose status is STALE.
        R   R   N(   R   R5   Rp   Rr   R   (   R"   R   R   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR!     s
    
 c         C   sG  |  i  d j p' | d j	 p t i   |  i |  i j p d Sn | p |  i |  _ t i i t	 i
 t i |  i   i    |  _  t i   |  _ g  } |  i  i d  D] } | t i |  q ~ } t d   | D  |  _ g  } |  i  i d  D] } | t i |  q ~ } t d   | D  |  _ x |  i i   D]v } xm t | i  D]\ \ }	 }
 |
 |  i j o |  i |
 | i |	 <n |
 |  i j o |  i |
 | i |	 <qXqXWqBWxw |  i i   D]f } h  } | g } x? | D]7 } t | t  o | i | i  q| | | i <qW| i   | _ qW|  i i   d S(   s   A helper function that ensures that self._index is
        up-to-date.  If the index is older than self.INDEX_TIMEOUT,
        then download it again.Ns   packages/packagec         s   s"   x |  ] } | i  | f Vq Wd  S(   N(   R   (   R   t   p(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>4  s    s   collections/collectionc         s   s"   x |  ] } | i  | f Vq Wd  S(   N(   R   (   R   t   c(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>9  s    (   RY   R0   t   timeRZ   t   INDEX_TIMEOUTRU   t   nltkt	   internalst   ElementWrapperR   R(   R   R   t   getrootR8   R   R+   t   dictRW   R3   RV   R   R   R4   R&   t   extendR   R5   R[   R   (   R"   R   R:   R   R5   t   _[2]R   R`   R>   R   t   child_idt   queueR;   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR      s@    	'//    	 c         C   s   |  i    |  i S(   s   
        Return the XML index describing the packages available from
        the data server.  If necessary, this index will be downloaded
        from the data server.
        (   R   RY   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   indexQ  s    
c         C   s\   |  i    | |  i j o |  i | Sn | |  i j o |  i | Sn t d |   d S(   sL   Return the L{Package} or L{Collection} record for the
           given item.s   Package %r not found in indexN(   R   RW   RV   R   (   R"   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   Z  s    
c         C   s   |  i    x8 |  i i d  D]$ } | i d  | j o | Sq q Wx8 |  i i d  D]$ } | i d  | j o | SqX qX Wt d |   d S(   s-   Return the XML info record for the given items   packages/packageR   s   collections/collections   Package %r not found in indexN(   R   RY   R8   R9   R   (   R"   R   RA   R>   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   xmlinfob  s    
  c         C   s4   |  i  } y |  i |  Wn | |  _    n Xd  S(   N(   RU   R   (   R"   R   t   original_url(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _set_urlq  s    		c         C   s   |  i  S(    (   RU   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   <lambda>{  s    t   docs2   
        The URL for the data server's index file.c         C   s   xB t  i i D]4 } t i i |  o t  i i |  o | Sq q Wt i d j o! d t i	 j o t i	 d } n0 t i i
 d  } | d j o t d   n t i i | d  S(   s	  
        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 C{download_dir} argument when
        calling L{download()}.

        On Windows, the default download directory is
        C{I{PYTHONHOME}/lib/nltk}, where C{I{PYTHONHOME}} is the
        directory containing Python (e.g. C{C:\Python25}).

        On all other platforms, the default directory is determined
        as follows:

          - If C{/usr/share} exists and is writable, then
            return C{/usr/share/nltk}
          - If C{/usr/local/share} exists and is writable, then 
            return C{/usr/local/share/nltk}
          - If C{/usr/lib} exists and is writable, then 
            return C{/usr/lib/nltk}
          - If C{/usr/local/lib} exists and is writable, then 
            return C{/usr/local/lib/nltk}
          - Otherwise, return C{~/nltk_data}, where C{~} is the
            current user's home directory.
        t   win32t   APPDATAs   ~/s+   Could not find a default download directoryt	   nltk_data(   R   t   dataR   R   R   R   t   is_writablet   syst   platformt   environt
   expanduserR   R   (   R"   t   nltkdirt   homedir(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR]   ~  s      c         C   s   | |  _  |  i i   d  S(   N(   RX   R[   R   (   R"   RP   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _set_download_dir  s    	c         C   s   |  i  S(    (   RX   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    s  
        The default directory to which packages will be downloaded.
        This defaults to the value returned by L{default_download_dir()}.
        To override this default on a case-by-case basis, use the
        C{download_dir} argument when calling L{download()}.c         C   sY   t  o> y t |   i   WqU t j
 o t |   i   qU Xn t |   i   d  S(   N(   t   TKINTERt   DownloaderGUIt   mainloopt   TclErrort   DownloaderShellt   run(   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    i  ()   R-   R.   R/   R   RT   Rq   Rt   Rr   Rs   R0   R%   R1   Rk   R   R5   R   R   R`   R   R   R   R   R   R   R   R   R   Rp   R   R!   R   R   R   R   R   t   propertyR   R]   R   RP   R   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRQ   c  sT   	%	'								C		G#	 1					
		-			R   c           B   sG   e  Z d    Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s   | |  _  d  S(   N(   t   _ds(   R"   t
   dataserver(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%     s    c         G   sT   d d GHd t  d   | D  t |  d d } d | i |  GHd d GHd  S(   NRa   iK   iD   c         s   s   x |  ] } t  |  Vq Wd  S(   N(   Rj   (   R   t   o(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>  s    i   Rc   s       (   R   Rj   R   (   R"   t   optionst   spc(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _simple_interactive_menu  s    	,c         C   s  d GHxt  o|  i d d d d d  t d  i   } | p Hq n | i   i   d } | i   d	 } | d
 j o' H|  i i |  i i d t	 d t  ns | d j o |  i
   nX | d j o |  i   n= | d j o d  Sn( | d j o |  i |  n
 d | GHHq Wd  S(   Ns   NLTK Downloaders   d) Downloads   l) Lists	   c) Configs   h) Helps   q) Quits   Downloader> i    i   t   lR|   R}   t   hR   Rg   Rf   R   s   Command %r unrecogmized(   Rg   Rf   (   R1   R  Rx   R   Ry   R   R   R   RP   Rk   t   _simple_interactive_helpt   _simple_interactive_configt   _simple_interactive_download(   R"   R   t   commandt   args(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s,    
	c         C   s;  | oR x-| D]C } y |  i  i | d d Wq t t f j
 o } | GHq Xq Wn x t o Hd GHt d  } | i   d j o/ |  i  i |  i  i d t	 d t d t q\ q\ | i   d j o d  Sq\ | oY xQ | i
   D]C } y |  i  i | d d Wq t t f j
 o } | GHq Xq WPq\ q\ Wd  S(   NR   s       s*   Download which package (l=list; x=cancel)?s     Identifier> R  R|   R}   R~   Rf   Rg   R   (   Rf   Rg   R   (   R   R   R   R   R1   Rx   Ry   R   RP   Rk   R   (   R"   R
  t   argR   R   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s*     (
 $c         C   s   Hd GHd GHd GHd GHd  S(   Ns	   Commands:s:     d) Download a package or collection              h) Helps:     l) List packages & collections                   q) Quits      c) View & Modify Configuration(    (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s
    c         C   s^   Hd GHd |  i  i GHd t |  i  i    GHd t |  i  i    GHHd GHd |  i  i GHd  S(   Ns   Data Server:s     - URL: <%s>s$     - %d Package Collections Availables$     - %d Individual Packages Availables   Local Machine:s     - Data directory: %s(   R   R   Rj   R`   R5   RP   (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _show_config  s    c         C   sx  |  i    xgt o_H|  i d d d d  t d  i   i   } | d j o |  i    q | d j o^ t d  i   i   } | d j o	 d GHqot i i |  o | |  i	 _
 qod | GHq | d j o t d  i   i   } | d j o	 d GHqo| i d  p d | } n y | |  i	 _ Wqot j
 o } d | | f GHqoXq | d j o Pq q Wd  S(   Ns   s) Show Configs   u) Set Server URLs   d) Set Data Dirs   m) Main Menus   Config> R   R   s     New Directory> R   Rf   Rg   s     Cancelled!s)   Directory %r not found!  Create it first.t   us     New URL> s   http://s   Error reading <%r>:
  %st   m(   R   Rf   Rg   (   R   Rf   Rg   (   R  R1   R  Rx   R   Ry   R   R   R   R   RP   t
   startswithR   RJ   (   R"   R   t
   new_dl_dirt   new_urlR   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s8    


	
	(	   R-   R.   R%   R  R   R  R  R  R  (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s   		
				R   c           B   s  e  Z d  Z d d d d d d d d d	 d
 d d d g Z h  d d <d d <d d <d d <Z h  d d <d d <d d <d d <d d <d d <Z d Z d d d d d g Z x" e D] Z e e j p t	  q Wx" e D] Z e e j p t	  q Wx" e D] Z e e j p t	  qWdO Z
 h  dP e i <dQ e i <dR e i <dS e i <Z dT Z dU Z dV Z dW Z d$ Z e d%  Z d&   Z d'   Z d(   Z d)   Z d*   Z d+   Z e d,  Z d-   Z  d.   Z! d/   Z" d0   Z# d1   Z$ d2   Z% d3   Z& d4 Z' e Z( d5   Z) d6   Z* d7   Z+ d Z, d8   Z- d9   Z. d:   Z/ d;   Z0 d<   Z1 d=   Z2 d>   Z3 d?   Z4 d@   Z5 dA   Z6 dB   Z7 e8 i9 dC  Z: dD   Z; dE   Z< d Z= dF   Z> dG   Z? dH   Z@ dI   ZA dJ   ZB dK eC iD f dL     YZE dM ZF dN   ZG RS(X   sU   
    Graphical interface for downloading packages from the NLTK data
    server.
    R   t
   Identifiert   Namet   Sizet   Statuss   Unzipped Sizet	   Copyrightt   Contactt   Licenset   Authors   SVN Revisiont   Subdirt   Checksumi    i   i   i   i-   i
   i   i   s   #000s   #cccs   #afas   #080s   #ffas   #880s   #faas   #800s   #fffs   #888s   #45cs   #aaas   #67as   #f00s   helvetica -16 boldc         C   s9  | |  _  | |  _ t i   |  _ g  |  _ g  |  _ t |  _ h  |  _	 g  |  _
 d |  _ |  i d  t   } |  _ | i d  | i d  | i d |  i d  | i d |  i  | i d |  i  t |  _ h  |  _ |  i   |  i   |  i   |  i   |  i   |  i i d  |  i i d	 |  i  d  S(
   Ni    s   NLTK Downloader Started!s   +50+50s   NLTK Downloadert
   backgroundi   s   <Control-q>s   <Control-x>s	   <Destroy>(   R   t   _use_threadst	   threadingt   Lockt   _download_lockt   _download_msg_queuet   _download_abort_queueRk   t   _downloadingt   _afteridt   _log_messagest   _log_indentt   _logt   Tkt   topt   geometryt   titlet	   configuret   _BACKDROP_COLORt   bindt   destroyt
   _destroyedt   _column_varst   _init_widgetst
   _init_menut   _fill_tablet
   _show_infot   _select_columnst   _tablet   selectt   _destroy(   R"   R   t   use_threadsR)  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%   ~  s2    										




c         C   s.   |  i  i d t i   d |  i | f  d  S(   Ns   %s %s%ss    | (   R%  Rl   R   t   ctimeR&  (   R"   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR'    s    c            s  t    i d d d d d d d d } | i d	 d
 d t d d  | i d d d | i d d d t  | d d i d d d d  t  |  } | i d d d d d d  t  |  } | i d d d d d d  t  |  } | i d d d d d d  t  | d d i d d d d  t  |  } | i d d d d d d  t  | d d i d d d d  t    i d d d d d   i d } | i d d d d  d   i d <d   i d <d d d  d! g   _ h    _	 x t
   i  D]o \ } } t | d" | d#   i }	 |	 i d d$ d | d d d%  |	 i d&   i  |	   i	 | i   <qWg  }
   i D] } |
   i i | d  qv~
 } t |   i d' | d d d( d) d*   i   _   i i d d+   i d xK t
   i  D]: \ } }   i i |   i  }   i i | d, | qW  i i d t d d    i i     i i d-   i    i i d.   i    i i d/   i    i i d0   i    i i d1   i    i i d2   i   | i d d d d3 d4   i! f d5 d6   i" f g } h    _# x t
 |  D] \ } \ } }	 } t | d" |	 i d d d | d d7  t$ | d# d8 d d9 d: d; } | | f   i# | <| i d/   i%  | i d& |   f d<   | i d d d | d d=  q-W  i i d&   i%  t& | d" d> d?   i d, d   _'   i' i d d$  t& | d" d@ d?   i( d, d   _)   i) i d dA  t | d" dB d+   i d d   i d   _* t+ | d, dC d d) d   i, d d dD d d   _-   i.     i- i d dA    i* i d d$  d  S(E   Nt   relieft   raisedt   borderi   t   padxi   t   padyi    t   sidR)  t   expandRv   t   botht   weighti   t   heightt   columnt   rowt   stickyt   newsi   i   i   i   R  t   sidet   bottomRf   t   highlightthicknesst   Collectionst   Corporat   Modelss   All Packagest   textt   fontt   lefti
   s
   <Button-1>t   column_weightst   listbox_heighti   t   reprfunct
   foregroundRi   s   <Double-Button-1>s   <space>s   <Return>s   <Left>s   <Right>s   <Control-a>R   s   Server Index:RP   s   Download Directory:R   t   couriert   groovet   disabledforegroundt   blackc            s     i  |  S(    (   t
   _info_edit(   R   t   key(   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    t   ewt   DownloadR	  t   Refresht   rightR   i   t   sunken(/   t   FrameR)  t   packR1   t   grid_rowconfiguret   grid_columnconfiguret   gridR-  t
   _tab_namest   _tabsR   t   Labelt	   _TAB_FONTR.  t   _select_tabRy   t   COLUMNSt   COLUMN_WEIGHTSR9   R   t   _table_reprfuncR7  t   columnconfigt   _MARK_COLORt   COLUMN_WIDTHSt   DEFAULT_COLUMN_WIDTHt   focust   bind_to_listboxest	   _downloadt   _table_markt	   _prev_tabt	   _next_tabt	   _mark_allR   R   t   _infot   Entryt
   _info_savet   Buttont   _download_buttont   _refresht   _refresh_buttont   _progresslabelt   Canvast   _PROGRESS_COLORt   _progressbart   _init_progressbar(   R"   t   f1t   tabframet
   tableframet   buttonframet	   infoframet   progressframeR   t   tabt   labelR:   RF  RS  Ri   R   R\  t   callbackt   entry(    (   R"   s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR2    s    '"""	 ", 
	 (	 !!
c            s)  t    i  } t  | d d } | i d d d d d   i d d  | i   | i d d	 d d
 d   f d    | i d d d d d   f d    | i   | i d d d d d   i  | i   | i d d d d d   i d d  | i d d d d d |  t  | d d } x   i i	 d D] } t
   i  } |   i j p t  |   i | <|   i j o | i d  n | i d | d d d | d   i  q1W| i d d d d d |  t  | d d } x>   i i	 d D], } | i d d | d |   f d   qW| i   x>   i i	 d D], } | i d d | d |   f d   q>W| i d d d d d |  t  | d d } | i d d d d d   i  | i d d d d d   i d d  | i d d  d d d |    i i d!   i    i i d |  d  S("   Nt   tearoffi    R  R^  t	   underlineR	  t   acceleratort   Returns   Change Server Indexi   c              s     i  d   S(   R   (   R[  (    (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    s   Change Download Directoryc              s     i  d   S(   RP   (   R[  (    (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    s   Show Logi   t   Exiti   s   Ctrl-xt   Filet   menui   t   variablet   Views
   Sort by %sc            s     i  i |  d   S(   t	   ascending(   R7  t   sort_by(   R   (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   3  s    s   Reverse sort by %sc            s     i  i |  d   S(   t
   descending(   R7  R  (   R   (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   9  s    t   Sortt   Aboutt   Instructionst   F1t   Helps   <F1>(   t   MenuR)  t   add_commandRu  t   add_separatort	   _show_logR/  t   add_cascadeR7  t   column_namest   IntVarR1  t   AssertionErrort   INITIAL_COLUMNSt   sett   add_checkbuttonR6  t   aboutt   helpR.  t   config(   R"   t   menubart   filemenut   viewmenuRF  t   vart   sortmenut   helpmenu(    (   R"   s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR3    sZ    



 ! 
 
c         C   sU   xN |  i  i   D]= \ } } | i   o |  i i |  q |  i i |  q Wd  S(   N(   R1  R   R9   R7  t   show_columnt   hide_column(   R"   RF  R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR6  G  s
     c         C   s+   |  i  i   |  i   |  i i d  d  S(   Ni    (   R   R   R4  R7  R8  (   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR  N  s    
c         C   s?   |  i    |  i | \ } } d | d <d | d <| i   d  S(   Nt   normalt   stateRa  R<  (   R|  Rz  Rs  (   R"   t   info_keyR  R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR[  S  s
    


c         C   s   |  i  } x |  i i   D] \ } } | d d j o q n | d  j	 o* | i | j o | i d j o
 | } q d | d <d | d <| | i    q W| i   d  S(   NR  t   disabledR  RX  R<  (   R7  Rz  R   R0   t   widgett   keysymR9   Rs  (   R"   R   Rs  R  R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR|  Z  s    	 -


c         C   s   |  i  i | i d  of t | t  o d | Sq | d j  o d | d Sq | d j  o d	 | d Sq d
 | d Sn | d j o t |  Sn	 d | Sd  S(   NR  s     %si   i   s	     %.1f KBg      @i   i   s	     %.1f MBs	     %.1f GBi    R   i   g      @i   @g      0Ag      A(   i    R   (   R7  R  R   R&   R'   RK   (   R"   RG  t   colt   val(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRn  f  s    c         C   sn   | |  i  i j o d  Sn y | |  i  _ |  i   Wn( t j
 o } t d t |   n X|  i   d  S(   Ns   Error Setting Server Index(   R   R   R4  R   t	   showerrorRK   R5  (   R"   R   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   p  s    c         C   s?   |  i  i | j o d  Sn | |  i  _ |  i   |  i   d  S(   N(   R   RP   R4  R5  (   R"   RP   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   z  s    
c         C   s   d G|  i  i GHx7 |  i i   D]& \ } } d | d <| i d d  q W|  i d d i d |  i  i  |  i d d i d |  i  i  x' |  i i   D] \ } } d | d <q Wd  S(	   Ns   showing infoR  R  i    t   endR   RP   R  (   R   R   Rz  R   t   deletet   insertRP   (   R"   R  t   cb(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR5    s     
!! c         G   so   xh t  |  i  D]W \ } } | i   |  i j o5 | d j o( |  i | d i   |  _ |  i   Sq q Wd  S(   Ni    i   (   R   Rg  Ry   t   _tabR4  (   R"   R   R   R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRw    s
     #c         G   s|   xu t  |  i  D]d \ } } | i   |  i j oB | t |  i  d j  o( |  i | d i   |  _ |  i   Sq q Wd  S(   Ni   (   R   Rg  Ry   R  Rj   Rh  R4  (   R"   R   R   R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRx    s
     0c         C   s$   | i  d i   |  _ |  i   d  S(   NRP  (   R  Ry   R  R4  (   R"   t   event(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRk    s    R`   c      	   C   s   |  i  i   } |  i  i   |  i d j o |  i i   } n |  i d j o |  i i   } n_ |  i d j o |  i i   } n< |  i d j o |  i i   } n d p t	 d |  i  g  } | D] } | |  i
 |  q ~ } |  i  i |  xy |  i i   D]h \ } } | |  i j o( | i d |  i d d |  i d	  q
| i d |  i d d |  i d	  q
W|  i  i d
 d d |  i   |  i  i |  |  i i d |  i  i i |  i  i i    |  i i d |  i  i i |  i  i i    d  S(   Ns   all packagesR   R   R`   i    s   bad tab value %rRV  R  i   R  t   orderR  i   i,  (   R7  t   selected_rowR   R  R   R5   R   R   R`   R  t   _package_to_columnsR   Rh  R   R,  t   _FRONT_TAB_COLORt   _BACK_TAB_COLORR  t   _color_tableR8  R)  t   aftert
   _scrollbarR  t   _mlbt   yview(   R"   R  R   R:   R6   t   rowsR  R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR4    s6    * 
c         C   s`   xO t  t |  i   D]8 } |  i i |  i | d f  } | |  i | d f <q W|  i   d  S(   NR  R  (   R   Rj   R7  R   Rp   R  (   R"   t   row_numRp   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _update_table_status  s
     c         G   s   |  i  o |  i |   Sn g  } t t |  i   D]8 } |  i | d f d j o | |  i | d f q5 q5 ~ } |  i i   } | o' | d  j	 o |  i | d f g } n |  i i | |  i i	  } d |  _
 |  i | |  d  S(   Ni    R   R  (   R  t   _download_threadedR   Rj   R7  R  R0   R   R   RP   R&  t   _download_cb(   R"   R   R:   RG  t   markedt	   selectiont   download_iter(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRu    s    
;	c      	      sb  y | i    } WnI t j
 o=   i     i i d   i d  } |   i d <d  Sn X  f d   } t | t  o   i | i	  nt | t
  oI | | i  | i d  j	 o   i | i i  n   i d   d  SnIt | t  o' | d | i i    i d 7_ nt | t  o | d | i i  n t | t  o | d | i i  n t | t  o | d	 | i i  n t | t  o | d
 | i i  nr t | t  o:   i d 8_ | d | i i    i | i i  n( t | t  o   i | i i  n   i i   i   i | |  } |   i d <d  S(   Ni
   i    R  c            s   |    i  d <  i |   d  S(   NRP  (   R  R'  (   R   (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    s   Downloading collection %ri   s   Downloading package %rs   Package %s is up-to-date!s   Finished downloading %r.s   Unzipping %ss#   Finished downloading collection %r.(   t   nextt   StopIterationR  R)  R  t   _show_progressR$  R&   RM   RN   RI   RL   RA   R0   t   _selectR   R=   R>   R&  R@   RG   RD   RE   R   R?   t   _clear_markRB   t	   _DL_DELAYR  (   R"   R  t   idsR   t   afteridR   (    (   R"   s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    sF    
	c         C   sV   xO t  t |  i   D]8 } |  i | d f | j o |  i i |  d  Sq q Wd  S(   NR  (   R   Rj   R7  R8  (   R"   R   RG  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s
     c         C   s   x t  t |  i   D] } |  i |  i | d f \ } } d
 \ } } |  i i | d | d | d | d | |  i i | d d |  i d d |  i d	 q Wd  S(   NR  RZ  t   whiteRV  t   selectforegroundR  t   selectbackgroundi    i   (   s   blackR  (   R   Rj   R7  t
   _ROW_COLORt	   rowconfigt   itemconfigureRp  (   R"   RG  t   bgt   sbgt   fgt   sfg(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s      c         C   sU   xN t  t |  i   D]7 } |  i | d f | j o d |  i | d f <q q Wd  S(   NR  R   i    (   R   Rj   R7  (   R"   R   RG  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s     c         G   s7   x0 t  t |  i   D] } d |  i | d f <q Wd  S(   Nt   Xi    (   R   Rj   R7  (   R"   R   RG  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRy  #  s     c         G   sy   |  i  i   } | d j oF |  i  | d d j o d |  i  | d f <qb d |  i  | d f <n |  i  i d d  d  S(   Ni    R   R  R   i   (   R7  R  R8  (   R"   R   R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyRv  '  s    c         C   s)   d i  |  i  } t |  i d |  d  S(   Ns   
s   NLTK Downloader Log(   R   R%  R   R)  (   R"   RP  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR  0  s    c         C   s   g  } x t  |  i  D] \ } } | d j o | i d  q | d j o | i | i  q | d j o | i |  i i |   q | i   i d d  } | i t | | d   q W| S(   s   
        Given a package, return a list of values describing that
        package, one for each column in L{self.COLUMNS}.
        i    R   R  R  Rc   R   s   n/a(	   R   Rl  Rl   R   R   Rp   Ry   t   replaceRo   (   R"   R   RG  t   column_indext   column_namet   attr(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR  4  s     c         G   s,   |  i  o d  Sn |  i i   t |  _  d  S(   N(   R0  R)  R/  R1   (   R"   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR/  J  s    c         G   sq   |  i  d  j	 o. x+ |  i i   D] } |  i  i |  q  Wn |  i o |  i o |  i   n |  i i	   d  S(   N(
   R)  R0   R$  R   t   after_cancelR#  R  t   _abort_downloadR1  R   (   R"   R   R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR9  O  s     c         O   s   |  i  i | |   d  S(   N(   R)  R   (   R"   R
  t   kwargs(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   ]  s    s      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         G   s\   y, t  |  i d |  i i   d d d d Wn) t  |  i d |  i i   d d n Xd  S(   Ns   Help: NLTK DowloaderRi   iK   RQ  t   fixeds   Help: NLTK Downloader(   R   R)  t   HELPR   (   R"   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s     c      	   G   sj   d d } d } y- d d k  l } | d | d |  i   Wn& t j
 o t |  i | |  n Xd  S(   Ns   NLTK Downloader
s   Written by Edward Lopers   About: NLTK Downloaderi(   t   MessageRL   R+  (   t   tkMessageBoxR  R   t   ImportErrorR   t   _top(   R"   R   t   ABOUTt   TITLER  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s    c         C   s	  |  i  } t | d  t | d  } } x t d t | d  d |  i  D]c } | i | |  i d d | |  i | d | d d |  i d d d	 t | d
 d  d qO W| i d  | i d d d | i d | i	 d d d d d |  i
 d  d  S(   NRi   RE  i    i   i   iRv   s	   #%02x0000iP   i   i   i   t   gradientR  t   hiddent   redbox(   R  R   R   t   _gradient_widtht   create_linet   abst
   addtag_allt
   itemconfigt   addtag_withtagt   create_rectangleR  (   R"   R   Ri   RE  R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s    	!% 	%c         C   s   |  i  } | d  j o0 | i d d d d d  | i d d d nW t | d  t | d  } } | t |  d d	 } | i d d d | | d	  d  S(
   NR  i    R  R  R  Ri   RE  id   i   (   R  R0   t   coordsR  R   (   R"   t   percentR   Ri   RE  Rf   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s    	!c         C   s   |  i  } |  i p | i d d d n | i d d d | i d  \ } } } } | d j o" | i d |  i d d d  n | i d d	 d  |  i i d
 |  i  } | |  i	 d <d  S(   NR  R  R  R  ii   i   i    ii   t   _progress_alive(
   R  R#  R  t   bboxt   moveR  R)  R  R  R$  (   R"   R   t   x1t   y1t   x2t   y2R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s    	
"c         G   s]  |  i  o |  i   d  Sn d |  i d <g  } t t |  i   D]8 } |  i | d f d j o | |  i | d f qC qC ~ } |  i i   } | o' | d  j	 o |  i | d f g } n t |  i	 i
 |  i	 i  } |  i g  j p t  |  i g  j p t  |  i | | |  i |  i |  i  i   d |  _ t |  _  |  i   |  i   d  S(   Nt   CancelRP  i    R   R  (   R#  R  R~  R   Rj   R7  R  R0   RQ   R   R   RP   R!  R  R"  t   _DownloadThreadR   t   startR&  R1   t   _monitor_message_queueR  (   R"   R   R:   RG  R  R  t   ds(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s(    

;		
c         C   s<   |  i  o. |  i i   |  i i d  |  i i   n d  S(   Nt   abort(   R#  R   t   acquireR"  Rl   t   release(   R"   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s    
R  c           B   s   e  Z d    Z d   Z RS(   c         C   sA   | |  _  | |  _ | |  _ | |  _ | |  _ t i i |   d  S(   N(   t   data_serverR   t   lockt   message_queueR  R  t   ThreadR%   (   R"   R  R   R  R  R  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR%     s    					c         C   s   xv |  i  i |  i  D]_ } |  i i   |  i i |  |  i o% |  i i d  |  i i   d  Sn |  i i   q W|  i i   |  i i d  |  i i   d  S(   Nt   abortedt   finished(	   R  R   R   R  R  R  Rl   R  R  (   R"   R   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s     
(   R-   R.   R%   R   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    s   	id   c            sY    f d   }   i  i   p d  Sn x  i D]} | d j p | d j o   i   t   _ d   i d <  i 2  i 2  i  i   | d j o | d    i	 d   n)   i i d   i	 d   } |   i d <d  Sq1 t | t  o   i	 | i  q1 t | t  oR | | i  | i d  j	 o   i | i i  n   i	 d   t   _ d  Sq1 t | t  o' | d	 | i i    i d
 7_ q1 t | t  o.   i i | i i  | d | i i  q1 t | t  o | d | i i  q1 t | t  o | d | i i  q1 t | t  o | d | i i  q1 t | t   o | d | i i  q1 t | t!  o:   i d
 8_ | d | i i    i" | i i  q1 t | t#  o!   i     i" | i i  q1 q1 W  i o d   i$ d <n   i 2  i  i     i i   i%   i&  } |   i d <d  S(   Nc            s   |    i  d <  i |   d  S(   NRP  (   R  R'  (   R   (   R"   (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR     s    R  R  R^  RP  s   Download aborted!id   R  s   Downloading collection %ri   s   Downloading package %rs   Package %s is up-to-date!s   Finished downloading %r.s   Unzipping %ss   Finished installing %ss#   Finished downloading collection %r.s   Aborting download...('   R   R  R!  R  Rk   R#  R~  R"  R  R  R0   R)  R  R$  R&   RM   RN   RI   RL   RA   R  R   R=   R>   R&  R@   R   R   RG   RD   RE   R   RF   R?   R  RB   R  t   _MONITOR_QUEUE_DELAYR  (   R"   R   R   R  (    (   R"   s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR    sn    
 
	
	

(   s   #000s   #ccc(   s   #afas   #080(   s   #ffas   #880(   s   #faas   #800(   s   #fffs   #888(   s   #000s   #ccc(   s   #fffs   #45c(   s   #aaas   #67a(   s   #f00s   #aaa(H   R-   R.   R/   Rl  Rm  Rq  Rr  R  R   R  R-  RQ   Rq   Rs   Rr   Rt   R  Rp  R  R  R  Rj  R1   R%   R'  R2  R3  R6  R  R[  R0   R|  Rn  R   R   R5  Rw  Rx  Rk  R  t   _rowsR4  R  Ru  R  R  R  R  R  Ry  Rv  R  R  R/  R9  R   Ru   t   dedentR  R  R  R  R  R  R  R  R  R  R  R  R  R  (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   8  s   *   +		\	:				
	
	
	
				'			+																	
		&	c         C   sm   t  |  t  o t |  d  }  n t   } x4 t o, |  i d  } | p Pn | i |  q/ W| i   S(   sz   
    Calculate and return the MD5 checksum for a given file.  C{file}
    may either be a filename or an open stream.
    t   rbi   i   i @  (   R&   R'   R   R   R1   R   R!   t	   hexdigest(   t   filet
   md5_digestR   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   X  s    	
c         C   s>   x7 t  |  | |  D]# } t | t  o t |  q q Wd S(   sZ   
    Extract the contents of the zip file C{filename} into the
    directory C{root}.
    N(   R   R&   RI   RJ   (   R   t   rootR   RL   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   j  s     c         c   s  | o5 t  i i d t i i |   d  t  i i   n y t i |   } WnP t i	 j
 o } t
 |  d  Vd  Sn( t j
 o } t
 |  |  Vd  Sn X| i   } g  } | D]! } | i d  o | | q q ~ } g  }	 | D]! } | i d  p |	 | q q ~	 }
 t i i |  p t i |  n x t |  D]w } | d  i d  } x[ t t |   D]G } t i i | | | d   } t i i |  p t i |  ququWqIWx t |
  D] \ } }  t i i | |  i d   } t | d  } y | i |   } Wn' t j
 o } t
 |  |  Vd  Sn X| i |  | i   | oN | d t |
  | d d t |
  j o! t  i i d  t  i i   qqW| o Hn d  S(	   Ns   Unzipping %si   s   Error with downloaded zip fileR
   iR   i
   Re   (   R   t   stdoutR   R   R   R   t   flusht   zipfilet   ZipFilet   errorRI   RJ   t   namelistR   R   R   Rn   R   Rj   R   R   R   R   R   (   R   R"  R   t   zfR   R(  R:   Rf   t   dirlistR   t   filelistt   dirnamet   piecesR   t   dirpathR   t   outt   contents(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR   s  sL    $	55   	
4c      
   C   s  g  } x t  t i i |  d   D] \ } } } t i | i  } d | | t i i | i  d f } t d   | i   D  } | i	 d d |  | i	 d d | i
  | i	 d d t | i   | i	 d	 |  | i	 d
 |  | i |  q" Wt t t i i |  d    }	 t	   }
 x[ | |	 D]O } | i d  |
 j o t d | i d    n |
 i | i d   q3Wt i d  } | i t i d   x | D] } | d i |  qW| i t i d   x |	 D] } | d i |  qWt |  | S(   s   
    Create a new data.xml index file, by combining the xml description
    files for various packages and collections.  C{root} should be the
    path to a directory containing the package xml and zip files; and
    the collection xml files.  The C{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: C{I{package}.zip}
    contains the package itself, as a compressed zip file; and
    C{I{package}.xml} is an xml description of the package.  The
    zipfile C{I{package}.zip} should expand to a single subdirectory
    named C{I{package/}}.  The base filename C{I{package}} must match
    the identifier given in the package's xml file.

    For each collection, there should be a single file
    C{I{collection}.zip}, describing the collection.

    All identifiers (for both packages and collections) must be unique.
    R5   s   %s/%s/%si   c         s   s   x |  ] } | i  Vq Wd  S(   N(   t	   file_size(   R   t   zf_info(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>  s    R   s   %sR   R   R   R   R`   R   s   Duplicate UID: %sR   i    (   t   _find_packagesR   R   R   R   R   R   R   t   infolistR  R   R   Rl   R   t   _find_collectionsR9   R   t   addR   t   Elementt   _indent_xml(   R"  t   base_urlR5   t   pkg_xmlR)  R   t   zipstatR   R   R`   t   uidsR6   t   top_eltRA   R>   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   build_index  s8     &!	   
R   c         C   s   t  |   d j o |  i p d i   d | d |  _ x |  D] } t | | d  q? Wx7 |  d  D]+ } | i p d i   d | d | _ qe W|  d i p d i   d | |  d _ n d S(   s   
    Helper for L{build_index()}: Given an XML ElementTree, modify it
    (and its descendents) C{text} and C{tail} attributes to generate
    an indented tree, where each nested element is indented by 2
    spaces with respect to its parent.
    i    R   s   
s     iN(   Rj   RP  R   R8  t   tail(   R*   R   R;   (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR8    s    %  )c            s   t  i i t  i i |  d  d   |  i d    j o# t d |  i d    f   n t   f d   | i   D  o t d     f   n d S(   sq   
    Helper for L{build_index()}: Perform some checks to make sure that
    the given package is consistent.
    i   i    R   s&   package identifier mismatch (%s vs %s)c         3   s4   x- |  ]& } |   j o | i    d   Vq Wd S(   R
   N(   R  (   R   R   (   t   uid(    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>  s   	s;   Zipfile %s.zip does not expand to a single subdirectory %s/N(   R   R   R   R   R9   R   R   R(  (   R:  t   zipfilenameR)  (    (   R@  s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _check_package  s    &c         C   s   t  i d d d |  g d t  i d t  i } | i   \ } } | i d j p | p | o3 t d t i i |   d t	 i
 |  f   n | i   d	 S(
   s   
    Helper for L{build_index()}: Calculate the subversion revision
    number for a given file (by using C{subprocess} to run C{svn}).
    t   svnRp   s   -vR#  t   stderri    s)   Error determining svn_revision for %s: %si   i   (   t
   subprocesst   Popent   PIPEt   communicatet
   returncodeR   R   R   R   Ru   Rv   (   R   R   R#  RD  (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   _svn_revision  s    	-c         c   s{   g  } xn t  i |   D]] \ } } } xK | D]C } | i d  o- t  i i | |  } t i |  i   Vq, q, Wq Wd S(   s   
    Helper for L{build_index()}: Yield a list of ElementTree.Element
    objects, each holding the xml for a single package collection.
    s   .xmlN(   R   R   R   R   R   R   R(   R   (   R"  R5   R,  t   subdirsR   R   t   xmlfile(    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR5    s      c         #   s  d d k  l } g  } xt i |   D]\ } } } d i | |  |   } xk| D]c} | i d  oMt i i | |  } | d  d }	 y t i |	  }
 Wn+ t	 j
 o } t
 d |	 | f   n Xy t i |  i   } Wn+ t	 j
 o } t
 d | | f   n Xt i i | d   d   | i d	    j o# t
 d
 | i d	    f   n t   f d   |
 i   D  o t
 d     f   n | |
 | f VqT qT Wy | i d  Wq& t
 j
 o q& Xq& Wd S(   st  
    Helper for L{build_index()}: Yield a list of tuples C{(pkg_xml,
    zf, subdir)}, where:
      - C{pkg_xml} is an ElementTree.Element holding the xml for a
        package
      - C{zf} is a zipfile.ZipFile for the package's contents.
      - C{subdir} is the subdirectory (relative to C{root}) where
        the package was found (e.g. 'corpora' or 'grammars').
    i(   t
   _path_fromR
   s   .xmlis   .zips   Error reading file %r!
%si   R   s&   package identifier mismatch (%s vs %s)c         3   s4   x- |  ]& } |   j o | i    d   Vq Wd S(   R
   N(   R  (   R   R   (   R@  (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys	   <genexpr>I  s   	s;   Zipfile %s.zip does not expand to a single subdirectory %s/s   .svnN(   t   nltk.corpus.reader.utilRM  R   R   R   R   R   R%  R&  RJ   R   R   R(   R   R   R9   R   R(  R   (   R"  RM  R5   R,  RK  R   t   relpathR   t   xmlfilenameRA  R)  R   R:  (    (   R@  s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR3  %  s:    
  c           C   s   t  t  i   d  S(   N(   R   t   _downloaderR   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   download_shell\  s    c           C   s   t  t  i   d  S(   N(   R   RQ  R   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyt   download_gui]  s    c           C   s   t  i   d  S(   N(   RQ  R!   (    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pyR!   ^  s    t   __main__(   t   OptionParsers   -ds   --dirt   destt   dirR  s!   download package to directory DIRt   metavart   DIRs   -qs   --quietR   t   actiont
   store_truet   defaults   work quietlys   -fs   --forceR   s"   download even if already installeds   -es   --exit-on-errorR   s   exit if an error occursR   RP   (O   R/   R   t   reR   R%  R   Ru   R  R   t	   cStringIOR    t   hashlibR   R1   R   t   TkinterR  t   nltk.draw.tableR   t	   nltk.drawR   Rk   R   R   t
   nltk.etreeR   R   R   t   import_from_stdlibR   t   objectR   R3   R<   R=   R?   R@   RB   RC   RD   RE   RF   RG   RH   RI   RM   RO   RQ   R   R   R   R   R   RE  R>  R8  RB  RJ  R5  R3  RQ  R   RR  RS  R!   R-   t   optparseRU  t   parsert
   add_optiont
   parse_argsR  R
  t   pkg_idRW  R   R   R   t   rv(    (    (    s%   /p/zhu/06/nlp/nltk/nltk/downloader.pys   <module>E   s   ]`


L&	  er   #		2	C				5						 