summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/DataFormatter.py
diff options
context:
space:
mode:
Diffstat (limited to 'Monitoring/src/main/python/DataProcessing/DataFormatter.py')
-rw-r--r--Monitoring/src/main/python/DataProcessing/DataFormatter.py145
1 files changed, 145 insertions, 0 deletions
diff --git a/Monitoring/src/main/python/DataProcessing/DataFormatter.py b/Monitoring/src/main/python/DataProcessing/DataFormatter.py
new file mode 100644
index 0000000..768b83b
--- /dev/null
+++ b/Monitoring/src/main/python/DataProcessing/DataFormatter.py
@@ -0,0 +1,145 @@
+'''
+Created on 08.08.2011
+
+@author: steger
+'''
+from DataProcessing.DataReader import DataReader
+from DataProcessing.Data import Data
+from DataProcessing.DataError import DataError
+
+class Formatter(DataReader):
+ def __init__(self, datasource):
+ '''
+ Constructor
+ '''
+ DataReader.__init__(self, datasource._data)
+
+ def _cell(self):
+ raise DataError("Implement _cell() method")
+
+ def header(self):
+ raise DataError("Implement header() method")
+
+ def serialize(self):
+ raise DataError("Implement serialize() method")
+
+ def _get_name(self):
+ return self.source.name
+
+
+ name = property(_get_name,None,None)
+class JsonFormatter(Formatter):
+ '''
+ @summary:
+ Serialize Data in JSON format
+ '''
+
+ def _cell(self, c):
+ '''
+ @summary: serialize a column in JSON format
+ '''
+ try:
+ feature = "\n \"FEATURE\": \"%s\"," % c.feature
+ except:
+ feature = ""
+ score = c.unit.reference.count('_')
+ if score == 0:
+ ret = """{%s
+ "NAME" : "%s",
+ "DIMENTSION" : "%s",
+ "UNIT" : "%s"
+ }""" % (feature, c.name, c.dimension.name, c.unit.reference)
+ elif score == 1:
+ prefix, base = c.unit.reference.split('_')
+ ret = """{%s
+ "NAME" : "%s",
+ "DIMENTSION" : "%s",
+ "PREFIX" : "%s",
+ "UNIT" : "%s"
+ }""" % (feature, c.name, c.dimension.name, prefix, base)
+ else:
+ ret = "ERROR: %s" % c
+ return ret
+
+ def header(self):
+ '''
+ @summary: serialize full header
+ '''
+ return """{
+ "NAME" : "DataHeader %s",
+ "HDRINFO" : [
+ %s
+ ]
+ }""" % (id(self.source.header), ",\n ".join([ self._cell(c) for c in self.headercells() ]))
+
+ def serialize(self):
+ '''
+ @summary: serialize the header and the new lines of the table into JSON format
+ @return: formatted string representation of the table
+ @rtype: string
+ '''
+ if not self.sourceExpanded.isSet():
+ return ""
+ r = []
+ for rec in self:
+ st = []
+ for d in rec:
+ if isinstance(d, Data):
+ #FIXME:
+ st.append( d._dump() )
+ else:
+ if d is None:
+ tmpd = "null"
+ else:
+ tmpd = str(d)
+ st.append( tmpd )
+ r.append("[ %s ]" % ", ".join(st))
+ return """{
+ "TYPE" : "%s",
+ "ID" : %d,
+ "HDR" : %s,
+ "DATA" : [
+ %s
+ ]
+}""" % (self.source.header.name, id(self), self.header(), ",\n ".join(r))
+
+class DumbFormatter(Formatter):
+ '''
+ @summary:
+ Serialize Data in a trivial format
+ '''
+
+ def _cell(self, c):
+ '''
+ @summary: serialize column
+ '''
+ try:
+ return "%s (%s/%s) [%s]" % (c.name, c.feature, c.dimension.name, c.unit)
+ except:
+ return "%s (/%s) [%s]" % (c.name, c.dimension.name, c.unit)
+
+ def header(self):
+ '''
+ @summary: serialize full header
+ '''
+ return "<DataHeader %s>: {%s: [%s]}" % (id(self.source.header), self.name, ", ".join([ self._cell(c) for c in self.headercells() ]))
+
+ def serialize(self):
+ '''
+ @summary: serialize the header and the new lines of the table
+ @return: formatted string representation of the table, if no new data are ready the empty string is returned
+ @rtype: string
+ '''
+ if not self.sourceExpanded.isSet():
+ return ""
+ r = []
+ for rec in self:
+ st = []
+ for d in rec:
+ if isinstance(d, Data):
+ #FIXME:
+ st.append( d._dump() )
+ else:
+ st.append( str(d) )
+ r.append("(%s)" % ", ".join(st))
+ return "{%s:\nHDR:%s\n DATA:[\n%s\n]}" % (str(self), self.header(), ", \n".join(r))