def parse_expression(self, graph, expression): if 'subj' in expression.attrib: self._subj = graph.get_word_node(expression.attrib['subj']) if 'pred' in expression.attrib: self._pred = graph.get_word_node(expression.attrib['pred']) if 'obj' in expression.attrib: self._obj = 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': self._subj = self.parse_children_as_word_node(graph, child) elif tag_name == 'pred': self._pred = self.parse_children_as_word_node(graph, child) elif tag_name == 'obj': self._obj = 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)
def _parse_node_with_attribs(self, graph, expression): self._name = TextUtils.tag_from_text(expression.tag) for attrib_name in expression.attrib: attrib_value = expression.attrib[attrib_name] if "<" in attrib_value and ">" in attrib_value: start = attrib_value.find("<") end = attrib_value.rfind(">") front = attrib_value[:start] middle = attrib_value[start:end+1] back = attrib_value[end+1:] root = TemplateNode() root.append(TemplateWordNode(front)) xml = ET.fromstring(middle) xml_node = TemplateNode() graph.parse_tag_expression(xml, xml_node) root.append(xml_node) root.append(TemplateWordNode(back)) self.set_attrib(attrib_name, root) else: self.set_attrib(attrib_name, TemplateWordNode(attrib_value)) self.parse_text(graph, self.get_text_from_element(expression)) for child in expression: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child))
def parse_type3_condition(self, graph, expression): for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name in ['name', 'var', 'bot']: pass elif tag_name == 'li': list_item = TemplateConditionListItemNode() name, var_type = self.get_condition_name(child) list_item._name = name list_item._var_type = var_type list_item._value = self.get_condition_value(graph, child) 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', 'var', 'bot', 'value']: 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> in condition element" % (tag_name), xml_element=expression)
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)
def _parse_node_with_attribs(self, graph, expression, attribs): attribs_found = [] for attrib in attribs: attrib_name = attrib[0] if attrib_name in expression.attrib: self.set_attrib(attrib_name, expression.attrib[attrib_name]) attribs_found.append(attrib_name) self.parse_text(graph, self.get_text_from_element(expression)) for child in expression: tag_name = TextUtils.tag_from_text(child.tag) for attrib in attribs: attrib_name = attrib[0] if tag_name == attrib_name: self.set_attrib(attrib[0], self.get_text_from_element(child)) else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) for attrib in attribs: attrib_name = attrib[0] if attrib_name not in attribs_found: if attrib[1] is not None: YLogger.debug(self, "Setting default value for attrib [%s]", attrib_name) self.set_attrib(attrib_name, attrib[1])
def parse_tag_expression(self, expression, branch): tag_name = TextUtils.tag_from_text(expression.tag) if self._template_factory.exists(tag_name): if tag_name == "condition": node_instance = self._template_factory.new_node_class(tag_name)() else: node_instance = self._template_factory.new_node_class(tag_name)() node_instance.parse_expression(self, expression) branch.children.append(node_instance) else: self.parse_unknown_as_xml_node(expression, branch)
def parse_expression(self, graph, expression): if 'format' in expression.attrib: self.interval_format = graph.get_word_node(expression.attrib['format']) 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 == 'format': self.interval_format = graph.get_word_node(self.get_text_from_element(child)) elif tag_name == 'style': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.style = node elif tag_name == 'from': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_from = node elif tag_name == 'to': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_to = node else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.interval_format is None: YLogger.warning(self, "Interval node, format missing, defaulting to 'c%%'!") self.interval_format = "%c" if self.style is None: YLogger.warning(self, "style node, format missing, defaulting to 'days'!") self.style = "days" if self.interval_from is None: YLogger.warning(self, "interval_from node, format missing !") if self.interval_to is None: YLogger.warning(self, "interval_to node, format missing !")
def node_from_element(self, element, userid="*"): node_name = TextUtils.tag_from_text(element.tag) if self._pattern_factory.exists(node_name) is False: raise ParserException("Unknown node name [%s]"%node_name) text = None if element.text is not None: text = TextUtils.strip_whitespace(element.text) node_class_instance = self._pattern_factory.new_node_class(node_name) node_instance = node_class_instance(element.attrib, text, userid) return node_instance
def parse_expression(self, graph, expression): 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 == 'item': item = self.parse_children_as_word_node(graph, child) self._items.append(item) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text)
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): 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 == 'card': card_class = graph.get_node_class_by_name("card") card = card_class() card.parse_expression(graph, child) self._cards.append(card) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text)
def parse_expression(self, graph, expression): if 'seconds' in expression.attrib: self._seconds = graph.get_word_node(expression.attrib['text']) 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 == 'seconds': self._seconds = 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)
def _parse_node_with_attrib(self, graph, expression, attrib_name, default_value=None): attrib_found = True if attrib_name in expression.attrib: self.set_attrib(attrib_name, expression.attrib[attrib_name]) 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 == attrib_name: self.set_attrib(attrib_name, self.get_text_from_element(child)) else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) if attrib_found is False: YLogger.debug(self, "Setting default value for attrib [%s]", attrib_name) self.set_attrib(attrib_name, default_value)
def parse_expression(self, graph, expression): if 'path' in expression.attrib: self.path = expression.attrib['path'] 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 == 'path': self.path = self.get_text_from_element(child) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.path is None: raise ParserException("EXTENSION node, path attribute missing !")
def parse_expression(self, graph, expression): variables = expression.findall('vars') if variables: if len(variables) > 1: if logging.getLogger().isEnabledFor(logging.WARNING): logging.warning( "Multiple <variables> found in select tag, using first" ) self.parse_vars(variables[0].text) queries = expression.findall('./') for query in queries: tag_name = TextUtils.tag_from_text(query.tag) if tag_name == 'q' or tag_name == 'notq': self.parse_query(graph, tag_name, query) if self.children: raise ParserException( "<select> node should not contains child text, use <select><vars></vars><q></q></select> only" )
def parse_expression(self, graph, expression): if 'path' in expression.attrib: self.path = expression.attrib['path'] 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 == 'path': self.path = self.get_text_from_element(child) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.path is None: raise ParserException("EXTENSION node, path attribute missing !")
def parse_expression(self, graph, expression): if 'seconds' in expression.attrib: self._seconds = graph.get_word_node(expression.attrib['seconds']) 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 == 'seconds': self._seconds = self.parse_children_as_word_node(graph, child) else: raise ParserException("Invalid child nodes found in delay") tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self._seconds is None: raise ParserException("Missing seconds from delay")
def parse_expression(self, graph, expression): if 'subj' in expression.attrib: self._subj = graph.get_word_node(expression.attrib['subj']) if self._subj == '': self._subj = None if 'pred' in expression.attrib: self._pred = graph.get_word_node(expression.attrib['pred']) if self._pred == '': self._pred = None if 'obj' in expression.attrib: self._obj = graph.get_word_node(expression.attrib['obj']) if self._obj == '': self._obj = None 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': self._subj = self.parse_children_as_word_node(graph, child) if len(self._subj.children) == 0: self._subj = None elif tag_name == 'pred': self._pred = self.parse_children_as_word_node(graph, child) if len(self._pred.children) == 0: self._pred = None elif tag_name == 'obj': self._obj = self.parse_children_as_word_node(graph, child) if len(self._obj.children) == 0: self._obj = None else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text)
def parse_expression(self, graph, expression): name_found = False var_found = False if 'name' in expression.attrib: self.name = self.parse_attrib_value_as_word_node( graph, expression, 'name') self.local = False name_found = True if 'var' in expression.attrib: self.name = self.parse_attrib_value_as_word_node( graph, expression, 'var') self.local = True var_found = True for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'name': self.name = self.parse_children_as_word_node(graph, child) self.local = False name_found = True elif tag_name == 'var': self.name = self.parse_children_as_word_node(graph, child) self.local = True var_found = True elif tag_name == "tuple": self._tuples = self.parse_children_as_word_node(graph, child) if name_found is False and var_found is False: raise ParserException("Invalid get, missing either name or var", xml_element=expression) if name_found is True and var_found is True: raise ParserException("Get node has both name AND var values", xml_element=expression)
def parse_expression(self, graph, expression): name_found = False var_found = False if 'name' in expression.attrib: self.name = self.parse_attrib_value_as_word_node(graph, expression, 'name') self.local = False name_found = True if 'var' in expression.attrib: self.name = self.parse_attrib_value_as_word_node(graph, expression, 'var') self.local = True var_found = True 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': self.name = self.parse_children_as_word_node(graph, child) self.local = False name_found = True elif tag_name == 'var': self.name = self.parse_children_as_word_node(graph, child) self.local = True var_found = True else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) if name_found is True and var_found is True: raise ParserException("Set node has both name AND var values", xml_element=expression) if name_found is False and var_found is False: raise ParserException("Set node has both name AND var values", xml_element=expression)
def parse_type1_condition(self, graph, expression): 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 in ['name', 'var', 'bot', 'value']: pass elif tag_name == 'li': raise ParserException("li element not allowed as child of condition element", xml_element=expression) elif tag_name == 'loop': raise ParserException("This type of condition cannot have <loop /> element", xml_element=expression) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text)
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: 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: YLogger.warning(self, "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_expression(self, graph, expression): if 'image' in expression.attrib: self._image = graph.get_word_node(expression.attrib['image']) if 'title' in expression.attrib: self._title = graph.get_word_node(expression.attrib['title']) if 'subtitle' in expression.attrib: self._subtitle = graph.get_word_node(expression.attrib['subtitle']) 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 == 'image': self._image = self.parse_children_as_word_node(graph, child) elif tag_name == 'title': self._title = self.parse_children_as_word_node(graph, child) elif tag_name == 'subtitle': self._subtitle = self.parse_children_as_word_node(graph, child) elif tag_name == 'button': button_class = graph.get_node_class_by_name("button") button = button_class() button.parse_expression(graph, child) self._buttons.append(button) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self._title is None: raise ParserException("Missing title element", xml_element=expression, nodename='card') if self._image is None: raise ParserException("Missing image element", xml_element=expression, nodename='card') if len(self._buttons) == 0: raise ParserException("Missing button elements", xml_element=expression, nodename='card')
def parse_expression(self, graph, expression): 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 == 'card': card_class = graph.get_node_class_by_name("card") card = card_class() card.parse_expression(graph, child) self._cards.append(card) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if len(self._cards) == 0: raise ParserException("Missing text element", xml_element=expression, nodename='carousel')
def parse_expression(self, graph, expression): if 'text' in expression.attrib: self._text = graph.get_word_node(expression.attrib['text']) 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 == '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)
def _parse_node_with_attribs(self, graph, expression): try: self._name = TextUtils.tag_from_text(expression.tag) for attrib_name in expression.attrib: attrib_value = expression.attrib[attrib_name] if "<" in attrib_value and ">" in attrib_value: start = attrib_value.find("<") end = attrib_value.rfind(">") front = attrib_value[:start] middle = attrib_value[start:end+1] back = attrib_value[end+1:] root = TemplateNode() root.append(TemplateWordNode(front)) xml = ET.fromstring(middle) xml_node = TemplateNode() graph.parse_tag_expression(xml, xml_node) root.append(xml_node) root.append(TemplateWordNode(back)) self.set_attrib(attrib_name, root) else: self.set_attrib(attrib_name, TemplateWordNode(attrib_value)) self.parse_text(graph, self.get_text_from_element(expression)) for child in expression: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) except Exception as e: print(e)
def parse_expression(self, graph, expression): if 'host' in expression.attrib: YLogger.warning(self, "'host' attrib not supported in sraix, moved to config, see documentation") if 'botid' in expression.attrib: YLogger.warning(self, "'botid' attrib not supported in sraix, moved to config, see documentation") if 'hint' in expression.attrib: YLogger.warning(self, "'hint' attrib not supported in sraix, moved to config, see documentation") if 'apikey' in expression.attrib: YLogger.warning(self, "'apikey' attrib not supported in sraix, moved to config, see documentation") if 'service' in expression.attrib: self.service = expression.attrib['service'] 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 == 'host': YLogger.warning(self, "'host' element not supported in sraix, moved to config, see documentation") elif tag_name == 'botid': YLogger.warning(self, "'botid' element not supported in sraix, moved to config, see documentation") elif tag_name == 'hint': YLogger.warning(self, "'hint' element not supported in sraix, moved to config, see documentation") elif tag_name == 'apikey': YLogger.warning(self, "'apikey' element not supported in sraix, moved to config, see documentation") elif tag_name == 'service': self.service = self.get_text_from_element(child) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.service is None: raise ParserException("SRAIX node, service attribute missing !")
def parse_expression(self, graph, expression): if 'image' in expression.attrib: self._image = graph.get_word_node(expression.attrib['image']) if 'title' in expression.attrib: self._title = graph.get_word_node(expression.attrib['title']) if 'subtitle' in expression.attrib: self._subtitle = graph.get_word_node(expression.attrib['subtitle']) 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 == 'image': self._image = self.parse_children_as_word_node(graph, child) elif tag_name == 'title': self._title = self.parse_children_as_word_node(graph, child) elif tag_name == 'subtitle': self._subtitle = self.parse_children_as_word_node(graph, child) elif tag_name == 'button': button_class = graph.get_node_class_by_name("button") button = button_class() button.parse_expression(graph, child) self._buttons.append(button) else: raise ParserException("Unsupport child elements of card") tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self._title is None: raise ParserException("Not title in card") if len(self._buttons) == 0: raise ParserException("No buttons in card")
def parse_expression(self, graph, expression): text_seconds = '0' if 'seconds' in expression.attrib: self._seconds = graph.get_word_node(expression.attrib['seconds']) text_seconds = self._seconds.word 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 == 'seconds': self._seconds = self.parse_children_as_word_node(graph, child) if len(self._seconds.children) == 1 and type( self._seconds.children[0]) is TemplateWordNode: text_seconds = self._seconds.children[0].word else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self._seconds is None: raise ParserException("Missing seconds element", xml_element=expression, nodename='delay') if text_seconds != '0': try: seconds = int(text_seconds) if seconds < 0: raise Exception() except Exception: raise ParserException("Seconds element is not valid integer", xml_element=expression, nodename='delay')
def parse_expression(self, graph, expression): if 'host' in expression.attrib: YLogger.warning(self, "'host' attrib not supported in sraix, moved to config, see documentation") if 'botid' in expression.attrib: YLogger.warning(self, "'botid' attrib not supported in sraix, moved to config, see documentation") if 'hint' in expression.attrib: YLogger.warning(self, "'hint' attrib not supported in sraix, moved to config, see documentation") if 'apikey' in expression.attrib: YLogger.warning(self, "'apikey' attrib not supported in sraix, moved to config, see documentation") if 'service' in expression.attrib: self.service = expression.attrib['service'] 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 == 'host': YLogger.warning(self, "'host' element not supported in sraix, moved to config, see documentation") elif tag_name == 'botid': YLogger.warning(self, "'botid' element not supported in sraix, moved to config, see documentation") elif tag_name == 'hint': YLogger.warning(self, "'hint' element not supported in sraix, moved to config, see documentation") elif tag_name == 'apikey': YLogger.warning(self, "'apikey' element not supported in sraix, moved to config, see documentation") elif tag_name == 'service': self.service = self.get_text_from_element(child) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.service is None: raise ParserException("SRAIX node, service attribute missing !")
def _parse_node_with_attrib(self, graph, expression, attrib_name, default_value=None): attrib_found = True if attrib_name in expression.attrib: self.set_attrib(attrib_name, expression.attrib[attrib_name]) 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 == attrib_name: value = self.get_text_from_element(child) if value is not None: self.set_attrib(attrib_name, value) else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) if attrib_found is False: YLogger.debug(self, "Setting default value for attrib [%s]", attrib_name) self.set_attrib(attrib_name, default_value)
def parse_type3_condition(self, graph, expression): for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name in ['name', 'var', 'bot']: pass elif tag_name == 'li': list_item = TemplateConditionListItemNode() name, var_type = self.get_condition_name(child) list_item._name = name list_item._var_type = var_type list_item._value = self.get_condition_value(graph, child) 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', 'var', 'bot', 'value']: 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> in condition element" % (tag_name), xml_element=expression)
def parse_type1_condition(self, graph, expression): 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 in ['name', 'var', 'bot', 'value']: pass elif tag_name == 'li': raise ParserException( "li element not allowed as child of condition element", xml_element=expression) elif tag_name == 'loop': raise ParserException( "This type of condition cannot have <loop /> element", xml_element=expression) else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text)
def parse_expression(self, graph, expression): name_found = False if 'name' in expression.attrib: self.name = self.parse_attrib_value_as_word_node(graph, expression, 'name') name_found = True 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': self.name = self.parse_children_as_word_node(graph, child) name_found = True else: graph.parse_tag_expression(child, self) self.parse_text(graph, self.get_tail_from_element(child)) if name_found is False: raise ParserException("Error, name not found in map", xml_element=expression)
def parse_expression(self, graph, expression): name_found = False var_found = False if 'name' in expression.attrib: self.name = self.parse_attrib_value_as_word_node(graph, expression, 'name') self.local = False name_found = True if 'var' in expression.attrib: self.name = self.parse_attrib_value_as_word_node(graph, expression, 'var') self.local = True var_found = True for child in expression: tag_name = TextUtils.tag_from_text(child.tag) if tag_name == 'name': self.name = self.parse_children_as_word_node(graph, child) self.local = False name_found = True elif tag_name == 'var': self.name = self.parse_children_as_word_node(graph, child) self.local = True var_found = True elif tag_name == "tuple": self._tuples = self.parse_children_as_word_node(graph, child) if name_found is False and var_found is False: raise ParserException("Invalid get, missing either name or var", xml_element=expression) if name_found is True and var_found is True: raise ParserException("Get node has both name AND var values", xml_element=expression)
def parse_expression(self, graph, expression): if 'format' in expression.attrib: self.interval_format = graph.get_word_node( expression.attrib['format']) 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 == 'format': self.interval_format = graph.get_word_node( self.get_text_from_element(child)) elif tag_name == 'style': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.style = node elif tag_name == 'from': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_from = node elif tag_name == 'to': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_to = node else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.interval_format is None: if logging.getLogger().isEnabledFor(logging.WARNING): logging.warning( "Interval node, format missing, defaulting to 'c%%'!") self.interval_format = "%c" if self.style is None: if logging.getLogger().isEnabledFor(logging.WARNING): logging.warning( "style node, format missing, defaulting to 'days'!") self.style = "days" if self.interval_from is None: if logging.getLogger().isEnabledFor(logging.WARNING): logging.warning("interval_from node, format missing !") if self.interval_to is None: if logging.getLogger().isEnabledFor(logging.WARNING): logging.warning("interval_to node, format missing !")
def parse_expression(self, graph, expression): mode_count = 0 text_content = '' if 'name' in expression.attrib: mode_count += 1 self._name = self.parse_attrib_value_as_word_node( graph, expression, 'name') self._type = 'name' if 'data' in expression.attrib: mode_count += 1 self._name = self.parse_attrib_value_as_word_node( graph, expression, 'data') self._type = 'data' if 'var' in expression.attrib: mode_count += 1 self._name = self.parse_attrib_value_as_word_node( graph, expression, 'var') self._type = 'var' if 'function' in expression.attrib: self._function = self.parse_attrib_value_as_word_node( graph, expression, 'function') function = expression.attrib['function'] if function not in ['len', 'delete', 'insert']: raise ParserException( ("Unknown function type [%s]" % function), xml_element=expression, nodename='json') if 'item' in expression.attrib: self._item = self.parse_attrib_value_as_word_node( graph, expression, 'item') if 'key' in expression.attrib: self._key = self.parse_attrib_value_as_word_node( graph, expression, 'key') if 'index' in expression.attrib: self._index = self.parse_attrib_value_as_word_node( graph, expression, 'index') indexNo = expression.attrib['index'] try: int(indexNo) except ValueError: raise ParserException( ("Index value is not numeric [%s]" % indexNo), xml_element=expression, nodename='json') if 'type' in expression.attrib: value_type = expression.attrib['type'] if value_type == 'string': self._is_convert = False expression_text = self.get_text_from_element(expression) self.parse_text(graph, expression_text) if expression_text is not None: text_content += expression_text for child in expression: tagName = TextUtils.tag_from_text(child.tag) if tagName == 'function': self._function = self.parse_children_as_word_node(graph, child) elif tagName == 'index': self._index = self.parse_children_as_word_node(graph, child) elif tagName == 'item': self._item = self.parse_children_as_word_node(graph, child) elif tagName == 'key': self._key = self.parse_children_as_word_node(graph, child) else: self._set_child_mark(graph, self.JSON_CHILD_IN) graph.parse_tag_expression(child, self) self._set_child_mark(graph, self.JSON_CHILD_OUT) text_content += self.JSON_CHILD_MARK expression_text = self.get_tail_from_element(child) self.parse_text(graph, expression_text) if expression_text is not None: text_content += expression_text if mode_count == 0: raise ParserException("Missing variable type", xml_element=expression, nodename='json') elif mode_count > 1: raise ParserException("Node has multiple variable type", xml_element=expression, nodename='json') if len(text_content) > 0: if self._format_check(text_content) is False: raise ParserException("Invalid contents format", xml_element=expression, nodename='json')
def parse_type3_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() name, var_type = self.get_condition_name(graph, child) if var_type == TemplateConditionVariable.MULTI: raise ParserException("Node has multiple variable types", xml_element=expression, nodename='condition') list_item._name = name list_item._var_type = var_type 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 if list_item._var_type == TemplateConditionVariable.DEFAULT and \ (list_item._value is not None or list_item._regex is not None): raise ParserException("Li element missing variable", xml_element=expression, nodename='condition') if list_item._var_type != TemplateConditionVariable.DEFAULT and \ list_item._value is None and list_item._regex is None: raise ParserException("Li element Missing value", xml_element=expression, nodename='condition') 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 parse_expression(self, graph, expression): mode_count = 0 if 'method' in expression.attrib: YLogger.warning(self, "'method' attrib not supported in sraix, moved to config, see documentation") if 'query' in expression.attrib: YLogger.warning(self, "'query' attrib not supported in sraix, moved to config, see documentation") if 'header' in expression.attrib: YLogger.warning(self, "'header' attrib not supported in sraix, moved to config, see documentation") if 'body' in expression.attrib: YLogger.warning(self, "'body' attrib not supported in sraix, moved to config, see documentation") if 'botName' in expression.attrib: bot_name = expression.attrib['botName'] if graph.aiml_parser.brain.botnames.botInfo(bot_name) is None: raise ParserException("BotName[%s] not found" % bot_name, xml_element=expression, nodename='sraix') mode_count += 1 self._botName = bot_name if 'locale' in expression.attrib: YLogger.warning(self, "'locale' attrib not supported in sraix, moved to config, see documentation") if 'time' in expression.attrib: YLogger.warning(self, "'time' attrib not supported in sraix, moved to config, see documentation") if 'userId' in expression.attrib: YLogger.warning(self, "'userId' attrib not supported in sraix, moved to config, see documentation") if 'topic' in expression.attrib: YLogger.warning(self, "'topic' attrib not supported in sraix, moved to config, see documentation") if 'deleteVariable' in expression.attrib: YLogger.warning(self, "'deleteVariable' attrib not supported in sraix, moved to config, see documentation") if 'metadata' in expression.attrib: YLogger.warning(self, "'metadata' attrib not supported in sraix, moved to config, see documentation") if 'config' in expression.attrib: YLogger.warning(self, "'config' attrib not supported in sraix, moved to config, see documentation") if 'service' in expression.attrib: service_name = expression.attrib['service'] if ServiceFactory.service_exists(service_name) is False: raise ParserException("Service[%s] not found" % service_name, xml_element=expression, nodename='sraix') mode_count += 1 self._service = service_name if 'default' in expression.attrib: self._default = expression.attrib['default'] 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 == 'host': mode_count += 1 self._host = self._parse_template_node(graph, child, 'host', False) elif tag_name == 'method': self._method = self._parse_template_node(graph, child, 'method', False) elif tag_name == 'query': self._query = self._parse_template_node(graph, child, 'query', True) elif tag_name == 'header': self._header = self._parse_template_node(graph, child, 'header', True) elif tag_name == 'body': self._body = self._parse_template_node(graph, child, 'body', False) elif tag_name == 'botName': YLogger.warning(self, "'botName' element not supported in sraix, moved to config, see documentation") elif tag_name == 'locale': self._locale = self._parse_template_node(graph, child, 'locale', False) elif tag_name == 'time': self._time = self._parse_template_node(graph, child, 'time', False) elif tag_name == 'userId': self._userId = self._parse_template_node(graph, child, 'userId', False) elif tag_name == 'topic': self._topic = self._parse_template_node(graph, child, 'topic', False) elif tag_name == 'deleteVariable': self._deleteVariable = self._parse_template_node(graph, child, 'deleteVariable', False) elif tag_name == 'metadata': self._metadata = self._parse_template_node(graph, child, 'metadata', False) elif tag_name == 'config': self._config = self._parse_template_node(graph, child, 'config', True) elif tag_name == 'service': YLogger.warning(self, "'service' element not supported in sraix, moved to config, see documentation") elif tag_name == 'default': YLogger.warning(self, "'default' element not supported in sraix, moved to config, see documentation") else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if mode_count == 0: raise ParserException("Missing type attribute or host element", xml_element=expression, nodename='sraix') elif mode_count > 1: raise ParserException("Node has Multiple type attribute or host element", xml_element=expression, nodename='sraix')
def parse_expression(self, graph, expression): if 'format' in expression.attrib: self._interval_format = graph.get_word_node( expression.attrib['format']) if 'style' in expression.attrib: self._style = graph.get_word_node(expression.attrib['style']) if 'from' in expression.attrib: self._interval_from = graph.get_word_node( expression.attrib['from']) if 'to' in expression.attrib: self._interval_to = graph.get_word_node(expression.attrib['to']) 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 == 'format': self._interval_format = graph.get_word_node( self.get_text_from_element(child)) elif tag_name == 'style': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self._style = node elif tag_name == 'from': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self._interval_from = node elif tag_name == 'to': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self._interval_to = node else: raise ParserException("No children allows in interval") tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.interval_format is None: YLogger.warning( self, "Interval node, format missing, defaulting to '%c'!") self._interval_format = TemplateWordNode("%c") if self.style is None: YLogger.warning( self, "style node, format missing, defaulting to 'days'!") self._style = TemplateWordNode("days") if self.interval_from is None: raise ParserException("interval_from node, format missing !") if self.interval_to is None: raise ParserException(self, "interval_to node, format missing !")
def parse_expression(self, graph, expression): if 'format' in expression.attrib: self.interval_format = self.parse_attrib_value_as_word_node(graph, expression, 'format') 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 == 'format': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_format = node elif tag_name == 'style': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.style = node elif tag_name == 'from': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_from = node elif tag_name == 'to': node = graph.get_base_node() node.parse_text(graph, self.get_text_from_element(child)) for sub_child in child: graph.parse_tag_expression(sub_child, node) node.parse_text(graph, self.get_text_from_element(child)) self.interval_to = node else: graph.parse_tag_expression(child, self) tail_text = self.get_tail_from_element(child) self.parse_text(graph, tail_text) if self.interval_format is None or len(self.interval_format.children) == 0: YLogger.warning(self, "Interval node, format missing, defaulting to '%c'") self.interval_format = graph.get_word_node('%c') if self.style is None or len(self.style.children) == 0: raise ParserException("interval_style node missing", xml_element=expression, nodename='interval') elif len(self.style.children) == 1 and type(self.style.children[0]) is TemplateWordNode: style = self.style.children[0].word if style not in ["years", "months", "weeks", "days", "hours", "minutes", "seconds", "microseconds", "ymd", "hms", "ymdhms"]: raise ParserException(("invalid interval stile [%s]" % style), xml_element=expression, nodename='interval') if self.interval_from is None or len(self.interval_from.children) == 0: raise ParserException("interval_from node missing", xml_element=expression, nodename='interval') if self.interval_to is None or len(self.interval_to.children) == 0: raise ParserException("interval_to node missing", xml_element=expression, nodename='interval')