From e7775a72cc28152ba8ab38ea5ab8ee7cf047c72f Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Wed, 21 Sep 2022 17:14:53 -0700 Subject: [PATCH] Generate list of image types to create programmatically in the Web UI (#850) * Generate list of image types to create programmatically based on rascsi capabilities, rather than a hard-coded list in the Web UI. * Add explicit sorting of dicts for display in the Web UI, to avoid random order in certain environments. * Remove redundant sorting line, and add code comments. * Add helptext for the SCSI-1 image type --- python/web/src/templates/index.html | 24 ++++++++------------- python/web/src/web.py | 15 +++++++++++++ python/web/src/web_utils.py | 33 +++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/python/web/src/templates/index.html b/python/web/src/templates/index.html index 493063fd..8f896305 100644 --- a/python/web/src/templates/index.html +++ b/python/web/src/templates/index.html @@ -336,7 +336,7 @@
- {% for key, value in device_types[type]["params"].items() %} + {% for key, value in device_types[type]["params"] | dictsort %} {% if value.isnumeric() %} @@ -544,9 +544,10 @@ @@ -557,18 +558,11 @@ diff --git a/python/web/src/web.py b/python/web/src/web.py index a51f308f..9c567763 100644 --- a/python/web/src/web.py +++ b/python/web/src/web.py @@ -49,6 +49,7 @@ from web_utils import ( get_valid_scsi_ids, map_device_types_and_names, get_device_name, + map_image_file_descriptions, auth_active, is_bridge_configured, upload_with_dropzonejs, @@ -133,6 +134,19 @@ def index(): scsi_ids, recommended_id = get_valid_scsi_ids(devices["device_list"], reserved_scsi_ids) formatted_devices = sort_and_format_devices(devices["device_list"]) + image_suffixes_to_create = map_image_file_descriptions( + # Here we strip out hdi and nhd, since they are proprietary PC-98 emulator formats + # that require a particular header to work. We can't generate them on the fly. + # We also reverse the resulting list, in order for hds to be the default in the UI. + # This might break if something like 'hdt' etc. gets added in the future. + sorted( + [suffix for suffix in server_info["schd"] if suffix not in {"hdi", "nhd"}], + reverse=True + ) + + server_info["scrm"] + + server_info["scmo"] + ) + valid_image_suffixes = "." + ", .".join( server_info["schd"] + server_info["scrm"] + @@ -174,6 +188,7 @@ def index(): netinfo=ractl_cmd.get_network_info(), device_types=device_types, free_disk=int(sys_cmd.disk_space()["free"] / 1024 / 1024), + image_suffixes_to_create=image_suffixes_to_create, valid_image_suffixes=valid_image_suffixes, cdrom_file_suffix=tuple(server_info["sccd"]), removable_file_suffix=tuple(server_info["scrm"]), diff --git a/python/web/src/web_utils.py b/python/web/src/web_utils.py index 98dcbc31..4531c4f8 100644 --- a/python/web/src/web_utils.py +++ b/python/web/src/web_utils.py @@ -99,6 +99,39 @@ def get_device_name(device_type): return device_type +def map_image_file_descriptions(file_suffixes): + """ + Takes a (list) of (str) file suffixes for images file types. + Returns a (dict) with file suffix and description pairs, both (str) + """ + supported_image_types = {} + for suffix in file_suffixes: + supported_image_types[suffix] = get_image_description(suffix) + + return supported_image_types + + +# pylint: disable=too-many-return-statements +def get_image_description(file_suffix): + """ + Takes a three char file suffix (str) file_suffix. + Returns the help text description for said file suffix. + """ + if file_suffix == "hds": + return _("Hard Disk Image (Generic)") + if file_suffix == "hda": + return _("Hard Disk Image (Apple)") + if file_suffix == "hdn": + return _("Hard Disk Image (NEC)") + if file_suffix == "hd1": + return _("Hard Disk Image (SCSI-1)") + if file_suffix == "hdr": + return _("Removable Disk Image") + if file_suffix == "mos": + return _("Magneto-Optical Disk Image") + return file_suffix + + def auth_active(group): """ Inspects if the group defined in (str) group exists on the system.