Example #1
0
    def controllerSetup(self):
        """
        Called to load the director configuration from the raw config data.
        This will then recover and load only the relevant controller
        sections.

        After loading the controllers each one's setUp will be called, passing
        it its own local section of the configuration.

        The controllers recovered will be stored in the internal controllers
        member variable.

        :returns: None.

        """
        self.log.info("controllerSetup: loading controllers from config.")
        c = config.get_cfg()

        # Recover and import the controllers:
        self.controllers = config.load_controllers(c.cfg,
                                                   ignore_exceptions=True)
        self.log.info("controllerSetup: %s controller(s) recovered." %
                      len(self.controllers))

        if self.controllers:
            # Setup all enabled controllers:
            for ctl in self.controllers:
                self.nameLookup[ctl.name] = ctl
                controller = ctl.mod
                if not controller:
                    self.log.warn("controllerSetup: %s module isn't loaded!" %
                                  ctl)
                    continue
                try:
                    if ctl.disabled == 'no':
                        controller.setUp(ctl.config)
                except Exception as e:
                    self.log.error("'%s' setUp error: %s" % (ctl, str(e)))
                    #sys.stderr.write("%s setUp error: %s" % (ctl, self.formatError()))
                    if not self.keep_going_on_exceptions():
                        # Stop!
                        raise
        else:
            self.log.warn("controllerSetup: no controllers found in config.")
    def controllerSetup(self):
        """
        Called to load the director configuration from the raw config data.
        This will then recover and load only the relevant controller
        sections.

        After loading the controllers each one's setUp will be called, passing
        it its own local section of the configuration.

        The controllers recovered will be stored in the internal controllers
        member variable.

        :returns: None.

        """
        self.log.info("controllerSetup: loading controllers from config.")
        c = config.get_cfg()

        # Recover and import the controllers:
        self.controllers = config.load_controllers(c.cfg, ignore_exceptions=True)
        self.log.info("controllerSetup: %s controller(s) recovered." % len(self.controllers))

        if self.controllers:
            # Setup all enabled controllers:
            for ctl in self.controllers:
                self.nameLookup[ctl.name] = ctl
                controller = ctl.mod
                if not controller:
                    self.log.warn("controllerSetup: %s module isn't loaded!" % ctl)
                    continue
                try:
                    if ctl.disabled == 'no':
                        controller.setUp(ctl.config)
                except Exception as e:
                    self.log.error("'%s' setUp error: %s" % (ctl, str(e)))
                    #sys.stderr.write("%s setUp error: %s" % (ctl, self.formatError()))
                    if not self.keep_going_on_exceptions():
                        # Stop!
                        raise
        else:
            self.log.warn("controllerSetup: no controllers found in config.")
    def testAgentControllerImport(self):
        """Test I can import Agent or Controller classes using the import_module
        """
        p = tempfile.mkdtemp()
        sys.path.append(p)
        try:
            # Create an agent inside a package so I can then test 
            # the absolute import on which the system is based.
            #
            # mypackage.myagent
            #
            mypkg = os.path.join(p,'mypackage')
            os.mkdir(mypkg)
            
            f = os.path.join(mypkg, '__init__.py')
            fd = open(f, 'wb')
            fd.write("\n")
            fd.close()
            
            # Create an agent module that import_module should find and load.
            #
            f = os.path.join(mypkg, 'myagent.py')
            fd = open(f, 'wb')
            fd.write("""

class Agent(object):
    pass
        
            """)
            fd.close()
            
            # This shouldn't create any import exceptions: 
            class Obj:
                type = 'agent'
                agent = 'mypackage.myagent'
            config.import_module(Obj.type, Obj())
            
            # Try this from configuration file:
            test_config = """
            [director]
            
            [agency]
            order = 1
            disabled = 'no'
            
            [fancyagent]
            order = 2
            cat = 'misc'
            agent = 'mypackage.myagent'
            
            [willnotshowup]
            disabled = 'yes'
            order = 3
            cat = 'misc'
            agent = 'mypackage.myagent'
            bob = '1234'
            port = 59876
            
            """
            config.set_cfg(test_config)
            c = config.get_cfg()
            
            objs = config.load_agents(c.cfg)

            # The agency will be in position 1 (order 1). There should be
            # two agents present, even though the second one is disabled.
            self.assertEquals(len(objs[1].agents), 2)
            
            # Check the config section is stored as part of the config 
            # attribute:
            a = objs[1].agents[1]
            self.assertEquals(a.config['disabled'], 'yes')
            self.assertEquals(a.config['order'], '3')
            self.assertEquals(a.config['cat'], 'misc')
            self.assertEquals(a.config['agent'], 'mypackage.myagent')
            self.assertEquals(a.config['bob'], '1234')
            self.assertEquals(a.config['port'], '59876')
            
            m = __import__('mypackage.myagent', fromlist=['mypackage',])
            self.assertEquals(isinstance(objs[1].agents[0].mod, m.Agent), True, "Agent not recovered correctly!")
            
            # The disabled agent should not be imported.
            m = __import__('mypackage.myagent', fromlist=['mypackage',])
            self.assertEquals(objs[1].agents[1].mod, None, "Agent was imported when it should not have been!")
            
            # try updating the config_objs and recheck that the change has been stored.
            config.update_objs(objs)
            c = config.get_cfg()
            self.assertEquals(isinstance(objs[1].agents[0].mod, m.Agent), True, "Agent not stored+updated correctly!")
            self.assertEquals(isinstance(c.agency.agents[0].mod, m.Agent), True, "Agent not stored+updated correctly!")
            
            
            # Create an controller module that import_module should find and load.
            #
            f = os.path.join(mypkg, 'mycontroller.py')
            fd = open(f, 'wb')
            fd.write("""

class Controller(object):
    pass
        
            """)
            fd.close()
            
            # This shouldn't create any import exceptions: 
            class Obj:
                type = 'controller'
                controller = 'mypackage.mycontroller'
            config.import_module(Obj.type, Obj())
            
            # Try this from configuration file:
            test_config = """
            [director]

            [agency]
            order = 1
            disabled = 'yes'
            
            [supercontroller]
            order = 2
            controller = 'mypackage.mycontroller'
            
            """
            config.set_cfg(test_config)
            c = config.get_cfg()
            
            self.assertEquals(len(c.cfg), 3)
            objs = config.load_controllers(c.cfg)
            
            msg = """
            Inital config != result from load_controllers
            
            c.objs:            
            %s
            
            loaded objs:
            %s
            
            """ % (c.cfg, objs)
            
            self.assertEquals(len(objs), 3, msg)
            
            m = __import__('mypackage.mycontroller', fromlist=['mypackage',])
            
            # The supercontroller will be in this position:
            self.assertEquals(isinstance(objs[2].mod, m.Controller), True, "Controller not recovered correctly!")

            # try updating the config_objs and recheck that the change has been stored.
            config.update_objs(objs)
            c = config.get_cfg()
            self.assertEquals(len(objs), 3)
            m = __import__('mypackage.mycontroller', fromlist=['mypackage',])
            self.assertEquals(isinstance(objs[2].mod, m.Controller), True, "Controller not recovered correctly!")
            
            
        finally:
            self.cleanUp(p)