RASCSI/src/web/device_utils.py
Daniel Markstedt 54b3e480a5
Web UI code cleanup and refactoring (#409)
* Remove dead code

* Clean up indentation

* Cleanup

* Move socket commands into its own file

* Move non-rascsi command methods into its own file

* Refactoring

* Bring back list_config_files

* Cleanup

* Cleanup of status messages

* Remove unused libraries

* Resolve pylint warnings

* Resolve pylint warnings

* Remove unused library

* Resolve pylint warnings

* Clean up status messages

* Add requests lib to requirements.txt

* Clean up status messages

* Resolve interpolation warnings for logging

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Cleanup

* Add html/head/body tags to the base document

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Resolve pylint warnings

* Add .pylintrc and suppress warnings for the generated protobuf module

* Resolve pylint warnings

* Clean up docstrings

* Fix error

* Cleanup

* Add info on pylint to README

* Store .pylintrc in parent dir to allow other Python packages to use it

* Tidy index.html

* Cleanup

* Resolve jinja-ninja warnings

* Cleanup

* Cleanup

* Cleanup

* Cleanup

* Cleanup

* Fix wording
2021-11-06 19:25:02 -05:00

50 lines
1.5 KiB
Python

"""
Module for RaSCSI device management utility methods
"""
def get_valid_scsi_ids(devices, reserved_ids):
"""
Takes a list of (dict)s devices, and list of (int)s reserved_ids.
Returns:
- (list) of (int)s valid_ids, which are the SCSI ids that are not reserved
- (int) recommended_id, which is the id that the Web UI should default to recommend
"""
occupied_ids = []
for device in devices:
occupied_ids.append(device["id"])
unoccupied_ids = [i for i in list(range(8)) if i not in reserved_ids + occupied_ids]
unoccupied_ids.sort()
valid_ids = [i for i in list(range(8)) if i not in reserved_ids]
valid_ids.sort(reverse=True)
if unoccupied_ids:
recommended_id = unoccupied_ids[-1]
else:
recommended_id = occupied_ids.pop(0)
return valid_ids, recommended_id
def sort_and_format_devices(devices):
"""
Takes a (list) of (dict)s devices and returns a (list) of (dict)s.
Sorts by SCSI ID acending (0 to 7).
For SCSI IDs where no device is attached, inject a (dict) with placeholder text.
"""
occupied_ids = []
for device in devices:
occupied_ids.append(device["id"])
formatted_devices = devices
# Add padding devices and sort the list
for i in range(8):
if i not in occupied_ids:
formatted_devices.append({"id": i, "device_type": "-", \
"status": "-", "file": "-", "product": "-"})
# Sort list of devices by id
formatted_devices.sort(key=lambda dic: str(dic["id"]))
return formatted_devices