
:\c           @` s  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z m Z m Z d d l m Z m Z d d l m Z m Z d d l m Z d d	 l m Z m Z m Z m Z d d
 l m Z d d l m  Z  m! Z! d d l" m# Z# d d l$ m% Z% d d l& m' Z' d d l( m) Z) m* Z* m+ Z+ m, Z, m- Z- d d l. m/ Z/ d d l0 m1 Z1 m2 Z2 d e) f d     YZ3 d e- f d     YZ4 d   Z5 d   Z6 e7 d  Z8 d   Z9 e: d k re9   n  d S(   s   Community Tab.i    (   t   absolute_importt   divisiont   print_functionN(   t   PYQT4(   t   Qtt   QTimert   Signal(   t   QImaget   QPixmap(   t   QHBoxLayoutt   QVBoxLayout(   t   AnacondaAPI(   t   CONFt   CONTENT_JSON_PATHt   CONTENT_PATHt   IMAGE_DATA_PATH(   t   LINKS_INFO_PATH(   t	   LOGO_PATHt   VIDEO_ICON_PATH(   t   logger(   t   parse(   t   load_style_sheet(   t
   ButtonBaset   FrameTabContentt   FrameTabHeadert   SpacerHorizontalt
   WidgetBase(   t   LineEditSearch(   t   ListItemContentt   ListWidgetContentt   ButtonTogglec           B` s    e  Z d  Z d   Z d   Z RS(   s"   Toggle button used in CSS styling.c         ` sF   t  t    j | |     j t    j j d   f d   d S(   s"   Toggle button used in CSS styling.c         ` s     j  S(   N(   t
   _fix_check(   t   v(   t   self(    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   <lambda>7   t    N(   t   superR   t   __init__t   setCheckablet   Truet   clickedt   connectt   None(   R!   t   argst   kwargs(    (   R!   sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR%   3   s    c         C` s1   |  j  d |  j    |  j  d |  j    d  S(   Nt   checkedt	   unchecked(   t   setPropertyt	   isChecked(   R!   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR   9   s    (   t   __name__t
   __module__t   __doc__R%   R   (    (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR   0   s   	t   CommunityTabc        
   B` s   e  Z d  Z e e e  Z e e e e e  Z e e  Z	 g  Z
 d Z d Z d Z d Z d Z d d d e e e e e d d 	 Z d   Z d   Z d   Z d d	  Z d
   Z d   Z d d  Z d   Z d   Z d   Z  d d  Z! d d  Z" RS(   s   Community tab.i   s   in-appt	   navigatorR#   c
         C` s  t  t |   j d |  d |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _	 g  |  _
 g  |  _ g  |  _ t   |  _ | |  _ g  |  _ d |  _ d |  _ | |  _ t   |  _ i  |  _ g  |  _ t t  j d d t j t j  |  _ t   |  _  t!   |  _" t#   |  _$ t%   |  _& |  j j' d  |  j" j( t j) t*  |  j  j+ d  |  j  j( t j) t*  |  j, d	  |  j" j- d
  |  j  j.   }
 |  j  j/ |
 j0 d d   t1   |  _2 t1   } | j3 |  j2  | j4   | j5 |  j   |  j$ j6 |  t1   } | j5 |  j"  |  j& j6 |  t7   } | j5 |  j$  | j5 |  j&  |  j6 |  |  j j8 j9 |  j:  |  j  j; j9 |  j<  d S(   s   Community tab.t   parentR#   i    i   id   i<   iM  t   Searcht   Tabi   t   Mi   N(=   R$   R4   R%   t	   _tab_namet   content_patht
   image_patht   bundle_patht   saved_content_patht   configt   _parentt   _downloaded_thumbnail_urlst   _downloaded_urlst   _downloaded_filepathsR   t   apit   content_urlst   content_infot   stept	   step_sizet   tagsR   t
   timer_loadt   pixmapst   filter_widgetsR   R   t   scaledR   t   KeepAspectRatiot   FastTransformationt   default_pixmapR   t   text_filterR   t   listR   t   frame_headerR   t   frame_contentt   setIntervalt   setAttributet   WA_MacShowFocusRectt   Falset   setPlaceholderTextt   setObjectNamet   setMinimumHeightt   fontMetricst   setMaximumWidtht   widthR	   t   filters_layoutt	   addLayoutt
   addStretcht	   addWidgett	   setLayoutR
   t   timeoutR)   t   set_content_listt   textChangedt   filter_content(   R!   R6   RI   RE   R;   R<   R?   R=   R>   t   tab_namet   fmt   layout_headert   layout_contentt   layout(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR%   S   sb    																		
		c         C` s   |  j    d S(   s   Setup tab content.N(   t   download_content(   R!   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   setup   s    c         C` sE   | j  } | |  j k r+ |  j j |  n  |  j sA |  j   n  d S(   s   Callbacl for download_content.N(   t   urlRB   t   removet   load_content(   R!   t   workert   outputt   errorRo   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   _json_downloaded   s
    		c         C` s   g  |  _  g  |  _ |  j r x |  j D] } | j   } | j d  d d } t j j |  j | g  } |  j  j	 |  |  j j	 |  |  j
 j | |  } | | _ | j j |  j  q% Wn
 |  j   d S(   s%   Download content to display in cards.t   /is   .jsonN(   RB   RC   RE   t   lowert   splitt   ost   sept   joinR;   t   appendRD   t   downloadRo   t   sig_finishedR)   Ru   Rq   (   R!   Ro   t   fnamet   filepathRr   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyRm      s    				c         C` s  g  } x|  j  D]} | j t j  d } g  } t j j |  r t | d   } | j   } Wd QXy t j	 |  } Wq t
 k
 r } t j t | | f   q Xn g  } d | k rx | D] }	 y d g |	 d <|	 j d d  |	 d <|	 d r3|	 j d  |	 d	 <|	 d	 j d
  d }
 |
 |	 d <n d } d |	 d <| |	 d	 <|	 j d d  |	 d <Wq t
 k
 rt j d j |	   q Xq W| |  j  } nd | k rXx | D] }	 y_ d g |	 d <|	 j d d  |	 d <|	 d	 r|	 d	 j d
  d }
 |
 |	 d <n
 d |	 d	 <Wqt
 k
 rCt j d j |	   qXqW| |  j  } n.d | k rx| D]}	 y d g |	 d <|	 j d d  } |	 j d d  } |  j | |  |	 d <|	 j d d  } | r3t | t  r3| j d d  |	 d	 <|	 d	 r&|	 d	 j d
  d }
 |
 |	 d <qGd |	 d <n d |	 d	 <d |	 d <Wqkt
 k
 rqt j d j |	   qkXqkW| |  j  } n  | r | j |  q q Wt |  j d   } | j   } Wd QXg  } y t j	 |  } Wn, t
 k
 r} t j t | | f   n X| j |  x t |  D]~ \ } }	 |	 d } | j d d  j d d  } | j d d  |	 d <|	 j d d  } t j j j |  j | g  |	 d <q)Wt | d d   } g  } x t |  D]q \ } }	 |	 j d  } t | t  r| d k } n | d k r"t } n  | r| j  |	  | j! |	  qqW| | } | |  _" t |  j# d    } t j$ | |  Wd QX|  j%   |  j& j' t( j) d! d"   d S(#   s$   Load downloaded and bundled content.it   rNt   videoRI   R#   t   urit	   thumbnailt   bannerRv   t
   image_filet
   date_startt   dates   Video parse failed: {0}t   eventRo   s   Event parse failed: {0}t   webinart   utm_campaignt   imaget   srct   image_file_paths   Webinar parse failed: {0}s   <p>s   </p>t    s   %20t   keyc         S` s   |  j  d  S(   NRI   (   t   get(   t   i(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR"   %  R#   t   stickyt   truet   wi   i#   (*   RC   Rx   Ry   Rz   t   patht   isfilet   opent   readt   jsont   loadst	   ExceptionR   Rt   t   strR   t   debugt   formatt   VIDEOS_LIMITt   EVENTS_LIMITt   add_campaignt
   isinstancet   dictt   WEBINARS_LIMITt   extendR=   t	   enumeratet   replaceR{   R<   t   sortedR*   RX   R|   Rp   RF   R>   t   dumpt   make_tag_filtersRJ   t   startt   randomt   randint(   R!   t   pathst   contentR   R   t   itemst   ft   dataRt   t   itemR<   Ro   R   R   R   R   t   filenamet   sticky_contentR   t	   is_sticky(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyRq      s     






		
	
c         C` sO   | rK | rK t  j i |  j d 6|  j d 6| d 6 } d j | |  } n  | S(   s8   Add tracking analytics campaing to url in content items.t
   utm_sourcet
   utm_mediumR   s   {0}?{1}(   R   t	   urlencodet
   UTM_SOURCEt
   UTM_MEDIUMR   (   R!   R   R   t
   parameters(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR   ?  s    	

c   	      C` s  |  j  sh t   |  _  xP |  j D]B } | j d g   } x' | D] } | r> |  j  j |  q> q> Wq Wn  d   |  j  D } xV |  j  D]K } xB |  j D]7 } | j d g   } | | k r | | c d 7<q q Wq Wt j d j |  j    g  |  _ x t	 |  j   D] } | | } d j | j
   |  j   } t |  } | j | j    | j |  j j d | j   t   | j j |  j  |  j j |  |  j j |  |  j j t    qWd S(   s@   Create tag filtering checkboxes based on available content tags.RI   c         S` s   i  |  ] } d  |  q S(   i    (    (   t   .0t   tag(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pys
   <dictcomp>U  s   	 i   s	   TAGS: {0}s	   {0} ({1})t
   checkboxesN(   RI   t   setRF   R   t   addR   R   R   RL   R   t
   capitalizet   stripR   RZ   Rw   t
   setCheckedR?   R'   R(   R)   Rg   R|   R_   Rb   R   (	   R!   t   content_itemRI   R   t	   tag_countt	   item_tagst   countt   tag_textR   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR   J  s2    		
%c   	      ` s  |  j  j   j   } g  t j d |  D] } | r( | ^ q( } g    x{ |  j D]p } | j   j   j   } | d } | j   r   j |  |  j j	 d | t
  qP |  j j	 d | t  qP Wxt |  j j    D] } |  j j |  } g  } x | D]| } | j   } | | j j   k pk| | j j   k pk| d j | j  j   k pk| | j j   k } | j |  q W| j t   f d   | j D   t |  r| j t  q | j t
  q Wd S(   s   
        Filter content by a search string on all the fields of the item.

        Using comma allows the use of several keywords, e.g. Peter,2015.
        s   \Wi    R   R   c         3` s!   |  ] } | j      k Vq d  S(   N(   Rw   (   R   R   (   t   selected_tags(    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pys	   <genexpr>  s    N(   RQ   t   textRw   t   reRx   RL   R0   R|   R?   R   R'   RX   t   rangeRR   R   R   R   t   titlet   venueR{   t   authorst   summaryt   anyRI   t   allt	   setHidden(	   R!   R   t   tR   t	   tag_partsR   R   t
   all_checkst   checks(    (   R   sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyRg   i  s0    +
* c         C` s  xt  |  j |  j |  j  D]z} | t |  j  k  rv|  j | } | j d d  } | j d d  } t d | d d | j d d  p d d | d d | j d d  p d d | j d d  p d d	 | j d	 g   d | d
 | d |  j  	} |  j j	 |  |  j
 | _
 | j d d  } t j j t |  } t j j |  r`t |  |  j
 | <q|  j | | |  q |  j j   |  j j |  j  Pq W|  j |  j 7_ |  j   d S(   sO   
        Add items to the list, gradually.

        Called by a timer.
        R   R#   R   R   t   subtitleR   R   R   RI   R   t   pixmapR   t   NaNN(   R   RG   RH   t   lenRF   R   R   RP   RR   t   addItemRK   Ry   R   R{   R   R   R   t   download_thumbnailRJ   t   stopt	   sig_readyt   emitR:   Rg   (   R!   R   R   R   R   R   R   t   local_image(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyRe     s6    #

c         C` s   | r | |  j  k r |  j  j |  |  j j | | d t } | | _ | | _ | | _ | j j	 |  j
  t j d j |   n  d S(   s"   Download all the video thumbnails.t   chunkeds   Fetching thumbnail {}N(   RA   R|   RD   R}   R'   Ro   R   R   R~   R)   t   convert_imageR   R   R   (   R!   R   Ro   R   Rr   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR     s    			c         C` sG  | } | |  j  k r d Sy t j d k r t r d d l m } d d l m } | r | j |  } | |  } t	 |  } t
 j |  }	 qt
   }	 nj | r t j j |  r | j d  d j   }
 |
 d k r t
 | d |
 }	 qt
 |  }	 n	 t
   }	 |	 |  j  | <Wn, t t f k
 rB} t j t |   n Xd S(   s   
        Load an image using PIL, and converts it to a QPixmap.

        This was needed as some image libraries are not found in some OS.
        Nt   darwini    (   t   ImageQt(   t   Imaget   .it   PNGt   JPEGt   JPGR   (   R   R   R   (   RK   t   syst   platformR   t   PIL.ImageQtR   t   PILR   R   R   R   t	   fromImageRy   R   R   Rx   t   uppert   IOErrort   OSErrorR   Rt   R   (   R!   Rr   Rs   Rt   R   R   R   R   t   qt_imageR   t	   extension(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR     s,    	c         C` sK   | d k r t   |  _ n	 | |  _ |  j |  j  |  j j |  j  d S(   s   Update custom CSS stylesheet.N(   R*   R   t   style_sheett   setStyleSheetRR   t   update_style_sheet(   R!   R   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR     s
    	c         C` s:   g  } | |  j  7} | |  j g 7} | |  j j   7} | S(   s   Fix tab order of UI widgets.(   RL   RQ   RR   t   ordered_widgets(   R!   t   next_widgetR   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR     s
    N(#   R1   R2   R3   R   R   t   intt   sig_video_startedt   objectt   sig_status_updatedR   t	   instancesR   R   R   R   R   R*   R   R   R   R   R   R%   Rn   Ru   Rm   Rq   R   R   Rg   Re   R   R   R   R   (    (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyR4   @   s>   B						'	*		&
c          C` s1   d }  |  d } |  d } |  d } | | | g S(   s0   Return content endpoints for development server.s5   http://api-dev-continuum-content.pantheonsite.io/api/t   videost   eventst   webinars(    (   t   devt
   VIDEOS_URLt
   EVENTS_URLt   WEBINARS_URL(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   dev_endpoints  s
    


c          C` s   d }  d } d } | |  | g S(   s/   Return content endpoints for production server.s   http://anaconda.com/api/videoss   http://anaconda.com/api/eventss    http://anaconda.com/api/webinars(    (   R  R  R  (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   production_endpoints  s    c         C` sE   |  r t    n t   } t d |  } | j t    | j   | S(   s7   Test production and development json content endpoints.RE   (   R  R	  R4   R   R   Rn   (   R  R   t   widget(    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   test_json_endpoint  s
    
c          C` s   d d l  }  d d l m } d d l m } |  j   } d } | d | d |  t j j | |  } |   } t	 d t
  } | j   t	 d t  } | j   | j   }	 t |   }
 |
 j   } Wd QXd	 | k r t j d
  n  t j |	  d S(   s   Run local test.i    N(   t   qapplication(   t   setup_loggers   testlog.logt
   log_foldert   log_filenameR  s   No JSON object could be decodedi   (   t   tempfilet"   anaconda_navigator.utils.qthelpersR  t   anaconda_navigator.utils.logsR  t   mkdtempRy   R   R{   R  R'   t   showRX   t   exec_R   R   R   t   exit(   R  R  R  R  R  t   log_patht   appt   widget_developmentt   widget_productiont   exR   R   (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt
   local_test  s$    	

t   __main__(;   R3   t
   __future__R    R   R   R   Ry   R   R   R   t   qtpyR   t   qtpy.QtCoreR   R   R   t
   qtpy.QtGuiR   R   t   qtpy.QtWidgetsR	   R
   t#   anaconda_navigator.api.anaconda_apiR   t   anaconda_navigator.configR   R   R   R   t!   anaconda_navigator.static.contentR   t    anaconda_navigator.static.imagesR   R   R  R   t"   anaconda_navigator.utils.py3compatR   t   anaconda_navigator.utils.stylesR   t   anaconda_navigator.widgetsR   R   R   R   R   t(   anaconda_navigator.widgets.helperwidgetsR   t(   anaconda_navigator.widgets.lists.contentR   R   R   R4   R  R	  RX   R  R  R1   (    (    (    sH   lib/python2.7/site-packages/anaconda_navigator/widgets/tabs/community.pyt   <module>   s:   "( 					