Commits

Anonymous committed 7cf8bdf

estetic changes + make better try except blocks

Comments (0)

Files changed (2)

primary/chatbotini.py

 import aiml
 import xmlfunctions
 
+# TODO: use __name__ trick if possible
+
 ELEMENT = xmlfunctions.xmltodict('chatbot_settings.xml')
 
 # Main settings
     """
     today = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
     ret = '[%s]' % (today)
+
     return ret
 
 
     """
     if bot == 'msn':
         filename = LOGDIR + '/msn_events'
+
     elif bot == 'gtalk':
         filename = LOGDIR + '/gtalk_events'
+
+    # TODO: embed into an exception block
     logfile = open(filename, 'a')
     logfile.write(now() + ' ' + message)
     logfile.close()
     fil = open(configfile)
     opt = fil.readlines()
     fil.close()
+
     for elem in opt:
         par = elem.split('=')
+
         if len(par) == 2:
             brainer.setBotPredicate(par[0].strip(), par[1].strip())
 
     """
     try:
         os.remove(CACHEDIR + str(senderemail) + '.txt')
+
     except OSError:
         pass
 
     Perform the particular action for the given command.
     """
     remove_cache(senderemail)
+
     if CHATBOT.getPredicate('name', senderemail) == '':
         dispname = senderemail.split('@')
+
         if len(dispname) == 2:
             CHATBOT.setPredicate('name', dispname[0], senderemail)
+
     remsg = CHATBOT.respond(message, senderemail)
     remsg = getheader() + remsg + getfooter()
+
     if not remsg:
         remsg = u"Sorry, can't understand.".encode('utf-8')
+
     return remsg
 
 
 # Load brain
 if USE_BRAIN and os.path.isfile(BRAIN):
     CHATBOT.bootstrap(brainFile=BRAIN)
+
 else:
     CHATBOT.bootstrap(learnFiles='ia_start.xml', commands='load aiml b')
     CHATBOT.saveBrain(BRAIN)

primary/xmlfunctions.py

         """Starting"""
         if iniciadicc is None:
             iniciadicc = {}
+
         dict.__init__(self, iniciadicc)
 
     def __getattr__(self, item):
         """Get or Set"""
         if '_text' in self:
             return self.__getitem__('_text')
+
         else:
             return ''
 
         if isinstance(elem, dict):
             return DictionaryXml(
                 (k, DictionaryXml.wrap_dict(v)) for (k, v) in elem.iteritems())
+
         elif isinstance(elem, list):
             return [DictionaryXml.wrap_dict(v) for v in elem]
+
         else:
             return elem
 
             return dict(
                 (k, DictionaryXml._unwrap_dict(v))
                 for (k, v) in elem.iteritems())
+
         elif isinstance(elem, list):
             return [DictionaryXml._unwrap_dict(v) for v in elem]
+
         else:
             return elem
 
         for (tag, child) in elemdict.iteritems():
             if str(tag) == '_text':
                 parent.text = str(child)
+
             elif isinstance(child, list):
                 # iterate through the array and convert
+
                 for listchild in child:
                     elem = ElementTree.Element(tag)
                     parent.append(elem)
                     _dicttoxml(elem, listchild)
+
             else:
                 elem = ElementTree.Element(tag)
                 parent.append(elem)
                 _dicttoxml(elem, child)
+
     else:
         parent.text = str(elemdict)
 
     for child in node:
         # recursively add the element's children
         newitem = _xmltodict(child, classdict)
+
         if child.tag in nodedict:
             # found duplicate tag, force a list
             if isinstance(nodedict[child.tag], list):
                 # append to existing list
                 nodedict[child.tag].append(newitem)
+
             else:
                 # convert to list
                 nodedict[child.tag] = [nodedict[child.tag], newitem]
+
         else:
             # only one, directly set the dictionary
             nodedict[child.tag] = newitem
 
     if node.text is None:
         text = ''
+
     else:
         text = node.text
 
         # value (if there is any)
         if len(text) > 0:
             nodedict['_text'] = text
+
     else:
         # if we don't have child nodes or attributes, just set the text
         nodedict = text
+
     return nodedict
 
 
     # If a string is passed in, try to open it as a file
     if isinstance(root, basestring):
         root = ElementTree.parse(root).getroot()
+
     elif not isinstance(root, ElementTree.Element):
         raise Exception('Expected ElementTree.Element or file path string')
+
     return classdict({root.tag: _xmltodict(root, classdict)})