def addClass(self, cls, name, config={}, start=True): """ Add the class 'cls' to the system configuring it using 'config'. @param cls: The class to add to the system. @type cls: ChimeraObject @param name: The name of the new class instance. @type name: str @param config: The configuration dictionary for the object. @type config: dict @param start: start the object after initialization. @type start: bool @raises ChimeraObjectException: Internal error on managed (user) object. @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location. @raises InvalidLocationException: When the requested location s invalid. @return: retuns a proxy for the object if sucessuful, False otherwise. @rtype: Proxy or bool """ location = Location(cls=cls.__name__, name=name, config=config) # names must not start with a digit if location.name[0] in "0123456789": raise InvalidLocationException( "Invalid instance name: %s (must start with a letter)" % location) if location in self.resources: raise InvalidLocationException( "Location %s is already in the system. Only one allowed (Tip: change the name!)." % location) # check if it's a valid ChimeraObject if not issubclass(cls, ChimeraObject): raise NotValidChimeraObjectException( "Cannot add the class %s. It doesn't descend from ChimeraObject." % cls.__name__) # run object __init__ and configure using location configuration # it runs on the same thread, so be a good boy # and don't block manager's thread try: obj = cls() except Exception: log.exception("Error in %s __init__." % location) raise ChimeraObjectException("Error in %s __init__." % location) try: for k, v in location.config.items(): obj[k] = v except (OptionConversionException, KeyError), e: log.exception("Error configuring %s." % location) raise ChimeraObjectException("Error configuring %s. (%s)" % (location, e))
def getProxy(self, location, name='0', host=None, port=None, lazy=False): """ Get a proxy for the object pointed by location. The given location can contain index instead of names, e.g. '/Object/0' to get objects when you don't know their names. location can also be a class. getProxy will return an instance named 'name' at the given host/port (or on the current manager, if None given). host and port parameters determines which Manager we will lookup for location/instance. If None, look at this Manager. host/port is only used when location is a class, otherwise, host and port are determined by location itself. lazy parameter determines if Manager will try to locate the selected Manager at host/port and ask them for a valid object/instance. If False, Manager just return an proxy for the selected parameters but can't guarantee that the returned Proxy have an active object bounded. For objects managed by this own Manager, lazy is always False. @param location: Object location or class. @type location: Location or class @param name: Instance name. @type name: str @param host: Manager's hostname. @type host: str @param port: Manager's port. @type port: int @param lazy: Manager's laziness (check for already bound objects on host/port Manager) @type lazy: bool @raises NotValidChimeraObjectException: When a object which doesn't inherites from ChimeraObject is given in location. @raises ObjectNotFoundException: When te request object or the Manager was not found. @raises InvalidLocationException: When the requested location s invalid. @return: Proxy for selected object. @rtype: Proxy """ if not location: raise ObjectNotFoundException("Couldn't find an object at the" " given location %s" % location) if not isinstance(location, StringType) and not isinstance( location, Location): if issubclass(location, ChimeraObject): location = Location(cls=location.__name__, name=name, host=host or self.getHostname(), port=port or self.getPort()) else: raise NotValidChimeraObjectException( "Can't get a proxy from non ChimeraObject's descendent object (%s)." % location) else: location = Location(location, host=host or self.getHostname(), port=port or self.getPort()) # who manages this location? if self._belongsToMe(location): ret = self.resources.get(location) if not ret: raise ObjectNotFoundException("Couldn't found an object at the" " given location %s" % location) p = Proxy(uri=ret.uri) if lazy: return p else: p.ping() return p else: if lazy: return Proxy(location) else: # contact other manager try: other = Proxy(location=MANAGER_LOCATION, host=location.host or host, port=location.port or port) except Pyro.errors.URIError, e: raise InvalidLocationException( "Invalid remote location given. '%s' (%s)." % (location, str(e))) if not other.ping(): raise ObjectNotFoundException( "Can't contact %s manager at %s." % (location, other.URI.address)) proxy = other.getProxy(location) if not proxy: raise ObjectNotFoundException( "Couldn't find an object at the" " given location %s" % location) else: return proxy