summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/DataProcessing/DataSource.py
blob: a384df078fbb355a9ca5909a798c7e9bcddca944 (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
'''
Created on Dec 10, 2012

@author: steger
'''
from threading import RLock
from DataProcessing.DataError import DataError, SamplerError
#FIXME: import dependency problem, circular reference
#from DataProcessing.DataReader import DataReader

class DataSource(object):
    '''
#FIXME: docs
    @summary: a template to represent any data generated by a tool or derived via various operations on data
    @ivar source: pointer to the origin of the data
    @note: DataSource instances reference their ancestor via the source property, in a recursive manner, the last item needs to implement a read and a write lock
    @ivar data: pointer to the actual data container
    @note: results of operations on data yield their result in the container referenced by the data property, the original data generator's data and source properties are meant to be the same
    @ivar name: a name for the data source class
    @ivar um: reference to the unit model of the ancient data source
    '''
    PASS = 0
    CLEARED = 1
    EXPANDED = 2

    def __init__(self, dependency = None):
        self._readers = set()
        if dependency is None:
            self._inputreader = None
        else:
            self._inputreader = self.readerClass(dependency)
        self._processlock = RLock()
        self._data = None

#FIXME: import error
    def registerReader(self, reader):
        '''
        @summary: registers a reader to catch clear and update events
        @param reader: data consumer
        @type reader: DataReader 
        @raise DataError: wrong argument
        '''
        try:
#        if isinstance(reader, DataReader):
            self._readers.add(reader)
            if len(self):
                reader.sourceexpanded()
            else:
                reader.sourcecleared()
#        else:
        except SamplerError:
            pass
        except:
            raise
            raise DataError("Expecting a DataReader, got %s" % reader)
    
    def deregisterReader(self, reader):
        '''
        @summary: removes a registered reader
        @param reader: data consumer
        @type reader: DataReader 
        '''
        try:
            self._readers.remove(reader)
        except KeyError:
            pass
    
    def __len__(self):
        raise DataError("%s must implement __len__ method" % self)

    def __getitem__(self, k):
        raise DataError("%s must implement __getitem__ method" % self)

    @property
    def name(self):
        raise DataError("%s must implement name property" % self)

    @property
    def readerClass(self):
        raise DataError("%s must implement readerClass property" % self)

    @property
    def data(self):
        self.process()
        return self._data

    def process(self):
        '''
        @summary: recursively process data records of the source chain
        @return: status of the data processing
        @rtype: int
        '''
        with self._processlock:
            if self._inputreader:
#                print "PROC SRC", self, self._inputreader.source
                self._inputreader.source.process()
#            print "PROC", self
            status = self._process()
            if status & self.CLEARED:
#                print "SRC cleared", self
                self._sourcecleared()
            if status & self.EXPANDED:
#                print "SRC expanded", self
                self._sourceexpanded()
            return status

    def _process(self):
        raise DataError("%s must implement _process method returning process status" % self)

    def _sourcecleared(self):
        for r in self._readers:
            r.sourcecleared()

    def _sourceexpanded(self):
        for r in self._readers:
            r.sourceexpanded()