σ
mάJ]c           @` sx  d  Z  d d l m Z m Z m Z m Z d d l Z e j e  Z	 d d l
 m Z d d l m Z d d l 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% Z d e f d     YZ e d e d   Z d e f d     YZ d e f d     YZ d e f d     YZ  d   Z! e d  Z" d   Z# d  Z$ d!   Z% d"   Z& d#   Z' d$   Z( d S(&   u   

i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   OrderedDict(   t   collections_abc(   t   contextmanageri   (   t   Document(   t   Modelt   collect_models(   t   settings(   t   make_globally_unique_idu
   FromCurdocu   OutputDocumentForu
   RenderItemu
   RenderRootu   RenderRootsu   standalone_docs_jsonu%   standalone_docs_json_and_render_itemsu   submodel_has_python_callbackst
   FromCurdocc           B` s   e  Z d  Z RS(   u    This class merely provides a non-None default value for ``theme``
    arguments, since ``None`` itself is a meaningful value for users to pass.

    (   t   __name__t
   __module__t   __doc__(    (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR   ;   s   c         #` s  t    t j  s< t    d k s< t d     D  rK t d   n  d   } t d     D  } d
 | k r | j d
  n  | rͺ   f d   } t	    } n± t |  d k rζ t
   } x   D] } | j |  qΜ Wnu t |  d k r@| j   } t    t | j  k r[  f d   } t	    } q[n   f d	   } t	    } t j   rt| j   n  t | |  | Vt |  |   d
 S(   u&   Find or create a (possibly temporary) Document to use for serializing
    Bokeh content.

    Typical usage is similar to:

    .. code-block:: python

         with OutputDocumentFor(models):
            (docs_json, [render_item]) = standalone_docs_json_and_render_items(models)

    Inside the context manager, the models will be considered to be part of a single
    Document, with any theme specified, which can thus be serialized as a unit. Where
    possible, OutputDocumentFor attempts to use an existing Document. However, this is
    not possible in three cases:

    * If passed a series of models that have no Document at all, a new Document will
      be created, and all the models will be added as roots. After the context manager
      exits, the new Document will continue to be the models' document.

    * If passed a subset of Document.roots, then OutputDocumentFor temporarily "re-homes"
      the models in a new bare Document that is only available inside the context manager.

    * If passed a list of models that have differnet documents, then OutputDocumentFor
      temporarily "re-homes" the models in a new bare Document that is only available
      inside the context manager.

    OutputDocumentFor will also perfom document validation before yielding, if
    ``settings.perform_document_validation()`` is True.


        objs (seq[Model]) :
            a sequence of Models that will be serialized, and need a common document

        apply_theme (Theme or FromCurdoc or None, optional):
            Sets the theme for the doc while inside this context manager. (default: None)

            If None, use whatever theme is on the document that is found or created

            If FromCurdoc, use curdoc().theme, restoring any previous theme afterwards

            If a Theme instance, use that theme, restoring any previous theme afterwards

        always_new (bool, optional) :
            Always return a new document, even in cases where it is otherwise possible
            to use an existing document on models.

    Yields:
        Document

    i    c         s` s   |  ] } t  | t  Vq d  S(   N(   t
   isinstanceR   (   t   .0t   x(    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pys	   <genexpr>y   s    u.   OutputDocumentFor expects a sequence of Modelsc           S` s   d  S(   N(    (    (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   finish|   t    c         s` s   |  ] } | j  Vq d  S(   N(   t   document(   R   R   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pys	   <genexpr>~   s    c           ` s   t     d  S(   N(   t   _dispose_temp_doc(    (   t   objs(    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR      s    i   c           ` s   t     d  S(   N(   R   (    (   R   (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR      s    c           ` s   t     d  S(   N(   R   (    (   R   (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR      s    N(   R   R   t   Sequencet   lent   allt
   ValueErrort   sett   Nonet   removet   _create_temp_docR   t   add_roott   popt   rootsR
   t   perform_document_validationt   validatet   _set_temp_themet   _unset_temp_theme(   R   t   apply_themet
   always_newR   t   docst   doct   model(    (   R   s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   OutputDocumentForB   s6    7<	 	
t
   RenderItemc           B` s2   e  Z d d d d d d   Z d   Z d   Z RS(   c         C` sΘ   | d  k r | d  k s0 | d  k	 r? | d  k	 r? t d   n  | d  k rW t   } n: t | t  r t g  | D] } | t   f ^ qp  } n  | |  _ | |  _ | |  _ t	 |  |  _
 | |  _ d  S(   Nu*   either docid or sessionid must be provided(   R   R   R   R   t   listR   t   docidt	   sessionidt	   elementidt   RenderRootsR"   t   use_for_title(   t   selfR/   R0   R1   R"   R3   t   root(    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   __init__¬   s    0+			c         C` s   i  } |  j  d  k	 r% |  j  | d <n |  j | d <|  j d  k	 rQ |  j | d <n  |  j rp |  j j   | d <n  |  j d  k	 r |  j | d <n  | S(   Nu   docidu	   sessionidu	   elementidu   rootsu   use_for_title(   R/   R   R0   R1   R"   t   to_jsonR3   (   R4   t   json(    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR7   »   s    	c         C` s0   t  | |  j  s t S|  j   | j   k Sd  S(   N(   R   t	   __class__t   FalseR7   (   R4   t   other(    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   __eq__Ξ   s    N(   R   R   R   R6   R7   R<   (    (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR-   ͺ   s   	t
   RenderRootc           B` s    e  Z d d d   Z d   Z RS(   c         C` s4   | |  _  | |  _ | p d |  _ | p* g  |  _ d  S(   Nu    (   R1   t   idt   namet   tags(   R4   R1   R>   R?   R@   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR6   Φ   s    		c         C` s*   t  | |  j  s t S|  j | j k Sd  S(   N(   R   R9   R:   R1   (   R4   R;   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR<   ά   s    N(   R   R   R   R6   R<   (    (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR=   Τ   s   R2   c           B` s5   e  Z d    Z d   Z d   Z d   Z d   Z RS(   c         C` s   | |  _  d  S(   N(   t   _roots(   R4   R"   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR6   δ   s    c         C` s   t  |  j j    S(   N(   R   RA   t   items(   R4   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   __len__η   s    c         C` s   t  | t  r1 t |  j j    | \ } } nC x@ |  j j   D] \ } } | j | k rA PqA qA Wt d |   t | | j | j | j	  S(   Nu   root with '%s' name not found(
   R   t   intR.   RA   RB   R?   R   R=   R>   R@   (   R4   t   keyR5   R1   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   __getitem__κ   s    "c         C` s   |  j  |  S(   N(   RF   (   R4   RE   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   __getattr__φ   s    c         C` s5   t  g  |  j j   D] \ } } | j | f ^ q  S(   N(   R   RA   RB   R>   (   R4   R5   R1   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR7   ω   s    (   R   R   R6   RC   RF   RG   R7   (    (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR2   β   s
   				c         C` s   t  |   \ } } | S(   u   

    (   t%   standalone_docs_json_and_render_items(   t   modelst	   docs_jsont   render_items(    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   standalone_docs_jsonό   s    c         C` sΡ  t  |  t t f  r! |  g }  n  t  |  t j  oF t d   |  D  sX t d   n  t |   r{ | r{ t j	 t
  n  i  } xΘ |  D]ΐ } t  | t  r¬ d } | } n* | } | j } | d k rΦ t d   n  | | k rϋ t   t   f | | <n  | | \ } } | d k	 r't   | | <q x | j D] } t   | | <q1Wq Wi  } x0 | j   D]" \ } \ } }	 | j   | | <q_Wg  }
 x9 | j   D]+ \ }	 \ } } |
 j t | d |  qW| |
 f S(   u   

    c         s` s$   |  ] } t  | t t f  Vq d  S(   N(   R   R   R   (   R   R   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pys	   <genexpr>
  s    u>   Expected a Model, Document, or Sequence of Models or DocumentsuH   A Bokeh Model must be part of a Document to render as standalone contentR"   N(   R   R   R   R   R   R   R   t   submodel_has_python_callbackst   logt   warningt   _CALLBACKS_WARNINGR   R   R   R   R"   RB   R7   t   appendR-   (   RI   t   suppress_callback_warningR)   t   model_or_docR+   R*   R/   R"   RJ   t   _RK   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyRH     s:    (		c         C` sU   t  } xH t |   D]: } t | j  d k sC t | j  d k r t } Pq q W| S(   u@    Traverses submodels to check for Python (event) callbacks

    i    (   R:   R	   R   t
   _callbackst   _event_callbackst   True(   RI   t   has_python_callbackR+   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyRM   1  s    *uF  
You are generating standalone HTML/JS output, but trying to use real Python
callbacks (i.e. with on_change or on_event). This combination cannot work.

Only JavaScript callbacks may be used with standalone output. For more
information on JavaScript callbacks with Bokeh, see:

    http://bokeh.pydata.org/en/latest/docs/user_guide/interaction/callbacks.html

Alternatively, to use real Python callbacks, a Bokeh server application may
be used. For more information on building and running Bokeh applications, see:

    http://bokeh.pydata.org/en/latest/docs/user_guide/server.html
c         C` sp   t    } xW |  D]O } | | j | j <| | _ x- | j   D] } | | j | j <| | _ q< Wq W|  | _ | S(   N(   R   t   _all_modelsR>   t   _temp_documentt
   referencesRA   (   RI   R*   t   mt   ref(    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR   P  s    			c         C` s>   x7 |  D]/ } d  | _ x | j   D] } d  | _ q# Wq Wd  S(   N(   R   RZ   R[   (   RI   R\   R]   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR   [  s    	c         C` sZ   |  j  |  _ | t k r> d d l m } | |   j  |  _  n | d  k	 rV | |  _  n  d  S(   Ni   (   t   curdoc(   t   themet
   _old_themeR   t   ioR^   R   (   R*   R'   R^   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR%   a  s     c         C` s)   t  |  d  s d  S|  j |  _ |  ` d  S(   Nu
   _old_theme(   t   hasattrR`   R_   (   R*   (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyR&   i  s    (   u
   FromCurdocu   OutputDocumentForu
   RenderItemu
   RenderRootu   RenderRootsu   standalone_docs_jsonu%   standalone_docs_json_and_render_itemsu   submodel_has_python_callbacks()   R   t
   __future__R    R   R   R   t   loggingt	   getLoggerR   RN   t   collectionsR   t   bokeh.util.futureR   t
   contextlibR   t   document.documentR   R+   R   R	   R
   t   util.serializationR   t   __all__t   objectR   R   R:   R,   R-   R=   R2   RL   RH   RM   RP   R   R   R%   R&   (    (    (    s/   lib/python2.7/site-packages/bokeh/embed/util.pyt   <module>	   s@   "       g*	.				