2022-11-30 05:19:17 +00:00
|
|
|
"""Module for mapping between return codes and translated strings"""
|
2022-01-21 23:08:29 +00:00
|
|
|
|
2022-12-05 17:58:23 +00:00
|
|
|
from piscsi.return_codes import ReturnCodes
|
2022-01-29 17:42:16 +00:00
|
|
|
from flask_babel import _, lazy_gettext
|
2022-01-21 23:08:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=too-few-public-methods
|
|
|
|
class ReturnCodeMapper:
|
2022-11-30 05:19:17 +00:00
|
|
|
"""Class for mapping between return codes and translated strings"""
|
2022-01-21 23:08:29 +00:00
|
|
|
|
2022-11-30 05:19:17 +00:00
|
|
|
# fmt: off
|
2022-01-21 23:08:29 +00:00
|
|
|
MESSAGES = {
|
2022-08-04 23:28:22 +00:00
|
|
|
ReturnCodes.DELETEFILE_SUCCESS:
|
|
|
|
_("File deleted: %(file_path)s"),
|
|
|
|
ReturnCodes.DELETEFILE_FILE_NOT_FOUND:
|
|
|
|
_("File to delete not found: %(file_path)s"),
|
2023-03-06 17:17:28 +00:00
|
|
|
ReturnCodes.DELETEFILE_UNABLE_TO_DELETE:
|
|
|
|
_("Could not delete file: %(file_path)s"),
|
2022-08-04 23:28:22 +00:00
|
|
|
ReturnCodes.RENAMEFILE_SUCCESS:
|
|
|
|
_("File moved to: %(target_path)s"),
|
|
|
|
ReturnCodes.RENAMEFILE_UNABLE_TO_MOVE:
|
|
|
|
_("Unable to move file to: %(target_path)s"),
|
|
|
|
ReturnCodes.DOWNLOADFILETOISO_SUCCESS:
|
|
|
|
_("Created CD-ROM ISO image with arguments \"%(value)s\""),
|
|
|
|
ReturnCodes.DOWNLOADTODIR_SUCCESS:
|
|
|
|
_("%(file_name)s downloaded to %(save_dir)s"),
|
|
|
|
ReturnCodes.WRITEFILE_SUCCESS:
|
|
|
|
_("File created: %(target_path)s"),
|
|
|
|
ReturnCodes.WRITEFILE_COULD_NOT_WRITE:
|
|
|
|
_("Could not create file: %(target_path)s"),
|
2023-03-06 17:17:28 +00:00
|
|
|
ReturnCodes.WRITEFILE_COULD_NOT_OVERWRITE:
|
|
|
|
_("A file with name %(target_path)s already exists"),
|
2022-08-04 23:28:22 +00:00
|
|
|
ReturnCodes.READCONFIG_SUCCESS:
|
|
|
|
_("Loaded configurations from: %(file_name)s"),
|
|
|
|
ReturnCodes.READCONFIG_COULD_NOT_READ:
|
|
|
|
_("Could not read configuration file: %(file_name)s"),
|
|
|
|
ReturnCodes.READCONFIG_INVALID_CONFIG_FILE_FORMAT:
|
|
|
|
_("Invalid configuration file format"),
|
|
|
|
ReturnCodes.READDRIVEPROPS_SUCCESS:
|
|
|
|
_("Read properties from file: %(file_path)s"),
|
|
|
|
ReturnCodes.READDRIVEPROPS_COULD_NOT_READ:
|
|
|
|
_("Could not read properties from file: %(file_path)s"),
|
|
|
|
ReturnCodes.ATTACHIMAGE_COULD_NOT_ATTACH:
|
|
|
|
_("Cannot insert an image for %(device_type)s into a %(current_device_type)s device"),
|
2022-08-12 03:19:02 +00:00
|
|
|
ReturnCodes.EXTRACTIMAGE_SUCCESS:
|
|
|
|
_("Extracted %(count)s file(s)"),
|
|
|
|
ReturnCodes.EXTRACTIMAGE_NO_FILES_SPECIFIED:
|
|
|
|
_("Unable to extract archive: No files were specified"),
|
|
|
|
ReturnCodes.EXTRACTIMAGE_NO_FILES_EXTRACTED:
|
|
|
|
_("No files were extracted (existing files are skipped)"),
|
|
|
|
ReturnCodes.EXTRACTIMAGE_COMMAND_ERROR:
|
|
|
|
_("Unable to extract archive: %(error)s"),
|
2022-08-04 23:28:22 +00:00
|
|
|
}
|
2022-11-30 05:19:17 +00:00
|
|
|
# fmt: on
|
2022-01-21 23:08:29 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def add_msg(payload):
|
2022-11-30 05:19:17 +00:00
|
|
|
"""adds a msg key to a given payload with a module return code
|
|
|
|
with a translated return code message string."""
|
2022-01-21 23:08:29 +00:00
|
|
|
if "return_code" not in payload:
|
|
|
|
return payload
|
|
|
|
|
2022-08-04 23:28:22 +00:00
|
|
|
parameters = payload.get("parameters")
|
2022-01-21 23:08:29 +00:00
|
|
|
|
2022-08-04 23:28:22 +00:00
|
|
|
if parameters:
|
|
|
|
payload["msg"] = lazy_gettext(
|
2022-02-27 05:46:35 +00:00
|
|
|
ReturnCodeMapper.MESSAGES[payload["return_code"]],
|
|
|
|
**parameters,
|
2022-11-30 05:19:17 +00:00
|
|
|
)
|
2022-08-04 23:28:22 +00:00
|
|
|
else:
|
|
|
|
payload["msg"] = lazy_gettext(ReturnCodeMapper.MESSAGES[payload["return_code"]])
|
|
|
|
|
2022-01-21 23:08:29 +00:00
|
|
|
return payload
|