class TemplateSystemNode(TemplateAttribNode): def __init__(self): TemplateAttribNode.__init__(self) self._timeout = TemplateWordNode("0") @property def timeout(self): return self._timeout @timeout.setter def timeout(self, timeout): self._timeout = TemplateWordNode(str(timeout)) def resolve_to_string(self, client_context): if client_context.brain.configuration.overrides.allow_system_aiml is True: command = self.resolve_children_to_string(client_context) process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) result = [] for line in process.stdout.readlines(): byte_string = line.decode("utf-8") result.append(byte_string.strip()) process.wait() resolved = " ".join(result) else: YLogger.warning(client_context, "System command node disabled in config") resolved = "" YLogger.debug(client_context, "[%s] resolved to [%s]", self.to_string(), resolved) return resolved def to_string(self): return "[SYSTEM timeout=%s]" % (self._timeout.to_string()) def set_attrib(self, attrib_name, attrib_value): if attrib_name != 'timeout': raise ParserException("Invalid attribute name [%s] for this node" % attrib_name) YLogger.warning(self, "System node timeout attrib currently ignored") self._timeout = attrib_value def to_xml(self, client_context): xml = "<system" timeout = self._timeout.to_xml(client_context) if timeout != "0": xml += ' timeout="%s"' % timeout xml += ">" xml += self.children_to_xml(client_context) xml += "</system>" return xml ####################################################################################################### # SYSTEM_EXPRESSION ::== # <system( TIMEOUT_ATTRIBUTE)>TEMPLATE_EXPRESSION</system> | # <system><timeout>TEMPLATE_EXPRESSION</timeout></system> # TIMEOUT_ATTRIBUTE :== timeout=”NUMBER” def parse_expression(self, graph, expression): self._parse_node_with_attrib(graph, expression, "timeout", "0")
def test_to_string_no_word(self): node = TemplateWordNode(None) self.assertEquals("[WORD]", node.to_string())
class TemplateDateNode(TemplateAttribNode): def __init__(self, date_format=None, locale=None): TemplateAttribNode.__init__(self) if date_format is None: self._format = TemplateWordNode("%c") else: self._format = TemplateWordNode(date_format) self._locale = None if locale is not None: self._locale = TemplateWordNode(locale) def resolve_to_string(self, client_context): time_now = datetime.datetime.now() local_time = None if self._locale is not None: local_time = locale.setlocale(locale.LC_TIME) try: if self._locale is not None: locale.setlocale( locale.LC_TIME, self._locale.resolve_to_string(client_context)) resolved_format = self._format.resolve_to_string(client_context) resolved = time_now.strftime(resolved_format) finally: if local_time is not None: locale.setlocale(locale.LC_TIME, local_time) YLogger.debug(client_context, "[%s] resolved to [%s]", self.to_string(), resolved) return resolved def to_string(self): if self._locale is None: return "[DATE format=%s]" % self._format.to_string() else: return "[DATE format=%s locale=%s]" % (self._format.to_string(), self._locale.to_string()) def set_attrib(self, attrib_name, attrib_value): if attrib_name == 'format': if isinstance(attrib_value, TemplateNode): self._format = attrib_value else: self._format = TemplateWordNode(attrib_value) elif attrib_name == 'locale': if isinstance(attrib_value, TemplateNode): self._locale = attrib_value else: self._locale = TemplateWordNode(attrib_value) else: raise ParserException("Invalid attribute name %s for this node" % (attrib_name)) def to_xml(self, client_context): if self._locale is None: xml = '<date format="%s" >' % self._format.to_xml(client_context) else: xml = '<date format="%s" locale="%s">' % (self._formatto_xml( client_context), self._localeto_xml(client_context)) xml += self.children_to_xml(client_context) xml += "</date>" return xml ####################################################################################################### # DATE_ATTRIBUTES ::== (format="LISP_DATE_FORMAT") | (jformat="JAVA DATE FORMAT") # DATE_ATTRIBUTE_TAG ::== <format>TEMPLATE_EXPRESSION</format> | <jformat>TEMPLATE_EXPRESSION</jformat> # DATE_EXPRESSION ::== <date( DATE_ATTRIBUTES)*/> | <date>(DATE_ATTRIBUTE_TAG)</date> # Pandorabots supports three extension attributes to the date element in templates: # locale # format # timezone def parse_expression(self, graph, expression): self._parse_node_with_attribs(graph, expression, [["format", "%c"], ["locale", None]])
def test_to_string(self): node = TemplateWordNode("Hello") self.assertEquals("[WORD]Hello", node.to_string())