<s> """<STR_LIT>""" <EOL> import ast <EOL> from traits . api import Any , Dict , HasTraits , Instance , Int , List <EOL> from traits . api import Str <EOL> from . assign import AssignFactory <EOL> from . function import FunctionFactory <EOL> from . namespace import Namespace <EOL> from ... _compat import STRING_BASE_CLASS <EOL> class Klass ( Namespace ) : <EOL> """<STR_LIT>""" <EOL> namespace = Instance ( Namespace ) <EOL> lineno = Int <EOL> name = Str <EOL> doc = Any <EOL> bases = List ( Str ) <EOL> attributes = Dict <EOL> traits = Dict <EOL> methods = Dict <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . name , self . lineno ) <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = { } <EOL> state [ '<STR_LIT>' ] = self . namespace <EOL> state [ '<STR_LIT>' ] = self . lineno <EOL> state [ '<STR_LIT:name>' ] = self . name <EOL> state [ '<STR_LIT>' ] = self . doc <EOL> state [ '<STR_LIT>' ] = self . bases <EOL> state [ '<STR_LIT>' ] = self . attributes <EOL> state [ '<STR_LIT>' ] = self . traits <EOL> state [ '<STR_LIT>' ] = self . methods <EOL> return state <EOL> def __setstate__ ( self , state ) : <EOL> """<STR_LIT>""" <EOL> self . namespace = state [ '<STR_LIT>' ] <EOL> self . lineno = state [ '<STR_LIT>' ] <EOL> self . name = state [ '<STR_LIT:name>' ] <EOL> self . doc = state [ '<STR_LIT>' ] <EOL> self . bases = state [ '<STR_LIT>' ] <EOL> self . attributes = state [ '<STR_LIT>' ] <EOL> self . traits = state [ '<STR_LIT>' ] <EOL> self . methods = state [ '<STR_LIT>' ] <EOL> return <EOL> class KlassFactory ( HasTraits ) : <EOL> """<STR_LIT>""" <EOL> def from_ast ( self , namespace , node ) : <EOL> """<STR_LIT>""" <EOL> klass = Klass ( <EOL> namespace = namespace , <EOL> lineno = node . lineno , <EOL> name = node . name , <EOL> doc = ast . get_docstring ( node , clean = False ) , <EOL> bases = [ self . _get_name ( base ) for base in node . bases ] <EOL> ) <EOL> KlassVisitor ( klass ) . visit ( node ) <EOL> return klass <EOL> def _get_name ( self , node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( node , STRING_BASE_CLASS ) : <EOL> name = node <EOL> elif isinstance ( node , ast . Name ) : <EOL> name = node . id <EOL> else : <EOL> names = [ self . _get_name ( child ) for child in ast . walk ( node ) if child is not node ] <EOL> name = '<STR_LIT:.>' . join ( names ) <EOL> return name <EOL> class KlassVisitor ( ast . NodeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , klass ) : <EOL> """<STR_LIT>""" <EOL> self . klass = klass <EOL> self . _function_factory = FunctionFactory ( ) <EOL> self . _assign_factory = AssignFactory ( ) <EOL> return <EOL> def visit_FunctionDef ( self , node ) : <EOL> """<STR_LIT>""" <EOL> function = self . _function_factory . from_ast ( self . klass , node ) <EOL> self . klass . locals [ node . name ] = function <EOL> self . klass . methods [ node . name ] = function <EOL> return <EOL> def visit_Assign ( self , node ) : <EOL> """<STR_LIT>""" <EOL> assign = self . _assign_factory . from_ast ( self . klass , node ) <EOL> if assign . source : <EOL> assign . is_trait = self . klass . is_trait ( assign . source ) <EOL> else : <EOL> assign . is_trait = False <EOL> for target in assign . targets : <EOL> self . klass . locals [ target ] = assign <EOL> self . klass . _is_trait [ target ] = assign . is_trait <EOL> if assign . is_trait : <EOL> self . klass . traits [ target ] = assign <EOL> else : <EOL> self . klass . attributes [ target ] = assign <EOL> return </s>
