services_tab.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from __future__ import annotations
  2. from mainui.utils import parse_host_port, url_from_host_port
  3. class ServicesTabMixin:
  4. def _apply_service_url(
  5. self,
  6. *,
  7. host: str,
  8. port: int,
  9. client_attr: str,
  10. status_widget: str,
  11. log_fn,
  12. log_prefix: str,
  13. ping_fn,
  14. ) -> None:
  15. url = url_from_host_port(host, port).rstrip("/")
  16. client = getattr(self, client_attr, None)
  17. if client is None:
  18. return
  19. client.base_url = url
  20. if self.services is not None and hasattr(self.services, status_widget):
  21. getattr(self.services, status_widget).setText(f"Status: {url}")
  22. log_fn(f"{log_prefix}{url}")
  23. ping_fn()
  24. def _init_services_tab(self) -> None:
  25. if self.services is None:
  26. self.log("[SERVICES] No Services tab. Using config defaults.")
  27. self._ping_backend_service()
  28. return
  29. mri_host, mri_port = parse_host_port(self.hardware_api.base_url)
  30. i_host, i_port = parse_host_port(self.interp_api.base_url)
  31. a_host, a_port = parse_host_port(self.analysis.base_url)
  32. self.services.leMRIHost.setText(mri_host)
  33. self.services.sbMRIPort.setValue(mri_port)
  34. if hasattr(self.services, "leIHost"):
  35. self.services.leIHost.setText(i_host)
  36. if hasattr(self.services, "sbIPort"):
  37. self.services.sbIPort.setValue(i_port)
  38. self.services.leAHost.setText(a_host)
  39. self.services.sbAPort.setValue(a_port)
  40. self.services.btnApplyMRI.clicked.connect(self.apply_mri_service)
  41. if hasattr(self.services, "btnApplyInterp"):
  42. self.services.btnApplyInterp.clicked.connect(self.apply_interp_service)
  43. self.services.btnApplyAnalysis.clicked.connect(self.apply_analysis_service)
  44. self.apply_mri_service()
  45. self.apply_interp_service()
  46. self.apply_analysis_service()
  47. def apply_mri_service(self) -> None:
  48. if self.services is None:
  49. return
  50. self._apply_service_url(
  51. host=self.services.leMRIHost.text(),
  52. port=int(self.services.sbMRIPort.value()),
  53. client_attr="hardware_api",
  54. status_widget="lblMRIStatus",
  55. log_fn=self.log,
  56. log_prefix="[SERVICES] srv-hardware set: ",
  57. ping_fn=self._ping_backend_service,
  58. )
  59. def apply_interp_service(self) -> None:
  60. if self.services is None:
  61. return
  62. if not (hasattr(self.services, "leIHost") and hasattr(self.services, "sbIPort")):
  63. return
  64. self._apply_service_url(
  65. host=self.services.leIHost.text(),
  66. port=int(self.services.sbIPort.value()),
  67. client_attr="interp_api",
  68. status_widget="lblIStatus",
  69. log_fn=self.log,
  70. log_prefix="[SERVICES] srv-interp set: ",
  71. ping_fn=self._ping_interp_service,
  72. )
  73. def apply_analysis_service(self) -> None:
  74. if self.services is None:
  75. return
  76. self._apply_service_url(
  77. host=self.services.leAHost.text(),
  78. port=int(self.services.sbAPort.value()),
  79. client_attr="analysis",
  80. status_widget="lblAStatus",
  81. log_fn=self.spec_log,
  82. log_prefix="[SERVICES] Analysis backend set: ",
  83. ping_fn=self._ping_analysis_service,
  84. )