def dependency(self, state, component, *args, **kwargs): mode = state.medium.get_mode(component) for_component = self._get_dependency_for_component(component) if for_component is None: raise UndefinedDependency( 'Component %s is not defined. Defined components are: %r' %\ (component, self._get_defined_components(), )) canonical_name = for_component.get(mode, None) if canonical_name is None: raise UndefinedDependency( 'Component %s is not defined for the mode %r. ' 'Defined handlers are for the modes: %r' %\ (component, mode, for_component.keys(), )) # What we might pass in registration is either a callable object # or its canonical name. # Here we handle lazy imports in this second case. if callable(canonical_name): function = canonical_name else: function = reflect.named_object(canonical_name) if not component.providedBy(function): raise UndefinedDependency( 'Expected object %r to provide the interface %r!' %\ (function, component, )) return function(*args, **kwargs)
def analyse_data(self, data): if isinstance(data, list) and data[0] == METHOD_ATOM: instance = self._externalizer.lookup(data[1]) if instance is None: raise ValueError( "Failed to lookup the externalize object: %r" % (data[1], )) return getattr(instance, data[2]) if isinstance(data, list) and data[0] == FUNCTION_ATOM: return reflect.named_object(data[1]) return super(Unserializer, self).analyse_data(data)
def do_import(self, iter, force=False): canonical_name, auto = self._parse_row(iter) if force or auto: try: o = reflect.named_object(canonical_name) if IApplication.providedBy(o): o.load() elif isinstance(o, types.ModuleType): reflect.named_module(canonical_name) else: raise TypeError("Uknown type of canonical name target: " "%r -> %s" % (canonical_name, type(o))) except Exception, e: error.handle_exception('imports', e, 'Error importing')
def spawn_agency(self, *components): ''' Spawn new agency, returns the reference. Usage: > spawn_agency() Also takes a list of components to switch into production mode. By default all the components work in test mode. Components are the canonical names of interfaces classes used by dependencies (example: flt.agents.hapi.interface.IServerFactory). ''' ag = agency.Agency() self._agencies.append(ag) for canonical_name in components: comp = reflect.named_object(canonical_name) ag.set_mode(comp, ExecMode.production) tun = tunneling.Backend(version=self._tunneling_version, bridge=self._tunneling_bridge) d = ag.initiate(self._database, self._journaler, self, self._messaging, tun) d.addCallback(defer.override_result, ag) return d
def spawn_agency(self, *components, **kwargs): ''' Spawn new agency, returns the reference. Usage: > spawn_agency() Also takes a list of components to switch into production mode. By default all the components work in test mode. Components are the canonical names of interfaces classes used by dependencies (example: flt.agents.hapi.interface.IServerFactory). ''' hostdef = kwargs.pop('hostdef', None) ip = kwargs.pop('ip', None) hostname = kwargs.pop('hostname', None) start_host = kwargs.pop('start_host', True) disable_monitoring = kwargs.pop('disable_monitoring', True) if kwargs: raise AttributeError("Unexpected kwargs argument %r" % (kwargs, )) ag = agency.Agency() ag.set_host_def(hostdef) self._agencies.append(ag) for canonical_name in components: comp = reflect.named_object(canonical_name) ag.set_mode(comp, ExecMode.production) tun_backend = tunneling.EmuBackend(version=self._tunneling_version, bridge=self._tunneling_bridge) if disable_monitoring: ag.disable_protocol('setup-monitoring', 'Task') counter = getattr(self, '_agency_counter', -1) self._agency_counter = counter + 1 queue_name = "agency_%d" % (self._agency_counter, ) msg = rabbitmq.Client(self._messaging, queue_name) tun = tunneling.Tunneling(tun_backend) d = ag.initiate(self._database, self._journaler, self, ip, hostname, start_host, msg, tun) d.addCallback(defer.override_result, ag) d.addCallback(defer.bridge_param, self.wait_for_idle) return d
def dependency(self, state, component, *args, **kwargs): mode = state.medium.get_mode(component) for_component = self._get_dependency_for_component(component) if for_component is None: raise UndefinedDependency( 'Component %s is not defined. Defined components are: %r' %\ (component, self._get_defined_components(), )) canonical_name = for_component.get(mode, None) if canonical_name is None: raise UndefinedDependency( 'Component %s is not defined for the mode %r. ' 'Defined handlers are for the modes: %r' %\ (component, mode, for_component.keys(), )) # What we might pass in registration is either a callable object # or its canonical name. # Here we handle lazy imports in this second case. if callable(canonical_name): function = canonical_name else: function = reflect.named_object(canonical_name) if not component.providedBy(function): raise UndefinedDependency( 'Expected object %r to provide the interface %r!' %\ (function, component, )) result = function(*args, **kwargs) # for purpose of registration we might want to pass the reference # to the dependency to the inside to make it easier to register it if getattr(state.medium, 'keeps_track_of_dependencies', False): state.medium.register_dependency_reference( result, component, mode, args, kwargs) return result
def restore_type(self, type_name): value = reflect.named_object(type_name) if issubclass(value, type): raise ValueError("type %r unserialized to something that " "isn't a type: %r" % (type_name, value)) return value
def unpack_function(self, data): return reflect.named_object(data[1])