Skip to content

Commit

Permalink
Add implementation of a better XML to dict decoding
Browse files Browse the repository at this point in the history
This is the code proposed in sccn#36
  • Loading branch information
dojeda committed Feb 26, 2019
1 parent e209638 commit 26b2d86
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions Python/pyxdf/pyxdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import logging

import numpy as np
from xmljson import parker

__all__ = ['load_xdf']
__version__ = '1.14.0'
Expand Down Expand Up @@ -265,17 +266,18 @@ def __init__(self, xml):
if tag == 1:
# read [FileHeader] chunk
xml_string = f.read(chunklen - 2)
fileheader = _xml2dict(ET.fromstring(xml_string))
fileheader = _load_xml(ET.fromstring(xml_string))
elif tag == 2:
# read [StreamHeader] chunk...
# read [Content]
xml_string = f.read(chunklen - 6)
decoded_string = xml_string.decode('utf-8', 'replace')
hdr = _xml2dict(ET.fromstring(decoded_string))
hdr = _load_xml(ET.fromstring(decoded_string))
old_hdr = _xml2dict(ET.fromstring(decoded_string))
streams[StreamId] = hdr
logger.debug(' found stream ' + hdr['info']['name'][0])
logger.debug(' found stream ' + hdr['info']['name'])
# initialize per-stream temp data
temp[StreamId] = StreamData(hdr)
temp[StreamId] = StreamData(old_hdr)
elif tag == 3:
# read [Samples] chunk...
# noinspection PyBroadException
Expand Down Expand Up @@ -334,7 +336,7 @@ def __init__(self, xml):
elif tag == 6:
# read [StreamFooter] chunk
xml_string = f.read(chunklen - 6)
streams[StreamId]['footer'] = _xml2dict(ET.fromstring(xml_string))
streams[StreamId]['footer'] = _load_xml(ET.fromstring(xml_string))
elif tag == 4:
# read [ClockOffset] chunk
temp[StreamId].clock_times.append(struct.unpack('<d', f.read(8))[0])
Expand Down Expand Up @@ -415,6 +417,11 @@ def _xml2dict(t):
return {t.tag: dd or t.text}


def _load_xml(t):
"""Convert an attribute-less etree.Element into a dict."""
return parker.data(t, preserve_root=True)


def _scan_forward(f):
"""Scan forward through the given file object until after the next
boundary chunk."""
Expand Down

0 comments on commit 26b2d86

Please sign in to comment.