<s> """<STR_LIT>""" <EOL> import datetime <EOL> from copy import deepcopy <EOL> from . loader import Config <EOL> from IPython . utils . traitlets import HasTraits , Instance <EOL> from IPython . utils . text import indent , wrap_paragraphs <EOL> class ConfigurableError ( Exception ) : <EOL> pass <EOL> class MultipleInstanceError ( ConfigurableError ) : <EOL> pass <EOL> class Configurable ( HasTraits ) : <EOL> config = Instance ( Config , ( ) , { } ) <EOL> created = None <EOL> def __init__ ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> config = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if config is not None : <EOL> self . config = config <EOL> super ( Configurable , self ) . __init__ ( ** kwargs ) <EOL> self . created = datetime . datetime . now ( ) <EOL> def _config_changed ( self , name , old , new ) : <EOL> """<STR_LIT>""" <EOL> traits = self . traits ( config = True ) <EOL> section_names = [ cls . __name__ for cls in reversed ( self . __class__ . __mro__ ) if <EOL> issubclass ( cls , Configurable ) and issubclass ( self . __class__ , cls ) ] <EOL> for sname in section_names : <EOL> if new . _has_section ( sname ) : <EOL> my_config = new [ sname ] <EOL> for k , v in traits . items ( ) : <EOL> if k [ <NUM_LIT:0> ] . upper ( ) == k [ <NUM_LIT:0> ] and not k . startswith ( '<STR_LIT:_>' ) : <EOL> raise ConfigurableError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . __class__ . __name__ , k ) ) <EOL> try : <EOL> config_value = my_config [ k ] <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> setattr ( self , k , deepcopy ( config_value ) ) <EOL> @ classmethod <EOL> def class_get_help ( cls ) : <EOL> """<STR_LIT>""" <EOL> cls_traits = cls . class_traits ( config = True ) <EOL> final_help = [ ] <EOL> final_help . append ( '<STR_LIT>' % cls . __name__ ) <EOL> final_help . append ( len ( final_help [ <NUM_LIT:0> ] ) * '<STR_LIT:->' ) <EOL> for k , v in cls . class_traits ( config = True ) . items ( ) : <EOL> help = cls . class_get_trait_help ( v ) <EOL> final_help . append ( help ) <EOL> return '<STR_LIT:\n>' . join ( final_help ) <EOL> @ classmethod <EOL> def class_get_trait_help ( cls , trait ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> header = "<STR_LIT>" % ( cls . __name__ , trait . name , trait . __class__ . __name__ ) <EOL> lines . append ( header ) <EOL> try : <EOL> dvr = repr ( trait . get_default_value ( ) ) <EOL> except Exception : <EOL> dvr = None <EOL> if dvr is not None : <EOL> if len ( dvr ) > <NUM_LIT:64> : <EOL> dvr = dvr [ : <NUM_LIT> ] + '<STR_LIT>' <EOL> lines . append ( indent ( '<STR_LIT>' % dvr , <NUM_LIT:4> ) ) <EOL> if '<STR_LIT>' in trait . __class__ . __name__ : <EOL> lines . append ( indent ( '<STR_LIT>' % ( trait . values , ) ) ) <EOL> help = trait . get_metadata ( '<STR_LIT>' ) <EOL> if help is not None : <EOL> help = '<STR_LIT:\n>' . join ( wrap_paragraphs ( help , <NUM_LIT> ) ) <EOL> lines . append ( indent ( help , <NUM_LIT:4> ) ) <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> @ classmethod <EOL> def class_print_help ( cls ) : <EOL> """<STR_LIT>""" <EOL> print ( cls . class_get_help ( ) ) <EOL> @ classmethod <EOL> def class_config_section ( cls ) : <EOL> """<STR_LIT>""" <EOL> def c ( s ) : <EOL> """<STR_LIT>""" <EOL> s = '<STR_LIT>' . join ( wrap_paragraphs ( s , <NUM_LIT> ) ) <EOL> return '<STR_LIT>' + s . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> breaker = '<STR_LIT:#>' + '<STR_LIT:->' * <NUM_LIT> <EOL> s = "<STR_LIT>" % cls . __name__ <EOL> lines = [ breaker , s , breaker , '<STR_LIT>' ] <EOL> desc = cls . class_traits ( ) . get ( '<STR_LIT:description>' ) <EOL> if desc : <EOL> desc = desc . default_value <EOL> else : <EOL> desc = getattr ( cls , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if desc : <EOL> lines . append ( c ( desc ) ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> parents = [ ] <EOL> for parent in cls . mro ( ) : <EOL> if issubclass ( parent , Configurable ) and not parent in ( Configurable , SingletonConfigurable , cls ) : <EOL> parents . append ( parent ) <EOL> if parents : <EOL> pstr = '<STR_LIT:U+002CU+0020>' . join ( [ p . __name__ for p in parents ] ) <EOL> lines . append ( c ( '<STR_LIT>' % ( cls . __name__ , pstr ) ) ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> for name , trait in cls . class_traits ( config = True ) . items ( ) : <EOL> help = trait . get_metadata ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> lines . append ( c ( help ) ) <EOL> lines . append ( '<STR_LIT>' % ( cls . __name__ , name , trait . get_default_value ( ) ) ) <EOL> lines . append ( '<STR_LIT>' ) <EOL> return '<STR_LIT:\n>' . join ( lines ) <EOL> class SingletonConfigurable ( Configurable ) : <EOL> """<STR_LIT>""" <EOL> _instance = None <EOL> @ classmethod <EOL> def _walk_mro ( cls ) : <EOL> """<STR_LIT>""" <EOL> for subclass in cls . mro ( ) : <EOL> if issubclass ( cls , subclass ) and issubclass ( subclass , SingletonConfigurable ) and subclass != SingletonConfigurable : <EOL> yield subclass <EOL> @ classmethod <EOL> def clear_instance ( cls ) : <EOL> """<STR_LIT>""" <EOL> if not cls . initialized ( ) : <EOL> return <EOL> for subclass in cls . _walk_mro ( ) : <EOL> if isinstance ( subclass . _instance , cls ) : <EOL> subclass . _instance = None <EOL> @ classmethod <EOL> def instance ( cls , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if cls . _instance is None : <EOL> inst = cls ( * args , ** kwargs ) <EOL> for subclass in cls . _walk_mro ( ) : <EOL> subclass . _instance = inst <EOL> if isinstance ( cls . _instance , cls ) : <EOL> return cls . _instance <EOL> else : <EOL> raise MultipleInstanceError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % cls . __name__ <EOL> ) <EOL> @ classmethod <EOL> def initialized ( cls ) : <EOL> """<STR_LIT>""" <EOL> return hasattr ( cls , "<STR_LIT>" ) and cls . _instance is not None <EOL> class LoggingConfigurable ( Configurable ) : <EOL> """<STR_LIT>""" <EOL> log = Instance ( '<STR_LIT>' ) <EOL> def _log_default ( self ) : <EOL> from IPython . config . application import Application <EOL> return Application . instance ( ) . log </s>
