<s> """<STR_LIT>""" <EOL> import inspect <EOL> import re <EOL> import sys <EOL> import types <EOL> from types import FunctionType <EOL> try : <EOL> from types import ClassType , InstanceType <EOL> ClassTypes = ( ClassType , type ) <EOL> except : <EOL> ClassTypes = ( type , ) <EOL> import six <EOL> def _compute_maxsize ( ) : <EOL> import platform <EOL> if not six . PY3 and sys . version_info [ <NUM_LIT:1> ] < <NUM_LIT:6> : <EOL> architecture = platform . architecture ( ) [ <NUM_LIT:0> ] <EOL> if architecture == "<STR_LIT>" : <EOL> return sys . maxint <EOL> elif architecture == "<STR_LIT>" : <EOL> return <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> <EOL> else : <EOL> raise NotImplementedError ( "<STR_LIT>" % ( architecture , ) ) <EOL> else : <EOL> return sys . maxsize <EOL> if not six . PY3 and sys . version_info [ <NUM_LIT:1> ] < <NUM_LIT:6> : <EOL> maxsize = _compute_maxsize ( ) <EOL> bytes = str <EOL> else : <EOL> maxsize = sys . maxsize <EOL> def import_item ( name ) : <EOL> """<STR_LIT>""" <EOL> parts = name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if len ( parts ) == <NUM_LIT:2> : <EOL> package , obj = parts <EOL> module = __import__ ( package , fromlist = [ obj ] ) <EOL> try : <EOL> pak = module . __dict__ [ obj ] <EOL> except KeyError : <EOL> raise ImportError ( '<STR_LIT>' % obj ) <EOL> return pak <EOL> else : <EOL> return __import__ ( parts [ <NUM_LIT:0> ] ) <EOL> if six . PY3 : <EOL> def isidentifier ( s , dotted = False ) : <EOL> if dotted : <EOL> return all ( isidentifier ( a ) for a in s . split ( "<STR_LIT:.>" ) ) <EOL> return s . isidentifier ( ) <EOL> else : <EOL> _name_re = re . compile ( r"<STR_LIT>" ) <EOL> def isidentifier ( s , dotted = False ) : <EOL> if dotted : <EOL> return all ( isidentifier ( a ) for a in s . split ( "<STR_LIT:.>" ) ) <EOL> return bool ( _name_re . match ( s ) ) <EOL> SequenceTypes = ( list , tuple , set , frozenset ) <EOL> class NoDefaultSpecified ( object ) : pass <EOL> NoDefaultSpecified = NoDefaultSpecified ( ) <EOL> class Undefined ( object ) : pass <EOL> Undefined = Undefined ( ) <EOL> class TraitError ( Exception ) : <EOL> pass <EOL> def class_of ( object ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( object , six . string_types ) : <EOL> return add_article ( object ) <EOL> return add_article ( object . __class__ . __name__ ) <EOL> def add_article ( name ) : <EOL> """<STR_LIT>""" <EOL> if name [ : <NUM_LIT:1> ] . lower ( ) in '<STR_LIT>' : <EOL> return '<STR_LIT>' + name <EOL> return '<STR_LIT>' + name <EOL> def repr_type ( obj ) : <EOL> """<STR_LIT>""" <EOL> the_type = type ( obj ) <EOL> if ( not six . PY3 ) and the_type is InstanceType : <EOL> the_type = obj . __class__ <EOL> msg = '<STR_LIT>' % ( obj , the_type ) <EOL> return msg <EOL> def is_trait ( t ) : <EOL> """<STR_LIT>""" <EOL> return ( isinstance ( t , TraitType ) or <EOL> ( isinstance ( t , type ) and issubclass ( t , TraitType ) ) ) <EOL> def parse_notifier_name ( name ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( name , str ) : <EOL> return [ name ] <EOL> elif name is None : <EOL> return [ '<STR_LIT>' ] <EOL> elif isinstance ( name , ( list , tuple ) ) : <EOL> for n in name : <EOL> assert isinstance ( n , str ) , "<STR_LIT>" <EOL> return name <EOL> class _SimpleTest : <EOL> def __init__ ( self , value ) : self . value = value <EOL> def __call__ ( self , test ) : <EOL> return test == self . value <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % self . value <EOL> def __str__ ( self ) : <EOL> return self . __repr__ ( ) <EOL> def getmembers ( object , predicate = None ) : <EOL> """<STR_LIT>""" <EOL> results = [ ] <EOL> for key in dir ( object ) : <EOL> try : <EOL> value = getattr ( object , key ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if not predicate or predicate ( value ) : <EOL> results . append ( ( key , value ) ) <EOL> results . sort ( ) <EOL> return results <EOL> class TraitType ( object ) : <EOL> """<STR_LIT>""" <EOL> metadata = { } <EOL> default_value = Undefined <EOL> info_text = '<STR_LIT>' <EOL> def __init__ ( self , default_value = NoDefaultSpecified , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> if default_value is not NoDefaultSpecified : <EOL> self . default_value = default_value <EOL> if len ( metadata ) > <NUM_LIT:0> : <EOL> if len ( self . metadata ) > <NUM_LIT:0> : <EOL> self . _metadata = self . metadata . copy ( ) <EOL> self . _metadata . update ( metadata ) <EOL> else : <EOL> self . _metadata = metadata <EOL> else : <EOL> self . _metadata = self . metadata <EOL> self . init ( ) <EOL> def init ( self ) : <EOL> pass <EOL> def get_default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . default_value <EOL> def instance_init ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> self . set_default_value ( obj ) <EOL> def set_default_value ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> mro = type ( obj ) . mro ( ) <EOL> meth_name = '<STR_LIT>' % self . name <EOL> for cls in mro [ : mro . index ( self . this_class ) + <NUM_LIT:1> ] : <EOL> if meth_name in cls . __dict__ : <EOL> break <EOL> else : <EOL> dv = self . get_default_value ( ) <EOL> newdv = self . _validate ( obj , dv ) <EOL> obj . _trait_values [ self . name ] = newdv <EOL> return <EOL> obj . _trait_dyn_inits [ self . name ] = cls . __dict__ [ meth_name ] <EOL> def __get__ ( self , obj , cls = None ) : <EOL> """<STR_LIT>""" <EOL> if obj is None : <EOL> return self <EOL> else : <EOL> try : <EOL> value = obj . _trait_values [ self . name ] <EOL> except KeyError : <EOL> if self . name in obj . _trait_dyn_inits : <EOL> value = obj . _trait_dyn_inits [ self . name ] ( obj ) <EOL> value = self . _validate ( obj , value ) <EOL> obj . _trait_values [ self . name ] = value <EOL> return value <EOL> else : <EOL> raise TraitError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> except Exception : <EOL> raise TraitError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> return value <EOL> def __set__ ( self , obj , value ) : <EOL> new_value = self . _validate ( obj , value ) <EOL> old_value = self . __get__ ( obj ) <EOL> obj . _trait_values [ self . name ] = new_value <EOL> if old_value != new_value : <EOL> obj . _notify_trait ( self . name , old_value , new_value ) <EOL> def _validate ( self , obj , value ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return self . validate ( obj , value ) <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> valid = self . is_valid_for ( value ) <EOL> if valid : <EOL> return value <EOL> else : <EOL> raise TraitError ( '<STR_LIT>' % value ) <EOL> elif hasattr ( self , '<STR_LIT>' ) : <EOL> return self . value_for ( value ) <EOL> else : <EOL> return value <EOL> def info ( self ) : <EOL> return self . info_text <EOL> def error ( self , obj , value ) : <EOL> if obj is not None : <EOL> e = "<STR_LIT>" % ( self . name , class_of ( obj ) , <EOL> self . info ( ) , repr_type ( value ) ) <EOL> else : <EOL> e = "<STR_LIT>" % ( self . name , self . info ( ) , repr_type ( value ) ) <EOL> raise TraitError ( e ) <EOL> def get_metadata ( self , key ) : <EOL> return getattr ( self , '<STR_LIT>' , { } ) . get ( key , None ) <EOL> def set_metadata ( self , key , value ) : <EOL> getattr ( self , '<STR_LIT>' , { } ) [ key ] = value <EOL> class MetaHasTraits ( type ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( mcls , name , bases , classdict ) : <EOL> """<STR_LIT>""" <EOL> for k , v in six . iteritems ( classdict ) : <EOL> if isinstance ( v , TraitType ) : <EOL> v . name = k <EOL> elif inspect . isclass ( v ) : <EOL> if issubclass ( v , TraitType ) : <EOL> vinst = v ( ) <EOL> vinst . name = k <EOL> classdict [ k ] = vinst <EOL> return super ( MetaHasTraits , mcls ) . __new__ ( mcls , name , bases , classdict ) <EOL> def __init__ ( cls , name , bases , classdict ) : <EOL> """<STR_LIT>""" <EOL> for k , v in six . iteritems ( classdict ) : <EOL> if isinstance ( v , TraitType ) : <EOL> v . this_class = cls <EOL> super ( MetaHasTraits , cls ) . __init__ ( name , bases , classdict ) <EOL> class HasTraits ( six . with_metaclass ( MetaHasTraits , object ) ) : <EOL> def __new__ ( cls , * args , ** kw ) : <EOL> new_meth = super ( HasTraits , cls ) . __new__ <EOL> if new_meth is object . __new__ : <EOL> inst = new_meth ( cls ) <EOL> else : <EOL> inst = new_meth ( cls , ** kw ) <EOL> inst . _trait_values = { } <EOL> inst . _trait_notifiers = { } <EOL> inst . _trait_dyn_inits = { } <EOL> for key in dir ( cls ) : <EOL> try : <EOL> value = getattr ( cls , key ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if isinstance ( value , TraitType ) : <EOL> value . instance_init ( inst ) <EOL> return inst <EOL> def __init__ ( self , * args , ** kw ) : <EOL> for key , value in six . iteritems ( kw ) : <EOL> setattr ( self , key , value ) <EOL> def _notify_trait ( self , name , old_value , new_value ) : <EOL> callables = self . _trait_notifiers . get ( name , [ ] ) <EOL> more_callables = self . _trait_notifiers . get ( '<STR_LIT>' , [ ] ) <EOL> callables . extend ( more_callables ) <EOL> try : <EOL> cb = getattr ( self , '<STR_LIT>' % name ) <EOL> except : <EOL> pass <EOL> else : <EOL> callables . append ( cb ) <EOL> for c in callables : <EOL> if callable ( c ) : <EOL> argspec = inspect . getargspec ( c ) <EOL> nargs = len ( argspec [ <NUM_LIT:0> ] ) <EOL> if isinstance ( c , types . MethodType ) : <EOL> offset = - <NUM_LIT:1> <EOL> else : <EOL> offset = <NUM_LIT:0> <EOL> if nargs + offset == <NUM_LIT:0> : <EOL> c ( ) <EOL> elif nargs + offset == <NUM_LIT:1> : <EOL> c ( name ) <EOL> elif nargs + offset == <NUM_LIT:2> : <EOL> c ( name , new_value ) <EOL> elif nargs + offset == <NUM_LIT:3> : <EOL> c ( name , old_value , new_value ) <EOL> else : <EOL> raise TraitError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> raise TraitError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def _add_notifiers ( self , handler , name ) : <EOL> if name not in self . _trait_notifiers : <EOL> nlist = [ ] <EOL> self . _trait_notifiers [ name ] = nlist <EOL> else : <EOL> nlist = self . _trait_notifiers [ name ] <EOL> if handler not in nlist : <EOL> nlist . append ( handler ) <EOL> def _remove_notifiers ( self , handler , name ) : <EOL> if name in self . _trait_notifiers : <EOL> nlist = self . _trait_notifiers [ name ] <EOL> try : <EOL> index = nlist . index ( handler ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> del nlist [ index ] <EOL> def on_trait_change ( self , handler , name = None , remove = False ) : <EOL> """<STR_LIT>""" <EOL> if remove : <EOL> names = parse_notifier_name ( name ) <EOL> for n in names : <EOL> self . _remove_notifiers ( handler , n ) <EOL> else : <EOL> names = parse_notifier_name ( name ) <EOL> for n in names : <EOL> self . _add_notifiers ( handler , n ) <EOL> @ classmethod <EOL> def class_trait_names ( cls , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> return cls . class_traits ( ** metadata ) . keys ( ) <EOL> @ classmethod <EOL> def class_traits ( cls , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> traits = dict ( [ memb for memb in getmembers ( cls ) if isinstance ( memb [ <NUM_LIT:1> ] , TraitType ) ] ) <EOL> if len ( metadata ) == <NUM_LIT:0> : <EOL> return traits <EOL> for meta_name , meta_eval in metadata . items ( ) : <EOL> if type ( meta_eval ) is not FunctionType : <EOL> metadata [ meta_name ] = _SimpleTest ( meta_eval ) <EOL> result = { } <EOL> for name , trait in traits . items ( ) : <EOL> for meta_name , meta_eval in metadata . items ( ) : <EOL> if not meta_eval ( trait . get_metadata ( meta_name ) ) : <EOL> break <EOL> else : <EOL> result [ name ] = trait <EOL> return result <EOL> def trait_names ( self , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> return self . traits ( ** metadata ) . keys ( ) <EOL> def traits ( self , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> traits = dict ( [ memb for memb in getmembers ( self . __class__ ) if isinstance ( memb [ <NUM_LIT:1> ] , TraitType ) ] ) <EOL> if len ( metadata ) == <NUM_LIT:0> : <EOL> return traits <EOL> for meta_name , meta_eval in metadata . items ( ) : <EOL> if type ( meta_eval ) is not FunctionType : <EOL> metadata [ meta_name ] = _SimpleTest ( meta_eval ) <EOL> result = { } <EOL> for name , trait in traits . items ( ) : <EOL> for meta_name , meta_eval in metadata . items ( ) : <EOL> if not meta_eval ( trait . get_metadata ( meta_name ) ) : <EOL> break <EOL> else : <EOL> result [ name ] = trait <EOL> return result <EOL> def trait_metadata ( self , traitname , key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> trait = getattr ( self . __class__ , traitname ) <EOL> except AttributeError : <EOL> raise TraitError ( "<STR_LIT>" % <EOL> ( self . __class__ . __name__ , traitname ) ) <EOL> else : <EOL> return trait . get_metadata ( key ) <EOL> class ClassBasedTraitType ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> def error ( self , obj , value ) : <EOL> kind = type ( value ) <EOL> if ( not six . PY3 ) and kind is InstanceType : <EOL> msg = '<STR_LIT>' % value . __class__ . __name__ <EOL> else : <EOL> msg = '<STR_LIT>' % ( str ( kind ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] , repr ( value ) ) <EOL> if obj is not None : <EOL> e = "<STR_LIT>" % ( self . name , class_of ( obj ) , <EOL> self . info ( ) , msg ) <EOL> else : <EOL> e = "<STR_LIT>" % ( self . name , self . info ( ) , msg ) <EOL> raise TraitError ( e ) <EOL> class Type ( ClassBasedTraitType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default_value = None , klass = None , allow_none = True , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> if default_value is None : <EOL> if klass is None : <EOL> klass = object <EOL> elif klass is None : <EOL> klass = default_value <EOL> if not ( inspect . isclass ( klass ) or isinstance ( klass , six . string_types ) ) : <EOL> raise TraitError ( "<STR_LIT>" ) <EOL> self . klass = klass <EOL> self . _allow_none = allow_none <EOL> super ( Type , self ) . __init__ ( default_value , ** metadata ) <EOL> def validate ( self , obj , value ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if issubclass ( value , self . klass ) : <EOL> return value <EOL> except : <EOL> if ( value is None ) and ( self . _allow_none ) : <EOL> return value <EOL> self . error ( obj , value ) <EOL> def info ( self ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . klass , six . string_types ) : <EOL> klass = self . klass <EOL> else : <EOL> klass = self . klass . __name__ <EOL> result = '<STR_LIT>' + klass <EOL> if self . _allow_none : <EOL> return result + '<STR_LIT>' <EOL> return result <EOL> def instance_init ( self , obj ) : <EOL> self . _resolve_classes ( ) <EOL> super ( Type , self ) . instance_init ( obj ) <EOL> def _resolve_classes ( self ) : <EOL> if isinstance ( self . klass , six . string_types ) : <EOL> self . klass = import_item ( self . klass ) <EOL> if isinstance ( self . default_value , six . string_types ) : <EOL> self . default_value = import_item ( self . default_value ) <EOL> def get_default_value ( self ) : <EOL> return self . default_value <EOL> class DefaultValueGenerator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> self . args = args <EOL> self . kw = kw <EOL> def generate ( self , klass ) : <EOL> return klass ( * self . args , ** self . kw ) <EOL> class Instance ( ClassBasedTraitType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass = None , args = None , kw = None , <EOL> allow_none = True , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> self . _allow_none = allow_none <EOL> if ( klass is None ) or ( not ( inspect . isclass ( klass ) or isinstance ( klass , six . string_types ) ) ) : <EOL> raise TraitError ( '<STR_LIT>' <EOL> '<STR_LIT>' % klass ) <EOL> self . klass = klass <EOL> if args is None and kw is None : <EOL> default_value = None <EOL> else : <EOL> if args is None : <EOL> args = ( ) <EOL> elif kw is None : <EOL> kw = { } <EOL> if not isinstance ( kw , dict ) : <EOL> raise TraitError ( "<STR_LIT>" ) <EOL> if not isinstance ( args , tuple ) : <EOL> raise TraitError ( "<STR_LIT>" ) <EOL> default_value = DefaultValueGenerator ( * args , ** kw ) <EOL> super ( Instance , self ) . __init__ ( default_value , ** metadata ) <EOL> def validate ( self , obj , value ) : <EOL> if value is None : <EOL> if self . _allow_none : <EOL> return value <EOL> self . error ( obj , value ) <EOL> if isinstance ( value , self . klass ) : <EOL> return value <EOL> else : <EOL> self . error ( obj , value ) <EOL> def info ( self ) : <EOL> if isinstance ( self . klass , six . string_types ) : <EOL> klass = self . klass <EOL> else : <EOL> klass = self . klass . __name__ <EOL> result = class_of ( klass ) <EOL> if self . _allow_none : <EOL> return result + '<STR_LIT>' <EOL> return result <EOL> def instance_init ( self , obj ) : <EOL> self . _resolve_classes ( ) <EOL> super ( Instance , self ) . instance_init ( obj ) <EOL> def _resolve_classes ( self ) : <EOL> if isinstance ( self . klass , six . string_types ) : <EOL> self . klass = import_item ( self . klass ) <EOL> def get_default_value ( self ) : <EOL> """<STR_LIT>""" <EOL> dv = self . default_value <EOL> if isinstance ( dv , DefaultValueGenerator ) : <EOL> return dv . generate ( self . klass ) <EOL> else : <EOL> return dv <EOL> class This ( ClassBasedTraitType ) : <EOL> """<STR_LIT>""" <EOL> info_text = '<STR_LIT>' <EOL> def __init__ ( self , ** metadata ) : <EOL> super ( This , self ) . __init__ ( None , ** metadata ) <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , self . this_class ) or ( value is None ) : <EOL> return value <EOL> else : <EOL> self . error ( obj , value ) <EOL> class Any ( TraitType ) : <EOL> default_value = None <EOL> info_text = '<STR_LIT>' <EOL> class Int ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = <NUM_LIT:0> <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , int ) : <EOL> return value <EOL> self . error ( obj , value ) <EOL> class CInt ( Int ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return int ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> if six . PY3 : <EOL> Long , CLong = Int , CInt <EOL> Integer = Int <EOL> else : <EOL> class Long ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = long ( <NUM_LIT:0> ) <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , long ) : <EOL> return value <EOL> if isinstance ( value , int ) : <EOL> return long ( value ) <EOL> self . error ( obj , value ) <EOL> class CLong ( Long ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return long ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> class Integer ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = <NUM_LIT:0> <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , int ) : <EOL> return value <EOL> if isinstance ( value , long ) : <EOL> return int ( value ) <EOL> if sys . platform == "<STR_LIT>" : <EOL> from System import Int64 <EOL> if isinstance ( value , Int64 ) : <EOL> return int ( value ) <EOL> self . error ( obj , value ) <EOL> class Float ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = <NUM_LIT:0.0> <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , float ) : <EOL> return value <EOL> if isinstance ( value , int ) : <EOL> return float ( value ) <EOL> self . error ( obj , value ) <EOL> class CFloat ( Float ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return float ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> class Complex ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = <NUM_LIT:0.0> + <NUM_LIT> <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , complex ) : <EOL> return value <EOL> if isinstance ( value , ( float , int ) ) : <EOL> return complex ( value ) <EOL> self . error ( obj , value ) <EOL> class CComplex ( Complex ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return complex ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> class Bytes ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = six . b ( '<STR_LIT>' ) <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , bytes ) : <EOL> return value <EOL> self . error ( obj , value ) <EOL> class CBytes ( Bytes ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return bytes ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> class Unicode ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = six . u ( '<STR_LIT>' ) <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , six . text_type ) : <EOL> return value <EOL> if isinstance ( value , six . binary_type ) : <EOL> return unicode ( value ) <EOL> self . error ( obj , value ) <EOL> class CUnicode ( Unicode ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return unicode ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> class ObjectName ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> info_text = "<STR_LIT>" <EOL> if six . PY3 : <EOL> coerce_str = staticmethod ( lambda _ , s : s ) <EOL> else : <EOL> def coerce_str ( self , obj , value ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( value , unicode ) : <EOL> try : <EOL> return str ( value ) <EOL> except UnicodeEncodeError : <EOL> self . error ( obj , value ) <EOL> return value <EOL> def validate ( self , obj , value ) : <EOL> value = self . coerce_str ( obj , value ) <EOL> if isinstance ( value , str ) and isidentifier ( value ) : <EOL> return value <EOL> self . error ( obj , value ) <EOL> class DottedObjectName ( ObjectName ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> value = self . coerce_str ( obj , value ) <EOL> if isinstance ( value , str ) and isidentifier ( value , dotted = True ) : <EOL> return value <EOL> self . error ( obj , value ) <EOL> class Bool ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = False <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , bool ) : <EOL> return value <EOL> self . error ( obj , value ) <EOL> class CBool ( Bool ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return bool ( value ) <EOL> except : <EOL> self . error ( obj , value ) <EOL> class Enum ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , values , default_value = None , allow_none = True , ** metadata ) : <EOL> self . values = values <EOL> self . _allow_none = allow_none <EOL> super ( Enum , self ) . __init__ ( default_value , ** metadata ) <EOL> def validate ( self , obj , value ) : <EOL> if value is None : <EOL> if self . _allow_none : <EOL> return value <EOL> if value in self . values : <EOL> return value <EOL> self . error ( obj , value ) <EOL> def info ( self ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT>' + repr ( self . values ) <EOL> if self . _allow_none : <EOL> return result + '<STR_LIT>' <EOL> return result <EOL> class CaselessStrEnum ( Enum ) : <EOL> """<STR_LIT>""" <EOL> def validate ( self , obj , value ) : <EOL> if value is None : <EOL> if self . _allow_none : <EOL> return value <EOL> if not isinstance ( value , basestring ) : <EOL> self . error ( obj , value ) <EOL> for v in self . values : <EOL> if v . lower ( ) == value . lower ( ) : <EOL> return v <EOL> self . error ( obj , value ) <EOL> class Container ( Instance ) : <EOL> """<STR_LIT>""" <EOL> klass = None <EOL> _valid_defaults = SequenceTypes <EOL> _trait = None <EOL> def __init__ ( self , trait = None , default_value = None , allow_none = True , <EOL> ** metadata ) : <EOL> """<STR_LIT>""" <EOL> if default_value is None and not is_trait ( trait ) : <EOL> default_value = trait <EOL> trait = None <EOL> if default_value is None : <EOL> args = ( ) <EOL> elif isinstance ( default_value , self . _valid_defaults ) : <EOL> args = ( default_value , ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( self . __class__ . __name__ , default_value ) ) <EOL> if is_trait ( trait ) : <EOL> self . _trait = trait ( ) if isinstance ( trait , type ) else trait <EOL> self . _trait . name = '<STR_LIT>' <EOL> elif trait is not None : <EOL> raise TypeError ( "<STR_LIT>" % repr_type ( trait ) ) <EOL> super ( Container , self ) . __init__ ( klass = self . klass , args = args , <EOL> allow_none = allow_none , ** metadata ) <EOL> def element_error ( self , obj , element , validator ) : <EOL> e = "<STR_LIT>" % ( self . name , class_of ( obj ) , validator . info ( ) , repr_type ( element ) ) <EOL> raise TraitError ( e ) <EOL> def validate ( self , obj , value ) : <EOL> value = super ( Container , self ) . validate ( obj , value ) <EOL> if value is None : <EOL> return value <EOL> value = self . validate_elements ( obj , value ) <EOL> return value <EOL> def validate_elements ( self , obj , value ) : <EOL> validated = [ ] <EOL> if self . _trait is None or isinstance ( self . _trait , Any ) : <EOL> return value <EOL> for v in value : <EOL> try : <EOL> v = self . _trait . validate ( obj , v ) <EOL> except TraitError : <EOL> self . element_error ( obj , v , self . _trait ) <EOL> else : <EOL> validated . append ( v ) <EOL> return self . klass ( validated ) <EOL> class List ( Container ) : <EOL> """<STR_LIT>""" <EOL> klass = list <EOL> def __init__ ( self , trait = None , default_value = None , minlen = <NUM_LIT:0> , maxlen = maxsize , <EOL> allow_none = True , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> self . _minlen = minlen <EOL> self . _maxlen = maxlen <EOL> super ( List , self ) . __init__ ( trait = trait , default_value = default_value , <EOL> allow_none = allow_none , ** metadata ) <EOL> def length_error ( self , obj , value ) : <EOL> e = "<STR_LIT>" % ( self . name , class_of ( obj ) , self . _minlen , self . _maxlen , value ) <EOL> raise TraitError ( e ) <EOL> def validate_elements ( self , obj , value ) : <EOL> length = len ( value ) <EOL> if length < self . _minlen or length > self . _maxlen : <EOL> self . length_error ( obj , value ) <EOL> return super ( List , self ) . validate_elements ( obj , value ) <EOL> class Set ( Container ) : <EOL> """<STR_LIT>""" <EOL> klass = set <EOL> class Tuple ( Container ) : <EOL> """<STR_LIT>""" <EOL> klass = tuple <EOL> def __init__ ( self , * traits , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> default_value = metadata . pop ( '<STR_LIT>' , None ) <EOL> allow_none = metadata . pop ( '<STR_LIT>' , True ) <EOL> if len ( traits ) == <NUM_LIT:1> and default_value is None and not is_trait ( traits [ <NUM_LIT:0> ] ) : <EOL> default_value = traits [ <NUM_LIT:0> ] <EOL> traits = ( ) <EOL> if default_value is None : <EOL> args = ( ) <EOL> elif isinstance ( default_value , self . _valid_defaults ) : <EOL> args = ( default_value , ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % ( self . __class__ . __name__ , default_value ) ) <EOL> self . _traits = [ ] <EOL> for trait in traits : <EOL> t = trait ( ) if isinstance ( trait , type ) else trait <EOL> t . name = '<STR_LIT>' <EOL> self . _traits . append ( t ) <EOL> if self . _traits and default_value is None : <EOL> args = None <EOL> super ( Container , self ) . __init__ ( klass = self . klass , args = args , <EOL> allow_none = allow_none , ** metadata ) <EOL> def validate_elements ( self , obj , value ) : <EOL> if not self . _traits : <EOL> return value <EOL> if len ( value ) != len ( self . _traits ) : <EOL> e = "<STR_LIT>" % ( self . name , class_of ( obj ) , len ( self . _traits ) , repr_type ( value ) ) <EOL> raise TraitError ( e ) <EOL> validated = [ ] <EOL> for t , v in zip ( self . _traits , value ) : <EOL> try : <EOL> v = t . validate ( obj , v ) <EOL> except TraitError : <EOL> self . element_error ( obj , v , t ) <EOL> else : <EOL> validated . append ( v ) <EOL> return tuple ( validated ) <EOL> class Dict ( Instance ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , default_value = None , allow_none = True , ** metadata ) : <EOL> """<STR_LIT>""" <EOL> if default_value is None : <EOL> args = ( ( ) , ) <EOL> elif isinstance ( default_value , dict ) : <EOL> args = ( default_value , ) <EOL> elif isinstance ( default_value , SequenceTypes ) : <EOL> args = ( default_value , ) <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' % default_value ) <EOL> super ( Dict , self ) . __init__ ( klass = dict , args = args , <EOL> allow_none = allow_none , ** metadata ) <EOL> class TCPAddress ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> default_value = ( '<STR_LIT:127.0.0.1>' , <NUM_LIT:0> ) <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> if isinstance ( value , tuple ) : <EOL> if len ( value ) == <NUM_LIT:2> : <EOL> if isinstance ( value [ <NUM_LIT:0> ] , six . string_types ) and isinstance ( value [ <NUM_LIT:1> ] , int ) : <EOL> port = value [ <NUM_LIT:1> ] <EOL> if port >= <NUM_LIT:0> and port <= <NUM_LIT> : <EOL> return value <EOL> self . error ( obj , value ) <EOL> class CRegExp ( TraitType ) : <EOL> """<STR_LIT>""" <EOL> info_text = '<STR_LIT>' <EOL> def validate ( self , obj , value ) : <EOL> try : <EOL> return re . compile ( value ) <EOL> except : <EOL> self . error ( obj , value ) </s>
