info_file_proxy.py 4.23 KB
Newer Older
1 2 3 4
# -*- coding: utf-8 -*-
# Copyright (c) 2016-2018 Braintech Sp. z o.o. [Ltd.] <http://www.braintech.pl>
# All rights reserved.

5 6
"""
Module implements specific info file manifest for Svarog.
7 8 9 10 11 12

What is different from info_file_proxy:

* root name (here we haver rs:rawSignal and xmlns)
* tags namespace (here every tag has rs: previx)
* tags ordering (svarog requires specific tags ordering)
13
* additional tags (svarog requires only and only tags described in SvarogFileWriteProxy.ORDER)
14

15 16 17
Author:
   Mateusz Kruszyński <mateusz.kruszynski@gmail.com>
"""
18 19
from . import generic_info_file_proxy
from . import signal_logging as logger
20

21
LOGGER = logger.get_logger('info_file_proxy')
22 23 24


class InfoDocument(generic_info_file_proxy.OpenBciDocument):
25 26
    """Subclass xml_document, so that we can add rs: prefix before every tag name."""

27 28 29 30
    prefix = 'rs:'

    def createElement(self, tagName):
        """Redefine the method so that every added tag has 'rs:' prefix."""
31
        return super(InfoDocument, self).createElement(''.join([InfoDocument.prefix, tagName]))
32 33 34


class InfoFileWriteProxy(generic_info_file_proxy.GenericInfoFileWriteProxy):
35
    """
36 37 38 39 40
    Subclass write proxy.

    *ensure that every element has rs: prefix
    *ensure tags ordering
    *ensure tags required by svarog
41
    """
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    ORDER = [
        'rs:exportFileName',
        'rs:sourceFileName',
        'rs:sourceFileFormat',
        'rs:samplingFrequency',
        'rs:channelCount',
        'rs:sampleCount',
        'rs:calibration',
        'rs:sampleType',
        'rs:byteOrder',
        'rs:pageSize',
        'rs:blocksPerPage',
        'rs:channelLabels',
        'rs:calibrationGain',
        'rs:calibrationOffset',
        # 'rs:amplifierNull',
        'rs:firstSampleTimestamp',
        'rs:videoFileName',
        'rs:videoFileOffset',
    ]

    def _create_xml_factory(self):
        """Redefine the method - return Svarog document."""
        return InfoDocument()

    def _set_remaining_tags(self):
69
        """Set all default (hardcoded) tags and other tags as now we we have all needed data."""
70 71 72 73 74 75 76 77 78 79 80 81 82
        self.set_attributes({
            'file_format': [''],
            'export_file_name': 'name',
            'calibration': 1.0,
            'sample_type': 'DOUBLE',
            'byte_order': 'LITTLE_ENDIAN',
            'page_size': 20.0,
            'blocks_per_page': 5,
            # 'export_date':'2010-04-26T11:02:51'
        })
        self._reorder_elements()

    def _reorder_elements(self):
83
        """Redefine self._xml_factory so that it has tags required by svarog in required order."""
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        new_factory = self._create_xml_factory()
        l_xml_root = new_factory.createElement('rawSignal')
        l_xml_root.setAttribute('xmlns:rs',
                                "http://signalml.org/rawsignal")
        new_factory.appendChild(l_xml_root)

        elems = []
        for order_key in InfoFileWriteProxy.ORDER:
            try:
                elem = self._xml_factory.getElementsByTagName(order_key)[0]
                elems.append(elem)
                l_xml_root.appendChild(elem)
            except IndexError:
                LOGGER.warning("Couldn`t find '" + str(order_key) +
                               "' attribute. Created info file will not be totally correct!!!!")

        self._xml_factory = new_factory


class InfoFileReadProxy(generic_info_file_proxy.GenericInfoFileReadProxy):
104
    """Sublassed read proxy - every `get_` method need to be subclassed to add rs: prefix before tag name."""
105 106

    def _get_simple_param(self, p_param_name):
107 108 109 110 111 112
        """
        Return text value from tag in format in specific format.

         <param id=p_param_name>text_value</param>.
        """
        return super(InfoFileReadProxy, self)._get_simple_param(''.join([InfoDocument.prefix, p_param_name]))
113 114

    def _get_list_param(self, p_param_name, p_subparam_name):
115 116 117
        """
        Return a list of text values form tag in specific format.

118 119 120 121 122 123
        <p_param_name>
            <param>text value1</param>
            <param>text value2</param>
            ...
        </p_param_name>
        """
124 125
        return super(InfoFileReadProxy, self)._get_list_param(''.join([InfoDocument.prefix, p_param_name]),
                                                              ''.join([InfoDocument.prefix, p_subparam_name]))