utils.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from pathlib import Path
  2. from typing import Dict
  3. from PySide6.QtUiTools import QUiLoader
  4. from PySide6.QtCore import (
  5. QFile,
  6. QIODevice,
  7. )
  8. class _NoValue:
  9. """Type to annotate the absence of a value
  10. For example in a list of parameter defaults. In general `None` cannot
  11. be used, as it may be an actual value, hence we use a local, private
  12. type.
  13. """
  14. pass
  15. def load_ui(name, parent=None):
  16. ui_file_name = Path(__file__).parent / 'resources' / 'ui' / f"{name}.ui"
  17. ui_file = QFile(ui_file_name)
  18. if not ui_file.open(QIODevice.ReadOnly):
  19. raise RuntimeError(
  20. f"Cannot open {ui_file_name}: {ui_file.errorString()}")
  21. loader = QUiLoader()
  22. ui = loader.load(ui_file, parentWidget=parent)
  23. ui_file.close()
  24. if not ui:
  25. raise RuntimeError(
  26. f"Cannot load UI {ui_file_name}: {loader.errorString()}")
  27. return ui
  28. def render_cmd_call(cmdname: str, cmdkwargs: Dict):
  29. """Minimalistic Python-like rendering of commands for the logs"""
  30. cmdkwargs = cmdkwargs.copy()
  31. ds_path = cmdkwargs.pop('dataset', None)
  32. if ds_path:
  33. if hasattr(ds_path, 'pathobj'):
  34. ds_path = ds_path.path
  35. ds_path = str(ds_path)
  36. # show commands running on datasets as dataset method calls
  37. rendered = "<b>Running:</b> "
  38. rendered += f"<code>Dataset({ds_path!r})." if ds_path else ''
  39. rendered += f"{cmdname}<nobr>("
  40. rendered += ', '.join(
  41. f"<i>{k}</i>={v!r}"
  42. for k, v in cmdkwargs.items()
  43. if k not in ('return_type', 'result_xfm')
  44. )
  45. rendered += ")</code>"
  46. return rendered