From e3d4a37fc09447a287e8d13b191d1672283a20d2 Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Tue, 5 Sep 2023 17:04:29 +0200
Subject: [PATCH] PickyBoi: add source filter, format

---
 src/calng/PickyBoi.py | 49 ++++++++++++++++++++++++++++++++++---------
 1 file changed, 39 insertions(+), 10 deletions(-)

diff --git a/src/calng/PickyBoi.py b/src/calng/PickyBoi.py
index 034d5770..a5706961 100644
--- a/src/calng/PickyBoi.py
+++ b/src/calng/PickyBoi.py
@@ -1,3 +1,5 @@
+import re
+
 import numpy as np
 from karabo.bound import (
     BOOL_ELEMENT,
@@ -66,7 +68,10 @@ class PickyBoi(PythonDevice):
 
             BOOL_ELEMENT(expected)
             .key("breakStreamAfterEnd")
-            .description("If active, end the output stream after the range of selected train IDs has been forwarded.")
+            .description(
+                "If active, end the output stream after the range of selected train "
+                "IDs has been forwarded."
+            )
             .assignmentOptional()
             .defaultValue(False)
             .reconfigurable()
@@ -120,6 +125,17 @@ class PickyBoi(PythonDevice):
             .reconfigurable()
             .commit(),
 
+            STRING_ELEMENT(expected)
+            .key("sourceFilterRegex")
+            .assignmentOptional()
+            .defaultValue("")
+            .description(
+                "If set, only data from sources matching this regex will be forwarded. "
+                "If not set, all sources are forwarded."
+            )
+            .reconfigurable()
+            .commit(),
+
             INT64_ELEMENT(expected)
             .key("ppuTrainOffset")
             .description(
@@ -249,6 +265,12 @@ class PickyBoi(PythonDevice):
             data = input_channel.read(input_index)
             meta = all_metadata[input_index]
             source = meta.get("source")
+
+            if (source_re := self.unsafe_get("sourceFilterRegex")) and not re.match(
+                source_re, source
+            ):
+                continue
+
             current_tid = Timestamp.fromHashAttributes(
                 meta.getAttributes("timestamp")
             ).getTrainId()
@@ -304,10 +326,11 @@ class PickyBoi(PythonDevice):
 
                     # if breakStreamAfterEnd is set, end the stream
                     # and stop acquiring
-                    if (self.get("breakStreamAfterEnd")
-                        and state is State.ACQUIRING):
+                    if self.get("breakStreamAfterEnd") and state is State.ACQUIRING:
                         self.signalEndOfStream("output")
-                        self.log.INFO("End of stream reached, following setting breakStreamAfterEnd.")
+                        self.log.INFO(
+                            "End of stream reached, following setting breakStreamAfterEnd."
+                        )
                     self.updateState(State.PASSIVE)
             elif target_tid > current_tid:
                 # wait
@@ -318,14 +341,20 @@ class PickyBoi(PythonDevice):
 
                 # if breakStreamAfterEnd is set, end the stream
                 # and stop acquiring
-                last_train_to_get = (max(self._trains_to_get)
-                                     if len(self._trains_to_get) > 0
-                                     else current_tid)
-                if (self.get("breakStreamAfterEnd")
+                last_train_to_get = (
+                    max(self._trains_to_get)
+                    if len(self._trains_to_get) > 0
+                    else current_tid
+                )
+                if (
+                    self.get("breakStreamAfterEnd")
                     and current_tid > last_train_to_get
-                    and state is State.ACQUIRING):
+                    and state is State.ACQUIRING
+                ):
                     self.signalEndOfStream("output")
-                    self.log.INFO("End of stream reached, following setting breakStreamAfterEnd.")
+                    self.log.INFO(
+                        "End of stream reached, following setting breakStreamAfterEnd."
+                    )
                     self.updateState(State.PASSIVE)
                     state = self.get("state")
 
-- 
GitLab