From 606b07e0573adb8b2cf485b45919bca9c6ba1b77 Mon Sep 17 00:00:00 2001
From: lukken <lukken@astron.nl>
Date: Tue, 19 Oct 2021 08:25:31 +0000
Subject: [PATCH] L2SS-287: Create entrypoint wrapper and remove duplicate code

---
 .../tangostationcontrol/common/entrypoint.py  | 21 +++++++++++++++++++
 .../tangostationcontrol/devices/boot.py       |  9 +++-----
 .../devices/docker_device.py                  | 10 +++------
 .../devices/observation.py                    |  7 ++++---
 .../devices/observation_control.py            |  5 +++--
 .../devices/opcua_device.py                   |  9 ++++----
 .../tangostationcontrol/devices/recv.py       | 11 ++++------
 .../tangostationcontrol/devices/sdp/sdp.py    | 12 +++++------
 .../tangostationcontrol/devices/sdp/sst.py    | 10 ++++-----
 .../tangostationcontrol/devices/sdp/xst.py    |  9 +++-----
 .../tangostationcontrol/devices/unb2.py       |  7 ++-----
 11 files changed, 56 insertions(+), 54 deletions(-)
 create mode 100644 tangostationcontrol/tangostationcontrol/common/entrypoint.py

diff --git a/tangostationcontrol/tangostationcontrol/common/entrypoint.py b/tangostationcontrol/tangostationcontrol/common/entrypoint.py
new file mode 100644
index 000000000..d5b9d4952
--- /dev/null
+++ b/tangostationcontrol/tangostationcontrol/common/entrypoint.py
@@ -0,0 +1,21 @@
+import sys
+
+from tango.server import run
+
+from tangostationcontrol.common.lofar_logging import configure_logger
+
+
+def entry(Device, **kwargs):
+    """General device entrypoint"""
+
+    # Remove first argument which is filename
+    args = sys.argv[1:]
+
+    # Setup logging
+    configure_logger()
+
+    # Start the device server
+    if type(Device) is tuple:
+        return run(Device, args=args, **kwargs)
+    else:
+        return run((Device,), args=args, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/boot.py b/tangostationcontrol/tangostationcontrol/devices/boot.py
index 6909e2bab..c4021d9d9 100644
--- a/tangostationcontrol/tangostationcontrol/devices/boot.py
+++ b/tangostationcontrol/tangostationcontrol/devices/boot.py
@@ -23,6 +23,7 @@ import numpy
 
 from device_decorators import *
 
+from tangostationcontrol.common.entry import entry
 from tangostationcontrol.devices.hardware_device import hardware_device
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
 
@@ -276,10 +277,6 @@ class Boot(hardware_device):
 # ----------
 # Run server
 # ----------
-def main(args=None, **kwargs):
+def main(**kwargs):
     """Main function of the Boot module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((Boot,), args=args, **kwargs)
+    return entry(Boot, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/docker_device.py b/tangostationcontrol/tangostationcontrol/devices/docker_device.py
index 5945a152b..5f08204d8 100644
--- a/tangostationcontrol/tangostationcontrol/devices/docker_device.py
+++ b/tangostationcontrol/tangostationcontrol/devices/docker_device.py
@@ -22,11 +22,11 @@ import asyncio
 from device_decorators import *
 
 # Additional import
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.clients.docker_client import DockerClient
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.hardware_device import hardware_device
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
-from tangostationcontrol.common.lofar_version import get_version
 
 __all__ = ["Docker", "main"]
 
@@ -122,10 +122,6 @@ class Docker(hardware_device):
 # ----------
 # Run server
 # ----------
-def main(args=None, **kwargs):
+def main(**kwargs):
     """Main function of the Docker module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((Docker,), args=args, **kwargs)
+    return entry(Docker, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/observation.py b/tangostationcontrol/tangostationcontrol/devices/observation.py
index 86a730320..c5b6d4693 100644
--- a/tangostationcontrol/tangostationcontrol/devices/observation.py
+++ b/tangostationcontrol/tangostationcontrol/devices/observation.py
@@ -11,9 +11,10 @@ from tango.server import Device, run, command, attribute
 import numpy
 from time import time
 
-from tangostationcontrol.devices.device_decorators import *
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
 from tangostationcontrol.common.lofar_version import get_version
+from tangostationcontrol.devices.device_decorators import *
 
 from json import loads
 
@@ -110,6 +111,6 @@ class Observation(Device):
 # ----------
 # Run server
 # ----------
-def main(args = None, **kwargs):
+def main(**kwargs):
     """Main function of the ObservationControl module."""
-    return run((Observation,), args = args, **kwargs)
+    return entry(Observation, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/observation_control.py b/tangostationcontrol/tangostationcontrol/devices/observation_control.py
index 5676a7a18..869db4e92 100644
--- a/tangostationcontrol/tangostationcontrol/devices/observation_control.py
+++ b/tangostationcontrol/tangostationcontrol/devices/observation_control.py
@@ -14,6 +14,7 @@ import numpy
 import time
 from json import loads
 
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
 from tangostationcontrol.common.lofar_version import get_version
 from tangostationcontrol.devices.device_decorators import *
@@ -437,6 +438,6 @@ class ObservationControl(Device):
 # ----------
 # Run server
 # ----------
-def main(args = None, **kwargs):
+def main(**kwargs):
     """Main function of the ObservationControl module."""
-    return run((ObservationControl, Observation), verbose = True, args = args, **kwargs)
+    return entry((ObservationControl, Observation), verbose=True, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/opcua_device.py b/tangostationcontrol/tangostationcontrol/devices/opcua_device.py
index 078e21b24..a6c3bfa28 100644
--- a/tangostationcontrol/tangostationcontrol/devices/opcua_device.py
+++ b/tangostationcontrol/tangostationcontrol/devices/opcua_device.py
@@ -25,11 +25,10 @@ import numpy
 import asyncio
 # Additional import
 
-from devices.device_decorators import *
-
-from clients.opcua_client import OPCUAConnection
-from devices.hardware_device import hardware_device
-from common.lofar_logging import device_logging_to_python, log_exceptions
+from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
+from tangostationcontrol.clients.opcua_client import OPCUAConnection
+from tangostationcontrol.devices.device_decorators import *
+from tangostationcontrol.devices.hardware_device import hardware_device
 
 __all__ = ["opcua_device", "main"]
 
diff --git a/tangostationcontrol/tangostationcontrol/devices/recv.py b/tangostationcontrol/tangostationcontrol/devices/recv.py
index ca0d3938b..f4f94b2e7 100644
--- a/tangostationcontrol/tangostationcontrol/devices/recv.py
+++ b/tangostationcontrol/tangostationcontrol/devices/recv.py
@@ -19,8 +19,9 @@ from tango import AttrWriteType
 import numpy
 
 # Additional import
-from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
+from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.device_decorators import *
 from tangostationcontrol.devices.opcua_device import opcua_device
 
@@ -257,10 +258,6 @@ class RECV(opcua_device):
 # ----------
 # Run server
 # ----------
-def main(args=None, **kwargs):
+def main(**kwargs):
     """Main function of the RECV module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((RECV,), args=args, **kwargs)
+    return entry(RECV, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py b/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py
index 661498183..8d51cbdb1 100644
--- a/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py
+++ b/tangostationcontrol/tangostationcontrol/devices/sdp/sdp.py
@@ -19,12 +19,14 @@ from tango import AttrWriteType
 # Additional import
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.opcua_device import opcua_device
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
 
 import numpy
 
 __all__ = ["SDP", "main"]
 
+
 @device_logging_to_python()
 class SDP(opcua_device):
     # -----------------
@@ -69,7 +71,7 @@ class SDP(opcua_device):
         mandatory=False,
         default_value=[[0.0] * 12] * 16
     )
-    
+
     FPGA_sdp_info_station_id_RW_default = device_property(
         dtype='DevVarULongArray',
         mandatory=True
@@ -171,10 +173,6 @@ class SDP(opcua_device):
 # ----------
 # Run server
 # ----------
-def main(args=None, **kwargs):
+def main(**kwargs):
     """Main function of the SDP module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((SDP,), args=args, **kwargs)
+    return entry(SDP, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/sdp/sst.py b/tangostationcontrol/tangostationcontrol/devices/sdp/sst.py
index 1cca268d2..08613015d 100644
--- a/tangostationcontrol/tangostationcontrol/devices/sdp/sst.py
+++ b/tangostationcontrol/tangostationcontrol/devices/sdp/sst.py
@@ -17,6 +17,7 @@ from tango.server import device_property, attribute
 from tango import AttrWriteType
 # Additional import
 
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.clients.opcua_client import OPCUAConnection
 from tangostationcontrol.clients.statistics_client import StatisticsClient
@@ -27,6 +28,7 @@ import numpy
 
 __all__ = ["SST", "main"]
 
+
 class SST(Statistics):
 
     STATISTICS_COLLECTOR_CLASS = SSTCollector
@@ -113,10 +115,6 @@ class SST(Statistics):
 # ----------
 # Run server
 # ----------
-def main(args=None, **kwargs):
+def main(**kwargs):
     """Main function of the SST Device module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((SST,), args=args, **kwargs)
+    return entry(SST, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/sdp/xst.py b/tangostationcontrol/tangostationcontrol/devices/sdp/xst.py
index 98d0725d2..4bbdee6a6 100644
--- a/tangostationcontrol/tangostationcontrol/devices/sdp/xst.py
+++ b/tangostationcontrol/tangostationcontrol/devices/sdp/xst.py
@@ -17,6 +17,7 @@ from tango.server import device_property, attribute
 from tango import AttrWriteType
 
 # Additional import
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.clients.opcua_client import OPCUAConnection
 from tangostationcontrol.clients.statistics_client import StatisticsClient
@@ -151,10 +152,6 @@ class XST(Statistics):
 # ----------
 # Run server
 # ----------
-def main(args=None, **kwargs):
+def main(**kwargs):
     """Main function of the XST Device module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((XST,), args=args, **kwargs)
+    return entry(XST, **kwargs)
diff --git a/tangostationcontrol/tangostationcontrol/devices/unb2.py b/tangostationcontrol/tangostationcontrol/devices/unb2.py
index 495c80da6..4250e3c99 100644
--- a/tangostationcontrol/tangostationcontrol/devices/unb2.py
+++ b/tangostationcontrol/tangostationcontrol/devices/unb2.py
@@ -17,6 +17,7 @@ from tango.server import device_property, attribute
 from tango import AttrWriteType
 # Additional import
 
+from tangostationcontrol.common.entrypoint import entry
 from tangostationcontrol.clients.attribute_wrapper import attribute_wrapper
 from tangostationcontrol.devices.opcua_device import opcua_device
 from tangostationcontrol.common.lofar_logging import device_logging_to_python, log_exceptions
@@ -146,8 +147,4 @@ class UNB2(opcua_device):
 # ----------
 def main(args=None, **kwargs):
     """Main function of the UNB2 module."""
-
-    from tangostationcontrol.common.lofar_logging import configure_logger
-    configure_logger()
-
-    return run((UNB2,), args=args, **kwargs)
+    return entry(UNB2, **kwargs)
-- 
GitLab