コード例 #1
0
ファイル: manager.py プロジェクト: arritrancos/chimera
    def __init__(self, host=None, port=None):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # register ourselves
        self.resources.add(
            MANAGER_LOCATION,
            self,
            getManagerURI(self.getHostname(),
                          self.getPort()))
コード例 #2
0
    def __init__(self, host=None, port=None, local=False):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        if not local:
            try:
                ManagerLocator.locate()
                raise ChimeraException("Chimera is already running"
                                       " on this system. Use chimera-admin"
                                       " to manage it.")
            except ManagerNotFoundException:
                # ok, we are alone.
                pass

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # finder beacon
        if not local:
            self.beacon = ManagerBeacon(self)
            self.beaconThread = threading.Thread(target=self.beacon.run)
            self.beaconThread.setDaemon(True)
            self.beaconThread.start()
        else:
            self.beacon = None

        # register ourself
        self.resources.add(MANAGER_LOCATION, self,
                           getManagerURI(self.getHostname(), self.getPort()))

        # signals
        signal.signal(signal.SIGTERM, self._sighandler)
        signal.signal(signal.SIGINT, self._sighandler)
        atexit.register(self._sighandler)
コード例 #3
0
ファイル: manager.py プロジェクト: phsilva/chimera
    def __init__(self, host=None, port=None):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # register ourself
        self.resources.add(MANAGER_LOCATION, self,
                           getManagerURI(self.getHostname(), self.getPort()))
コード例 #4
0
ファイル: manager.py プロジェクト: carriercomm/chimera-1
class Manager (RemoteObject):

    """
    This is the main class of Chimera.

    Use this class to get Proxies, add objects to the system, and so on.

    This class handles objects life-cycle as described in ILifecycle.

    @group Add/Remove: add*, remove
    @group Start/Stop: start, stop
    @group Proxy: getProxy
    @group Shutdown: wait, shutdown

    """
    
    def __init__(self, host = None, port = None):
        RemoteObject.__init__ (self)
        
        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        # our daemon server
        self.adapter = ManagerAdapter (self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # register ourselves
        self.resources.add(MANAGER_LOCATION, self, getManagerURI(self.getHostname(), self.getPort()))

    # private
    def __repr__ (self):
        if hasattr(self, 'adapter') and self.adapter:
            return "<Manager for %s:%d at %s>" % (self.adapter.hostname, self.adapter.port, hex(id(self)))
        else:
            return "<Manager at %s>" % hex(id(self))

    # adapter host/port
    def getHostname (self):
        if self.adapter:
            return self.adapter.hostname
        else:
            return None

    def getPort (self):
        if self.adapter:
            return self.adapter.port
        else:
            return None

    # reflection (console)
    def getResources (self):
        """
        Returns a list with the Location of all the available resources
        """
        return self.resources.keys()
    
    def getResourcesByClass(self, cls):
        ret = self.resources.getByClass(cls)
        return [ x.location for x in ret ]
        
    # helpers
    
    def getDaemon (self):
        return self.adapter

    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
コード例 #5
0
ファイル: manager.py プロジェクト: phsilva/chimera
class Manager(RemoteObject):
    """
    This is the main class of Chimera.

    Use this class to get Proxies, add objects to the system, and so on.

    This class handles objects life-cycle as described in ILifecycle.

    @group Add/Remove: add*, remove
    @group Start/Stop: start, stop
    @group Proxy: getProxy
    @group Shutdown: wait, shutdown

    """
    def __init__(self, host=None, port=None):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # register ourself
        self.resources.add(MANAGER_LOCATION, self,
                           getManagerURI(self.getHostname(), self.getPort()))

    # private
    def __repr__(self):
        if hasattr(self, 'adapter') and self.adapter:
            return "<Manager for %s:%d at %s>" % (
                self.adapter.hostname, self.adapter.port, hex(id(self)))
        else:
            return "<Manager at %s>" % hex(id(self))

    # adapter host/port
    def getHostname(self):
        if self.adapter:
            return self.adapter.hostname
        else:
            return None

    def getPort(self):
        if self.adapter:
            return self.adapter.port
        else:
            return None

    # reflection (console)
    def getResources(self):
        """
        Returns a list with the Location of all the available resources
        """
        return self.resources.keys()

    def getResourcesByClass(self, cls):
        ret = self.resources.getByClass(cls)
        return [x.location for x in ret]

    # helpers

    def getDaemon(self):
        return self.adapter

    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
コード例 #6
0
ファイル: test_resources.py プロジェクト: orsa-unige/chimera
 def __init__(self):
     # each test will receive a fresh new class, so define our fixture right here
     self.res = ResourcesManager()
コード例 #7
0
ファイル: test_resources.py プロジェクト: orsa-unige/chimera
class TestResources:
    def __init__(self):
        # each test will receive a fresh new class, so define our fixture right here
        self.res = ResourcesManager()

    def test_add(self):

        assert len(self.res) == 0

        assert self.res.add("/Location/l1", "instance-1", "uri-1") == 0

        # location already added
        assert_raises(InvalidLocationException, self.res.add, "/Location/l1",
                      "instance-1", "uri-1")

        assert self.res.add("/Location/l2", "instance-2", "uri-2") == 1

        assert_raises(InvalidLocationException, self.res.add, "wrong location",
                      "instance-2", "uri-2")

        assert "/Location/l1" in self.res
        assert "/Location/l2" in self.res
        assert "/Location/0" in self.res
        assert not "/LocationNotExistent/l2" in self.res

        assert len(self.res) == 2

    def test_str(self):

        assert len(self.res) == 0
        assert self.res.add("/Location/l1", "instance-1", "uri-1") == 0
        assert type(str(self.res.get('/Location/0'))) is str

    def test_remove(self):

        assert len(self.res) == 0

        assert self.res.add("/Location/l1", "instance-1", "uri-1") == 0
        assert self.res.remove("/Location/l1") == True

        assert_raises(ObjectNotFoundException, self.res.remove, "/What/l1")
        assert_raises(InvalidLocationException, self.res.remove,
                      "wrong location")

        assert "/Location/l1" not in self.res

    def test_get(self):

        assert len(self.res) == 0

        assert self.res.add("/Location/l2", "instance-2", "uri-2") == 0
        assert self.res.add("/Location/l1", "instance-1", "uri-1") == 1

        ret = self.res.get("/Location/l1")

        assert ret.location == "/Location/l1"
        assert ret.instance == "instance-1"
        assert ret.uri == "uri-1"

        assert_raises(ObjectNotFoundException, self.res.get, "/Location/l99")

        # get using subscription
        assert self.res["/Location/l1"].location == "/Location/l1"
        assert_raises(KeyError, self.res.__getitem__,
                      "/LocationNotExistent/l1")
        assert_raises(KeyError, self.res.__getitem__, "wrong location")

        # get by index
        assert self.res.get("/Location/0").location == "/Location/l2"
        assert self.res.get("/Location/1").location == "/Location/l1"
        assert_raises(ObjectNotFoundException, self.res.get, '/Location/9')
        assert_raises(ObjectNotFoundException, self.res.get,
                      '/LocationNotExistent/0')
        assert_raises(InvalidLocationException, self.res.get, 'wrong location')

    def test_get_by_class(self):

        assert len(self.res) == 0

        assert self.res.add("/Location/l1", "instance-1", "uri-1") == 0
        assert self.res.add("/Location/l2", "instance-2", "uri-2") == 1

        entries = [self.res.get("/Location/l1"), self.res.get("/Location/l2")]

        # get by class
        found = self.res.getByClass("Location")

        assert (entries == found)

    def test_get_by_class_and_bases(self):

        assert len(self.res) == 0

        class Base(object):
            pass

        class A(Base):
            pass

        class B(A):
            pass

        assert self.res.add("/A/a", A(), "a-uri") == 0
        assert self.res.add("/B/b", B(), "b-uri") == 0

        assert self.res.add("/A/aa", A(), "a-uri") == 1
        assert self.res.add("/B/bb", B(), "b-uri") == 1

        entries = [
            self.res.get("/A/a"),
            self.res.get("/B/b"),
            self.res.get("/A/aa"),
            self.res.get("/B/bb")
        ]

        # get by class
        found = self.res.getByClass("Base", checkBases=True)

        assert (entries == found)
コード例 #8
0
class Manager(RemoteObject):
    """
    This is the main class of Chimera.

    Use this class to get Proxies, add objects to the system, and so on.

    This class handles objects life-cycle as described in ILifecycle.

    @group Add/Remove: add*, remove
    @group Start/Stop: start, stop
    @group Proxy: getProxy
    @group Shutdown: wait, shutdown

    """
    def __init__(self, host=None, port=None, local=False):
        RemoteObject.__init__(self)

        log.info("Starting manager.")

        self.resources = ResourcesManager()
        self.classLoader = ClassLoader()

        # identity
        self.setGUID(MANAGER_LOCATION)

        # shutdown event
        self.died = threading.Event()

        if not local:
            try:
                ManagerLocator.locate()
                raise ChimeraException("Chimera is already running"
                                       " on this system. Use chimera-admin"
                                       " to manage it.")
            except ManagerNotFoundException:
                # ok, we are alone.
                pass

        # our daemon server
        self.adapter = ManagerAdapter(self, host, port)
        self.adapterThread = threading.Thread(target=self.adapter.requestLoop)
        self.adapterThread.setDaemon(True)
        self.adapterThread.start()

        # finder beacon
        if not local:
            self.beacon = ManagerBeacon(self)
            self.beaconThread = threading.Thread(target=self.beacon.run)
            self.beaconThread.setDaemon(True)
            self.beaconThread.start()
        else:
            self.beacon = None

        # register ourself
        self.resources.add(MANAGER_LOCATION, self,
                           getManagerURI(self.getHostname(), self.getPort()))

        # signals
        signal.signal(signal.SIGTERM, self._sighandler)
        signal.signal(signal.SIGINT, self._sighandler)
        atexit.register(self._sighandler)

    # private
    def __repr__(self):
        if hasattr(self, 'adapter') and self.adapter:
            return "<Manager for %s:%d at %s>" % (
                self.adapter.hostname, self.adapter.port, hex(id(self)))
        else:
            return "<Manager at %s>" % hex(id(self))

    def _sighandler(self, sig=None, frame=None):
        self.shutdown()

    # adapter host/port
    def getHostname(self):
        if self.adapter:
            return self.adapter.hostname
        else:
            return None

    def getPort(self):
        if self.adapter:
            return self.adapter.port
        else:
            return None

    # reflection (console)
    def getResources(self):
        """
        Returns a list with the Location of all the available resources
        """
        return self.resources.keys()

    def getResourcesByClass(self, cls):
        resources = self.getResources()
        toRet = []
        for r in resources:
            if r.cls == cls:
                toRet.append(r)
        return toRet

    # helpers

    def getDaemon(self):
        return self.adapter

    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)

            return Proxy(uri=ret.uri)

        else:

            if lazy:
                return Proxy(location)
            else:

                # contact other manager
                other = Proxy(location=MANAGER_LOCATION,
                              host=location.host or host,
                              port=location.port or port)

                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 found an object at the"
                        " given location %s" % location)
                else:
                    return proxy

    def _belongsToMe(self, location):

        meHost = self.getHostname()
        meName = socket.gethostbyname(meHost)
        mePort = self.getPort()

        return (location.host == None or location.host in (meHost, meName)) and \
               (location.port == None or location.port == self.getPort())

    # shutdown management

    def shutdown(self):
        """
        Ask the system to shutdown. Closing all sockets and stopping
        all threads.

        @return: Nothing
        @rtype: None
        """

        # die, but only if we are alive ;)
        if not self.died.isSet():

            log.info("Shuting down manager.")

            # stop objects
            # damm 2.4, on 2.5 try/except/finally works
            try:
                try:

                    elderly_first = sorted(
                        list(self.resources.values()),
                        cmp=lambda x, y: cmp(x.created, y.created),
                        reverse=True)

                    for resource in elderly_first:

                        # except Manager
                        if resource.location == MANAGER_LOCATION: continue

                        # stop object
                        self.stop(resource.location)

                except ChimeraException:
                    pass
            finally:
                # kill our adapter
                self.adapter.shutdown(disconnect=True)
                if self.beacon:
                    self.beacon.shutdown()
                    self.beaconThread.join()

                # die!
                self.died.set()
                log.info("Manager finished.")

    def wait(self):
        """
        Ask the system to wait until anyone calls L{shutdown}.

        If nobody calls L{shutdown}, you can stop the system using
        Ctrl+C.

        @return: Nothing
        @rtype: None
        """

        try:
            while not self.died.isSet():
                time.sleep(1)
        except IOError:
            # On Windows, Ctrl+C on a sleep call raise IOError 'cause
            # of the interrupted syscall
            pass

    # objects lifecycle

    def addLocation(self, location, path=[], start=True):
        """
        Add the class pointed by 'location' to the system configuring it using 'config'.

        Manager will look for the class in 'path' plus sys.path.

        @param path: The class search path.
        @type path: list

        @param start: start the object after initialization.
        @type start: bool

        @raises ChimeraObjectException: Internal error on managed (user) object.
        @raises ClassLoaderException: Class not found.
        @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
        """

        if type(location) != Location:
            location = Location(location)

        # get the class
        cls = None

        cls = self.classLoader.loadClass(location.cls, path)

        return self.addClass(cls, location.name, location.config, start)

    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))

        # connect
        obj.__setlocation__(location)
        next = len(self.resources.getByClass(location.cls))
        uri = self.adapter.connect(obj,
                                   index=str(
                                       Location(cls=location.cls, name=next)))
        self.resources.add(location, obj, uri)

        if start:
            self.start(location)

        return Proxy(uri=uri)
コード例 #9
0
ファイル: test_resources.py プロジェクト: agati/chimera
class TestResources:

    def __init__ (self):
        # each test will receive a fresh new class, so define our fixture right here
        self.res = ResourcesManager ()

    def test_add (self):

        assert len (self.res) == 0

        assert self.res.add ("/Location/l1", "instance-1", "uri-1") == 0

        # location already added
        assert_raises(InvalidLocationException, self.res.add, "/Location/l1", "instance-1", "uri-1")
        
        assert self.res.add ("/Location/l2", "instance-2", "uri-2") == 1
        
        assert_raises(InvalidLocationException, self.res.add, "wrong location", "instance-2", "uri-2")

        assert "/Location/l1" in self.res
        assert "/Location/l2" in self.res
        assert "/Location/0" in self.res
        assert not "/LocationNotExistent/l2" in self.res

        assert len (self.res) == 2

    def test_str (self):

        assert len (self.res) == 0
        assert self.res.add ("/Location/l1", "instance-1", "uri-1") == 0
        assert type(str(self.res.get('/Location/0'))) == StringType
        

    def test_remove (self):

        assert len (self.res) == 0

        assert self.res.add ("/Location/l1", "instance-1", "uri-1") == 0
        assert self.res.remove ("/Location/l1") == True

        assert_raises(ObjectNotFoundException, self.res.remove, "/What/l1")
        assert_raises(InvalidLocationException, self.res.remove, "wrong location")

        assert "/Location/l1" not in self.res

    def test_get (self):

        assert len (self.res) == 0


        assert self.res.add ("/Location/l2", "instance-2", "uri-2") == 0
        assert self.res.add ("/Location/l1", "instance-1", "uri-1") == 1

        ret = self.res.get ("/Location/l1")

        assert ret.location == "/Location/l1"
        assert ret.instance == "instance-1"
        assert ret.uri == "uri-1"

        assert_raises(ObjectNotFoundException, self.res.get, "/Location/l99")

        # get using subscription
        assert self.res["/Location/l1"].location == "/Location/l1"
        assert_raises(KeyError, self.res.__getitem__, "/LocationNotExistent/l1")
        assert_raises(KeyError, self.res.__getitem__, "wrong location")        
        

        # get by index
        assert self.res.get("/Location/0").location == "/Location/l2"
        assert self.res.get("/Location/1").location == "/Location/l1"
        assert_raises(ObjectNotFoundException, self.res.get, '/Location/9')
        assert_raises(ObjectNotFoundException, self.res.get, '/LocationNotExistent/0')        
        assert_raises(InvalidLocationException, self.res.get, 'wrong location')


    def test_get_by_class (self):
        
        assert len (self.res) == 0
        
        assert self.res.add ("/Location/l1", "instance-1", "uri-1") == 0
        assert self.res.add ("/Location/l2", "instance-2", "uri-2") == 1

        entries = [self.res.get ("/Location/l1"), self.res.get ("/Location/l2")]

        # get by class
        found = self.res.getByClass ("Location")
        
        assert (entries == found)


    def test_get_by_class_and_bases (self):

        assert len (self.res) == 0
        
        class Base(object): pass
        class A(Base): pass
        class B(A): pass

        assert self.res.add ("/A/a", A(), "a-uri") == 0
        assert self.res.add ("/B/b", B(), "b-uri") == 0

        assert self.res.add ("/A/aa", A(), "a-uri") == 1
        assert self.res.add ("/B/bb", B(), "b-uri") == 1

        entries = [self.res.get ("/A/a"), self.res.get ("/B/b"), self.res.get ("/A/aa"), self.res.get ("/B/bb")]

        # get by class
        found = self.res.getByClass ("Base", checkBases=True)
        
        assert (entries == found)
コード例 #10
0
ファイル: test_resources.py プロジェクト: agati/chimera
 def __init__ (self):
     # each test will receive a fresh new class, so define our fixture right here
     self.res = ResourcesManager ()