Przeglądaj źródła

Rough sketch of credential selection widget

Substantial code-duplication with SiblingChoiceParamWidget for now.

Closes #195
Closes #94
Michael Hanke 1 rok temu
rodzic
commit
2f8ab192b8
2 zmienionych plików z 68 dodań i 0 usunięć
  1. 2 0
      datalad_gooey/param_form_utils.py
  2. 66 0
      datalad_gooey/param_widgets.py

+ 2 - 0
datalad_gooey/param_form_utils.py

@@ -235,6 +235,8 @@ def _get_parameter_widget_factory(
             basedir=basedir)
     elif name == 'cfg_proc':
         type_widget = pw.CfgProcParamWidget
+    elif name == 'credential':
+        type_widget = pw.CredentialChoiceParamWidget
     elif name == 'recursion_limit':
         type_widget = functools.partial(pw.PosIntParamWidget, allow_none=True)
     # now parameters where we make decisions based on their configuration

+ 66 - 0
datalad_gooey/param_widgets.py

@@ -506,3 +506,69 @@ class SiblingChoiceParamWidget(ChoiceParamWidget):
         self._set_placeholder_msg()
         if self.count():
             self.setEnabled(True)
+
+
+class CredentialChoiceParamWidget(QComboBox, GooeyParamWidgetMixin):
+    """Choice widget with items from `credentials()`"""
+    def __init__(self, parent=None):
+        super().__init__(parent=parent)
+        self.setEditable(True)
+        self.setInsertPolicy(QComboBox.InsertAtTop)
+        self.setEnabled(True)
+        self.currentTextChanged.connect(self._handle_input)
+        self.setSizeAdjustPolicy(
+            QComboBox.AdjustToMinimumContentsLengthWithIcon)
+        self.setPlaceholderText('--auto--')
+        self._saw_dataset = False
+
+    def _init_gooey_from_other_params(self, spec: Dict) -> None:
+        if 'dataset' not in spec:
+            # we have items and no context change is required
+            return
+        self._saw_dataset = True
+        self._init_choices(
+            spec['dataset'] if spec['dataset'] != _NoValue else None)
+
+    def _init_choices(self, dataset=None):
+        # the dataset has changed: query!
+        # reset first
+        self.clear()
+        from datalad_next.credentials import Credentials
+        from datalad.support.exceptions import (
+            CapturedException,
+            NoDatasetFound,
+        )
+        self.addItem('')
+        try:
+            for res in Credentials.__call__(
+                dataset=dataset,
+                action='query',
+                return_type='generator',
+                result_renderer='disabled',
+                on_failure='ignore',
+            ):
+                name = res.get('name')
+                if (not name or res.get('status') != 'ok'
+                        or res.get('type') != 'credential'):
+                    # not a good sibling
+                    continue
+                self.addItem(name)
+        except NoDatasetFound as e:
+            CapturedException(e)
+            # TODO this should happen upon validation of the
+            # `dataset` parameter value
+            QMessageBox.critical(
+                self,
+                'No dataset selected',
+                'The path selected for the <code>dataset</code> parameter '
+                'does not point to a valid dataset. '
+                'Please select another path!'
+            )
+            self._saw_dataset = False
+
+    def _set_gooey_param_value_in_widget(self, value):
+        self.setCurrentText(value or '')
+
+    def _handle_input(self):
+        self._set_gooey_param_value(
+            self.currentText() or _NoValue)