added file tail reader
This commit is contained in:
parent
659560222a
commit
8c2485d70f
@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import fileinput
|
import fileinput
|
||||||
|
import tailer
|
||||||
|
|
||||||
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
from dsmr_parser.clients.telegram_buffer import TelegramBuffer
|
||||||
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
from dsmr_parser.exceptions import ParseError, InvalidChecksumError
|
||||||
@ -120,3 +121,31 @@ class FileInputReader(object):
|
|||||||
logger.warning(str(e))
|
logger.warning(str(e))
|
||||||
except ParseError as e:
|
except ParseError as e:
|
||||||
logger.error('Failed to parse telegram: %s', 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)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user