def get_result(self, cmd_id): """ Get result of deferred command :type cmd_id: int :param cmd_id: ID of the command :rtype: tuple :return: the status and the result of the command """ request = json.dumps(["join", [cmd_id], {}]) LOGGER_FWK.debug("Joining command server %d" % cmd_id) try: conn = self._proto_factory.create() conn.connect() conn.send(request) status, result = json.loads(conn.receive()) self._check_cmd_status(status, result) except socket.error as E: err, err_msg = E msg = "Socket error (%d): %s" % (err, err_msg) LOGGER_FWK.error(msg) raise DeviceException(DeviceException.OPERATION_FAILED, msg) except: msg = "Failed to communicate with embedded command server" LOGGER_FWK.error(msg) raise DeviceException(DeviceException.OPERATION_FAILED, msg) finally: conn.disconnect() return status, result
def send_cmd(self, cmd, args={}): """ Send a command to embedded server via socket :type cmd: string :param cmd: command name :type args: dict :param args: command arguments :rtype: tuple :return: the status and the result of the command """ request = json.dumps([cmd, [], args]) LOGGER_FWK.info("Sending command %s %s" % (cmd, str(args))) try: conn = self._proto_factory.create() conn.connect() conn.send(request) status, result = json.loads(conn.receive()) self._check_cmd_status(status, result) except: msg = "Failed to communicate with embedded command server" raise DeviceException(DeviceException.OPERATION_FAILED, msg) finally: conn.disconnect() if status == CommandServerApi.SRV_CMD_DEFERRED: LOGGER_FWK.debug("Command ID: %d" % result) return status, result
def get_config_value(config_dict, config_dict_name, config_name, default_value=None, default_cast_type=str): """ Return the value of the given config name The type of the value can be checked before assignment A default value can be given in case the config name does not exist :type config_name: string :param config_name: name of the property value to retrieve :type default_value: string :param default_value: default_value of the property :type default_cast_type: type object :param default_cast_type: type to cast (int, str, list ...) By default cast into str type. :rtype: string or type of default_cast_type :return: config value """ # Read the config value from dut config dictionary config_value = config_dict.get(config_name, default_value) # In case the config value is not None, trying to cast the value if config_value is not None: # Cast the value to the given type # Stripping is done to suppress end and start spaces of values try: if default_cast_type == "str_to_bool": config_value = str_to_bool(str(config_value).strip()) elif default_cast_type == "str_to_dict": config_value = str_to_dict(str(config_value).strip()) else: config_value = default_cast_type(config_value) except ValueError: debug_msg = "Wrong value used for dictionary %s entry: '%s'. Returning default value '%s' !" \ % (str(config_dict_name), str(config_name), str(default_value)) LOGGER_FWK.debug(debug_msg) config_value = default_value return config_value
def get_value(self, key, default_value=None, default_cast_type=str): """ Return the value of the given device config name The type of the value can be checked before assignment A default value can be given in case the config name does not exist :type key: string :param key: name of the property value to retrieve :type default_value: object :param default_value: default_value of the property :type default_cast_type: type object :param default_cast_type: type to cast (int, str, list ...) By default cast into str type. :rtype: string or type of default_cast_type :return: config value """ value = self.get(key, default_value) # In case the config value is not None, trying to cast the value if value is not None: # Cast the value to the given type # Stripping is done to suppress end and start spaces of values try: if default_cast_type == "str_to_bool": value = str_to_bool(str(value).strip()) elif default_cast_type == "str_to_dict": value = str_to_dict(str(value)) else: value = default_cast_type(value) except ValueError: LOGGER_FWK.debug( "Cannot convert {0} to {1}, return {2}".format( key, default_cast_type, default_value)) value = default_value # Store new value # TODO: do not store the value for now because of side effects, need to see if it's expected behavior # self[key] = value return value
def compute_timeout_from_file_size(file_path, min_timeout=0): """ Compute a timeout depending the file's size. :type file_path: str :param file_path: File from which a timeout will be computed :type min_timeout: int :param min_timeout: Minimum timeout (in sec) to set if the file size is too small :rtype: int :return: timeout (in sec) computed from the file size """ if os.path.isfile(file_path): app_size = os.path.getsize(file_path) timeout = int(app_size / 1024 / 4) if timeout < min_timeout: # Set a minimum installation timeout timeout = min_timeout LOGGER_FWK.debug("app size: %dB, timeout: %ds" % (app_size, timeout)) else: timeout = min_timeout return timeout
def create(module_name, device, global_conf): """ Create and return list of device module. :type module_name: str :param module_name: name of the module to be created :type device: py:class:`~src.Device.Model.IDevice.py` :param module_name: device instance that request the module creation :type global_conf: dict :param global_conf: ACS global configuration :rtype: list of module """ modules = [] if device.config.device_modules and module_name in device.config.device_modules: module_configurations = device.config.device_modules[module_name] if not module_configurations: raise AcsConfigException( "Cannot load \"{0}\" device module".format(module_name), "Cannot find module configuration.") for module_configuration in module_configurations: module = DeviceModuleFactory._instantiate_module( module_name, module_configuration) module.device = device module.logger = logging.getLogger( "%s.%s" % (ACS_LOGGER_NAME, module_name.upper())) module.global_conf = global_conf module.name = module_name module.load_conf() DeviceModuleFactory._update_parameter(module, module_configuration, module_name) LOGGER_FWK.debug("Create Module '{0}' based on : {1}".format( module_name, module_configuration.class_name)) LOGGER_FWK.debug("Module default parameters values are:") for key, value in module.configuration.items(): LOGGER_FWK.debug("\t {0} : {1}".format(key, value)) LOGGER_DEVICE_STATS.info( "Create device_module={0}; device_module_class={1}; device_module_conf={2}" .format(module_name, module_configuration.class_name, module_configuration.config)) modules.append(module) return modules