Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ndokter/dsmr_parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ndokter committed Apr 12, 2023
2 parents d3fab4f + 5a59c36 commit ae8a2ba
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
30 changes: 10 additions & 20 deletions dsmr_parser/clients/telegram_buffer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import re

# - Match all characters after start of telegram except for the start
# itself again '^\/]+', which eliminates incomplete preceding telegrams.
# - Do non greedy match using '?' so start is matched up to the first
# checksum that's found.
# - The checksum is optional '{0,4}' because not all telegram versions
# support it.
_FIND_TELEGRAMS_REGEX = re.compile(r"\/[^\/]+?\![A-F0-9]{0,4}\0?\r\n", re.DOTALL)


class TelegramBuffer(object):
"""
Expand All @@ -8,14 +16,14 @@ class TelegramBuffer(object):
"""

def __init__(self):
self._buffer = ''
self._buffer = ""

def get_all(self):
"""
Remove complete telegrams from buffer and yield them.
:rtype generator:
"""
for telegram in self._find_telegrams():
for telegram in _FIND_TELEGRAMS_REGEX.findall(self._buffer):
self._remove(telegram)
yield telegram

Expand All @@ -37,21 +45,3 @@ def _remove(self, telegram):
index = self._buffer.index(telegram) + len(telegram)

self._buffer = self._buffer[index:]

def _find_telegrams(self):
"""
Find complete telegrams in buffer from start ('/') till ending
checksum ('!AB12\r\n').
:rtype: list
"""
# - Match all characters after start of telegram except for the start
# itself again '^\/]+', which eliminates incomplete preceding telegrams.
# - Do non greedy match using '?' so start is matched up to the first
# checksum that's found.
# - The checksum is optional '{0,4}' because not all telegram versions
# support it.
return re.findall(
r'\/[^\/]+?\![A-F0-9]{0,4}\0?\r\n',
self._buffer,
re.DOTALL
)
9 changes: 7 additions & 2 deletions dsmr_parser/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ def __init__(self, telegram_specification, apply_checksum_validation=True):
telegram DSMR version (v4 and up).
:type telegram_specification: dict
"""
self.telegram_specification = telegram_specification
self.apply_checksum_validation = apply_checksum_validation
self.telegram_specification = telegram_specification
# Regexes are compiled once to improve performance
self.telegram_specification_regexes = {
signature: re.compile(signature, re.DOTALL)
for signature in self.telegram_specification['objects'].keys()
}

def parse(self, telegram_data, encryption_key="", authentication_key=""): # noqa: C901
"""
Expand Down Expand Up @@ -80,7 +85,7 @@ def parse(self, telegram_data, encryption_key="", authentication_key=""): # noq
telegram = Telegram()

for signature, parser in self.telegram_specification['objects'].items():
pattern = re.compile(signature, re.DOTALL)
pattern = self.telegram_specification_regexes[signature]
matches = pattern.findall(telegram_data)

# Some signatures are optional and may not be present,
Expand Down

0 comments on commit ae8a2ba

Please sign in to comment.