From 7e6d6bc9b8db86c94b00531f36446a163bbafd6d Mon Sep 17 00:00:00 2001
From: David Hammer <dhammer@mailbox.org>
Date: Thu, 2 Dec 2021 16:14:59 +0100
Subject: [PATCH] Conform to Karabo best practices (round 1 from @flucke's
 comments)

Importing State from bound, using OVERWRITE_ELEMENT, letting framework handle
inheriting schemata instead of calling superclass expectedParameters.
---
 src/calng/AgipdCorrection.py | 25 ++++++++++++++++++++-----
 src/calng/DsscCorrection.py  | 30 ++++++++++++++++++++++++------
 src/calng/ModuleStacker.py   |  1 -
 src/calng/base_correction.py |  2 +-
 4 files changed, 45 insertions(+), 13 deletions(-)

diff --git a/src/calng/AgipdCorrection.py b/src/calng/AgipdCorrection.py
index b4a6d22d..9777cb8f 100644
--- a/src/calng/AgipdCorrection.py
+++ b/src/calng/AgipdCorrection.py
@@ -9,10 +9,11 @@ from karabo.bound import (
     KARABO_CLASSINFO,
     NODE_ELEMENT,
     OUTPUT_CHANNEL,
+    OVERWRITE_ELEMENT,
     STRING_ELEMENT,
     VECTOR_STRING_ELEMENT,
+    State,
 )
-from karabo.common.states import State
 
 from . import base_gpu, calcat_utils, utils
 from ._version import version as deviceVersion
@@ -345,7 +346,13 @@ class AgipdCalcatFriend(calcat_utils.BaseCalcatFriend):
         super(AgipdCalcatFriend, AgipdCalcatFriend).add_schema(
             schema, managed_keys, "AGIPD-Type", param_prefix, status_prefix
         )
-        schema.setDefaultValue(f"{param_prefix}.memoryCells", 352)
+
+        (
+            OVERWRITE_ELEMENT(schema)
+            .key(f"{param_prefix}.memoryCells")
+            .setNewDefaultValue(352)
+            .commit()
+        )
 
         (
             DOUBLE_ELEMENT(schema)
@@ -436,9 +443,17 @@ class AgipdCorrection(BaseCorrection):
 
     @staticmethod
     def expectedParameters(expected):
-        super(AgipdCorrection, AgipdCorrection).expectedParameters(expected)
-        expected.setDefaultValue("dataFormat.memoryCells", 352)
-        expected.setDefaultValue("preview.selectionMode", "cell")
+        (
+            OVERWRITE_ELEMENT(expected)
+            .key("dataFormat.memoryCells")
+            .setNewDefaultValue(352)
+            .commit(),
+
+            OVERWRITE_ELEMENT(expected)
+            .key("preview.selectionMode")
+            .setNewDefaultValue("cell")
+            .commit(),
+        )
         # WIP: fixed gain mode
         (
             STRING_ELEMENT(expected)
diff --git a/src/calng/DsscCorrection.py b/src/calng/DsscCorrection.py
index 9543268e..8785c6ef 100644
--- a/src/calng/DsscCorrection.py
+++ b/src/calng/DsscCorrection.py
@@ -2,8 +2,13 @@ import enum
 
 import cupy
 import numpy as np
-from karabo.bound import DOUBLE_ELEMENT, KARABO_CLASSINFO, VECTOR_STRING_ELEMENT
-from karabo.common.states import State
+from karabo.bound import (
+    DOUBLE_ELEMENT,
+    KARABO_CLASSINFO,
+    OVERWRITE_ELEMENT,
+    VECTOR_STRING_ELEMENT,
+    State,
+)
 
 from . import base_gpu, calcat_utils, utils
 from ._version import version as deviceVersion
@@ -114,7 +119,12 @@ class DsscCalcatFriend(calcat_utils.BaseCalcatFriend):
         super(DsscCalcatFriend, DsscCalcatFriend).add_schema(
             schema, managed_keys, "DSSC-Type", param_prefix, status_prefix
         )
-        schema.setDefaultValue(f"{param_prefix}.memoryCells", 400)
+        (
+            OVERWRITE_ELEMENT(schema)
+            .key(f"{param_prefix}.memoryCells")
+            .setNewDefaultValue(400)
+            .commit()
+        )
         (
             DOUBLE_ELEMENT(schema)
             .key(f"{param_prefix}.pulseIdChecksum")
@@ -161,9 +171,17 @@ class DsscCorrection(BaseCorrection):
 
     @staticmethod
     def expectedParameters(expected):
-        super(DsscCorrection, DsscCorrection).expectedParameters(expected)
-        expected.setDefaultValue("dataFormat.memoryCells", 400)
-        expected.setDefaultValue("preview.selectionMode", "pulse")
+        (
+            OVERWRITE_ELEMENT(expected)
+            .key("dataFormat.memoryCells")
+            .setNewDefaultValue(400)
+            .commit(),
+
+            OVERWRITE_ELEMENT(expected)
+            .key("preview.selectionMode")
+            .setNewDefaultValue("pulse")
+            .commit(),
+        )
         DsscCalcatFriend.add_schema(expected, DsscCorrection._managed_keys)
         add_correction_step_schema(
             expected,
diff --git a/src/calng/ModuleStacker.py b/src/calng/ModuleStacker.py
index 6a3a48ca..95abe95e 100644
--- a/src/calng/ModuleStacker.py
+++ b/src/calng/ModuleStacker.py
@@ -31,7 +31,6 @@ class ModuleStacker(TrainMatcher.TrainMatcher):
 
     @staticmethod
     def expectedParameters(expected):
-        super(ModuleStacker, ModuleStacker).expectedParameters(expected)
         (
             FLOAT_ELEMENT(expected)
             .key("timeOfFlight")
diff --git a/src/calng/base_correction.py b/src/calng/base_correction.py
index bcd9db80..23943d33 100644
--- a/src/calng/base_correction.py
+++ b/src/calng/base_correction.py
@@ -26,12 +26,12 @@ from karabo.bound import (
     MetricPrefix,
     PythonDevice,
     Schema,
+    State,
     Timestamp,
     Trainstamp,
     Unit,
 )
 from karabo.common.api import KARABO_SCHEMA_DISPLAY_TYPE_SCENES as DT_SCENES
-from karabo.common.states import State
 from karabo import version as karaboVersion
 from pkg_resources import parse_version
 
-- 
GitLab