summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/DataFormatter.py
blob: 768b83b8fc9f5645b5259c0fcae5b8e1ee19bef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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))