import json

from pandajedi.jedimsgprocessor.base_msg_processor import BaseMsgProcPlugin
from pandajedi.jedimsgprocessor.tape_carousel_msg_processor import TapeCarouselMsgProcPlugin
from pandajedi.jedimsgprocessor.hpo_msg_processor import HPOMsgProcPlugin

from pandacommon.pandalogger import logger_utils


# logger
base_logger = logger_utils.setup_logger(__name__.split('.')[-1])


# Atlas iDDS message processing plugin, a bridge connect to other idds related message processing plugins
class AtlasIddsMsgProcPlugin(BaseMsgProcPlugin):

    def initialize(self):
        self.plugin_TapeCarousel = TapeCarouselMsgProcPlugin()
        self.plugin_TapeCarousel.initialize()
        self.plugin_HPO = HPOMsgProcPlugin()
        self.plugin_HPO.initialize()

    def process(self, msg_obj):
        # logger
        tmp_log = logger_utils.make_logger(base_logger, method_name='process')
        # start
        tmp_log.info('start')
        tmp_log.debug('sub_id={0} ; msg_id={1}'.format(msg_obj.sub_id, msg_obj.msg_id))
        # parse json
        try:
            msg_dict = json.loads(msg_obj.data)
        except Exception as e:
            err_str = 'failed to parse message json {2} , skipped. {0} : {1}'.format(e.__class__.__name__, e, msg_obj.data)
            tmp_log.error(err_str)
            raise
        # sanity check
        try:
            msg_type = msg_dict['msg_type']
        except Exception as e:
            err_str = 'failed to parse message object dict {2} , skipped. {0} : {1}'.format(e.__class__.__name__, e, msg_dict)
            tmp_log.error(err_str)
            raise
        # run different plugins according to message type
        try:
            if msg_type in ('file_stagein', 'collection_stagein'):
                self.plugin_TapeCarousel.process(msg_obj, decoded_data=msg_dict)
            elif msg_type in ('file_hyperparameteropt', 'collection_hyperparameteropt'):
                self.plugin_HPO.process(msg_obj, decoded_data=msg_dict)
            else:
                raise ValueError('invalid msg_type value: {0}'.format(msg_type))
        except Exception:
            raise
        # done
        tmp_log.info('done')

@@ -559,16 +559,59 @@
 (self):%0A
+        BaseMsgProcPlugin.initialize(self)%0A
         
@@ -685,29 +685,149 @@
 .plugin_
-TapeCarousel.
+HPO = HPOMsgProcPlugin()%0A        # for each plugin%0A        for _plugin in %5Bself.plugin_TapeCarousel, self.plugin_HPO%5D:%0A            # 
 initiali
@@ -828,18 +828,21 @@
 itialize
-()
+ each
 %0A       
@@ -846,81 +846,218 @@
     
-self.
+    _
 plugin
-_HPO = HPOMsgProcPlugin()%0A        self.plugin_HPO.initialize()
+.initialize()%0A            # use the same taskBuffer interface%0A            try:%0A                del _plugin.tbIF%0A            except Exception:%0A                pass%0A            _plugin.tbIF = self.tbIF
 %0A%0A  

save some sessions