ó
ø`9Sc           @   s¼   d  d l  Z d  d l Z d  d l Z d  d l Z d  d l Td  d l Td  d l m Z d  d l m	 Z	 d  d l
 Td d g Z d „  Z d „  Z d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   *(   t   gui(   t   txtt   downloadAeronetSiteListFilet   downloadAeronetDubovikDataFilec         C   sC   t  |  d d d ƒ } | j | ƒ | j ƒ  t j k r; t St Sd S(   sá   Download AERONET site list file and store it at the given location on
    disk.

    Return 'True' if the operation succeeds and 'False' otherwise.

    'parentWidget' has to be the widget to own the download progress dialog.s   Updating AERONET site list...s"   Downloading AERONET site list files2   http://aeronet.gsfc.nasa.gov/aeronet_locations.txtN(   t   HttpRequestDialogt   setDestinationFilePatht   exec_t   QDialogt   Rejectedt   Falset   True(   t   parentWidgett   filePatht   requestDialog(    (    s   code\AeronetDownloader.pyR   "   s    	c         C   s÷  i | d 6d d 6d d 6| d d 6d d 6d	 d
 6| d d 6d d 6| d k rU d n d d 6d d 6d d 6d d 6} t  |  d d t j | ƒ t j | ƒ t j | ƒ f d t j | ƒ ƒ } | j ƒ  t j k rÖ t	 S| j
 ƒ  } t |  | ƒ } | j ƒ  t j k r
t	 St j j t j j | ƒ | j ƒ  ƒ }	 t  |  d d t j | ƒ t j | ƒ t j | ƒ f | j ƒ  ƒ }
 |
 j |	 ƒ |
 j ƒ  t j k ršt	 St j j | ƒ \ } } xÃ t rwyQ t j |	 ƒ } z0 | j ƒ  | g k sët ‚ | j | | ƒ PWd | j ƒ  XWqµt t t j f k
 rst j |  d d t j  |	 ƒ t j! t j" Bt j! ƒ } | t j" k rtt	 SqµXqµWxx t ròy t j# |	 ƒ PWq{t k
 rît j |  d d t j  |	 ƒ t j! t j$ Bt j! ƒ } | t j$ k rïPqïq{Xq{Wt S(    s  Download specified AERONET inversion product data file and store it at
    the given location on disk.

    Return 'True' if the operation succeeds and 'False' otherwise.

    'parentWidget' has to be the widget to own download progress and AERONET
    download confirmation dialogs.t   sitei   t   dayt   monthil  t   yeari   t   day2i   t   month2t   year2t   DUBOVs   1.5t   151t   21t   TYPEt   40t   MODEt   10t   AVGt   Downloadt   Submits    Downloading AERONET data file...s7   Querying AERONET inversion product for %s, %s, level %ss?   http://aeronet.gsfc.nasa.gov/cgi-bin/print_warning_opera_v2_invs:   Downloading AERONET inversion product for %s, %s, level %sNs   Failed to save data to disks;   Failed to extract data from the downloaded zip archive (%s)s!   Failed to clean up temporary datas0   Failed to delete the downloaded zip archive (%s)(%   R   R   t   quotet   quoteNumbert   urllibt	   urlencodeR   R   R	   R
   t   getDownloadedDatat   AeronetConfirmationDialogt   ost   patht   joint   dirnamet   getProductFileNamet   getProductUrlR   t   splitR   t   zipfilet   ZipFilet   namelistt   AssertionErrort   extractt   closet   IOErrort   OSErrort
   BadZipfilet   QMessageBoxt   criticalt	   quotePatht   Retryt   Abortt   removet   Ignore(   R   t   siteNameR   t	   dataLevelR   t	   queryDatat   queryDialogt   queryResponseTextt   confirmationDialogt   zipFilePatht   downloadDialogt   productFileDirt   productFileNamet   zipFilet   answer(    (    s   code\AeronetDownloader.pyR   7   s„    
(	(				R%   c           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s£   Dialog that displays contents of an AERONET download confirmation page
    and allows the user to either agree with proposed conditions or cancel the
    download.c   
      C   s§  t  j |  | ƒ |  j | ƒ t ƒ  } t ƒ  |  _ |  j j |  j ƒ | j |  j ƒ |  j	 d  k	 rØ t d ƒ } | j t ƒ | j t ƒ | j j |  j ƒ t d ƒ } | j t ƒ | j j |  j ƒ | | g } n5 t d ƒ } | j t ƒ | j j |  j ƒ | g } t ƒ  } | j ƒ  x | D] }	 | j |	 ƒ q'W| j ƒ  | j | ƒ |  j | ƒ |  j |  j ƒ |  j t j d ƒ ƒ |  j |  j ƒ  t j  @ƒ d  S(   Ns   &Accepts   Do &Not Acceptt   Closet   world(!   R   t   __init__t   parseConfirmationPageTextt   QVBoxLayoutt   HtmlDisplayWidgett   outputWindowt   setTextt   htmlTextt	   addWidgett
   productUrlt   Nonet   QPushButtont   setAutoDefaultR
   t
   setDefaultR   t   clickedt   connectt   acceptt   rejectt   QHBoxLayoutt
   addStretcht	   addLayoutt	   setLayoutt   setWindowTitlet   windowTitlet   setWindowIconR   t   loadIcont   setWindowFlagst   windowFlagst   Qtt   WindowContextHelpButtonHint(
   t   selft   parentt   confirmationPageTextt   layoutt   acceptButtont   doNotAcceptButtont   buttonst   closeButtont   buttonBoxLayoutt   button(    (    s   code\AeronetDownloader.pyRK   ¼   s<    			

c         C   s   |  j  S(   N(   RS   (   Rh   (    (    s   code\AeronetDownloader.pyR+   ñ   s    c         C   s   |  j  S(   N(   RF   (   Rh   (    (    s   code\AeronetDownloader.pyR*   ô   s    c         C   sÌ  t  j d | t  j ƒ } | d  k	 s* t ‚ | j d ƒ |  _ t  j d | t  j ƒ } | d  k	 sf t ‚ | j ƒ  } d } d } t  j | | t  j ƒ } | d  k	 rË | j ƒ  | j ƒ  k  rË | | d 7} n  t  j d | t  j ƒ } | d  k	 rT| j d ƒ |  _	 d |  j	 |  _
 d	 }	 t  j d
 |	 | t  j ƒ }
 |
 d  k	 sEt ‚ |
 j ƒ  } n` d  |  _
 d  |  _	 d } t  j d
 | | t  j ƒ } | d  k	 sšt ‚ | j ƒ  } | | d 7} | | | | !|  _ d  S(   Ns   <title>([^<]*)</title>i   s   <body[^>]*>t    s/   <center><h1>AERONET Download Site</h1></center>s   
sF   <a href="/zip_files_inv/([^"]*)"[^>]*><img src="/accept.gif"[^>]*></a>s+   http://aeronet.gsfc.nasa.gov/zip_files_inv/s#   If you accept the above conditions,s   <p>s-   No data is available during the given period.s   <p>
(   t   ret   searcht
   IGNORECASERT   R0   t   groupRa   t   endt   startRF   RS   RQ   (   Rh   t   pageTextt
   titleMatcht	   bodyMatcht   htmlTextStartt   htmlPrefixTextt
   headerTextt   headerMatcht   zipFileMatcht   acceptMessageTextt   acceptMessageMatcht   htmlTextEndt   errorMessageTextt   errorMessageMatch(    (    s   code\AeronetDownloader.pyRL   ø   s@    	$			(   t   __name__t
   __module__t   __doc__RK   R+   R*   RL   (    (    (    s   code\AeronetDownloader.pyR%   ¬   s
   	5		RN   c           B   s)   e  Z d  Z d d d d „ Z d „  Z RS(   s6   A widget suitable for displaying of static HTML pages.iP   i   c         C   s3   t  j |  | ƒ |  j t ƒ | |  _ | |  _ d S(   s~   'width' and 'height' should specify preferred dimensions of the
        output window, in characters (using the default font).N(   t	   QTextEditRK   t   setReadOnlyR   t   widtht   height(   Rh   R‹   RŒ   Ri   (    (    s   code\AeronetDownloader.pyRK   G  s    	c         C   s¤   t  |  j ƒ  ƒ } |  j ƒ  } t |  j ƒ  j ƒ  ƒ } | j d |  j ƒ | d | d } | j ƒ  |  j | d | } | |  j ƒ  j	 ƒ  j ƒ  7} t
 | | ƒ S(   Nt   xi   (   t   QFontMetricst   fontt
   frameWidtht   intt   documentt   documentMarginR‹   RŒ   t   verticalScrollBart   sizeHintt   QSize(   Rh   t   metricsR   t   marginR‹   RŒ   (    (    s   code\AeronetDownloader.pyR•   S  s    &N(   R†   R‡   Rˆ   RT   RK   R•   (    (    (    s   code\AeronetDownloader.pyRN   D  s   (   t   os.pathR&   Rs   R"   R-   t   PyQt4.QtCoret   PyQt4.QtGuit   common.utilsR   R   t   common.HttpRequestDialogt   __all__R   R   R   R%   R‰   RN   (    (    (    s   code\AeronetDownloader.pyt   <module>   s   


		u˜