def parse_interval_expression(self, expression, branch): # TemplateInternalNode # aise ParserException ("Error, interval not implemented!") interval_node = TemplateIntervalNode() branch.children.append(interval_node) if 'format' in expression.attrib: interval_node.format = TemplateWordNode(expression.attrib['format']) head_text = self.get_text_from_element(expression) self.parse_text(head_text, interval_node) for child in expression: if child.tag == 'format': interval_node.format = TemplateWordNode(self.get_text_from_element(child)) elif child.tag == 'style': 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) interval_node.style = node elif child.tag == 'from': 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) interval_node.interval_from = node elif child.tag == 'to': 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) interval_node.interval_to = node else: self.parse_tag_expression(child, interval_node) tail_text = self.get_tail_from_element(child) self.parse_text(tail_text, interval_node) if interval_node.format is None: logging.warning("Interval node, format missing !") if interval_node.style is None: logging.warning("style node, format missing !") if interval_node.interval_from is None: logging.warning("interval_from node, format missing !") if interval_node.interval_to is None: logging.warning("interval_to node, format missing !")
def test_resolve_to_string_format_none(self): root = TemplateNode() node = TemplateIntervalNode() node._interval_format = None node.style = TemplateWordNode("days") node.interval_from = TemplateWordNode("Thu Oct 6 16:35:11 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") root.append(node) self.assertEqual("732", root.resolve_to_string(self._client_context))
def test_to_xml(self): root = TemplateNode() node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("years") node.interval_from = TemplateWordNode("Thu Oct 6 16:35:11 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") root.append(node) xml = root.xml_tree(self._client_context) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><interval format="%c" style="years"><from>Thu Oct 6 16:35:11 2014</from><to>Fri Oct 7 16:35:11 2016</to></interval></template>', xml_str)
def test_to_xml(self): root = TemplateNode() node = TemplateIntervalNode() node.format = TemplateWordNode("%c") node.style = TemplateWordNode("years") node.interval_from = TemplateWordNode("Thu Oct 6 16:35:11 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") root.append(node) xml = root.xml_tree(self.bot, self.clientid) self.assertIsNotNone(xml) xml_str = ET.tostring(xml, "utf-8").decode("utf-8") self.assertEqual('<template><interval format="%c" style="years"><from>Thu Oct 6 16:35:11 2014</from><to>Fri Oct 7 16:35:11 2016</to></interval></template>', xml_str)
def test_node_days(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("days") node.interval_from = TemplateWordNode("Thu Oct 6 16:35:11 2016") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "1")
def test_node_ymdhms(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("ymdhms") node.interval_from = TemplateWordNode("Thu Jul 14 16:33:09 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "2 years, 2 months, 23 days, 0 hours, 2 minutes, 2 seconds")
def test_node_hours(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.format = TemplateWordNode("%c") node.style = TemplateWordNode("hours") node.interval_from = TemplateWordNode("Thu Oct 7 12:35:11 2016") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self.bot, self.clientid) self.assertIsNotNone(response) self.assertEqual(response, "4")
def test_node_ymdhms(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.format = TemplateWordNode("%c") node.style = TemplateWordNode("ymdhms") node.interval_from = TemplateWordNode("Thu Jul 14 16:33:09 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self.bot, self.clientid) self.assertIsNotNone(response) self.assertEqual(response, "2 years, 2 months, 23 days, 0 hours, 2 minutes, 2 seconds")
def test_node_hours(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.format = TemplateWordNode("%c") node.style = TemplateWordNode("hours") node.interval_from = TemplateWordNode("Thu Oct 7 12:35:11 2016") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self.bot, self.clientid) self.assertIsNotNone(response) self.assertEqual(response, "4")
def test_node_days(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("days") node.interval_from = TemplateWordNode("Thu Oct 6 16:35:11 2016") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "1")
def test_node_unknown(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("unknown") node.interval_from = TemplateWordNode("Thu Jul 14 16:33:09 2014") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._bot, self._clientid) self.assertIsNotNone(response) self.assertEqual(response, "")
def test_node_microseconds(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("microseconds") node.interval_from = TemplateWordNode("Thu Oct 7 16:35:09 2016") node.interval_to = TemplateWordNode("Fri Oct 7 16:35:09 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._bot, self._clientid) self.assertIsNotNone(response) self.assertEqual(response, "0")
def test_node_years(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() self.assertEqual("[INTERVAL]", node.to_string()) node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("years") node.interval_from = TemplateWordNode("Thu Oct 06 16:35:11 2014") node.interval_to = TemplateWordNode("Fri Oct 07 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "2")
def test_node_years(self): root = TemplateNode() self.assertIsNotNone(root) self.assertIsNotNone(root.children) self.assertEqual(len(root.children), 0) node = TemplateIntervalNode() self.assertEqual("[INTERVAL]", node.to_string()) node.interval_format = TemplateWordNode("%c") node.style = TemplateWordNode("years") node.interval_from = TemplateWordNode("Thu Oct 06 16:35:11 2014") node.interval_to = TemplateWordNode("Fri Oct 07 16:35:11 2016") self.assertIsNotNone(node) root.append(node) self.assertEqual(len(root.children), 1) response = root.resolve(self._client_context) self.assertIsNotNone(response) self.assertEqual(response, "2")