From 1e723f5d1812a4346284b7af6afe1e85530e8c0d Mon Sep 17 00:00:00 2001
From: Jan David Mol <mol@astron.nl>
Date: Tue, 14 May 2019 11:14:14 +0000
Subject: [PATCH] SW-699: Fixed syntax errors, put Messaging config in separate
 file to avoid circular imports

---
 .gitattributes                                |  1 +
 LCS/Messaging/python/messaging/CMakeLists.txt |  1 +
 LCS/Messaging/python/messaging/Service.py     |  2 +-
 LCS/Messaging/python/messaging/__init__.py    | 11 +---------
 LCS/Messaging/python/messaging/config.py      | 22 +++++++++++++++++++
 LCS/Messaging/python/messaging/messagebus.py  | 13 +----------
 .../TBB/TBBClient/lib/tbbbuslistener.py       |  2 +-
 MAC/Services/src/PipelineControl.py           |  2 +-
 QA/QA_Service/lib/QABusListener.py            |  2 +-
 .../MoMQueryServiceServer/momqueryservice.py  |  1 -
 SAS/OTDB_Services/OTDBBusListener.py          |  2 +-
 .../lib/RABusListener.py                      |  2 +-
 .../radbbuslistener.py                        |  2 +-
 .../lib/changeshandler.py                     |  2 +-
 .../lib/translation_service_rpc.py            |  2 +-
 15 files changed, 35 insertions(+), 32 deletions(-)
 create mode 100644 LCS/Messaging/python/messaging/config.py

diff --git a/.gitattributes b/.gitattributes
index daa0d11f273..9755cb69a44 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -1617,6 +1617,7 @@ LCS/Messaging/python/messaging/CMakeLists.txt -text
 LCS/Messaging/python/messaging/RPC.py -text
 LCS/Messaging/python/messaging/Service.py -text
 LCS/Messaging/python/messaging/__init__.py -text
+LCS/Messaging/python/messaging/config.py -text
 LCS/Messaging/python/messaging/exceptions.py -text
 LCS/Messaging/python/messaging/messagebus.py -text
 LCS/Messaging/python/messaging/messages.py -text
diff --git a/LCS/Messaging/python/messaging/CMakeLists.txt b/LCS/Messaging/python/messaging/CMakeLists.txt
index 7dc82934b1c..07133a8b9fd 100644
--- a/LCS/Messaging/python/messaging/CMakeLists.txt
+++ b/LCS/Messaging/python/messaging/CMakeLists.txt
@@ -11,6 +11,7 @@ include(PythonInstall)
 
 set(_py_files
   __init__.py
+  config.py
   exceptions.py
   messagebus.py
   messages.py
diff --git a/LCS/Messaging/python/messaging/Service.py b/LCS/Messaging/python/messaging/Service.py
index ca3269b829c..0440d91f6a9 100644
--- a/LCS/Messaging/python/messaging/Service.py
+++ b/LCS/Messaging/python/messaging/Service.py
@@ -122,7 +122,7 @@ class Service(AbstractBusListener):
         # then we need to listen to <servicename>.#
         if use_service_methods:
            subject = self.service_name + ".#"
-        else
+        else:
            subject = self.service_name
 
         # TODO: Create queue "address"
diff --git a/LCS/Messaging/python/messaging/__init__.py b/LCS/Messaging/python/messaging/__init__.py
index e0535559e7b..5a0ec953fee 100644
--- a/LCS/Messaging/python/messaging/__init__.py
+++ b/LCS/Messaging/python/messaging/__init__.py
@@ -24,25 +24,16 @@
 Module initialization file.
 """
 
+from .config import *
 from .exceptions import *
 from .messages import *
 from .messagebus import *
 from .RPC import *
 from .Service import *
 import logging
-from lofar.common import isProductionEnvironment, isTestEnvironment
 
 def setQpidLogLevel(qpidLogLevel):
     for name, logger in list(logging.Logger.manager.loggerDict.items()):
         if name.startswith('qpid.') and isinstance(logger, logging.Logger):
             logger.setLevel(qpidLogLevel)
 
-def adaptNameToEnvironment(name):
-    if isProductionEnvironment():
-        return name #return original name only for PRODUCTION LOFARENV
-
-    if isTestEnvironment():
-        return 'test.%s' % name #return 'test.' prefixed name only for TEST LOFARENV
-
-    # in all other cases prefix queue/bus name with 'devel.'
-    return 'devel.%s' % name
diff --git a/LCS/Messaging/python/messaging/config.py b/LCS/Messaging/python/messaging/config.py
new file mode 100644
index 00000000000..106cce1cc58
--- /dev/null
+++ b/LCS/Messaging/python/messaging/config.py
@@ -0,0 +1,22 @@
+from lofar.common import isProductionEnvironment, isTestEnvironment
+
+def adaptNameToEnvironment(name):
+    if isProductionEnvironment():
+        return name #return original name only for PRODUCTION LOFARENV
+
+    if isTestEnvironment():
+        return 'test.%s' % name #return 'test.' prefixed name only for TEST LOFARENV
+
+    # in all other cases prefix queue/bus name with 'devel.'
+    return 'devel.%s' % name
+
+# Default settings for often used parameters.
+if isProductionEnvironment():
+    DEFAULT_BROKER = "scu001.control.lofar"
+elif isTestEnvironment():
+    DEFAULT_BROKER = "scu199.control.lofar"
+else: # development environment
+    DEFAULT_BROKER = "localhost"
+
+# default exchange to use for all messages
+DEFAULT_BUSNAME = adaptNameToEnvironment("lofar")
diff --git a/LCS/Messaging/python/messaging/messagebus.py b/LCS/Messaging/python/messaging/messagebus.py
index bfabad7f914..fd05be61aea 100644
--- a/LCS/Messaging/python/messaging/messagebus.py
+++ b/LCS/Messaging/python/messaging/messagebus.py
@@ -28,7 +28,7 @@ Provide an easy way exchange messages on the message bus.
 
 from lofar.messaging.exceptions import MessageBusError, MessageFactoryError
 from lofar.messaging.messages import to_qpid_message, MESSAGE_FACTORY
-from lofar.messaging import adaptNameToEnvironment
+from lofar.messaging.config import DEFAULT_BROKER, DEFAULT_BUSNAME
 from lofar.common.util import raise_exception, is_iterable
 from lofar.common.datetimeutils import to_milliseconds_since_unix_epoch, from_milliseconds_since_unix_epoch
 from lofar.common import isProductionEnvironment, isTestEnvironment
@@ -45,17 +45,6 @@ from time import sleep
 
 logger = logging.getLogger(__name__)
 
-# Default settings for often used parameters.
-if isProductionEnvironment():
-    DEFAULT_BROKER = "scu001.control.lofar"
-elif isTestEnvironment():
-    DEFAULT_BROKER = "scu199.control.lofar"
-else: # development environment
-    DEFAULT_BROKER = "localhost"
-
-# default exchange to use for all messages
-DEFAULT_BUSNAME = adaptNameToEnvironment("lofar")
-
 DEFAULT_RECEIVER_CAPACITY = 1
 DEFAULT_TIMEOUT = 5
 
diff --git a/MAC/Services/TBB/TBBClient/lib/tbbbuslistener.py b/MAC/Services/TBB/TBBClient/lib/tbbbuslistener.py
index 5454bea0dc7..9ec25b6590d 100644
--- a/MAC/Services/TBB/TBBClient/lib/tbbbuslistener.py
+++ b/MAC/Services/TBB/TBBClient/lib/tbbbuslistener.py
@@ -36,7 +36,7 @@ class TBBBusListener(AbstractBusListener):
         :param subjects: the subjects filter string to listen for.
         :param broker: valid Qpid broker host
         """
-        super(TBBBusListener, self).__init__(address=busname, subject=DEFAULT_TBB_NOTIFICATION_PREFIX+"#", broker, **kwargs)
+        super(TBBBusListener, self).__init__(address=busname, subject=DEFAULT_TBB_NOTIFICATION_PREFIX+"#", broker=broker, **kwargs)
 
     def _handleMessage(self, msg):
         # try to handle an incoming message, and call the associated on<SomeMessage> method
diff --git a/MAC/Services/src/PipelineControl.py b/MAC/Services/src/PipelineControl.py
index e257974e2cb..b02f62f5f25 100755
--- a/MAC/Services/src/PipelineControl.py
+++ b/MAC/Services/src/PipelineControl.py
@@ -329,7 +329,7 @@ class PipelineDependencies(object):
     return startable
 
 class PipelineControl(OTDBBusListener):
-  def __init__(self, busname=DEFAULT_BUSNAME, broker=DEFAULT_BROKER)
+  def __init__(self, busname=DEFAULT_BUSNAME, broker=DEFAULT_BROKER):
     super(PipelineControl, self).__init__(busname=busname, broker=broker)
 
     logger.info('PipelineControl busname=%s', busname)
diff --git a/QA/QA_Service/lib/QABusListener.py b/QA/QA_Service/lib/QABusListener.py
index 9919f904c2f..82439da3bcb 100644
--- a/QA/QA_Service/lib/QABusListener.py
+++ b/QA/QA_Service/lib/QABusListener.py
@@ -46,7 +46,7 @@ class QABusListener(AbstractBusListener):
             numthreads= <int>  Number of parallel threads processing messages (default: 1)
             verbose=   <bool>  Output extra logging over stdout (default: False)
         """
-        super(QABusListener, self).__init__(address=busname, subject=DEFAULT_QA_NOTIFICATION_SUBJECT_PREFIX+"#", broker, **kwargs)
+        super(QABusListener, self).__init__(address=busname, subject=DEFAULT_QA_NOTIFICATION_SUBJECT_PREFIX+"#", broker=broker, **kwargs)
 
     def _handleMessage(self, msg):
         logger.debug("QABusListener.handleMessage: %s" %str(msg))
diff --git a/SAS/MoM/MoMQueryService/MoMQueryServiceServer/momqueryservice.py b/SAS/MoM/MoMQueryService/MoMQueryServiceServer/momqueryservice.py
index c174a11077e..7d614da6177 100755
--- a/SAS/MoM/MoMQueryService/MoMQueryServiceServer/momqueryservice.py
+++ b/SAS/MoM/MoMQueryService/MoMQueryServiceServer/momqueryservice.py
@@ -1486,7 +1486,6 @@ def main():
                       help='Address of the qpid broker, default: %default')
     parser.add_option("-b", "--busname", dest="busname", type="string", default=DEFAULT_BUSNAME,
                       help="Name of the bus exchange on the qpid broker, [default: %default]")
-                      help="Name for this service, [default: %default]")
     parser.add_option_group(dbcredentials.options_group(parser))
     parser.set_defaults(dbcredentials="MoM")
     (options, args) = parser.parse_args()
diff --git a/SAS/OTDB_Services/OTDBBusListener.py b/SAS/OTDB_Services/OTDBBusListener.py
index 96129e0b56d..a6a501e7695 100644
--- a/SAS/OTDB_Services/OTDBBusListener.py
+++ b/SAS/OTDB_Services/OTDBBusListener.py
@@ -50,7 +50,7 @@ class OTDBBusListener(AbstractBusListener):
             numthreads= <int>  Number of parallel threads processing messages (default: 1)
             verbose=   <bool>  Output extra logging over stdout (default: False)
         """
-        super(OTDBBusListener, self).__init__(address=busname, subject=DEFAULT_OTDB_NOTIFICATION_SUBJECT, broker, **kwargs)
+        super(OTDBBusListener, self).__init__(address=busname, subject=DEFAULT_OTDB_NOTIFICATION_SUBJECT, broker=broker, **kwargs)
 
     def _handleMessage(self, msg):
         logger.debug("OTDBBusListener.handleMessage: %s" %str(msg))
diff --git a/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RABusListener.py b/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RABusListener.py
index 829055dfe84..3977a3175b0 100644
--- a/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RABusListener.py
+++ b/SAS/ResourceAssignment/RATaskSpecifiedService/lib/RABusListener.py
@@ -52,7 +52,7 @@ class RATaskSpecifiedBusListener(AbstractBusListener):
             numthreads= <int>  Number of parallel threads processing messages (default: 1)
             verbose=   <bool>  Output extra logging over stdout (default: False)
         """
-        super(RATaskSpecifiedBusListener, self).__init__(address=busname, subject=DEFAULT_RA_TASK_SPECIFIED_NOTIFICATION_SUBJECT, broker, **kwargs)
+        super(RATaskSpecifiedBusListener, self).__init__(address=busname, subject=DEFAULT_RA_TASK_SPECIFIED_NOTIFICATION_SUBJECT, broker=broker, **kwargs)
 
     def _handleMessage(self, msg):
         logger.debug("RABusListener.handleMessage: %s" %str(msg))
diff --git a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radbbuslistener.py b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radbbuslistener.py
index f3c090d014d..6766435b6d8 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentDatabase/radbbuslistener.py
+++ b/SAS/ResourceAssignment/ResourceAssignmentDatabase/radbbuslistener.py
@@ -52,7 +52,7 @@ class RADBBusListener(AbstractBusListener):
             verbose=   <bool>  Output extra logging over stdout (default: False)
         """
 
-        super(RADBBusListener, self).__init__(address=busname, subject=DEFAULT_NOTIFICATION_PREFIX+"#", broker, **kwargs)
+        super(RADBBusListener, self).__init__(address=busname, subject=DEFAULT_NOTIFICATION_PREFIX+"#", broker=broker, **kwargs)
 
 
     def _handleMessage(self, msg):
diff --git a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/changeshandler.py b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/changeshandler.py
index 7bdb331f1cd..33bf4afcdc0 100644
--- a/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/changeshandler.py
+++ b/SAS/ResourceAssignment/ResourceAssignmentEditor/lib/changeshandler.py
@@ -52,7 +52,7 @@ CHANGE_DELETE_TYPE = 'delete'
 CHANGE_EVENT_TYPE = 'event'
 
 class ChangesHandler:
-    def __init__(self, busname=DEFAULT_BUSNAME
+    def __init__(self, busname=DEFAULT_BUSNAME,
                  broker=DEFAULT_BROKER, momqueryrpc=None, radbrpc=None, sqrpc=None, **kwargs):
         """
         ChangesHandler listens on the lofar notification message bus and keeps track of all the change notifications.
diff --git a/SAS/SpecificationServices/lib/translation_service_rpc.py b/SAS/SpecificationServices/lib/translation_service_rpc.py
index dda87435b95..a23ea36a9da 100644
--- a/SAS/SpecificationServices/lib/translation_service_rpc.py
+++ b/SAS/SpecificationServices/lib/translation_service_rpc.py
@@ -8,7 +8,7 @@ logger = logging.getLogger(__file__)
 
 class TranslationRPC(RPCWrapper):
 
-    def __init__(self, busname=DEFAULT_BUSNAME
+    def __init__(self, busname=DEFAULT_BUSNAME,
                  broker=DEFAULT_BROKER,
                  timeout=120):
         super(TranslationRPC, self).__init__(busname, SPECIFICATIONTRANSLATION_SERVICENAME, broker, timeout=timeout)
-- 
GitLab