<s> import copy <EOL> import six <EOL> from wiring . dependency import Factory <EOL> from wiring . providers import ( <EOL> FactoryProvider , <EOL> FunctionProvider , <EOL> InstanceProvider <EOL> ) <EOL> from wiring . scopes import ProcessScope , SingletonScope , ThreadScope <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class GraphValidationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class SelfDependencyError ( GraphValidationError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , specification ) : <EOL> self . specification = specification <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> repr ( self . specification ) <EOL> ) <EOL> class MissingDependencyError ( GraphValidationError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dependant , dependency ) : <EOL> self . dependant = dependant <EOL> """<STR_LIT>""" <EOL> self . dependency = dependency <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return ( <EOL> "<STR_LIT>" <EOL> ) . format ( <EOL> dependency = repr ( self . dependency ) , <EOL> dependant = repr ( self . dependant ) , <EOL> ) <EOL> class DependencyCycleError ( GraphValidationError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cycle ) : <EOL> self . cycle = tuple ( cycle ) <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( <EOL> cycle = '<STR_LIT>' . join ( <EOL> list ( map ( repr , self . cycle ) ) + [ repr ( self . cycle [ <NUM_LIT:0> ] ) ] <EOL> ) <EOL> ) <EOL> class UnknownScopeError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , scope_type ) : <EOL> self . scope_type = scope_type <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return ( <EOL> "<STR_LIT>" <EOL> ) . format ( <EOL> scope = repr ( self . scope_type ) <EOL> ) <EOL> class Graph ( object ) : <EOL> """<STR_LIT>""" <EOL> class FactoryProxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , graph , specification ) : <EOL> self . graph = graph <EOL> self . specification = specification <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> return self . graph . get ( self . specification , * args , ** kwargs ) <EOL> def __init__ ( self ) : <EOL> self . providers = { } <EOL> """<STR_LIT>""" <EOL> self . scopes = { } <EOL> """<STR_LIT>""" <EOL> self . register_scope ( SingletonScope , SingletonScope ( ) ) <EOL> self . register_scope ( ProcessScope , ProcessScope ( ) ) <EOL> self . register_scope ( ThreadScope , ThreadScope ( ) ) <EOL> def acquire ( self , specification , arguments = None ) : <EOL> """<STR_LIT>""" <EOL> if arguments is None : <EOL> realized_dependencies = { } <EOL> else : <EOL> realized_dependencies = copy . copy ( arguments ) <EOL> provider = self . providers [ specification ] <EOL> scope = None <EOL> if provider . scope is not None : <EOL> try : <EOL> scope = self . scopes [ provider . scope ] <EOL> except KeyError : <EOL> raise UnknownScopeError ( provider . scope ) <EOL> if scope is not None and specification in scope : <EOL> return scope [ specification ] <EOL> dependencies = six . iteritems ( provider . dependencies ) <EOL> for argument , dependency_specification in dependencies : <EOL> if argument not in realized_dependencies : <EOL> if isinstance ( dependency_specification , Factory ) : <EOL> realized_dependencies [ argument ] = self . FactoryProxy ( <EOL> self , <EOL> dependency_specification . specification <EOL> ) <EOL> else : <EOL> realized_dependencies [ argument ] = self . acquire ( <EOL> dependency_specification <EOL> ) <EOL> args = [ ] <EOL> kwargs = { } <EOL> for argument , value in six . iteritems ( realized_dependencies ) : <EOL> if isinstance ( argument , six . integer_types ) : <EOL> if len ( args ) <= argument : <EOL> args . extend ( [ None ] * ( argument + <NUM_LIT:1> - len ( args ) ) ) <EOL> args [ argument ] = value <EOL> elif isinstance ( argument , six . string_types ) : <EOL> kwargs [ argument ] = value <EOL> else : <EOL> raise TypeError ( <EOL> "<STR_LIT>" . format ( repr ( argument ) ) <EOL> ) <EOL> instance = provider ( * args , ** kwargs ) <EOL> if scope is not None : <EOL> scope [ specification ] = instance <EOL> return instance <EOL> def get ( self , specification , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> arguments = dict ( enumerate ( args ) ) <EOL> arguments . update ( kwargs ) <EOL> return self . acquire ( specification , arguments = arguments ) <EOL> def register_provider ( self , specification , provider ) : <EOL> """<STR_LIT>""" <EOL> if provider . scope is not None and provider . scope not in self . scopes : <EOL> raise UnknownScopeError ( provider . scope ) <EOL> self . providers [ specification ] = provider <EOL> def unregister_provider ( self , specification ) : <EOL> """<STR_LIT>""" <EOL> del self . providers [ specification ] <EOL> def register_factory ( self , specification , factory , scope = None ) : <EOL> """<STR_LIT>""" <EOL> self . register_provider ( <EOL> specification , <EOL> FactoryProvider ( factory , scope = scope ) <EOL> ) <EOL> def register_function ( self , specification , function , scope = None ) : <EOL> """<STR_LIT>""" <EOL> self . register_provider ( <EOL> specification , <EOL> FunctionProvider ( function , scope = scope ) <EOL> ) <EOL> def register_instance ( self , specification , instance ) : <EOL> """<STR_LIT>""" <EOL> self . register_provider ( specification , InstanceProvider ( instance ) ) <EOL> def register_scope ( self , scope_type , instance ) : <EOL> """<STR_LIT>""" <EOL> self . scopes [ scope_type ] = instance <EOL> def unregister_scope ( self , scope_type ) : <EOL> """<STR_LIT>""" <EOL> del self . scopes [ scope_type ] <EOL> def validate ( self ) : <EOL> """<STR_LIT>""" <EOL> index = [ <NUM_LIT:0> ] <EOL> indices = { } <EOL> lowlinks = { } <EOL> stack = [ ] <EOL> def strongconnect ( specification ) : <EOL> indices [ specification ] = index [ <NUM_LIT:0> ] <EOL> lowlinks [ specification ] = index [ <NUM_LIT:0> ] <EOL> index [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> stack . append ( specification ) <EOL> provider = self . providers [ specification ] <EOL> dependencies = six . itervalues ( provider . dependencies ) <EOL> for dependency in dependencies : <EOL> if isinstance ( dependency , Factory ) : <EOL> dependency = dependency . specification <EOL> if dependency not in self . providers : <EOL> raise MissingDependencyError ( specification , dependency ) <EOL> if dependency == specification : <EOL> raise SelfDependencyError ( specification ) <EOL> if dependency not in indices : <EOL> strongconnect ( dependency ) <EOL> lowlinks [ specification ] = min ( <EOL> lowlinks [ specification ] , <EOL> lowlinks [ dependency ] <EOL> ) <EOL> elif dependency in stack : <EOL> lowlinks [ specification ] = min ( <EOL> lowlinks [ specification ] , <EOL> indices [ dependency ] <EOL> ) <EOL> if lowlinks [ specification ] == indices [ specification ] : <EOL> component = [ ] <EOL> while True : <EOL> component . append ( stack . pop ( ) ) <EOL> if component [ - <NUM_LIT:1> ] == specification : <EOL> break <EOL> if len ( component ) > <NUM_LIT:1> : <EOL> raise DependencyCycleError ( reversed ( component ) ) <EOL> for specification , provider in six . iteritems ( self . providers ) : <EOL> if specification not in indices : <EOL> strongconnect ( specification ) </s>
