def __loadtimestamped(self, tsdata): try: self.timestampindex = self.headers.index(tsdata) self.data = BufferedTimestampedList(200, self.fillBuffer, 20) except ValueError as e: self.__loadnormal()
class CSVHistoryReader(AbstractReader): def __init__(self, wrapper, filehandle, firstLineHeader=True, headers=None, delimiter=',', timestampfield=None): # if not os.path.exists(filename): # raise Exception("Historic data for" + str(self) + "not available") super(CSVHistoryReader, self).__init__(wrapper, timestampfield) try: self.filehandle = filehandle self.reader = csv.reader(self.filehandle, delimiter=delimiter) if firstLineHeader: self.headers = self.reader.next() if headers: self.headers = headers # read and sort the data sd = self.wrapper.getSensorDescription() tsdata = timestampfield if not tsdata and sd.isTimestampedStream(): tsdata = sd.timestamp.inField if tsdata: self.__loadtimestamped(tsdata) else: self.__loadnormal() except IOError as error: raise error def __loadtimestamped(self, tsdata): try: self.timestampindex = self.headers.index(tsdata) self.data = BufferedTimestampedList(200, self.fillBuffer, 20) except ValueError as e: self.__loadnormal() def __loadnormal(self): self.data = [] for row in self.reader: self.data.append(row) def fillBuffer(self, aList): sd = self.wrapper.getSensorDescription() for x in range(0, aList.bufferLength): try: row = self.reader.next() ts = sd.parseTimestamp(row[self.timestampindex]) aList.add(ts, row) except StopIteration: # print sd.sensorID, "no more data" aList.stopFillFunction() break aList.fillFunctionComplete() def tick(self, clock): if isinstance(self.data, list): return self.data.pop(0) if len(self.data) > 0 else None else: # clock.pause() x = self.data.next(clock.now()) # clock.continue_running() return x def setTimeframe(self, startdate, enddate): if not isinstance(self.data, list): Log.i("Searching start date in historic data for", self.wrapper.getSensorDescription().sensorID, "...") if self.data.scrollTo(startdate): Log.i("done") else: Log.w("no historic data beginning at", startdate, "found") super(CSVHistoryReader, self).setTimeframe(startdate, enddate)