From 92d6cf3ab3b03f47041a17cb4d4e57ef1edd4436 Mon Sep 17 00:00:00 2001
From: Philip ABBET <philip.abbet@idiap.ch>
Date: Wed, 18 Oct 2017 10:48:38 +0200
Subject: [PATCH] Add a 'StdoutDataSink' class for debugging purposes

---
 beat/backend/python/data.py | 75 +++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/beat/backend/python/data.py b/beat/backend/python/data.py
index c9a39cf..e65bc1b 100755
--- a/beat/backend/python/data.py
+++ b/beat/backend/python/data.py
@@ -47,6 +47,9 @@ from .dataformat import DataFormat
 from .algorithm import Algorithm
 
 
+#----------------------------------------------------------
+
+
 class DataSource(object):
 
     """Interface of all the Data Sources
@@ -74,6 +77,9 @@ class DataSource(object):
         pass
 
 
+#----------------------------------------------------------
+
+
 class DataSink(object):
 
     """Interface of all the Data Sinks
@@ -103,6 +109,57 @@ class DataSink(object):
         pass
 
 
+#----------------------------------------------------------
+
+
+class StdoutDataSink(DataSink):
+
+    """Data Sink that prints informations about the written data on stdout
+
+    Note: The written data is lost! Use ii for debugging purposes
+    """
+
+    def __init__(self):
+        super(StdoutDataSink, self).__init__()
+        self.dataformat = None
+        self.prefix = ''
+        self.display_data = True
+
+    def setup(self, dataformat, prefix=None, display_data=True):
+        self.dataformat = dataformat
+        self.display_data = display_data
+
+        if prefix is not None:
+            self.prefix = prefix
+            if self.prefix != '':
+                self.prefix += ' '
+
+    def write(self, data, start_data_index, end_data_index):
+        """Write a block of data
+
+        Parameters:
+
+          data (beat.core.baseformat.baseformat) The block of data to write
+
+          start_data_index (int): Start index of the written data
+
+          end_data_index (int): End index of the written data
+
+        """
+
+        if self.display_data:
+            print '%s(%d -> %d): %s' % (self.prefix, start_data_index, end_data_index, str(data))
+        else:
+            print '%s(%d -> %d): <data>' % (self.prefix, start_data_index, end_data_index)
+
+
+    def isConnected(self):
+        return True
+
+
+#----------------------------------------------------------
+
+
 class CachedDataSource(DataSource):
     """Data Source that load data from the Cache"""
 
@@ -445,6 +502,9 @@ class CachedDataSource(DataSource):
         self.preloaded = True
 
 
+#----------------------------------------------------------
+
+
 class CachedDataSink(DataSink):
 
     """Data Sink that save data in the Cache
@@ -753,6 +813,9 @@ class CachedDataSink(DataSink):
         return (self.filename is not None)
 
 
+#----------------------------------------------------------
+
+
 class MemoryDataSource(DataSource):
 
     """Interface of all the Data Sources
@@ -796,6 +859,9 @@ class MemoryDataSource(DataSource):
         return (0, 0)
 
 
+#----------------------------------------------------------
+
+
 class MemoryDataSink(DataSink):
 
     """Data Sink that directly transmit data to associated MemoryDataSource
@@ -831,6 +897,9 @@ class MemoryDataSink(DataSink):
         return len(self.data_sources) > 0
 
 
+#----------------------------------------------------------
+
+
 def load_data_index(cache_prefix, hash_path):
     """Loads a cached-data index if it exists. Returns empty otherwise.
 
@@ -885,6 +954,9 @@ def load_data_index(cache_prefix, hash_path):
     return sorted(retval) + [end_index + 1]
 
 
+#----------------------------------------------------------
+
+
 def _foundCommonIndices(lst):
     """Returns the list of common indices, given a list of list of indices
     """
@@ -896,6 +968,9 @@ def _foundCommonIndices(lst):
     return common_indices
 
 
+#----------------------------------------------------------
+
+
 def foundSplitRanges(lst, n_split):
     """Splits a list of lists of indices into n splits for parallelization
     purposes. """
-- 
GitLab