Esempio n. 1
0
    def get_resource(self, *args, **kwargs):
        """
        Retrieve device parameters.

        @param params DriverParameter.ALL or a list of parameters to retrieve.
        @param timeout Timeout for each involved instrument interation,
                self._timeout by default.

        @retval parameter : value dict.
        @raises InstrumentParameterException if missing or invalid get parameters.
        @raises InstrumentStateException if command not allowed in current state
        """

        if log.isEnabledFor(logging.DEBUG):
            log.debug("args=%s kwargs=%s" % (str(args), str(kwargs)))

        self._assert_state(TrhphDriverState.CONNECTED)

        params = kwargs.get('params', None)
        if params is None:
            #            raise InstrumentParameterException(msg=
            #                    "'params' parameter required")
            params = args[0]

        if params == DriverParameter.ALL:
            params = TrhphParameter.list()
        elif not isinstance(params, (list, tuple)):
            raise InstrumentParameterException(
                msg='params must be list or tuple.')

        timeout = kwargs.get('timeout', self._timeout)

        # gather data collection params if any of them are requested:
        data_collec_params = {}
        if TrhphParameter.TIME_BETWEEN_BURSTS in params or \
           TrhphParameter.VERBOSE_MODE in params:
            seconds, is_data_only = self._get_data_collection_params(timeout)
            verbose = not is_data_only
            data_collec_params[TrhphParameter.TIME_BETWEEN_BURSTS] = seconds
            data_collec_params[TrhphParameter.VERBOSE_MODE] = verbose

        params = list(set(params))  # remove any duplicates

        result = {}
        for param in params:
            if param == TrhphParameter.TIME_BETWEEN_BURSTS or \
               param == TrhphParameter.VERBOSE_MODE:
                value = data_collec_params[param]
            else:
                #                value = InstErrorCode.INVALID_PARAMETER
                raise InstrumentParameterException(msg='invalid parameter %s' %
                                                   param)
            result[param] = value

        return result
    def _get_params_OLD(self, valid_params, invalid_params=None):
        # TODO Remove this method

        invalid_params = invalid_params or []

        if len(invalid_params) == 0:
            # use valid_params exactly as given
            params = valid_params
        else:
            if valid_params == DriverParameter.ALL:
                valid_params = TrhphParameter.list()

            params = valid_params + invalid_params

        result = self.driver.get_resource(params=params, timeout=self._timeout)
        log.info("get result = %s" % str(result))
        self.assertTrue(isinstance(result, dict))

        if params == DriverParameter.ALL:
            all_requested_params = TrhphParameter.list()
        else:
            all_requested_params = params

        # check all requested params are in the result
        for p in all_requested_params:
            self.assertTrue(p in result)

        for p in valid_params:
            self.assertFalse(InstErrorCode.is_error(result.get(p)))
            if TrhphParameter.TIME_BETWEEN_BURSTS == p:
                seconds = result.get(p)
                self.assertTrue(isinstance(seconds, int))
            if TrhphParameter.VERBOSE_MODE == p:
                is_data_only = result.get(p)
                self.assertTrue(isinstance(is_data_only, bool))

        for p in invalid_params:
            res_p = result.get(p)
            print '::::: %s' % str(res_p)
            self.assertTrue(InstErrorCode.is_error(res_p))

        return result
    def _get_params_OLD(self, valid_params, invalid_params=None):
        # TODO Remove this method

        invalid_params = invalid_params or []

        if len(invalid_params) == 0:
            # use valid_params exactly as given
            params = valid_params
        else:
            if valid_params == DriverParameter.ALL:
                valid_params = TrhphParameter.list()

            params = valid_params + invalid_params

        result = self.driver.get_resource(params=params, timeout=self._timeout)
        log.info("get result = %s" % str(result))
        self.assertTrue(isinstance(result, dict))

        if params == DriverParameter.ALL:
            all_requested_params = TrhphParameter.list()
        else:
            all_requested_params = params

        # check all requested params are in the result
        for p in all_requested_params:
            self.assertTrue(p in result)

        for p in valid_params:
            self.assertFalse(InstErrorCode.is_error(result.get(p)))
            if TrhphParameter.TIME_BETWEEN_BURSTS == p:
                seconds = result.get(p)
                self.assertTrue(isinstance(seconds, int))
            if TrhphParameter.VERBOSE_MODE == p:
                is_data_only = result.get(p)
                self.assertTrue(isinstance(is_data_only, bool))

        for p in invalid_params:
            res_p = result.get(p)
            print '::::: %s' % str(res_p)
            self.assertTrue(InstErrorCode.is_error(res_p))

        return result
    def get_resource(self, *args, **kwargs):
        """
        Retrieve device parameters.

        @param params DriverParameter.ALL or a list of parameters to retrieve.
        @param timeout Timeout for each involved instrument interation,
                self._timeout by default.

        @retval parameter : value dict.
        @raises InstrumentParameterException if missing or invalid get parameters.
        @raises InstrumentStateException if command not allowed in current state
        """
        
        if log.isEnabledFor(logging.DEBUG):
            log.debug("args=%s kwargs=%s" % (str(args), str(kwargs)))

        self._assert_state(TrhphDriverState.CONNECTED)

        params = kwargs.get('params', None)
        if params is None:
#            raise InstrumentParameterException(msg=
#                    "'params' parameter required")
            params = args[0]

        if params == DriverParameter.ALL:
            params = TrhphParameter.list()
        elif not isinstance(params, (list, tuple)):
            raise InstrumentParameterException(msg=
                    'params must be list or tuple.')

        timeout = kwargs.get('timeout', self._timeout)

        # gather data collection params if any of them are requested:
        data_collec_params = {}
        if TrhphParameter.TIME_BETWEEN_BURSTS in params or \
           TrhphParameter.VERBOSE_MODE in params:
            seconds, is_data_only = self._get_data_collection_params(timeout)
            verbose = not is_data_only
            data_collec_params[TrhphParameter.TIME_BETWEEN_BURSTS] = seconds
            data_collec_params[TrhphParameter.VERBOSE_MODE] = verbose

        params = list(set(params))  # remove any duplicates

        result = {}
        for param in params:
            if param == TrhphParameter.TIME_BETWEEN_BURSTS or \
               param == TrhphParameter.VERBOSE_MODE:
                value = data_collec_params[param]
            else:
#                value = InstErrorCode.INVALID_PARAMETER
                raise InstrumentParameterException(msg='invalid parameter %s' % param)
            result[param] = value

        return result
    def _get_params(self, params):

        result = self.driver.get_resource(params=params, timeout=self._timeout)
        log.info("get result = %s" % str(result))
        self.assertTrue(isinstance(result, dict))

        if params == DriverParameter.ALL:
            all_requested_params = TrhphParameter.list()
        else:
            all_requested_params = params

        # check all requested params are in the result
        for p in all_requested_params:
            self.assertTrue(p in result)

            if TrhphParameter.TIME_BETWEEN_BURSTS == p:
                seconds = result.get(p)
                self.assertTrue(isinstance(seconds, int))
            elif TrhphParameter.VERBOSE_MODE == p:
                is_data_only = result.get(p)
                self.assertTrue(isinstance(is_data_only, bool))

        return result
    def _get_params(self, params):

        result = self.driver.get_resource(params=params, timeout=self._timeout)
        log.info("get result = %s" % str(result))
        self.assertTrue(isinstance(result, dict))

        if params == DriverParameter.ALL:
            all_requested_params = TrhphParameter.list()
        else:
            all_requested_params = params

        # check all requested params are in the result
        for p in all_requested_params:
            self.assertTrue(p in result)

            if TrhphParameter.TIME_BETWEEN_BURSTS == p:
                seconds = result.get(p)
                self.assertTrue(isinstance(seconds, int))
            elif TrhphParameter.VERBOSE_MODE == p:
                is_data_only = result.get(p)
                self.assertTrue(isinstance(is_data_only, bool))

        return result