def test_bad_config(self):
        """
        Test that configurations with a missing data particle dict and missing
        data particle class key causes a configuration exception
        """
        # test a config with a missing particle classes dict
        config = {}
        stream_handle = open(os.path.join(RESOURCE_PATH,
                                          'short_SNA_SNA.txt'), 'r')

        with self.assertRaises(ConfigurationException):
            self.parser = NutnrJCsppParser(config, None, stream_handle,
                                           self.state_callback, self.pub_callback,
                                           self.exception_callback)

        # test a config with a missing data particle class key
        config = {
            DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                METADATA_PARTICLE_CLASS_KEY: NutnrJCsppMetadataTelemeteredDataParticle,
            }
        }

        with self.assertRaises(ConfigurationException):
            self.parser = NutnrJCsppParser(config, None, stream_handle,
                                           self.state_callback, self.pub_callback,
                                           self.exception_callback)
    def create_parser(self, stream_handle, telem_flag=True, state=None):
        """
        Initialize the parser with the given stream handle, using the
        telemetered config if the flag is set, recovered if it is not
        """
        if telem_flag:
            # use telemetered config
            config = {
                DataSetDriverConfigKeys.PARTICLE_MODULE: 'mi.dataset.parser.nutnr_j_cspp',
                DataSetDriverConfigKeys.PARTICLE_CLASS: None,
                DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                    METADATA_PARTICLE_CLASS_KEY: NutnrJCsppMetadataTelemeteredDataParticle,
                    DATA_PARTICLE_CLASS_KEY: NutnrJCsppTelemeteredDataParticle
                }
            }
        else:
            # use recovered config
            config = {
                DataSetDriverConfigKeys.PARTICLE_MODULE: 'mi.dataset.parser.nutnr_j_cspp',
                DataSetDriverConfigKeys.PARTICLE_CLASS: None,
                DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                    METADATA_PARTICLE_CLASS_KEY: NutnrJCsppMetadataRecoveredDataParticle,
                    DATA_PARTICLE_CLASS_KEY: NutnrJCsppRecoveredDataParticle
                }
            }

        self.parser = NutnrJCsppParser(config, state, stream_handle, 
                                  self.state_callback, self.pub_callback,
                                  self.exception_callback)
    def create_parser(self, stream_handle, telem_flag=True):
        """
        Initialize the parser with the given stream handle, using the
        telemetered config if the flag is set, recovered if it is not
        """
        if telem_flag:
            # use telemetered config
            config = {
                DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                    METADATA_PARTICLE_CLASS_KEY:
                    NutnrJCsppMetadataTelemeteredDataParticle,
                    LIGHT_PARTICLE_CLASS_KEY:
                    NutnrJCsppTelemeteredDataParticle,
                    DARK_PARTICLE_CLASS_KEY:
                    NutnrJCsppDarkTelemeteredDataParticle
                }
            }
        else:
            # use recovered config
            config = {
                DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                    METADATA_PARTICLE_CLASS_KEY:
                    NutnrJCsppMetadataRecoveredDataParticle,
                    LIGHT_PARTICLE_CLASS_KEY: NutnrJCsppRecoveredDataParticle,
                    DARK_PARTICLE_CLASS_KEY:
                    NutnrJCsppDarkRecoveredDataParticle
                }
            }

        self.parser = NutnrJCsppParser(config, stream_handle,
                                       self.exception_callback)
    def _build_parser(self, stream_handle):

        parser_config = {
            DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                METADATA_PARTICLE_CLASS_KEY:
                NutnrJCsppMetadataRecoveredDataParticle,
                DATA_PARTICLE_CLASS_KEY: NutnrJCsppRecoveredDataParticle
            }
        }

        parser = NutnrJCsppParser(parser_config, stream_handle,
                                  self._exception_callback)
        return parser
Exemple #5
0
    def _build_parser(self, parser_state, stream_in, data_key):
        """
        Build and return a parser for the data_key type parser
        @param parser_state starting parser state to pass to parser
        @param stream_in Handle of open file to pass to parser
        @param data_key Key to determine which parser type is built
        """
        # build the parser based on which key is passed in 
        if data_key == DataSourceKey.NUTNR_J_CSPP_TELEMETERED:
            config = self._parser_config.get(DataSourceKey.NUTNR_J_CSPP_TELEMETERED)
            config.update({
                DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                    METADATA_PARTICLE_CLASS_KEY: NutnrJCsppMetadataTelemeteredDataParticle,
                    DATA_PARTICLE_CLASS_KEY: NutnrJCsppTelemeteredDataParticle
                }
            })

        elif data_key == DataSourceKey.NUTNR_J_CSPP_RECOVERED:
            config = self._parser_config.get(DataSourceKey.NUTNR_J_CSPP_RECOVERED)
            config.update({
                DataSetDriverConfigKeys.PARTICLE_CLASSES_DICT: {
                    METADATA_PARTICLE_CLASS_KEY: NutnrJCsppMetadataRecoveredDataParticle,
                    DATA_PARTICLE_CLASS_KEY: NutnrJCsppRecoveredDataParticle
                }
            })

        else:
            log.warn("Cannot build parser for unknown data source key %s", data_key)
            raise ConfigurationException("Cannot build parser for unknown data source key %s" % \
                                         data_key)
        
        parser = NutnrJCsppParser(
            config,
            parser_state,
            stream_in,
            lambda state, ingested: self._save_parser_state(state, data_key,
                                                            ingested),
            self._data_callback,
            self._sample_exception_callback
        )

        return parser