added file tail reader

This commit is contained in:
Hans Erik van Elburg 2019-12-24 00:57:44 +01:00
parent 659560222a
commit 8c2485d70f

View File

@ -1,5 +1,6 @@
import logging
import fileinput
import tailer
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
@ -120,3 +121,31 @@ class FileInputReader(object):
logger.warning(str(e))
except ParseError as e:
logger.error('Failed to parse telegram: %s', e)
class FileTailReader(object):
def __init__(self, file, telegram_specification):
self._file = file
self.telegram_parser = TelegramParser(telegram_specification)
self.telegram_buffer = TelegramBuffer()
self.telegram_specification = telegram_specification
def read_as_object(self):
"""
Read complete DSMR telegram's from a files tail and return a Telegram object.
:rtype: generator
"""
with open(self._file,"rb") as file_handle:
for data in tailer.follow(file_handle):
str = data.decode()
self.telegram_buffer.append(str)
for telegram in self.telegram_buffer.get_all():
try:
yield Telegram(telegram, self.telegram_parser, self.telegram_specification)
except dsmr_parser.exceptions.InvalidChecksumError as e:
logger.warning(str(e))
except dsmr_parser.exceptions.ParseError as e:
logger.error('Failed to parse telegram: %s', e)