<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , print_function <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> import collections <EOL> from copy import copy <EOL> import datetime <EOL> import dateutil . parser <EOL> import difflib <EOL> from importlib import import_module <EOL> import inspect <EOL> import numbers <EOL> import re <EOL> import types <EOL> from warnings import warn <EOL> from six import string_types , iteritems <EOL> from . . util . dependencies import import_optional <EOL> from . . util . future import with_metaclass <EOL> from . . util . string import nice_join <EOL> from . property_containers import PropertyValueList , PropertyValueDict , PropertyValueContainer <EOL> from . import enums <EOL> pd = import_optional ( '<STR_LIT>' ) <EOL> def field ( name ) : <EOL> '''<STR_LIT>''' <EOL> return dict ( field = name ) <EOL> def value ( val ) : <EOL> '''<STR_LIT>''' <EOL> return dict ( value = val ) <EOL> bokeh_bool_types = ( bool , ) <EOL> try : <EOL> import numpy as np <EOL> bokeh_bool_types += ( np . bool8 , ) <EOL> except ImportError : <EOL> pass <EOL> bokeh_integer_types = ( numbers . Integral , ) <EOL> class _NotSet ( object ) : <EOL> pass <EOL> class DeserializationError ( Exception ) : <EOL> pass <EOL> class PropertyFactory ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def autocreate ( cls ) : <EOL> """<STR_LIT>""" <EOL> return cls ( ) <EOL> def make_properties ( self , base_name ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> class PropertyDescriptor ( PropertyFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = None , help = None , serialized = True ) : <EOL> """<STR_LIT>""" <EOL> self . _serialized = serialized <EOL> self . _default = default <EOL> self . __doc__ = help <EOL> self . alternatives = [ ] <EOL> self . validate ( self . _raw_default ( ) ) <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ <EOL> def make_properties ( self , base_name ) : <EOL> return [ BasicProperty ( descriptor = self , name = base_name ) ] <EOL> def _has_stable_default ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . _default , types . FunctionType ) : <EOL> return False <EOL> else : <EOL> return True <EOL> @ classmethod <EOL> def _copy_default ( cls , default ) : <EOL> if not isinstance ( default , types . FunctionType ) : <EOL> return copy ( default ) <EOL> else : <EOL> return default ( ) <EOL> def _raw_default ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _copy_default ( self . _default ) <EOL> def themed_default ( self , cls , name , theme_overrides ) : <EOL> """<STR_LIT>""" <EOL> overrides = theme_overrides <EOL> if overrides is None or name not in overrides : <EOL> overrides = cls . _overridden_defaults ( ) <EOL> if name in overrides : <EOL> default = self . _copy_default ( overrides [ name ] ) <EOL> else : <EOL> default = self . _raw_default ( ) <EOL> return self . prepare_value ( cls , name , default ) <EOL> @ property <EOL> def serialized ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _serialized <EOL> def matches ( self , new , old ) : <EOL> try : <EOL> if new is None or old is None : <EOL> return new is old <EOL> else : <EOL> return new == old <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except Exception : <EOL> pass <EOL> return False <EOL> def from_json ( self , json , models = None ) : <EOL> """<STR_LIT>""" <EOL> return json <EOL> def transform ( self , value ) : <EOL> """<STR_LIT>""" <EOL> return value <EOL> def validate ( self , value ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def is_valid ( self , value ) : <EOL> try : <EOL> self . validate ( value ) <EOL> except ValueError : <EOL> return False <EOL> else : <EOL> return True <EOL> @ classmethod <EOL> def _wrap_container ( cls , value ) : <EOL> if isinstance ( value , list ) : <EOL> if isinstance ( value , PropertyValueList ) : <EOL> return value <EOL> else : <EOL> return PropertyValueList ( value ) <EOL> elif isinstance ( value , dict ) : <EOL> if isinstance ( value , PropertyValueDict ) : <EOL> return value <EOL> else : <EOL> return PropertyValueDict ( value ) <EOL> else : <EOL> return value <EOL> def prepare_value ( self , cls , name , value ) : <EOL> try : <EOL> self . validate ( value ) <EOL> except ValueError as e : <EOL> for tp , converter in self . alternatives : <EOL> if tp . is_valid ( value ) : <EOL> value = converter ( value ) <EOL> break <EOL> else : <EOL> raise e <EOL> else : <EOL> value = self . transform ( value ) <EOL> return self . _wrap_container ( value ) <EOL> @ property <EOL> def has_ref ( self ) : <EOL> return False <EOL> def accepts ( self , tp , converter ) : <EOL> tp = ParameterizedPropertyDescriptor . _validate_type_param ( tp ) <EOL> self . alternatives . append ( ( tp , converter ) ) <EOL> return self <EOL> def __or__ ( self , other ) : <EOL> return Either ( self , other ) <EOL> class Property ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name ) <EOL> def __get__ ( self , obj , owner = None ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def __set__ ( self , obj , value ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def __delete__ ( self , obj ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def class_default ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def serializable_value ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return self . __get__ ( obj ) <EOL> def set_from_json ( self , obj , json , models ) : <EOL> """<STR_LIT>""" <EOL> return self . __set__ ( obj , json ) <EOL> @ property <EOL> def serialized ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def has_ref ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def trigger_if_changed ( self , obj , old ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> class BasicProperty ( Property ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , descriptor , name ) : <EOL> super ( BasicProperty , self ) . __init__ ( name ) <EOL> self . descriptor = descriptor <EOL> self . __doc__ = self . descriptor . __doc__ <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . name , self . descriptor ) <EOL> def class_default ( self , cls ) : <EOL> """<STR_LIT>""" <EOL> return self . descriptor . themed_default ( cls , self . name , None ) <EOL> def instance_default ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> return self . descriptor . themed_default ( obj . __class__ , self . name , obj . themed_values ( ) ) <EOL> @ property <EOL> def serialized ( self ) : <EOL> return self . descriptor . serialized <EOL> def set_from_json ( self , obj , json , models = None ) : <EOL> """<STR_LIT>""" <EOL> return super ( BasicProperty , self ) . set_from_json ( obj , <EOL> self . descriptor . from_json ( json , models ) , <EOL> models ) <EOL> @ property <EOL> def has_ref ( self ) : <EOL> return self . descriptor . has_ref <EOL> def _get ( self , obj ) : <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . name , obj . __class__ . __name__ ) ) <EOL> if self . name not in obj . _property_values : <EOL> return self . _get_default ( obj ) <EOL> else : <EOL> return obj . _property_values [ self . name ] <EOL> def __get__ ( self , obj , owner = None ) : <EOL> if obj is not None : <EOL> return self . _get ( obj ) <EOL> elif owner is not None : <EOL> return self <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def _trigger ( self , obj , old , value , hint = None ) : <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> obj . trigger ( self . name , old , value , hint ) <EOL> def _get_default ( self , obj ) : <EOL> if self . name in obj . _property_values : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> default = self . instance_default ( obj ) <EOL> if not self . descriptor . _has_stable_default ( ) : <EOL> if isinstance ( default , PropertyValueContainer ) : <EOL> default . _unmodified_default_value = True <EOL> default . _register_owner ( obj , self ) <EOL> obj . _property_values [ self . name ] = default <EOL> return default <EOL> def _real_set ( self , obj , old , value , hint = None ) : <EOL> unchanged = self . descriptor . matches ( value , old ) and ( hint is None ) <EOL> if unchanged : <EOL> return <EOL> was_set = self . name in obj . _property_values <EOL> if was_set : <EOL> old_attr_value = obj . _property_values [ self . name ] <EOL> else : <EOL> old_attr_value = old <EOL> if old_attr_value is not value : <EOL> if isinstance ( old_attr_value , PropertyValueContainer ) : <EOL> old_attr_value . _unregister_owner ( obj , self ) <EOL> if isinstance ( value , PropertyValueContainer ) : <EOL> value . _register_owner ( obj , self ) <EOL> obj . _property_values [ self . name ] = value <EOL> self . _trigger ( obj , old , value , hint ) <EOL> def __set__ ( self , obj , value ) : <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( self . name , obj . __class__ . __name__ ) ) <EOL> value = self . descriptor . prepare_value ( obj . __class__ , self . name , value ) <EOL> old = self . __get__ ( obj ) <EOL> self . _real_set ( obj , old , value ) <EOL> def _notify_mutated ( self , obj , old , hint = None ) : <EOL> value = self . __get__ ( obj ) <EOL> value = self . descriptor . prepare_value ( obj . __class__ , self . name , value ) <EOL> self . _real_set ( obj , old , value , hint ) <EOL> def __delete__ ( self , obj ) : <EOL> if self . name in obj . _property_values : <EOL> del obj . _property_values [ self . name ] <EOL> def trigger_if_changed ( self , obj , old ) : <EOL> new_value = self . __get__ ( obj ) <EOL> if not self . descriptor . matches ( old , new_value ) : <EOL> self . _trigger ( obj , old , new_value ) <EOL> class Include ( PropertyFactory ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , delegate , help = "<STR_LIT>" , use_prefix = True ) : <EOL> if not ( isinstance ( delegate , type ) and issubclass ( delegate , HasProps ) ) : <EOL> raise ValueError ( "<STR_LIT>" % delegate ) <EOL> self . delegate = delegate <EOL> self . help = help <EOL> self . use_prefix = use_prefix <EOL> def make_properties ( self , base_name ) : <EOL> props = [ ] <EOL> delegate = self . delegate <EOL> if self . use_prefix : <EOL> prefix = re . sub ( "<STR_LIT>" , "<STR_LIT>" , base_name ) + "<STR_LIT:_>" <EOL> else : <EOL> prefix = "<STR_LIT>" <EOL> for subpropname in delegate . properties ( with_bases = False ) : <EOL> fullpropname = prefix + subpropname <EOL> subprop = delegate . lookup ( subpropname ) <EOL> if isinstance ( subprop , BasicProperty ) : <EOL> descriptor = copy ( subprop . descriptor ) <EOL> if "<STR_LIT:%s>" in self . help : <EOL> doc = self . help % subpropname . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> else : <EOL> doc = self . help <EOL> descriptor . __doc__ = doc <EOL> props += descriptor . make_properties ( fullpropname ) <EOL> return props <EOL> class Override ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> if len ( kwargs ) == <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . default_overridden = '<STR_LIT:default>' in kwargs <EOL> if self . default_overridden : <EOL> self . default = kwargs . pop ( '<STR_LIT:default>' ) <EOL> if len ( kwargs ) > <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % ( kwargs ) ) <EOL> _EXAMPLE_TEMPLATE = """<STR_LIT>""" <EOL> class MetaHasProps ( type ) : <EOL> def __new__ ( meta_cls , class_name , bases , class_dict ) : <EOL> names = set ( ) <EOL> names_with_refs = set ( ) <EOL> container_names = set ( ) <EOL> overridden_defaults = { } <EOL> for name , prop in class_dict . items ( ) : <EOL> if not isinstance ( prop , Override ) : <EOL> continue <EOL> if prop . default_overridden : <EOL> overridden_defaults [ name ] = prop . default <EOL> for name , default in overridden_defaults . items ( ) : <EOL> del class_dict [ name ] <EOL> generators = dict ( ) <EOL> for name , generator in class_dict . items ( ) : <EOL> if isinstance ( generator , PropertyFactory ) : <EOL> generators [ name ] = generator <EOL> elif isinstance ( generator , type ) and issubclass ( generator , PropertyFactory ) : <EOL> generators [ name ] = generator . autocreate ( ) <EOL> dataspecs = { } <EOL> new_class_attrs = { } <EOL> def add_prop ( prop ) : <EOL> name = prop . name <EOL> if name in new_class_attrs : <EOL> raise RuntimeError ( "<STR_LIT>" % ( class_name , name ) ) <EOL> new_class_attrs [ name ] = prop <EOL> names . add ( name ) <EOL> if prop . has_ref : <EOL> names_with_refs . add ( name ) <EOL> if isinstance ( prop , BasicProperty ) : <EOL> if isinstance ( prop . descriptor , ContainerProperty ) : <EOL> container_names . add ( name ) <EOL> if isinstance ( prop . descriptor , DataSpec ) : <EOL> dataspecs [ name ] = prop <EOL> for name , generator in generators . items ( ) : <EOL> props = generator . make_properties ( name ) <EOL> replaced_self = False <EOL> for prop in props : <EOL> if prop . name in generators : <EOL> if generators [ prop . name ] is generator : <EOL> replaced_self = True <EOL> add_prop ( prop ) <EOL> else : <EOL> pass <EOL> else : <EOL> add_prop ( prop ) <EOL> if not replaced_self : <EOL> del class_dict [ name ] <EOL> class_dict . update ( new_class_attrs ) <EOL> class_dict [ "<STR_LIT>" ] = names <EOL> class_dict [ "<STR_LIT>" ] = names_with_refs <EOL> class_dict [ "<STR_LIT>" ] = container_names <EOL> if len ( overridden_defaults ) > <NUM_LIT:0> : <EOL> class_dict [ "<STR_LIT>" ] = overridden_defaults <EOL> if dataspecs : <EOL> class_dict [ "<STR_LIT>" ] = dataspecs <EOL> if "<STR_LIT>" in class_dict : <EOL> path = class_dict [ "<STR_LIT>" ] <EOL> class_dict [ "<STR_LIT>" ] += _EXAMPLE_TEMPLATE % dict ( path = path ) <EOL> return super ( MetaHasProps , meta_cls ) . __new__ ( meta_cls , class_name , bases , class_dict ) <EOL> def __init__ ( cls , class_name , bases , nmspc ) : <EOL> if class_name == '<STR_LIT>' : <EOL> return <EOL> cls_attrs = cls . __dict__ . keys ( ) <EOL> for attr in cls_attrs : <EOL> for base in bases : <EOL> if issubclass ( base , HasProps ) and attr in base . properties ( ) : <EOL> warn ( ( '<STR_LIT>' + '<STR_LIT>' + '<STR_LIT>' + '<STR_LIT>' ) % <EOL> ( attr , base . __name__ , attr , class_name , <EOL> base . __name__ , attr , <EOL> class_name , attr , <EOL> base . __name__ , attr ) , <EOL> RuntimeWarning , stacklevel = <NUM_LIT:2> ) <EOL> if "<STR_LIT>" in cls . __dict__ : <EOL> our_props = cls . properties ( ) <EOL> for key in cls . __dict__ [ "<STR_LIT>" ] . keys ( ) : <EOL> if key not in our_props : <EOL> warn ( ( '<STR_LIT>' ) % ( key , class_name ) , <EOL> RuntimeWarning , stacklevel = <NUM_LIT:2> ) <EOL> def accumulate_from_superclasses ( cls , propname ) : <EOL> cachename = "<STR_LIT>" + propname <EOL> if cachename not in cls . __dict__ : <EOL> s = set ( ) <EOL> for c in inspect . getmro ( cls ) : <EOL> if issubclass ( c , HasProps ) and hasattr ( c , propname ) : <EOL> base = getattr ( c , propname ) <EOL> s . update ( base ) <EOL> setattr ( cls , cachename , s ) <EOL> return cls . __dict__ [ cachename ] <EOL> def accumulate_dict_from_superclasses ( cls , propname ) : <EOL> cachename = "<STR_LIT>" + propname <EOL> if cachename not in cls . __dict__ : <EOL> d = dict ( ) <EOL> for c in inspect . getmro ( cls ) : <EOL> if issubclass ( c , HasProps ) and hasattr ( c , propname ) : <EOL> base = getattr ( c , propname ) <EOL> for k , v in base . items ( ) : <EOL> if k not in d : <EOL> d [ k ] = v <EOL> setattr ( cls , cachename , d ) <EOL> return cls . __dict__ [ cachename ] <EOL> def abstract ( cls ) : <EOL> """<STR_LIT>""" <EOL> if not issubclass ( cls , HasProps ) : <EOL> raise TypeError ( "<STR_LIT>" % cls . __name__ ) <EOL> return cls <EOL> class HasProps ( with_metaclass ( MetaHasProps , object ) ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , ** properties ) : <EOL> super ( HasProps , self ) . __init__ ( ) <EOL> self . _property_values = dict ( ) <EOL> for name , value in properties . items ( ) : <EOL> setattr ( self , name , value ) <EOL> def __setattr__ ( self , name , value ) : <EOL> if name . startswith ( "<STR_LIT:_>" ) : <EOL> super ( HasProps , self ) . __setattr__ ( name , value ) <EOL> return <EOL> props = sorted ( self . properties ( ) ) <EOL> deprecated = getattr ( self , '<STR_LIT>' , [ ] ) <EOL> if name in props or name in deprecated : <EOL> super ( HasProps , self ) . __setattr__ ( name , value ) <EOL> else : <EOL> matches , text = difflib . get_close_matches ( name . lower ( ) , props ) , "<STR_LIT>" <EOL> if not matches : <EOL> matches , text = props , "<STR_LIT>" <EOL> raise AttributeError ( "<STR_LIT>" % <EOL> ( name , self . __class__ . __name__ , text , nice_join ( matches ) ) ) <EOL> def set_from_json ( self , name , json , models = None ) : <EOL> """<STR_LIT>""" <EOL> if name in self . properties ( ) : <EOL> prop = self . lookup ( name ) <EOL> prop . set_from_json ( self , json , models ) <EOL> else : <EOL> logger . warn ( "<STR_LIT>" , name , self ) <EOL> def update ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for k , v in kwargs . items ( ) : <EOL> setattr ( self , k , v ) <EOL> def update_from_json ( self , json_attributes , models = None ) : <EOL> """<STR_LIT>""" <EOL> for k , v in json_attributes . items ( ) : <EOL> self . set_from_json ( k , v , models ) <EOL> def _clone ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( ** self . _property_values ) <EOL> @ classmethod <EOL> def lookup ( cls , name ) : <EOL> return getattr ( cls , name ) <EOL> @ classmethod <EOL> def properties_with_refs ( cls ) : <EOL> """<STR_LIT>""" <EOL> return accumulate_from_superclasses ( cls , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def properties_containers ( cls ) : <EOL> """<STR_LIT>""" <EOL> return accumulate_from_superclasses ( cls , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def properties ( cls , with_bases = True ) : <EOL> """<STR_LIT>""" <EOL> if with_bases : <EOL> return accumulate_from_superclasses ( cls , "<STR_LIT>" ) <EOL> else : <EOL> return set ( cls . __properties__ ) <EOL> @ classmethod <EOL> def _overridden_defaults ( cls ) : <EOL> """<STR_LIT>""" <EOL> return accumulate_dict_from_superclasses ( cls , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def dataspecs ( cls ) : <EOL> """<STR_LIT>""" <EOL> return set ( cls . dataspecs_with_props ( ) . keys ( ) ) <EOL> @ classmethod <EOL> def dataspecs_with_props ( cls ) : <EOL> """<STR_LIT>""" <EOL> return accumulate_dict_from_superclasses ( cls , "<STR_LIT>" ) <EOL> def properties_with_values ( self , include_defaults = True ) : <EOL> '''<STR_LIT>''' <EOL> result = dict ( ) <EOL> if include_defaults : <EOL> keys = self . properties ( ) <EOL> else : <EOL> keys = set ( self . _property_values . keys ( ) ) <EOL> if self . themed_values ( ) : <EOL> keys |= set ( self . themed_values ( ) . keys ( ) ) <EOL> for key in keys : <EOL> prop = self . lookup ( key ) <EOL> if not prop . serialized : <EOL> continue <EOL> value = prop . serializable_value ( self ) <EOL> if not include_defaults : <EOL> if isinstance ( value , PropertyValueContainer ) and value . _unmodified_default_value : <EOL> continue <EOL> result [ key ] = value <EOL> return result <EOL> def set ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> for kw in kwargs : <EOL> setattr ( self , kw , kwargs [ kw ] ) <EOL> def themed_values ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return getattr ( self , '<STR_LIT>' ) <EOL> else : <EOL> return None <EOL> def apply_theme ( self , property_values ) : <EOL> """<STR_LIT>""" <EOL> old_dict = None <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> old_dict = getattr ( self , '<STR_LIT>' ) <EOL> if old_dict is property_values : <EOL> return <EOL> removed = set ( ) <EOL> if old_dict is not None : <EOL> removed . update ( set ( old_dict . keys ( ) ) ) <EOL> added = set ( property_values . keys ( ) ) <EOL> old_values = dict ( ) <EOL> for k in added . union ( removed ) : <EOL> old_values [ k ] = getattr ( self , k ) <EOL> if len ( property_values ) > <NUM_LIT:0> : <EOL> setattr ( self , '<STR_LIT>' , property_values ) <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> delattr ( self , '<STR_LIT>' ) <EOL> for k , v in old_values . items ( ) : <EOL> prop = self . lookup ( k ) <EOL> prop . trigger_if_changed ( self , v ) <EOL> def unapply_theme ( self ) : <EOL> self . apply_theme ( property_values = dict ( ) ) <EOL> def pprint_props ( self , indent = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for key , value in self . properties_with_values ( ) . items ( ) : <EOL> print ( "<STR_LIT>" % ( "<STR_LIT:U+0020>" * indent , key , value ) ) <EOL> class PrimitiveProperty ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> _underlying_type = None <EOL> def validate ( self , value ) : <EOL> super ( PrimitiveProperty , self ) . validate ( value ) <EOL> if not ( value is None or isinstance ( value , self . _underlying_type ) ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( nice_join ( [ cls . __name__ for cls in self . _underlying_type ] ) , value , type ( value ) . __name__ ) ) <EOL> def from_json ( self , json , models = None ) : <EOL> if json is None or isinstance ( json , self . _underlying_type ) : <EOL> return json <EOL> else : <EOL> expected = nice_join ( [ cls . __name__ for cls in self . _underlying_type ] ) <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , expected , json ) ) <EOL> class Bool ( PrimitiveProperty ) : <EOL> """<STR_LIT>""" <EOL> _underlying_type = bokeh_bool_types <EOL> class Int ( PrimitiveProperty ) : <EOL> """<STR_LIT>""" <EOL> _underlying_type = bokeh_integer_types <EOL> class Float ( PrimitiveProperty ) : <EOL> """<STR_LIT>""" <EOL> _underlying_type = ( numbers . Real , ) <EOL> class Complex ( PrimitiveProperty ) : <EOL> """<STR_LIT>""" <EOL> _underlying_type = ( numbers . Complex , ) <EOL> class String ( PrimitiveProperty ) : <EOL> """<STR_LIT>""" <EOL> _underlying_type = string_types <EOL> class Regex ( String ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , regex , default = None , help = None ) : <EOL> self . regex = re . compile ( regex ) <EOL> super ( Regex , self ) . __init__ ( default = default , help = help ) <EOL> def validate ( self , value ) : <EOL> super ( Regex , self ) . validate ( value ) <EOL> if not ( value is None or self . regex . match ( value ) is not None ) : <EOL> raise ValueError ( "<STR_LIT>" % ( self . regex . pattern , value ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . regex . pattern ) <EOL> class JSON ( String ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> super ( JSON , self ) . validate ( value ) <EOL> if value is None : return <EOL> try : <EOL> import json <EOL> json . loads ( value ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> class ParameterizedPropertyDescriptor ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def _validate_type_param ( type_param ) : <EOL> if isinstance ( type_param , type ) : <EOL> if issubclass ( type_param , PropertyDescriptor ) : <EOL> return type_param ( ) <EOL> else : <EOL> type_param = type_param . __name__ <EOL> elif isinstance ( type_param , PropertyDescriptor ) : <EOL> return type_param <EOL> raise ValueError ( "<STR_LIT>" % type_param ) <EOL> @ property <EOL> def type_params ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> @ property <EOL> def has_ref ( self ) : <EOL> return any ( type_param . has_ref for type_param in self . type_params ) <EOL> class ContainerProperty ( ParameterizedPropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def _has_stable_default ( self ) : <EOL> return False <EOL> class Seq ( ContainerProperty ) : <EOL> """<STR_LIT>""" <EOL> def _is_seq ( self , value ) : <EOL> return isinstance ( value , collections . Container ) and not isinstance ( value , collections . Mapping ) <EOL> def _new_instance ( self , value ) : <EOL> return value <EOL> def __init__ ( self , item_type , default = None , help = None ) : <EOL> self . item_type = self . _validate_type_param ( item_type ) <EOL> super ( Seq , self ) . __init__ ( default = default , help = help ) <EOL> @ property <EOL> def type_params ( self ) : <EOL> return [ self . item_type ] <EOL> def validate ( self , value ) : <EOL> super ( Seq , self ) . validate ( value ) <EOL> if value is not None : <EOL> if not ( self . _is_seq ( value ) and all ( self . item_type . is_valid ( item ) for item in value ) ) : <EOL> if self . _is_seq ( value ) : <EOL> invalid = [ ] <EOL> for item in value : <EOL> if not self . item_type . is_valid ( item ) : <EOL> invalid . append ( item ) <EOL> raise ValueError ( "<STR_LIT>" % ( self , invalid ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( self , value ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . item_type ) <EOL> def from_json ( self , json , models = None ) : <EOL> if json is None : <EOL> return None <EOL> elif isinstance ( json , list ) : <EOL> return self . _new_instance ( [ self . item_type . from_json ( item , models ) for item in json ] ) <EOL> else : <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , json ) ) <EOL> class List ( Seq ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , item_type , default = [ ] , help = None ) : <EOL> super ( List , self ) . __init__ ( item_type , default = default , help = help ) <EOL> def _is_seq ( self , value ) : <EOL> return isinstance ( value , list ) <EOL> class Array ( Seq ) : <EOL> """<STR_LIT>""" <EOL> def _is_seq ( self , value ) : <EOL> import numpy as np <EOL> return isinstance ( value , np . ndarray ) <EOL> def _new_instance ( self , value ) : <EOL> import numpy as np <EOL> return np . array ( value ) <EOL> class Dict ( ContainerProperty ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , keys_type , values_type , default = { } , help = None ) : <EOL> self . keys_type = self . _validate_type_param ( keys_type ) <EOL> self . values_type = self . _validate_type_param ( values_type ) <EOL> super ( Dict , self ) . __init__ ( default = default , help = help ) <EOL> @ property <EOL> def type_params ( self ) : <EOL> return [ self . keys_type , self . values_type ] <EOL> def validate ( self , value ) : <EOL> super ( Dict , self ) . validate ( value ) <EOL> if value is not None : <EOL> if not ( isinstance ( value , dict ) and all ( self . keys_type . is_valid ( key ) and self . values_type . is_valid ( val ) for key , val in iteritems ( value ) ) ) : <EOL> raise ValueError ( "<STR_LIT>" % ( self , value ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . keys_type , self . values_type ) <EOL> def from_json ( self , json , models = None ) : <EOL> if json is None : <EOL> return None <EOL> elif isinstance ( json , dict ) : <EOL> return { self . keys_type . from_json ( key , models ) : self . values_type . from_json ( value , models ) for key , value in iteritems ( json ) } <EOL> else : <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , json ) ) <EOL> class Tuple ( ContainerProperty ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tp1 , tp2 , * type_params , ** kwargs ) : <EOL> self . _type_params = list ( map ( self . _validate_type_param , ( tp1 , tp2 ) + type_params ) ) <EOL> super ( Tuple , self ) . __init__ ( default = kwargs . get ( "<STR_LIT:default>" ) , help = kwargs . get ( "<STR_LIT>" ) ) <EOL> @ property <EOL> def type_params ( self ) : <EOL> return self . _type_params <EOL> def validate ( self , value ) : <EOL> super ( Tuple , self ) . validate ( value ) <EOL> if value is not None : <EOL> if not ( isinstance ( value , ( tuple , list ) ) and len ( self . type_params ) == len ( value ) and all ( type_param . is_valid ( item ) for type_param , item in zip ( self . type_params , value ) ) ) : <EOL> raise ValueError ( "<STR_LIT>" % ( self , value ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , "<STR_LIT:U+002CU+0020>" . join ( map ( str , self . type_params ) ) ) <EOL> def from_json ( self , json , models = None ) : <EOL> if json is None : <EOL> return None <EOL> elif isinstance ( json , list ) : <EOL> return tuple ( type_param . from_json ( item , models ) for type_param , item in zip ( self . type_params , json ) ) <EOL> else : <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , json ) ) <EOL> class Instance ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , instance_type , default = None , help = None ) : <EOL> if not isinstance ( instance_type , ( type , ) + string_types ) : <EOL> raise ValueError ( "<STR_LIT>" % instance_type ) <EOL> if isinstance ( instance_type , type ) and not issubclass ( instance_type , HasProps ) : <EOL> raise ValueError ( "<STR_LIT>" % instance_type ) <EOL> self . _instance_type = instance_type <EOL> super ( Instance , self ) . __init__ ( default = default , help = help ) <EOL> @ property <EOL> def instance_type ( self ) : <EOL> if isinstance ( self . _instance_type , str ) : <EOL> module , name = self . _instance_type . rsplit ( "<STR_LIT:.>" , <NUM_LIT:1> ) <EOL> self . _instance_type = getattr ( import_module ( module , "<STR_LIT>" ) , name ) <EOL> return self . _instance_type <EOL> def _has_stable_default ( self ) : <EOL> return False <EOL> @ property <EOL> def has_ref ( self ) : <EOL> return True <EOL> def validate ( self , value ) : <EOL> super ( Instance , self ) . validate ( value ) <EOL> if value is not None : <EOL> if not isinstance ( value , self . instance_type ) : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> ( self . instance_type . __name__ , value , type ( value ) . __name__ ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . instance_type . __name__ ) <EOL> def from_json ( self , json , models = None ) : <EOL> if json is None : <EOL> return None <EOL> elif isinstance ( json , dict ) : <EOL> from . . model import Model <EOL> if issubclass ( self . instance_type , Model ) : <EOL> if models is None : <EOL> raise DeserializationError ( "<STR_LIT>" % self ) <EOL> else : <EOL> model = models . get ( json [ "<STR_LIT:id>" ] ) <EOL> if model is not None : <EOL> return model <EOL> else : <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , json ) ) <EOL> else : <EOL> attrs = { } <EOL> for name , value in iteritems ( json ) : <EOL> prop = self . instance_type . lookup ( name ) <EOL> attrs [ name ] = prop . from_json ( value , models ) <EOL> return self . instance_type ( ** attrs ) <EOL> else : <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , json ) ) <EOL> class This ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Any ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Function ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Event ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Interval ( ParameterizedPropertyDescriptor ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , interval_type , start , end , default = None , help = None ) : <EOL> self . interval_type = self . _validate_type_param ( interval_type ) <EOL> self . interval_type . validate ( start ) <EOL> self . interval_type . validate ( end ) <EOL> self . start = start <EOL> self . end = end <EOL> super ( Interval , self ) . __init__ ( default = default , help = help ) <EOL> @ property <EOL> def type_params ( self ) : <EOL> return [ self . interval_type ] <EOL> def validate ( self , value ) : <EOL> super ( Interval , self ) . validate ( value ) <EOL> if not ( value is None or self . interval_type . is_valid ( value ) and value >= self . start and value <= self . end ) : <EOL> raise ValueError ( "<STR_LIT>" % ( self . interval_type , self . start , self . end , value ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . interval_type , self . start , self . end ) <EOL> class Byte ( Interval ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default = <NUM_LIT:0> , help = None ) : <EOL> super ( Byte , self ) . __init__ ( Int , <NUM_LIT:0> , <NUM_LIT:255> , default = default , help = help ) <EOL> class Either ( ParameterizedPropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tp1 , tp2 , * type_params , ** kwargs ) : <EOL> self . _type_params = list ( map ( self . _validate_type_param , ( tp1 , tp2 ) + type_params ) ) <EOL> help = kwargs . get ( "<STR_LIT>" ) <EOL> def choose_default ( ) : <EOL> return self . _type_params [ <NUM_LIT:0> ] . _raw_default ( ) <EOL> default = kwargs . get ( "<STR_LIT:default>" , choose_default ) <EOL> super ( Either , self ) . __init__ ( default = default , help = help ) <EOL> @ property <EOL> def type_params ( self ) : <EOL> return self . _type_params <EOL> def validate ( self , value ) : <EOL> super ( Either , self ) . validate ( value ) <EOL> if not ( value is None or any ( param . is_valid ( value ) for param in self . type_params ) ) : <EOL> raise ValueError ( "<STR_LIT>" % ( nice_join ( self . type_params ) , value ) ) <EOL> def transform ( self , value ) : <EOL> for param in self . type_params : <EOL> try : <EOL> return param . transform ( value ) <EOL> except ValueError : <EOL> pass <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> def from_json ( self , json , models = None ) : <EOL> for tp in self . type_params : <EOL> try : <EOL> return tp . from_json ( json , models ) <EOL> except DeserializationError : <EOL> pass <EOL> else : <EOL> raise DeserializationError ( "<STR_LIT>" % ( self , json ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , "<STR_LIT:U+002CU+0020>" . join ( map ( str , self . type_params ) ) ) <EOL> def __or__ ( self , other ) : <EOL> return self . __class__ ( * ( self . type_params + [ other ] ) , default = self . _default , help = self . help ) <EOL> class Enum ( String ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , enum , * values , ** kwargs ) : <EOL> if not ( not values and isinstance ( enum , enums . Enumeration ) ) : <EOL> enum = enums . enumeration ( enum , * values ) <EOL> self . _enum = enum <EOL> default = kwargs . get ( "<STR_LIT:default>" , enum . _default ) <EOL> help = kwargs . get ( "<STR_LIT>" ) <EOL> super ( Enum , self ) . __init__ ( default = default , help = help ) <EOL> @ property <EOL> def allowed_values ( self ) : <EOL> return self . _enum . _values <EOL> def validate ( self , value ) : <EOL> super ( Enum , self ) . validate ( value ) <EOL> if not ( value is None or value in self . _enum ) : <EOL> raise ValueError ( "<STR_LIT>" % ( value , nice_join ( self . allowed_values ) ) ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , "<STR_LIT:U+002CU+0020>" . join ( map ( repr , self . allowed_values ) ) ) <EOL> class Auto ( Enum ) : <EOL> def __init__ ( self ) : <EOL> super ( Auto , self ) . __init__ ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ <EOL> class Color ( Either ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = None , help = None ) : <EOL> types = ( Enum ( enums . NamedColor ) , <EOL> Regex ( "<STR_LIT>" ) , <EOL> Tuple ( Byte , Byte , Byte ) , <EOL> Tuple ( Byte , Byte , Byte , Percent ) ) <EOL> super ( Color , self ) . __init__ ( * types , default = default , help = help ) <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ <EOL> class MinMaxBounds ( Either ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , accept_datetime = False , default = '<STR_LIT>' , help = None ) : <EOL> if accept_datetime : <EOL> types = ( <EOL> Auto , <EOL> Tuple ( Float , Float ) , <EOL> Tuple ( Datetime , Datetime ) , <EOL> ) <EOL> else : <EOL> types = ( <EOL> Auto , <EOL> Tuple ( Float , Float ) , <EOL> ) <EOL> super ( MinMaxBounds , self ) . __init__ ( * types , default = default , help = help ) <EOL> def validate ( self , value ) : <EOL> super ( MinMaxBounds , self ) . validate ( value ) <EOL> if value is None : <EOL> pass <EOL> elif value [ <NUM_LIT:0> ] is None or value [ <NUM_LIT:1> ] is None : <EOL> pass <EOL> elif value [ <NUM_LIT:0> ] >= value [ <NUM_LIT:1> ] : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return True <EOL> class Align ( PropertyDescriptor ) : <EOL> pass <EOL> class DashPattern ( Either ) : <EOL> """<STR_LIT>""" <EOL> _dash_patterns = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ <NUM_LIT:6> ] , <EOL> "<STR_LIT>" : [ <NUM_LIT:2> , <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : [ <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:6> , <NUM_LIT:4> ] , <EOL> "<STR_LIT>" : [ <NUM_LIT:6> , <NUM_LIT:4> , <NUM_LIT:2> , <NUM_LIT:4> ] , <EOL> } <EOL> def __init__ ( self , default = [ ] , help = None ) : <EOL> types = Enum ( enums . DashPattern ) , Regex ( r"<STR_LIT>" ) , Seq ( Int ) <EOL> super ( DashPattern , self ) . __init__ ( * types , default = default , help = help ) <EOL> def transform ( self , value ) : <EOL> value = super ( DashPattern , self ) . transform ( value ) <EOL> if isinstance ( value , string_types ) : <EOL> try : <EOL> return self . _dash_patterns [ value ] <EOL> except KeyError : <EOL> return [ int ( x ) for x in value . split ( ) ] <EOL> else : <EOL> return value <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ <EOL> class Size ( Float ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> super ( Size , self ) . validate ( value ) <EOL> if not ( value is None or <NUM_LIT:0.0> <= value ) : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> class Percent ( Float ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , value ) : <EOL> super ( Percent , self ) . validate ( value ) <EOL> if not ( value is None or <NUM_LIT:0.0> <= value <= <NUM_LIT:1.0> ) : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> class Angle ( Float ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Date ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = datetime . date . today ( ) , help = None ) : <EOL> super ( Date , self ) . __init__ ( default = default , help = help ) <EOL> def validate ( self , value ) : <EOL> super ( Date , self ) . validate ( value ) <EOL> if not ( value is None or isinstance ( value , ( datetime . date , ) + string_types + ( float , ) + bokeh_integer_types ) ) : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> def transform ( self , value ) : <EOL> value = super ( Date , self ) . transform ( value ) <EOL> if isinstance ( value , ( float , ) + bokeh_integer_types ) : <EOL> try : <EOL> value = datetime . date . fromtimestamp ( value ) <EOL> except ValueError : <EOL> value = datetime . date . fromtimestamp ( value / <NUM_LIT:1000> ) <EOL> elif isinstance ( value , string_types ) : <EOL> value = dateutil . parser . parse ( value ) . date ( ) <EOL> return value <EOL> class Datetime ( PropertyDescriptor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = datetime . date . today ( ) , help = None ) : <EOL> super ( Datetime , self ) . __init__ ( default = default , help = help ) <EOL> def validate ( self , value ) : <EOL> super ( Datetime , self ) . validate ( value ) <EOL> datetime_types = ( datetime . datetime , datetime . date ) <EOL> try : <EOL> import numpy as np <EOL> datetime_types += ( np . datetime64 , ) <EOL> except ImportError : <EOL> pass <EOL> if ( isinstance ( value , datetime_types ) ) : <EOL> return <EOL> if pd and isinstance ( value , ( pd . Timestamp ) ) : <EOL> return <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> def transform ( self , value ) : <EOL> value = super ( Datetime , self ) . transform ( value ) <EOL> return value <EOL> class RelativeDelta ( Dict ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default = { } , help = None ) : <EOL> keys = Enum ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) <EOL> values = Int <EOL> super ( RelativeDelta , self ) . __init__ ( keys , values , default = default , help = help ) <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ <EOL> class DataSpecProperty ( BasicProperty ) : <EOL> """<STR_LIT>""" <EOL> def serializable_value ( self , obj ) : <EOL> return self . descriptor . to_serializable ( obj , self . name , getattr ( obj , self . name ) ) <EOL> def set_from_json ( self , obj , json , models = None ) : <EOL> if isinstance ( json , dict ) : <EOL> old = getattr ( obj , self . name ) <EOL> if old is not None : <EOL> try : <EOL> self . descriptor . _type . validate ( old ) <EOL> if '<STR_LIT:value>' in json : <EOL> json = json [ '<STR_LIT:value>' ] <EOL> except ValueError : <EOL> if isinstance ( old , string_types ) and '<STR_LIT>' in json : <EOL> json = json [ '<STR_LIT>' ] <EOL> super ( DataSpecProperty , self ) . set_from_json ( obj , json , models ) <EOL> class DataSpec ( Either ) : <EOL> def __init__ ( self , typ , default , help = None ) : <EOL> super ( DataSpec , self ) . __init__ ( String , Dict ( String , Either ( String , typ ) ) , typ , default = default , help = help ) <EOL> self . _type = self . _validate_type_param ( typ ) <EOL> def make_properties ( self , base_name ) : <EOL> return [ DataSpecProperty ( descriptor = self , name = base_name ) ] <EOL> def to_serializable ( self , obj , name , val ) : <EOL> if val is None : <EOL> return None <EOL> try : <EOL> self . _type . validate ( val ) <EOL> return dict ( value = val ) <EOL> except ValueError : <EOL> pass <EOL> if isinstance ( val , string_types ) : <EOL> return dict ( field = val ) <EOL> return val <EOL> class NumberSpec ( DataSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default = None , help = None ) : <EOL> super ( NumberSpec , self ) . __init__ ( Float , default = default , help = help ) <EOL> class StringSpec ( DataSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default , help = None ) : <EOL> super ( StringSpec , self ) . __init__ ( List ( String ) , default = default , help = help ) <EOL> def prepare_value ( self , cls , name , value ) : <EOL> if isinstance ( value , list ) : <EOL> if len ( value ) != <NUM_LIT:1> : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> value = dict ( value = value [ <NUM_LIT:0> ] ) <EOL> return super ( StringSpec , self ) . prepare_value ( cls , name , value ) <EOL> class FontSizeSpec ( DataSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default , help = None ) : <EOL> super ( FontSizeSpec , self ) . __init__ ( List ( String ) , default = default , help = help ) <EOL> def prepare_value ( self , cls , name , value ) : <EOL> if isinstance ( value , string_types ) : <EOL> warn ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( value , value , value ) , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> if len ( value ) > <NUM_LIT:0> and value [ <NUM_LIT:0> ] . isdigit ( ) : <EOL> value = dict ( value = value ) <EOL> return super ( FontSizeSpec , self ) . prepare_value ( cls , name , value ) <EOL> class UnitsSpecProperty ( DataSpecProperty ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , descriptor , name , units_prop ) : <EOL> super ( UnitsSpecProperty , self ) . __init__ ( descriptor , name ) <EOL> self . units_prop = units_prop <EOL> def _extract_units ( self , obj , value ) : <EOL> if isinstance ( value , dict ) : <EOL> if '<STR_LIT>' in value : <EOL> value = copy ( value ) <EOL> units = value . pop ( "<STR_LIT>" , None ) <EOL> if units : <EOL> self . units_prop . __set__ ( obj , units ) <EOL> return value <EOL> def __set__ ( self , obj , value ) : <EOL> value = self . _extract_units ( obj , value ) <EOL> super ( UnitsSpecProperty , self ) . __set__ ( obj , value ) <EOL> def set_from_json ( self , obj , json , models = None ) : <EOL> json = self . _extract_units ( obj , json ) <EOL> super ( UnitsSpecProperty , self ) . set_from_json ( obj , json , models ) <EOL> class UnitsSpec ( NumberSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default , units_type , units_default , help = None ) : <EOL> super ( UnitsSpec , self ) . __init__ ( default = default , help = help ) <EOL> self . _units_type = self . _validate_type_param ( units_type ) <EOL> self . _units_type . validate ( units_default ) <EOL> self . _units_type . _default = units_default <EOL> self . _units_type . _serialized = False <EOL> def make_properties ( self , base_name ) : <EOL> units_name = base_name + "<STR_LIT>" <EOL> units_props = self . _units_type . make_properties ( units_name ) <EOL> return units_props + [ UnitsSpecProperty ( descriptor = self , name = base_name , units_prop = units_props [ <NUM_LIT:0> ] ) ] <EOL> def to_serializable ( self , obj , name , val ) : <EOL> d = super ( UnitsSpec , self ) . to_serializable ( obj , name , val ) <EOL> if d is not None and '<STR_LIT>' not in d : <EOL> d [ "<STR_LIT>" ] = getattr ( obj , name + "<STR_LIT>" ) <EOL> return d <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . _units_type . _default ) <EOL> class AngleSpec ( UnitsSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default = None , units_default = "<STR_LIT>" , help = None ) : <EOL> super ( AngleSpec , self ) . __init__ ( default = default , units_type = Enum ( enums . AngleUnits ) , units_default = units_default , help = help ) <EOL> class DistanceSpec ( UnitsSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default = None , units_default = "<STR_LIT:data>" , help = None ) : <EOL> super ( DistanceSpec , self ) . __init__ ( default = default , units_type = Enum ( enums . SpatialUnits ) , units_default = units_default , help = help ) <EOL> def prepare_value ( self , cls , name , value ) : <EOL> try : <EOL> if value is not None and value < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> except TypeError : <EOL> pass <EOL> return super ( DistanceSpec , self ) . prepare_value ( cls , name , value ) <EOL> class ScreenDistanceSpec ( NumberSpec ) : <EOL> '''<STR_LIT>''' <EOL> def to_serializable ( self , obj , name , val ) : <EOL> d = super ( ScreenDistanceSpec , self ) . to_serializable ( obj , name , val ) <EOL> d [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> return d <EOL> def prepare_value ( self , cls , name , value ) : <EOL> try : <EOL> if value is not None and value < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> except TypeError : <EOL> pass <EOL> return super ( ScreenDistanceSpec , self ) . prepare_value ( cls , name , value ) <EOL> class DataDistanceSpec ( NumberSpec ) : <EOL> '''<STR_LIT>''' <EOL> def to_serializable ( self , obj , name , val ) : <EOL> d = super ( ScreenDistanceSpec , self ) . to_serializable ( obj , name , val ) <EOL> d [ "<STR_LIT>" ] = "<STR_LIT:data>" <EOL> return d <EOL> def prepare_value ( self , cls , name , value ) : <EOL> try : <EOL> if value is not None and value < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> except TypeError : <EOL> pass <EOL> return super ( DataDistanceSpec , self ) . prepare_value ( cls , name , value ) <EOL> class ColorSpec ( DataSpec ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , default , help = None ) : <EOL> super ( ColorSpec , self ) . __init__ ( Color , default = default , help = help ) <EOL> @ classmethod <EOL> def isconst ( cls , arg ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( arg , string_types ) and ( ( len ( arg ) == <NUM_LIT:7> and arg [ <NUM_LIT:0> ] == "<STR_LIT:#>" ) or arg in enums . NamedColor ) <EOL> @ classmethod <EOL> def is_color_tuple ( cls , val ) : <EOL> return isinstance ( val , tuple ) and len ( val ) in ( <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> @ classmethod <EOL> def format_tuple ( cls , colortuple ) : <EOL> if len ( colortuple ) == <NUM_LIT:3> : <EOL> return "<STR_LIT>" % ( colortuple , ) <EOL> else : <EOL> return "<STR_LIT>" % ( colortuple , ) <EOL> def to_serializable ( self , obj , name , val ) : <EOL> if val is None : <EOL> return dict ( value = None ) <EOL> if self . isconst ( val ) : <EOL> return dict ( value = val ) <EOL> if isinstance ( val , tuple ) : <EOL> return dict ( value = self . format_tuple ( val ) ) <EOL> if isinstance ( val , string_types ) : <EOL> return dict ( field = val ) <EOL> return val <EOL> def validate ( self , value ) : <EOL> try : <EOL> return super ( ColorSpec , self ) . validate ( value ) <EOL> except ValueError as e : <EOL> if self . is_color_tuple ( value ) : <EOL> return True <EOL> else : <EOL> raise e <EOL> def transform ( self , value ) : <EOL> if isinstance ( value , tuple ) : <EOL> value = tuple ( int ( v ) if i < <NUM_LIT:3> else v for i , v in enumerate ( value ) ) <EOL> return value </s>
