def load_from_file(self, filename, client_configuration, bot_root): configuration = ProgramyConfiguration(client_configuration) with open(filename, 'r+') as xml_data_file: tree = ET.parse(xml_data_file, parser=LineNumberingParser()) self.xml_data = tree.getroot() configuration.load_config_data(self, bot_root) return configuration
def parse_from_file(self, filename): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ self._filename = filename logging.info("Loading aiml file file: " + self._filename) tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() if aiml is None or aiml.tag != 'aiml': raise ParserException("Error, root tag is not <aiml>", filename=filename) else: try: self.parse_aiml(aiml, filename) except ParserException as parser_excep: parser_excep.filename = filename raise parser_excep except ET.ParseError as xmlpe: xmlpe.filename = filename xmlpe.xml_exception = xmlpe
def parse_from_file(self, filename): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ YLogger.info(self, "Loading aiml file: " + filename) try: tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() _, namespace = self.check_aiml_tag(aiml, filename=filename) start = datetime.datetime.now() num_categories = self.parse_aiml(aiml, namespace, filename) stop = datetime.datetime.now() diff = stop - start YLogger.info(self, "Processed %s with %d categories in %f.2 secs", filename, num_categories, diff.total_seconds()) except Exception as excep: YLogger.exception( self, "Failed to load contents of AIML file from [%s]" % filename, excep)
def parse_from_file(self, filename, userid="*"): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ YLogger.debug(self, "Loading aiml file: " + filename) try: tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() _, namespace = AIMLParser.check_aiml_tag(aiml, filename=filename) start = datetime.datetime.now() num_categories = self.parse_aiml(aiml, namespace, filename, userid=userid) stop = datetime.datetime.now() diff = stop - start YLogger.debug(self, "Processed %s with %d categories in %.6f secs", filename, num_categories, diff.total_seconds()) except Exception as excep: YLogger.exception( self, "Failed to load contents of AIML file from [%s]", excep, filename) parser_excep = ParserException( "Failed to load contents of AIML file : XML-Parser Exception [" + str(excep) + "]") self.handle_aiml_error(parser_excep, filename, None)
def parse_from_file(self, filename): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ logging.info("Loading aiml file: " + filename) try: #print("Parsing raw XML Tree") tree = ET.parse(filename, parser=LineNumberingParser()) #print("Parsed...") aiml = tree.getroot() if aiml is None or aiml.tag != 'aiml': raise ParserException("Error, root tag is not <aiml>", filename=filename) else: #print("Parsing loaded AIML tree") #start = datetime.datetime.now() self.parse_aiml(aiml, filename) #stop = datetime.datetime.now() #diff = stop - start #print("Parsed in %f sec"%(diff.total_seconds())) except Exception as e: logging.exception(e) logging.error( "Failed to load contents of AIML file from [%s] - [%s]" % (filename, e))
def parse_from_file(self, filename): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ logging.info("Loading aiml file: " + filename) try: tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() if aiml is None or aiml.tag != 'aiml': raise ParserException("Error, root tag is not <aiml>", filename=filename) else: start = datetime.datetime.now() num_categories = self.parse_aiml(aiml, filename) stop = datetime.datetime.now() diff = stop - start logging.info("Processed %s with %d categories in %f.2 secs" % (filename, num_categories, diff.total_seconds())) except Exception as e: logging.exception(e) logging.error( "Failed to load contents of AIML file from [%s] - [%s]" % (filename, e))
def parse_from_file(self, filename): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ if logging.getLogger().isEnabledFor(logging.INFO): logging.info("Loading aiml file: " + filename) try: tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() tag_name, namespace = self.check_aiml_tag(aiml, filename=filename) start = datetime.datetime.now() num_categories = self.parse_aiml(aiml, namespace, filename) stop = datetime.datetime.now() diff = stop - start if logging.getLogger().isEnabledFor(logging.INFO): logging.info("Processed %s with %d categories in %f.2 secs" % (filename, num_categories, diff.total_seconds())) except Exception as e: logging.exception(e) if logging.getLogger().isEnabledFor(logging.ERROR): logging.error( "Failed to load contents of AIML file from [%s] - [%s]" % (filename, e))
def test_working_xml(self): tree = ET.parse(os.path.dirname(__file__)+ os.sep + "working.xml", parser=LineNumberingParser()) aiml = tree.getroot() self.assertEqual(28, aiml._end_line_number) self.assertEqual(0, aiml._end_column_number) patterns = aiml.findall('category') self.assertEqual(1, len(patterns)) self.assertEqual(26, patterns[0]._end_line_number) self.assertEqual(4, patterns[0]._end_column_number)
def load_from_file(self, filename, client_configuration, bot_root, subs: Substitutions = None): configuration = ProgramyConfiguration(client_configuration) try: with open(filename, 'r+', encoding="utf-8") as xml_data_file: tree = ET.parse(xml_data_file, parser=LineNumberingParser()) self.xml_data = tree.getroot() configuration.load_config_data(self, bot_root, subs) except Exception as excep: YLogger.exception(self, "Failed to open xml config file [%s]", excep, filename) return configuration
def upload_from_file(self, filename, format=Store.XML_FORMAT, commit=True, verbose=False): count = 0 success = 0 try: groupname = self.get_just_filename_from_filepath(filename) print(groupname) tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() for expression in aiml: tag_name, namespace = AIMLParser.tag_and_namespace_from_text( expression.tag) if tag_name == 'topic': topic = expression.attrib['name'] for topic_expression in expression: that = self.find_element_str("that", topic_expression, namespace) pattern = self.find_element_str( "pattern", topic_expression, namespace) template = self.find_element_str( "template", topic_expression, namespace) if self.store_category(groupname, "*", topic, that, pattern, template) is True: success += 1 count += 1 elif tag_name == 'category': topic = self.find_element_str("topic", expression, namespace) that = self.find_element_str("that", expression, namespace) pattern = self.find_element_str("pattern", expression, namespace) template = self.find_element_str("template", expression, namespace) if self.store_category(groupname, "*", topic, that, pattern, template) is True: success += 1 count += 1 except Exception as excep: YLogger.exception( self, "Failed to load contents of AIML file from [%s]", excep, filename) return count, success
def parse_from_file(self, filename, userid="*"): YLogger.info(self, "Loading aiml file: " + filename) try: tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() _, namespace = AIMLParser.check_aiml_tag(aiml, filename=filename) start = datetime.datetime.now() num_categories = self.parse_aiml(aiml, namespace, filename, userid=userid) stop = datetime.datetime.now() diff = stop - start YLogger.info(self, "Processed %s with %d categories in %f.2 secs", filename, num_categories, diff.total_seconds()) return True except Exception as excep: YLogger.exception(self, "Failed to load contents of AIML file from [%s]", excep, filename) return False
def parse_from_file(self, filename): """ Parse an AIML file and return all the cateogeries found in the file :param filename: Name of file to parse :return list of categories parsed from file: """ self._filename = filename logging.info("Loading aiml file file: " + self._filename) try: tree = ET.parse(filename, parser=LineNumberingParser()) aiml = tree.getroot() if aiml is None or aiml.tag != 'aiml': raise ParserException("Error, root tag is not <aiml>", filename=filename) else: self.parse_aiml(aiml, filename) except Exception as e: logging.error( "Failed to load contents of AIML file from [%s] - [%s]" % (filename, e))
def test_broken_xml(self): with self.assertRaises(ET.ParseError) as raised: ET.parse(os.path.dirname(__file__) + "/broken.xml", parser=LineNumberingParser()) self.assertEqual(22, raised.exception.position[0]) self.assertEqual(10, raised.exception.position[1])
def load_from_file(self, filename): with open(filename, 'r+') as xml_data_file: tree = ET.parse(xml_data_file, parser=LineNumberingParser()) self.xml_data = tree.getroot() self.client_config.load_config_data(self)