def get_pattern(self, category_xml, namespace): patterns = self.find_all(category_xml, "pattern", namespace) if not patterns: raise ParserException("No pattern node found in category", xml_element=category_xml) elif len(patterns) > 1: raise ParserException("Multiple <pattern> nodes found in category", xml_element=category_xml) else: return patterns[0]
def get_template(self, category_xml, namespace): templates = self.find_all(category_xml, "template", namespace) if not templates: raise ParserException("No template node found in category", xml_element=category_xml) elif len(templates) > 1: raise ParserException("Multiple <template> nodes found in category", xml_element=category_xml) else: return self._template_parser.parse_template_expression(templates[0])
def test_parser_exception_xml_as_string(self): element = ET.fromstring("<template />") exception = ParserException("message", filename="test.xml", xml_exception="xml_exception_error", xml_element=element) self.assertEqual("message", exception.message) self.assertEqual("test.xml", exception.filename) self.assertEqual("xml_exception_error", exception.xml_exception) self.assertEqual(element, exception._xml_element) self.assertEqual("message in [test.xml] : xml_exception_error", exception.format_message())
def test_parser_exception_xml_as_string(self): element = ET.fromstring("<template />") exception = ParserException("message", filename="test.xml", xml_exception="xml_exception_error", xml_element=element) self.assertEqual("message", exception.message) self.assertEqual("test.xml", exception.filename) self.assertEqual("xml_exception_error", exception.xml_exception) self.assertEqual(element, exception._xml_element) self.assertEqual("message in [test.xml] : xml_exception_error", exception.format_message())
def parse_get_expression(self, expression, branch): get_node = TemplateGetNode() branch.children.append(get_node) name_found = False var_found = False if 'name' in expression.attrib: node = TemplateNode() name_node = TemplateWordNode(expression.attrib['name']) node.append(name_node) get_node.local = False name_found = True get_node.name = node if 'var' in expression.attrib: node = TemplateNode() var_node = TemplateWordNode(expression.attrib['var']) node.append(var_node) get_node.local = True var_found = True get_node.name = node for child in expression: if child.tag == 'name': node = TemplateNode() self.parse_text(self.get_text_from_element(child), node) for sub_child in child: self.parse_tag_expression(sub_child, node) self.parse_text(self.get_text_from_element(child), node) get_node.name = node get_node.local = False name_found = True elif child.tag == 'var': node = TemplateNode() self.parse_text(self.get_text_from_element(child), node) for sub_child in child: self.parse_tag_expression(sub_child, node) self.parse_text(self.get_text_from_element(child), node) get_node.name = node get_node.local = True var_found = True else: raise ParserException("Error, invalid get", xml_element=expression) if name_found is True and var_found is True: raise ParserException( "Error, get node has both name AND var values", xml_element=expression)
def parse_expression(self, graph, expression): if 'name' in expression.attrib: self._slotName = self.parse_attrib_value_as_word_node( graph, expression, 'name') if 'item' in expression.attrib: self._itemName = self.parse_attrib_value_as_word_node( graph, expression, 'item') if 'index' in expression.attrib: self._index = self.parse_attrib_value_as_word_node( graph, expression, 'index') if 'target' in expression.attrib: var_name = expression.attrib['target'] if var_name != '': self._varName = var_name if 'type' in expression.attrib: var_type = expression.attrib['type'] if var_type in self.VARIABLE_TYPE: self._varType = var_type else: raise ParserException("Invalid variable type [%s]" % var_type, xml_element=expression, nodename='nluslot') self.parse_text(graph, self.get_text_from_element(expression)) for child in expression: tagName = TextUtils.tag_from_text(child.tag) if tagName == 'name': self._slotName = self.parse_children_as_word_node(graph, child) if tagName == 'item': self._itemName = self.parse_children_as_word_node(graph, child) if tagName == 'index': self._index = self.parse_children_as_word_node(graph, child) else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) if self._slotName is None or self._itemName is None: raise ParserException("Missing either slot or item", xml_element=expression, nodename='nluslot') if self._varName is None: self._varType = None else: if self._varType is None: self._varType = 'var' if self._varType == 'var': keys = self._varName.split('.') if keys[0] == self.VAR_NLU_DATA and len(keys) == 2: self._varName = keys[0] self._keys = keys
def _parse_query(self, graph, query_name, query): subj = None pred = None obj = None for child in query: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'subj': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: YLogger.debug( self, "Variable [%s] defined in query element [%s], but not in vars!", child.text, tag_name) self.vars.append(child.text) subj = self.parse_children_as_word_node(graph, child) elif tag_name == 'pred': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: YLogger.debug( self, "Variable [%s] defined in query element [%s], but not in vars!", child.text, tag_name) self.vars.append(child.text) pred = self.parse_children_as_word_node(graph, child) elif tag_name == 'obj': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: YLogger.debug( self, "Variable [%s] defined in query element [%s], but not in vars!", child.text, tag_name) self.vars.append(child.text) obj = self.parse_children_as_word_node(graph, child) else: raise ParserException("Unknown query node [%s]" % tag_name) if subj is None: raise ParserException("<subj> element missing from select query") if pred is None: raise ParserException("<pred> element missing from select query") if obj is None: raise ParserException("<obj> element missing from select query") if query_name == "q": self._queries.append(Query(subj, pred, obj)) else: self._queries.append(NotQuery(subj, pred, obj))
def set_attrib(self, attrib_name, attrib_value): if attrib_name != 'level': raise ParserException("Invalid attribute name %s for this node", attrib_name) if attrib_value not in ['debug', 'info', 'warning', 'error']: raise ParserException( "Invalid attribute value %s for this node %s", attrib_value, attrib_name) self._level = attrib_value
def can_add(self, new_node): if isinstance(new_node, PatternRootNode): raise ParserException("Cannot add root node to template node") if isinstance(new_node, PatternTopicNode): raise ParserException("Cannot add topic node to template node") if isinstance(new_node, PatternThatNode): raise ParserException("Cannot add that node to template node") if isinstance(new_node, PatternTemplateNode): raise ParserException("Cannot add template node to template node")
def can_add(self, new_node): if new_node.is_root(): raise ParserException("Cannot add root node to template node") elif new_node.is_topic(): raise ParserException("Cannot add topic node to template node") elif new_node.is_that(): raise ParserException("Cannot add that node to template node") elif new_node.is_template(): raise ParserException("Cannot add template node to template node")
def parse_expression(self, graph, expression): self._parse_node_with_attrib(graph, expression, "index", "1") if self._index == 0: raise ParserException("index values are 1 based, cannot be 0", nodename='response') if self.children: raise ParserException("Node should not contain child text", xml_element=expression, nodename='response')
def can_add(self, new_node: PatternNode): if new_node.is_root(): raise ParserException("Cannot add root node to existing root node") if new_node.is_topic(): raise ParserException("Cannot add topic node to root node") if new_node.is_that(): raise ParserException("Cannot add that node to root node") if new_node.is_template(): raise ParserException("Cannot add template node to root node")
def can_add(self, new_node): if new_node.is_root(): raise ParserException("Cannot add root node to that node", nodename='that') if new_node.is_topic(): raise ParserException("Cannot add topic node to that node", nodename='that') if new_node.is_that(): raise ParserException("Cannot add that node to that node", nodename='that')
def __init__(self, attribs, text, userid='*', element=None): PatternNode.__init__(self, userid) if 'name' in attribs: self._set_name = attribs['name'].upper() if self._set_name == '': raise ParserException("No name specified as attribute or text", xml_element=element, nodename='set(pattern)') elif text: self._set_name = text.upper() else: raise ParserException("No name specified as attribute or text", xml_element=element, nodename='set(pattern)')
def parse_expression(self, graph, expression): mode_count = 0 if 'name' in expression.attrib: mode_count += 1 self.name = self.parse_attrib_value_as_word_node( graph, expression, 'name') self.property_type = 'name' if 'data' in expression.attrib: mode_count += 1 self.name = self.parse_attrib_value_as_word_node( graph, expression, 'data') self.property_type = 'data' if 'var' in expression.attrib: mode_count += 1 self.name = self.parse_attrib_value_as_word_node( graph, expression, 'var') self.property_type = 'var' self.parse_text(graph, self.get_text_from_element(expression)) for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'name': mode_count += 1 self.name = self.parse_children_as_word_node(graph, child) self.property_type = 'name' elif tag_name == 'data': mode_count += 1 self.name = self.parse_children_as_word_node(graph, child) self.property_type = 'data' elif tag_name == 'var': mode_count += 1 self.name = self.parse_children_as_word_node(graph, child) self.property_type = 'var' else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) if mode_count == 0: raise ParserException("Missing variable type", xml_element=expression, nodename='set') elif mode_count > 1: raise ParserException("Node has mulitple variable types", xml_element=expression, nodename='set')
def parse_expression(self, graph, expression): super(TemplateAddTripleNode, self).parse_expression(graph, expression) if self._subj is None: raise ParserException("<%s> node missing subject attribue/element" % self.node_name) if self._pred is None: raise ParserException("<%s> node missing predicate attribue/element" % self.node_name) if self._obj is None: raise ParserException("<%s> node missing object attribue/element" % self.node_name)
def get_template(self, category_xml): templates = category_xml.findall('template') if len(templates) == 0: raise ParserException("Error, no template node found in category", xml_element=category_xml) elif len(templates) > 1: raise ParserException( "Error, multiple <template> nodes found in category", xml_element=category_xml) else: return self.template_parser.parse_template_expression(templates[0])
def get_pattern(self, category_xml): patterns = category_xml.findall('pattern') if len(patterns) == 0: raise ParserException("Error, no pattern node found in category", xml_element=category_xml) elif len(patterns) > 1: raise ParserException( "Error, multiple <pattern> nodes found in category", xml_element=category_xml) else: return patterns[0]
def set_attrib(self, attrib_name, attrib_value): if attrib_name != 'index': raise ParserException("Invalid attribute name [%s] for this node" % (attrib_name)) if isinstance(attrib_value, int): self._index = attrib_value else: try: self._index = int(attrib_value) except: raise ParserException("None numeric format [%s] for this node [%s]", attrib_value, attrib_name)
def parse_type2_condition(self, graph, expression): for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name in ['name', 'data', 'var', 'bot']: pass elif tag_name == 'li': list_item = TemplateConditionListItemNode() _, var_type = self.get_condition_name(graph, child) if var_type != TemplateConditionVariable.DEFAULT: raise ParserException("Invalid li variable", xml_element=expression, nodename='condition') value = self.get_condition_value(graph, child) regex = self.get_condition_regex(graph, child) if regex is not None: if value is not None: raise ParserException("Value and regex elements exist", xml_element=expression, nodename='condition') list_item._regex = regex else: list_item._value = value list_item._var_type = self._var_type self.children.append(list_item) list_item.parse_text(graph, self.get_text_from_element(child)) for sub_pattern in child: if sub_pattern.tag in [ 'name', 'data', 'var', 'bot', 'value', 'regex' ]: pass elif sub_pattern.tag == 'loop': list_item.loop = True else: graph.parse_tag_expression(sub_pattern, list_item) tail_text = self.get_tail_from_element(sub_pattern) list_item.parse_text(graph, tail_text) else: raise ParserException("Invalid element <%s>" % tag_name, xml_element=expression, nodename='condition')
def check_aiml_tag(self, aiml, filename=None): # Null check just to be sure if aiml is None: raise ParserException("Null root tag", filename=filename) tag_name, namespace = self.tag_and_namespace_from_text(aiml.tag) # Then if check is just <aiml>, thats OK if tag_name != 'aiml': raise ParserException("Root tag is not <aiml>", filename=filename) return tag_name, namespace
def test_duplicate_grammar_exception_xml(self): element = ET.fromstring("<template />") xml_exception = ET.ParseError() xml_exception.position = [] xml_exception.position.append(1) xml_exception.position.append(2) exception = ParserException("message", filename="test.xml", xml_exception=xml_exception, xml_element=element) self.assertEqual("message", exception.message) self.assertEqual("test.xml", exception.filename) self.assertEqual(xml_exception, exception.xml_exception) self.assertEqual(element, exception._xml_element) self.assertEqual("message in [test.xml] at [line(1), column(2)]", exception.format_message())
def parse_expression(self, graph, expression): try: self._parse_node_with_attrib(graph, expression, "index", "1") except ParserException as excep: excep.nodename = 'topicstar' raise if self._index <= 0: raise ParserException("index values are 1 based, cannot be <= 0", nodename='topicstar') if self.children: raise ParserException("Node should not contain child text", xml_element=expression, nodename='topicstar')
def parse_expression(self, graph, expression): super(TemplateDeleteTripleNode, self).parse_expression(graph, expression) if self._subj is None: raise ParserException("Missing subject attribue/element", xml_element=expression, nodename='deletetriple') if self._pred is None: YLogger.debug(self, "Deletetriple missing predicate attribue/element. Delete <subj>") if self._obj is None: YLogger.debug(self, "Deletetriple missing object attribue/element. Delete <pred>") elif self._pred is None: raise ParserException("Missing predicate attribue/element", xml_element=expression, nodename='deletetriple')
def parse_query(self, graph, query_name, query): for child in query: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'subj': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "Variable [%s] defined in query element [%s], but not in vars!" % (child.text, tag_name)) self.vars.append(child.text) subj = self.parse_children_as_word_node(graph, child) elif tag_name == 'pred': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "Variable [%s] defined in query element [%s], but not in vars!" % (child.text, tag_name)) self.vars.append(child.text) pred = self.parse_children_as_word_node(graph, child) elif tag_name == 'obj': if child.text is not None and child.text.startswith("?"): if child.text not in self.vars: if logging.getLogger().isEnabledFor(logging.DEBUG): logging.debug( "Variable [%s] defined in query element [%s], but not in vars!" % (child.text, tag_name)) self.vars.append(child.text) obj = self.parse_children_as_word_node(graph, child) else: if logging.getLogger().isEnabledFor(logging.WARNING): logging.warning("Unknown tag name [%s] in select query", tag_name) if subj is None: raise ParserException("<subj> element missing from select query") if pred is None: raise ParserException("<pred> element missing from select query") if obj is None: raise ParserException("<obj> element missing from select query") if query_name == "q": self._queries.append(Query(subj, pred, obj)) else: self._queries.append(NotQuery(subj, pred, obj))
def parse_topic(self, topic_element, namespace, filename=None): if 'name' in topic_element.attrib: name = topic_element.attrib['name'] if name is None or not name: raise ParserException("Topic name empty or null", xml_element=topic_element) xml = "<topic>%s</topic>" % name YLogger.debug(self, "Topic attrib converted to %s", xml) topic_pattern = ET.fromstring(xml) else: raise ParserException("Missing name attribute for topic", xml_element=topic_element) category_found = False num_category = 0 for child in topic_element: tag_name, _ = self.tag_and_namespace_from_text(child.tag) if tag_name == 'category': if hasattr(child, "_start_line_number"): start_line = str(child._start_line_number) else: start_line = 0 if hasattr(child, "_end_line_number"): end_line = str(child._end_line_number) else: end_line = 0 if self._num_categories >= self.brain.bot.configuration.max_categories: self._category_over = True raise ParserException( ("Max categories [%d] exceeded" % self.brain.bot.configuration.max_categories), xml_element=child) parsed = self.parse_category(child, namespace, topic_pattern) if parsed[4] is not None: parsed[4].set_fileInfo(filename, start_line, end_line) category_found = True num_category += 1 else: raise ParserException("Unknown child node of topic, %s" % child.tag, xml_element=topic_element) if category_found is False: raise ParserException("No categories in topic", xml_element=topic_element) return num_category
def __init__(self, attribs, text, userid='*', element=None, brain=None): PatternNode.__init__(self, userid) self._pattern_text = None self._pattern_template = None self._pattern = None self._is_form = False self._is_NotOption = False if 'pattern' in attribs: self._pattern_text = attribs['pattern'] elif 'template' in attribs: name = attribs['template'] if name == '': raise ParserException("Specified Template Parameter is empty", xml_element=element, nodename='regex') template_name = name.upper() if brain is not None: if brain.regex_templates.has_regex(template_name) is False: raise ParserException("Template[%s] not found" % template_name, xml_element=element, nodename='regex') self._pattern_template = template_name elif 'form' in attribs: form_text = attribs['form'] if form_text.count('(?!') > 0: self._is_NotOption = True self._is_form = True self._pattern_text = form_text elif text: self._pattern_text = text else: raise ParserException( "No Parameter specified as attribute or text", xml_element=element, nodename='regex') if self._pattern_text is not None: if self._pattern_text == '': raise ParserException("Specified Parameter is empty", xml_element=element, nodename='regex') try: self._pattern = re.compile(self._pattern_text, re.IGNORECASE) except Exception: raise ParserException( "invalid regex expression (unable re.compile)", xml_element=element, nodename='regex')
def parse_expression(self, graph, expression): li_found = False for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'li': li_found = True li_node = graph.get_base_node() self.children.append(li_node) li_node.parse_template_node(graph, child) else: raise ParserException("Unsupported random child tag: %s" % (tag_name), xml_element=expression) if li_found is False: raise ParserException("No li children of random element!", xml_element=expression)
def parse_expression(self, graph, expression): for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'category': parsed = graph.aiml_parser.parse_category(child, namespace=None, topic_element=None, add_to_graph=False) learn_category = LearnCategory(parsed[0], parsed[1], parsed[2], parsed[3]) self.children.append(learn_category) elif tag_name == 'topic': raise ParserException("Not supported yet") else: raise ParserException("Invalid tag [%s] found in <learn>"%tag_name)
def parse_expression(self, graph, expression): subject = None predicate = None object = None if 'subj' in expression.attrib: subject = graph.get_word_node(expression.attrib['subj']) if 'pred' in expression.attrib: predicate = graph.get_word_node(expression.attrib['pred']) if 'obj' in expression.attrib: object = graph.get_word_node(expression.attrib['obj']) head_text = self.get_text_from_element(expression) self.parse_text(graph, head_text) for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'subj': subject = self.parse_children_as_word_node(graph, child) elif tag_name == 'pred': predicate = self.parse_children_as_word_node(graph, child) elif tag_name == 'obj': object = self.parse_children_as_word_node(graph, child) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if subject is None: raise ParserException( "<%s> node missing subject attribue/element" % self.node_name) if predicate is None: raise ParserException( "<%s> node missing predicate attribue/element" % self.node_name) if object is None: raise ParserException("<%s> node missing object attribue/element" % self.node_name) self._entity = RDFEntity(subject=subject, predicate=predicate, object=object)
def parse_expression(self, graph, expression): if 'text' in expression.attrib: self._text = graph.get_word_node(expression.attrib['text']) if 'url' in expression.attrib: self._url = graph.get_word_node(expression.attrib['url']) if 'postback' in expression.attrib: self._postback = graph.get_word_node(expression.attrib['postback']) head_text = self.get_text_from_element(expression) self.parse_text(graph, head_text) for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'text': self._text = self.parse_children_as_word_node(graph, child) elif tag_name == 'url': self._url = self.parse_children_as_word_node(graph, child) elif tag_name == 'postback': self._postback = self.parse_children_as_word_node(graph, child) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self._text is None: raise ParserException("Missing text element", xml_element=expression, nodename='button')
def test_parser_exception_basic(self): exception = ParserException("message") self.assertEqual("message", exception.message) exception.filename = "test.xml" self.assertEqual("test.xml", exception.filename) self.assertEqual("message in [test.xml]", exception.format_message())