<s> from __future__ import absolute_import <EOL> from collections import Iterable , OrderedDict , Sequence <EOL> import difflib <EOL> import itertools <EOL> import re <EOL> import warnings <EOL> import numpy as np <EOL> from six import string_types <EOL> from . . models import ( <EOL> BoxSelectTool , BoxZoomTool , CategoricalAxis , <EOL> TapTool , CrosshairTool , DataRange1d , DatetimeAxis , <EOL> FactorRange , Grid , HelpTool , HoverTool , LassoSelectTool , Legend , LinearAxis , <EOL> LogAxis , PanTool , PolySelectTool , ContinuousTicker , <EOL> PreviewSaveTool , Range , Range1d , UndoTool , RedoTool , ResetTool , ResizeTool , Tool , <EOL> WheelZoomTool , ColumnDataSource , GlyphRenderer ) <EOL> from . . core . properties import ColorSpec , Datetime <EOL> from . . util . string import nice_join <EOL> DEFAULT_PALETTE = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> def get_default_color ( plot = None ) : <EOL> colors = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" <EOL> ] <EOL> if plot : <EOL> renderers = plot . renderers <EOL> renderers = [ x for x in renderers if x . __view_model__ == "<STR_LIT>" ] <EOL> num_renderers = len ( renderers ) <EOL> return colors [ num_renderers ] <EOL> else : <EOL> return colors [ <NUM_LIT:0> ] <EOL> def get_default_alpha ( plot = None ) : <EOL> return <NUM_LIT:1.0> <EOL> def _glyph_doc ( args , props , desc ) : <EOL> params_tuple = tuple ( itertools . chain . from_iterable ( sorted ( list ( args . items ( ) ) ) ) ) <EOL> params = "<STR_LIT>" * len ( args ) % params_tuple <EOL> return """<STR_LIT>""" % ( desc , params , props ) <EOL> def _pop_renderer_args ( kwargs ) : <EOL> result = dict ( data_source = kwargs . pop ( '<STR_LIT:source>' , ColumnDataSource ( ) ) ) <EOL> for attr in [ '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> val = kwargs . pop ( attr , None ) <EOL> if val : result [ attr ] = val <EOL> return result <EOL> def _pop_colors_and_alpha ( glyphclass , kwargs , prefix = "<STR_LIT>" , default_alpha = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> result = dict ( ) <EOL> color = kwargs . pop ( prefix + "<STR_LIT>" , get_default_color ( ) ) <EOL> for argname in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if argname not in glyphclass . properties ( ) : continue <EOL> result [ argname ] = kwargs . pop ( prefix + argname , color ) <EOL> if "<STR_LIT>" in glyphclass . properties ( ) : <EOL> result [ "<STR_LIT>" ] = kwargs . pop ( prefix + "<STR_LIT>" , "<STR_LIT>" ) <EOL> alpha = kwargs . pop ( prefix + "<STR_LIT>" , default_alpha ) <EOL> for argname in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> if argname not in glyphclass . properties ( ) : continue <EOL> result [ argname ] = kwargs . pop ( prefix + argname , alpha ) <EOL> return result <EOL> def _process_sequence_literals ( glyphclass , kwargs , source ) : <EOL> dataspecs = glyphclass . dataspecs_with_props ( ) <EOL> for var , val in kwargs . items ( ) : <EOL> if not isinstance ( val , Iterable ) : continue <EOL> if isinstance ( val , dict ) : continue <EOL> if var not in dataspecs : continue <EOL> if isinstance ( val , string_types ) : continue <EOL> if ( isinstance ( dataspecs [ var ] . descriptor , ColorSpec ) and ColorSpec . is_color_tuple ( val ) ) : <EOL> continue <EOL> if isinstance ( val , np . ndarray ) and val . ndim != <NUM_LIT:1> : <EOL> raise RuntimeError ( "<STR_LIT>" % var ) <EOL> source . add ( val , name = var ) <EOL> kwargs [ var ] = var <EOL> def _make_glyph ( glyphclass , kws , extra ) : <EOL> if extra is None : return None <EOL> kws = kws . copy ( ) <EOL> kws . update ( extra ) <EOL> return glyphclass ( ** kws ) <EOL> def _update_legend ( plot , legend_name , glyph_renderer ) : <EOL> legends = plot . select ( type = Legend ) <EOL> if not legends : <EOL> legend = Legend ( plot = plot ) <EOL> plot . renderers = plot . renderers + [ legend ] <EOL> elif len ( legends ) == <NUM_LIT:1> : <EOL> legend = legends [ <NUM_LIT:0> ] <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % plot ) <EOL> specs = OrderedDict ( legend . legends ) <EOL> specs . setdefault ( legend_name , [ ] ) . append ( glyph_renderer ) <EOL> legend . legends = list ( specs . items ( ) ) <EOL> def _get_range ( range_input ) : <EOL> if range_input is None : <EOL> return DataRange1d ( ) <EOL> if isinstance ( range_input , Range ) : <EOL> return range_input <EOL> if isinstance ( range_input , Sequence ) : <EOL> if all ( isinstance ( x , string_types ) for x in range_input ) : <EOL> return FactorRange ( factors = range_input ) <EOL> if len ( range_input ) == <NUM_LIT:2> : <EOL> try : <EOL> return Range1d ( start = range_input [ <NUM_LIT:0> ] , end = range_input [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> pass <EOL> raise ValueError ( "<STR_LIT>" % str ( range_input ) ) <EOL> def _get_axis_class ( axis_type , range_input ) : <EOL> if axis_type is None : <EOL> return None <EOL> elif axis_type == "<STR_LIT>" : <EOL> return LinearAxis <EOL> elif axis_type == "<STR_LIT>" : <EOL> return LogAxis <EOL> elif axis_type == "<STR_LIT>" : <EOL> return DatetimeAxis <EOL> elif axis_type == "<STR_LIT>" : <EOL> if isinstance ( range_input , FactorRange ) : <EOL> return CategoricalAxis <EOL> elif isinstance ( range_input , Range1d ) : <EOL> try : <EOL> Datetime . validate ( Datetime ( ) , range_input . start ) <EOL> return DatetimeAxis <EOL> except ValueError : <EOL> pass <EOL> return LinearAxis <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % axis_type ) <EOL> def _get_num_minor_ticks ( axis_class , num_minor_ticks ) : <EOL> if isinstance ( num_minor_ticks , int ) : <EOL> if num_minor_ticks <= <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return num_minor_ticks <EOL> if num_minor_ticks is None : <EOL> return <NUM_LIT:0> <EOL> if num_minor_ticks == '<STR_LIT>' : <EOL> if axis_class is LogAxis : <EOL> return <NUM_LIT:10> <EOL> return <NUM_LIT:5> <EOL> _known_tools = { <EOL> "<STR_LIT>" : lambda : PanTool ( dimensions = [ "<STR_LIT:width>" , "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" : lambda : PanTool ( dimensions = [ "<STR_LIT:width>" ] ) , <EOL> "<STR_LIT>" : lambda : PanTool ( dimensions = [ "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" : lambda : WheelZoomTool ( dimensions = [ "<STR_LIT:width>" , "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" : lambda : WheelZoomTool ( dimensions = [ "<STR_LIT:width>" ] ) , <EOL> "<STR_LIT>" : lambda : WheelZoomTool ( dimensions = [ "<STR_LIT>" ] ) , <EOL> "<STR_LIT>" : lambda : PreviewSaveTool ( ) , <EOL> "<STR_LIT>" : lambda : ResizeTool ( ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : lambda : TapTool ( ) , <EOL> "<STR_LIT>" : lambda : CrosshairTool ( ) , <EOL> "<STR_LIT>" : lambda : BoxSelectTool ( ) , <EOL> "<STR_LIT>" : lambda : BoxSelectTool ( dimensions = [ '<STR_LIT:width>' ] ) , <EOL> "<STR_LIT>" : lambda : BoxSelectTool ( dimensions = [ '<STR_LIT>' ] ) , <EOL> "<STR_LIT>" : lambda : PolySelectTool ( ) , <EOL> "<STR_LIT>" : lambda : LassoSelectTool ( ) , <EOL> "<STR_LIT>" : lambda : BoxZoomTool ( dimensions = [ '<STR_LIT:width>' , '<STR_LIT>' ] ) , <EOL> "<STR_LIT>" : lambda : BoxZoomTool ( dimensions = [ '<STR_LIT:width>' ] ) , <EOL> "<STR_LIT>" : lambda : BoxZoomTool ( dimensions = [ '<STR_LIT>' ] ) , <EOL> "<STR_LIT>" : lambda : HoverTool ( tooltips = [ <EOL> ( "<STR_LIT:index>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ] ) , <EOL> "<STR_LIT>" : lambda : PreviewSaveTool ( ) , <EOL> "<STR_LIT>" : lambda : UndoTool ( ) , <EOL> "<STR_LIT>" : lambda : RedoTool ( ) , <EOL> "<STR_LIT>" : lambda : ResetTool ( ) , <EOL> "<STR_LIT>" : lambda : HelpTool ( ) , <EOL> } <EOL> def _tool_from_string ( name ) : <EOL> """<STR_LIT>""" <EOL> known_tools = sorted ( _known_tools . keys ( ) ) <EOL> if name in known_tools : <EOL> tool_fn = _known_tools [ name ] <EOL> if isinstance ( tool_fn , string_types ) : <EOL> tool_fn = _known_tools [ tool_fn ] <EOL> return tool_fn ( ) <EOL> else : <EOL> matches , text = difflib . get_close_matches ( name . lower ( ) , known_tools ) , "<STR_LIT>" <EOL> if not matches : <EOL> matches , text = known_tools , "<STR_LIT>" <EOL> raise ValueError ( "<STR_LIT>" % ( name , text , nice_join ( matches ) ) ) <EOL> def _process_axis_and_grid ( plot , axis_type , axis_location , minor_ticks , axis_label , rng , dim ) : <EOL> axiscls = _get_axis_class ( axis_type , rng ) <EOL> if axiscls : <EOL> if axiscls is LogAxis : <EOL> if dim == <NUM_LIT:0> : <EOL> plot . x_mapper_type = '<STR_LIT>' <EOL> elif dim == <NUM_LIT:1> : <EOL> plot . y_mapper_type = '<STR_LIT>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % dim ) <EOL> axis = axiscls ( plot = plot if axis_location else None ) <EOL> if isinstance ( axis . ticker , ContinuousTicker ) : <EOL> axis . ticker . num_minor_ticks = _get_num_minor_ticks ( axiscls , minor_ticks ) <EOL> axis_label = axis_label <EOL> if axis_label : <EOL> axis . axis_label = axis_label <EOL> grid = Grid ( plot = plot , dimension = dim , ticker = axis . ticker ) ; grid <EOL> if axis_location is not None : <EOL> getattr ( plot , axis_location ) . append ( axis ) <EOL> def _process_tools_arg ( plot , tools ) : <EOL> """<STR_LIT>""" <EOL> tool_objs = [ ] <EOL> temp_tool_str = "<STR_LIT>" <EOL> repeated_tools = [ ] <EOL> if isinstance ( tools , ( list , tuple ) ) : <EOL> for tool in tools : <EOL> if isinstance ( tool , Tool ) : <EOL> tool_objs . append ( tool ) <EOL> elif isinstance ( tool , string_types ) : <EOL> temp_tool_str += tool + '<STR_LIT:U+002C>' <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> tools = temp_tool_str <EOL> for tool in re . split ( r"<STR_LIT>" , tools . strip ( ) ) : <EOL> if tool == "<STR_LIT>" : <EOL> continue <EOL> tool_obj = _tool_from_string ( tool ) <EOL> tool_objs . append ( tool_obj ) <EOL> for typename , group in itertools . groupby ( <EOL> sorted ( [ tool . __class__ . __name__ for tool in tool_objs ] ) ) : <EOL> if len ( list ( group ) ) > <NUM_LIT:1> : <EOL> repeated_tools . append ( typename ) <EOL> if repeated_tools : <EOL> warnings . warn ( "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( repeated_tools ) ) <EOL> return tool_objs <EOL> class _list_attr_splat ( list ) : <EOL> def __setattr__ ( self , attr , value ) : <EOL> for x in self : <EOL> setattr ( x , attr , value ) <EOL> def __dir__ ( self ) : <EOL> if len ( set ( type ( x ) for x in self ) ) == <NUM_LIT:1> : <EOL> return dir ( self [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return dir ( self ) <EOL> _arg_template = "<STR_LIT>" <EOL> _doc_template = """<STR_LIT>""" <EOL> def _glyph_function ( glyphclass , extra_docs = None ) : <EOL> def func ( self , * args , ** kwargs ) : <EOL> legend_name = kwargs . pop ( "<STR_LIT>" , None ) <EOL> renderer_kws = _pop_renderer_args ( kwargs ) <EOL> source = renderer_kws [ '<STR_LIT>' ] <EOL> attributes = dict ( zip ( glyphclass . _args , args ) ) <EOL> kwargs . update ( attributes ) <EOL> glyph_ca = _pop_colors_and_alpha ( glyphclass , kwargs ) <EOL> _process_sequence_literals ( glyphclass , kwargs , source ) <EOL> _process_sequence_literals ( glyphclass , glyph_ca , source ) <EOL> nsglyph_ca = _pop_colors_and_alpha ( glyphclass , kwargs , prefix = '<STR_LIT>' , default_alpha = <NUM_LIT:0.1> ) <EOL> if any ( x . startswith ( '<STR_LIT>' ) for x in kwargs ) : <EOL> sglyph_ca = _pop_colors_and_alpha ( glyphclass , kwargs , prefix = '<STR_LIT>' ) <EOL> else : <EOL> sglyph_ca = None <EOL> if any ( x . startswith ( '<STR_LIT>' ) for x in kwargs ) : <EOL> hglyph_ca = _pop_colors_and_alpha ( glyphclass , kwargs , prefix = '<STR_LIT>' ) <EOL> else : <EOL> hglyph_ca = None <EOL> glyph = _make_glyph ( glyphclass , kwargs , glyph_ca ) <EOL> nsglyph = _make_glyph ( glyphclass , kwargs , nsglyph_ca ) <EOL> hglyph = _make_glyph ( glyphclass , kwargs , hglyph_ca ) <EOL> sglyph = _make_glyph ( glyphclass , kwargs , sglyph_ca ) <EOL> glyph_renderer = GlyphRenderer ( glyph = glyph , <EOL> nonselection_glyph = nsglyph , <EOL> selection_glyph = sglyph , <EOL> hover_glyph = hglyph , <EOL> ** renderer_kws ) <EOL> if legend_name : <EOL> _update_legend ( self , legend_name , glyph_renderer ) <EOL> for tool in self . select ( type = BoxSelectTool ) : <EOL> tool . renderers = tool . renderers + [ glyph_renderer ] <EOL> self . renderers = self . renderers + [ glyph_renderer ] <EOL> return glyph_renderer <EOL> func . __name__ = glyphclass . __view_model__ <EOL> arglines = [ ] <EOL> for arg in glyphclass . _args : <EOL> spec = getattr ( glyphclass , arg ) <EOL> desc = "<STR_LIT:U+0020>" . join ( x . strip ( ) for x in spec . __doc__ . strip ( ) . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:\n>' ) ) <EOL> arglines . append ( _arg_template % ( arg , spec . __class__ . __name__ , desc , spec . class_default ( glyphclass ) ) ) <EOL> kwlines = [ ] <EOL> kws = glyphclass . properties ( ) - set ( glyphclass . _args ) <EOL> for kw in sorted ( kws ) : <EOL> if kw == "<STR_LIT>" : continue <EOL> spec = getattr ( glyphclass , kw ) <EOL> if spec . __doc__ : <EOL> typ = spec . __class__ . __name__ <EOL> desc = "<STR_LIT:U+0020>" . join ( x . strip ( ) for x in spec . __doc__ . strip ( ) . split ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:\n>' ) ) <EOL> else : <EOL> typ = str ( spec ) <EOL> desc = "<STR_LIT>" <EOL> kwlines . append ( _arg_template % ( kw , typ , desc , spec . class_default ( glyphclass ) ) ) <EOL> func . __doc__ = _doc_template % ( glyphclass . __name__ , "<STR_LIT:\n>" . join ( arglines ) , "<STR_LIT:\n>" . join ( kwlines ) ) <EOL> if extra_docs : <EOL> func . __doc__ += extra_docs <EOL> return func </s>
