blind review on list-operations added by 2to3:
[nepi.git] / src / nepi / resources / omf / omf6_parser.py
index 4142029..85aa394 100644 (file)
@@ -3,9 +3,8 @@
 #    Copyright (C) 2013 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,6 +19,7 @@
 
 from nepi.util.logger import Logger
 
+import os
 import traceback
 import xml.etree.ElementTree as ET
 
@@ -51,15 +51,16 @@ class OMF6Parser(Logger):
         """
         super(OMF6Parser, self).__init__("OMF6API")
         self.mailbox={}
+        self.traces={}
+        self.trace='NULL'
 
         self.init_mailbox()
 
+
     def init_mailbox(self):
         self.mailbox['create'] = []
-        self.mailbox['configure'] = []
-        self.mailbox['request'] = []
+        self.mailbox['started'] = []
         self.mailbox['release'] = []
-        self.mailbox['inform'] = []
   
     def _check_for_tag(self, root, namespaces, tag):
         """  Check if an element markup is in the ElementTree
@@ -133,6 +134,9 @@ class OMF6Parser(Logger):
 
 
     def _inform_creation_ok(self, root, namespaces):
+        """ Parse and Display CREATION OK message
+
+        """
         #ET.dump(root)
         uid = self._check_for_tag(root, namespaces, "uid")
         cid = self._check_for_tag(root, namespaces, "cid")
@@ -141,6 +145,8 @@ class OMF6Parser(Logger):
         msg = "CREATION OK -- "
         if binary_path :
             msg = msg + "The resource : '"+binary_path
+        else :
+            msg = msg + "The interface"
         if uid :
             msg = msg + "' is listening to the topics : '"+ uid
         if member :
@@ -150,6 +156,9 @@ class OMF6Parser(Logger):
             self.mailbox['create'].append([cid, uid ])
 
     def _inform_creation_failed(self, root, namespaces):
+        """ Parse and Display CREATION FAILED message
+
+        """
         reason = self._check_for_tag(root, namespaces, "reason")
         cid = self._check_for_tag(root, namespaces, "cid")
         msg = "CREATION FAILED - The reason : "+reason
@@ -158,18 +167,46 @@ class OMF6Parser(Logger):
             self.mailbox['create'].append([cid, uid ])
 
     def _inform_status(self, root, namespaces):
+        """ Parse and Display STATUS message
+
+        """
         props = self._check_for_props(root, namespaces)
-        msg = "STATUS -- "
-        for elt in props.keys():
+        uid = self._check_for_tag(root, namespaces, "uid")
+        event = self._check_for_tag(root, namespaces, "event")
+
+        log = "STATUS -- "
+        for elt in props:
             ns, tag = elt.split('}')
             if tag == "it":
-                msg = msg + "membership : " + props[elt]+" -- "
+                log = log + "membership : " + props[elt]+" -- "
+            elif tag == "event":
+                self.mailbox['started'].append(uid)
+                log = log + "event : " + props[elt]+" -- "
+            elif tag == "msg":
+                if event == "STDOUT" : 
+                    filename = os.path.join("/tmp", "%s.out" % uid)
+                    f = open(filename,'a+')
+                    # XXX: Adding fake \n for visual formatting 
+                    msg = props[elt] # + "\n"
+                    f.write(msg)
+                    f.close()
+                elif event == "STDERR" :
+                    filename = os.path.join("/tmp", "%s.err" % uid)
+                    f = open(filename,'a+')
+                    # XXX: Adding fake \n for visual formatting 
+                    msg = props[elt] # + "\n"
+                    f.write(msg)
+                    f.close()
+                log = log + tag +" : " + props[elt]+" -- "
             else:
-                msg = msg + tag +" : " + props[elt]+" -- "
-        msg = msg + " STATUS "
-        self.info(msg)
+                log = log + tag +" : " + props[elt]+" -- "
+        log = log + " STATUS "
+        self.info(log)
 
     def _inform_released(self, root, namespaces):
+        """ Parse and Display RELEASED message
+
+        """
         #ET.dump(root)
         parent_id = self._check_for_tag(root, namespaces, "src")
         child_id = self._check_for_tag(root, namespaces, "res_id")
@@ -181,17 +218,24 @@ class OMF6Parser(Logger):
             self.mailbox['release'].append(cid)
 
     def _inform_error(self, root, namespaces):
+        """ Parse and Display ERROR message
+
+        """
         reason = self._check_for_tag(root, namespaces, "reason")
         msg = "The reason : "+reason
         self.error(msg)
 
     def _inform_warn(self, root, namespaces):
+        """ Parse and Display WARN message
+
+        """
         reason = self._check_for_tag(root, namespaces, "reason")
         msg = "The reason : "+reason
         self.warn(msg)
 
     def _parse_inform(self, root, namespaces):
-        """ Check the significative element in the answer and display it
+        """ Check the significative element in the answer
+            Then Parse it and display using specific method
 
         :param root: Root of the tree
         :type root: ElementTree Element
@@ -212,21 +256,33 @@ class OMF6Parser(Logger):
         
 
     def check_mailbox(self, itype, attr):
+        """ Check the mail box
+
+        :param itype: type of mail
+        :type itype: str
+        :param attr: value wanted
+        :type attr: str
+
+        """
         if itype == "create":
             for res in self.mailbox[itype]:
                 binary, uid = res
                 if binary == attr:
                     self.mailbox[itype].remove(res)
                     return uid
-        elif itype == "release":
+        else :
             for res in self.mailbox[itype]:
                 if attr == res:
                     self.mailbox[itype].remove(res)
                     return res
-           
                
 
     def handle(self, iq):
+        """ Check the mail box
+
+        :param iq: message received
+        :type itype: iq
+        """
         namespaces = "{http://schema.mytestbed.net/omf/6.0/protocol}"
         for i in iq['pubsub_event']['items']:
             root = ET.fromstring(str(i))