def load_security_services(self, brain_configuration): if brain_configuration.security is not None: if brain_configuration.security.authentication is not None: if brain_configuration.security.authentication.classname is not None: try: classobject = ClassLoader.instantiate_class( brain_configuration.security.authentication. classname) self._authentication = classobject( brain_configuration.security.authentication) except Exception as excep: logging.exception(excep) else: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("No authentication configuration defined") if brain_configuration.security.authorisation is not None: if brain_configuration.security.authorisation.classname is not None: try: classobject = ClassLoader.instantiate_class( brain_configuration.security.authorisation. classname) self._authorisation = classobject( brain_configuration.security.authorisation) except Exception as excep: logging.exception(excep) else: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("No authorisation configuration defined") else: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "No security configuration defined, running open...")
def load_security_services(self, configuration): if configuration.security is not None: if configuration.security.authentication is not None: if configuration.security.authentication.classname is not None: try: classobject = ClassLoader.instantiate_class( configuration.security.authentication.classname) self._authentication = classobject( configuration.security.authentication) except Exception as excep: YLogger.exception(self, excep) else: YLogger.debug(self, "No authentication configuration defined") if configuration.security.authorisation is not None: if configuration.security.authorisation.classname is not None: try: classobject = ClassLoader.instantiate_class( configuration.security.authorisation.classname) self._authorisation = classobject( configuration.security.authorisation) except Exception as excep: YLogger.exception(self, excep) else: YLogger.debug(self, "No authorisation configuration defined") else: YLogger.debug( self, "No security configuration defined, running open...")
def load_security_services(self, configuration): if configuration.security is not None: if configuration.security.authentication is not None: if configuration.security.authentication.classname is not None: try: classobject = ClassLoader.instantiate_class( configuration.security.authentication.classname) self._authentication = classobject(configuration.security.authentication) except Exception as excep: YLogger.exception(self, "Failed to load security services", excep) else: YLogger.debug(self, "No authentication configuration defined") if configuration.security.authorisation is not None: if configuration.security.authorisation.classname is not None: try: classobject = ClassLoader.instantiate_class( configuration.security.authorisation.classname) self._authorisation = classobject(configuration.security.authorisation) except Exception as excep: YLogger.exception(self, "Failed to instatiate authorisation class", excep) else: YLogger.debug(self, "No authorisation configuration defined") else: YLogger.debug(self, "No security configuration defined, running open...")
def load_security_services(self, client): if self._configuration is not None: if self._configuration.authentication is not None: if self._configuration.authentication.classname is not None: try: classobject = ClassLoader.instantiate_class( self._configuration.authentication.classname) self._authentication = classobject( self._configuration.authentication) self._authentication.initialise(client) except Exception as excep: YLogger.exception(self, "Failed to load security services", excep) else: YLogger.debug(self, "No authentication configuration defined") if self._configuration.authorisation is not None: if self._configuration.authorisation.classname is not None: try: classobject = ClassLoader.instantiate_class( self._configuration.authorisation.classname) self._authorisation = classobject( self._configuration.authorisation) self._authorisation.initialise(client) except Exception as excep: YLogger.exception( self, "Failed to instatiate authorisation class", excep) else: YLogger.debug(self, "No authorisation configuration defined") else: YLogger.debug( self, "No security configuration defined, running open...")
def initiate_sentiment_analyser(sentiment_config): if sentiment_config.classname is not None: analyser = None scores = None try: YLogger.debug(None, "Loading sentiment analyser from class [%s]", sentiment_config.classname) sentiment_class = ClassLoader.instantiate_class(sentiment_config.classname) analyser = sentiment_class() analyser.initialise() except Exception as excep: YLogger.exception(None, "Failed to initiate sentiment analyser", excep) try: YLogger.debug(None, "Loading sentiment scores from class [%s]", sentiment_config.classname) scores_class = ClassLoader.instantiate_class(sentiment_config.scores) scores = scores_class() except Exception as excep: YLogger.exception(None, "Failed to initiate sentiment analyser", excep) return analyser, scores else: YLogger.debug(None, "No configuration setting for sentiment analyser!") return None, None
def setUp(self): bot_config = BotConfiguration() bot = Bot(bot_config, TestClient()) bot.brain.configuration.debugfiles._save_errors = True bot.brain.configuration.debugfiles._save_duplicates = True bot.brain.template_factory.add_node( "base", ClassLoader.instantiate_class( "programy.parser.template.nodes.base.TemplateNode")) bot.brain.template_factory.add_node( "word", ClassLoader.instantiate_class( "programy.parser.template.nodes.word.TemplateWordNode")) bot.brain.pattern_factory.add_node( "oneormore", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.oneormore.PatternOneOrMoreWildCardNode" )) bot.brain.pattern_factory.add_node( "topic", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.topic.PatternTopicNode")) bot.brain.pattern_factory.add_node( "that", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.that.PatternThatNode")) bot.brain.pattern_factory.add_node( "template", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.template.PatternTemplateNode")) bot.brain.pattern_factory.add_node( "bot", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.bot.PatternBotNode")) bot.brain.pattern_factory.add_node( "set", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.set.PatternSetNode")) bot.brain.pattern_factory.add_node( "nlu", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.nlu.PatternNluNode")) bot.brain.pattern_factory.add_node( "word", ClassLoader.instantiate_class( "programy.parser.pattern.nodes.word.PatternWordNode")) self.parser = bot.brain.aiml_parser self.parser.create_debug_storage() self.assertIsNotNone(self.parser)
def load_tokenizer(configuration): if configuration is not None and configuration.tokenizer.classname is not None: YLogger.debug(None, "Loading tokenizer from class [%s]", configuration.tokenizer.classname) tokenizer_class = ClassLoader.instantiate_class(configuration.tokenizer.classname) return tokenizer_class(configuration.tokenizer.split_chars, configuration.tokenizer.punctuation_chars, configuration.tokenizer.before_concatenation_rule, configuration.tokenizer.after_concatenation_rule) else: tokenizer_class = ClassLoader.instantiate_class('programy.dialog.tokenizer.tokenizer_jp.TokenizerJP') return tokenizer_class(configuration.tokenizer.split_chars, configuration.tokenizer.punctuation_chars, configuration.tokenizer.before_concatenation_rule, configuration.tokenizer.after_concatenation_rule)
def load_tokenizer(self): if self.configuration is not None and self.configuration.tokenizer.classname is not None: YLogger.info(self, "Loading tokenizer from class [%s]", self.configuration.tokenizer.classname) tokenizer_class = ClassLoader.instantiate_class(self.configuration.tokenizer.classname) return tokenizer_class(self.configuration.tokenizer.split_chars) else: return Tokenizer(self.configuration.tokenizer.split_chars)
def load(self, collector, name=None): nodes = self.get_all_nodes() for node in nodes: try: collector.add_node(node.name, ClassLoader.instantiate_class(node.node_class)) except Exception as e: YLogger.exception(self, "Failed pre-instantiating %s Node [%s]", e, collector.type, node.node_class)
def get_root_node(self): try: root_class = self.new_node_class('root') return root_class() except: return ClassLoader.instantiate_class( "programy.parser.pattern.nodes.root.PatternRootNode")()
def load(self, collector, name=None): processors = self.get_all_processors() for processor in processors: try: collector.add_processor(ClassLoader.instantiate_class(processor.classname)()) except Exception as e: YLogger.exception(self, "Failed pre-instantiating Processor [%s]", e, processor.classname)
def _load_file_contents(self, processor_collection, filename): YLogger.debug(self, "Loading processors from file [%s]", filename) count = 0 line_no = 0 try: with open(filename, "r", encoding="utf-8") as file: for line in file: line_no += 1 line = line.strip() if line: if line[0] != '#': class_name = line.split() try: new_class = ClassLoader.instantiate_class( class_name[0]) processor_collection.add_processor(new_class()) count += 1 except Exception: error_info = "processor [%s] not found" % line processor_collection.set_error_info( filename, line_no, error_info) except FileNotFoundError: YLogger.error(self, "File not found [%s]", filename) return count
def load_brain_selector(self, configuration): if configuration.brain_selector is None: self._brain_selector = DefaultBrainSelector(configuration) else: try: self._brain_selector = ClassLoader.instantiate_class(configuration.brain_selector)(configuration) except Exception as e: self._brain_selector = DefaultBrainSelector(configuration)
def load_brain_selector(self, configuration): if configuration.brain_selector is None: self._brain_selector = DefaultBrainSelector(configuration, self._brains) else: try: self._brain_selector = ClassLoader.instantiate_class(configuration.brain_selector)(configuration, self._brains) except Exception as e: self._brain_selector = DefaultBrainSelector(configuration, self._brains)
def _load_account_linking_class(self, client): if self._configuration.account_linker is not None: if self._configuration.account_linker.classname is not None: classobject = ClassLoader.instantiate_class( self._configuration.account_linker.classname) self._account_linker = classobject( self._configuration.account_linker) self.account_linker.initialise(client)
def load_oob_processors(self, configuration): if configuration.oob is not None: if configuration.oob.default() is not None: try: YLogger.info(self, "Loading default oob") classobject = ClassLoader.instantiate_class(configuration.oob.default().classname) self._default_oob = classobject() except Exception as excep: YLogger.exception(self, "Failed to load OOB Processor", excep) for oob_name in configuration.oob.oobs(): try: YLogger.info(self, "Loading oob: %s", oob_name) classobject = ClassLoader.instantiate_class(configuration.oob.oob(oob_name).classname) self._oob[oob_name] = classobject() except Exception as excep: YLogger.exception(self, "Failed to load OOB", excep)
def load_bot_selector(self, configuration): if configuration.bot_selector is None: self._bot_selector = DefaultBotSelector(configuration) else: try: self._bot_selector = ClassLoader.instantiate_class(configuration.bot_selector)(configuration) except Exception: self._bot_selector = DefaultBotSelector(configuration)
def _load_authorisation_class(self, client): if self._configuration.authorisation is not None: if self._configuration.authorisation.classname is not None: classobject = ClassLoader.instantiate_class( self._configuration.authorisation.classname) self._authorisation = classobject( self._configuration.authorisation) self._authorisation.initialise(client)
def preload_services(cls, service_configs): loader = ClassLoader() for service_config in service_configs: name = service_config.name.upper() logging.debug("Preloading service [%s] -> [%s]", name, service_config.path) meta_class = loader.instantiate_class(service_config.path) new_class = meta_class(service_config) ServiceFactory.services[name] = new_class
def load_tokenizer(self): if self._configuration is not None and self._configuration.tokenizer.classname is not None: if logging.getLogger().isEnabledFor(logging.INFO): logging.info("Loading tokenizer from class [%s]", self._configuration.tokenizer.classname) tokenizer_class = ClassLoader.instantiate_class(self._configuration.tokenizer.classname) return tokenizer_class(self._configuration.tokenizer.split_chars) else: return Tokenizer(self._configuration.tokenizer.split_chars)
def preload_services(cls, services_config): loader = ClassLoader() for service_name in services_config.services(): name = service_name.upper() service_config = services_config.service(service_name) YLogger.debug(None, "Preloading service [%s] -> [%s]", name, service_config.classname) meta_class = loader.instantiate_class(service_config.classname) new_class = meta_class(service_config) ServiceFactory.services[name] = new_class
def load_renderer(self): try: if self.get_client_configuration().renderer is not None: clazz = ClassLoader.instantiate_class(self.get_client_configuration().renderer.renderer) return clazz(self) except Exception as e: YLogger.exception(None, "Failed to load config specified renderer", e) return self.get_default_renderer()
def add_trigger(self, event: str, classname: str): try: trigger = ClassLoader.instantiate_class(classname)() if event not in self._triggers: self._triggers[event] = [] self._triggers[event].append(trigger) except Exception as e: YLogger.exception(self, "Failed to add trigger [%s] -> [%s]", e, event, classname)
def load(self, node_factory): YLogger.debug(self, "Loading %s nodes from Mongo", self.collection_name()) nodes = self.get_all_nodes() for node in nodes: try: node_factory.add_node(node['name'], ClassLoader.instantiate_class(node['node_class'])) except Exception as e: YLogger.exception(self, "Failed pre-instantiating %s Node [%s]", e, node_factory.type, node['node_class'])
def load(self, handler, name=None): oobs = self.get_all_oobs() for oob in oobs: try: handler.add_oob(oob.name, ClassLoader.instantiate_class(oob.oob_class)()) except Exception as e: YLogger.exception(self, "Failed pre-instantiating OOB [%s]", e, oob.oob_class)
def test_instantiate_module_class(self): loader = ClassLoader() self.assertIsNotNone(loader) meta_class = loader.instantiate_class("test.utils.classes.testclass.TestClass") self.assertIsNotNone(meta_class) new_class = meta_class() self.assertIsNotNone(new_class) self.assertTrue(new_class.test_method())
def test_instantiate_module_class(self): loader = ClassLoader() self.assertIsNotNone(loader) meta_class = loader.instantiate_class("test.testclass.TestClass") self.assertIsNotNone(meta_class) new_class = meta_class() self.assertIsNotNone(new_class) self.assertTrue(new_class.test_method())
def resolve_to_string(self, client_context): data = self.resolve_children_to_string(client_context) new_class = ClassLoader.instantiate_class(self._path) instance = new_class() resolved = instance.execute(client_context, data) YLogger.debug(client_context, "[%s] resolved to [%s]", self.to_string(), resolved) return resolved
def preload_services(cls, services_config): loader = ClassLoader() for service_name in services_config.services(): name = service_name.upper() service_config = services_config.service(service_name) if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("Preloading service [%s] -> [%s]", name, service_config.classname) meta_class = loader.instantiate_class(service_config.classname) new_class = meta_class(service_config) ServiceFactory.services[name] = new_class
def load(self, collector, name=None): YLogger.info(self, "Loading %s triggers from Mongo", self.collection_name()) triggers = self.get_all_triggers() for trigger in triggers: try: collector.add_trigger(trigger['name'], ClassLoader.instantiate_class(trigger['trigger_class'])()) except Exception as excep: YLogger.exception(self, "Failed pre-instantiating Trigger [%s]", excep, trigger['trigger_class'])
def load(self, filename, *args, **kw): logging.debug("Loading processors from file [%s]", filename) count = 0 with open(filename, "r+") as file: for line in file: new_class = ClassLoader.instantiate_class(line) if new_class is not None: self.processors.append(new_class(*args, **kw)) count += 1 return count
def load(self, collector, name=None): YLogger.info(self, "Loading %s nodes from Mongo", self.collection_name()) nodes = self.get_all_nodes() for node in nodes: try: collector.add_processor(ClassLoader.instantiate_class(node['classname'])()) except Exception as excep: YLogger.exception(self, "Failed pre-instantiating Processor [%s]", excep, node['classname'])
def load_oob_processors(self, brain_configuration): if brain_configuration.oob is not None: if brain_configuration.oob.default() is not None: try: if logging.getLogger().isEnabledFor(logging.INFO): logging.info("Loading default oob") classobject = ClassLoader.instantiate_class(brain_configuration.oob.default().classname) self._default_oob = classobject() except Exception as excep: logging.exception(excep) for oob_name in brain_configuration.oob.oobs(): try: if logging.getLogger().isEnabledFor(logging.INFO): logging.info("Loading oob: %s", oob_name) classobject = ClassLoader.instantiate_class(brain_configuration.oob.oob(oob_name).classname) self._oob[oob_name] = classobject() except Exception as excep: logging.exception(excep)
def resolve_to_string(self, bot, clientid): data = self.resolve_children_to_string(bot, clientid) new_class = ClassLoader.instantiate_class(self._path) instance = new_class() resolved = instance.execute(bot, clientid, data) if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug("[%s] resolved to [%s]", self.to_string(), resolved) return resolved
def initiate_spellchecker(self): # TODO Move this to Spelling bass class if self.configuration is not None: if self.configuration.spelling.classname is not None: try: YLogger.info(self, "Loading spelling checker from class [%s]", self.configuration.spelling.classname) spell_class = ClassLoader.instantiate_class(self.configuration.spelling.classname) self._spell_checker = spell_class(self.configuration.spelling) except Exception as excep: YLogger.exception(self, "Failed to initiate spellcheker", excep) else: YLogger.warning(self, "No configuration setting for spelling checker!")
def process_config_line(self, line): if self.valid_config_line(line): splits = line.split("=") node_name = splits[0].strip() if node_name in self._nodes_config: YLogger.error(self, "Node already exists in config [%s]", line) return class_name = splits[1].strip() YLogger.debug(self, "Pre-instantiating %s Node [%s]", self._type, class_name) try: self._nodes_config[node_name] = ClassLoader.instantiate_class(class_name) except Exception as e: YLogger.exception(self, "Failed pre-instantiating %s Node [%s]"%(self._type, class_name), e)
def resolve(self, bot, clientid): try: data = self.resolve_children_to_string(bot, clientid) new_class = ClassLoader.instantiate_class(self._path) if new_class is not None: instance = new_class() resolved = instance.execute(data) logging.debug("[%s] resolved to [%s]", self.to_string(), resolved) return resolved except Exception as excep: logging.exception(excep) logging.error("Extension [%s] failed to execute", self._path) return ""
def add_dynamic_set(self, name, classname, config_file): self._dynamic_sets[name.upper()] = (ClassLoader.instantiate_class(classname))(config_file)