Skip to content

Commit

Permalink
Update event.py
Browse files Browse the repository at this point in the history
  • Loading branch information
blademckain committed Sep 22, 2020
1 parent 83290dc commit 8ebeb13
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/amcrest/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

_LOGGER = logging.getLogger(__name__)
_REG_PARSE_KEY_VALUE = re.compile(r"(?P<key>.+?)(?:=)(?P<value>.+?)(?:;|$)")
_REG_PARSE_MALFORMED_JSON = re.compile(r'(?P<key>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)\s:\s(?P<value>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)')
_REG_PARSE_MALFORMED_JSON = re.compile(
r'(?P<key>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)\s:\s(?P<value>"[^"\\]*(?:\\.[^"\\]*)*"|[^\s"]+)'
)

def _event_lines(ret):
line = ""
Expand Down Expand Up @@ -229,17 +231,22 @@ def event_actions(self, eventcodes, retries=None, timeout_cmd=None):
for event_info in self.event_stream(eventcodes, retries, timeout_cmd):
_LOGGER.debug("%s event info: %r", self, event_info)
payload = dict()
for Key, Value in _REG_PARSE_KEY_VALUE.findall(event_info.strip().replace('\n', '')):
for Key, Value \
in _REG_PARSE_KEY_VALUE.findall(event_info.strip().replace('\n', '')):
if Key == 'data':
tmpData = dict()
for DataKey, DataValue in _REG_PARSE_MALFORMED_JSON.findall(Value):
tmpData[DataKey.replace('"', '')] = DataValue.replace('"', '')
Value = tmpData
tmpData = dict()
for DataKey, DataValue \
in _REG_PARSE_MALFORMED_JSON.findall(Value):
tmpData[DataKey.replace('"', '')] = DataValue.replace('"', '')
Value = tmpData
payload[Key] = Value
_LOGGER.debug("%s generate new event, code: %s , payload: %s", self, payload['Code'], payload)
_LOGGER.debug(
"%s generate new event, code: %s , payload: %s",
self, payload['Code'], payload
)
yield payload['Code'], payload



class NoHeaderErrorFilter(logging.Filter):
"""
Expand Down

0 comments on commit 8ebeb13

Please sign in to comment.