class ProcessorManager(object): def __init__(self): self.__config = ConfigClient() self.__processors = [] self.__construct_processors() def get_processors(self): return self.__processors def get_processor(self, name): for p in self.__processors: if p.get_name() == name: return p def __construct_processors(self): processor_list = self.__config.get('processor') for processor in processor_list: conf = processor_list[processor] class_name = '%sProcessor' % conf['type'].capitalize() import_code = 'from src.processor.%s_processor import %s' % ( conf['type'], class_name) construct_code = "instance = %s(processor, conf)" % class_name try: exec(import_code+'\n'+construct_code) except ImportError: instance = BaseProcessor(processor, conf) pass self.__processors.append(instance)
class test_config_client(unittest.TestCase): def setUp(self): test_helper.setUp() self.config_server = Config(config_file=CONFIG_FILE) self.config_client = ConfigClient() def tearDown(self): test_helper.tearDown() self.config_client.shutdown() self.config_server.shutdown() def test_can_construct(self): self.assertTrue(self.config_client != None) def test_get_a_value(self): self.assertEqual('value0', self.config_client.get('key0')) self.assertEqual('value1', self.config_client.get('key1')) def test_get_watched_file_list(self): files = self.config_client.getWatchFiles() self.assertTrue(type(files) in [list, tuple]) self.assertEqual(files[0]['filepath'], TEST_LOG_FILE0) self.assertEqual(files[1]['filepath'], TEST_LOG_FILE1) def test_get_processor_config(self): conf = self.config_client.getProcessor('syslog0') self.assertEqual(conf['host'],'1.1.1.1')