Browse Source

Set utility functions in better importable modules

Michael Hanke 2 years ago
parent
commit
344ff2907f

+ 29 - 0
datalad_gooey/api_utils.py

@@ -1,4 +1,12 @@
 
 
+from collections.abc import Callable
+from itertools import zip_longest
+from typing import List
+
+from datalad.utils import getargspec
+
+from .utils import _NoValue
+
 
 
 def get_cmd_displayname(api, cmdname):
 def get_cmd_displayname(api, cmdname):
     dname = api.get(cmdname, {}).get(
     dname = api.get(cmdname, {}).get(
@@ -10,3 +18,24 @@ def get_cmd_displayname(api, cmdname):
         dname = f'Create a {" ".join(dname_parts[2:])}' \
         dname = f'Create a {" ".join(dname_parts[2:])}' \
                 f'{" " if len(dname_parts) > 2 else ""}sibling'
                 f'{" " if len(dname_parts) > 2 else ""}sibling'
     return dname
     return dname
+
+
+def get_cmd_params(cmd: Callable) -> List:
+    """Take a callable and return a list of parameter names, and their defaults
+
+    Parameter names and defaults are returned as 2-tuples. If a parameter has
+    no default, the special value `_NoValue` is used.
+    """
+    # lifted from setup_parser_for_interface()
+    args, varargs, varkw, defaults = getargspec(cmd, include_kwonlyargs=True)
+    return list(
+        zip_longest(
+            # fuse parameters from the back, to match with their respective
+            # defaults -- if soem have no defaults, they would be the first
+            args[::-1],
+            defaults[::-1],
+            # pad with a dedicate type, to be able to tell if there was a
+            # default or not
+            fillvalue=_NoValue)
+    # reverse the order again to match the original order in the signature
+    )[::-1]

+ 4 - 1
datalad_gooey/complete_api.py

@@ -9,7 +9,10 @@ from datalad.interface.base import (
 )
 )
 from datalad.utils import get_wrapped_class
 from datalad.utils import get_wrapped_class
 
 
-from .api_utils import get_cmd_displayname
+from .api_utils import (
+    get_cmd_displayname,
+    get_cmd_params,
+)
 
 
 
 
 # mapping of command interface classes to interface group titles
 # mapping of command interface classes to interface group titles

+ 4 - 26
datalad_gooey/param_form_utils.py

@@ -3,13 +3,11 @@ from collections.abc import Callable
 import functools
 import functools
 from itertools import (
 from itertools import (
     chain,
     chain,
-    zip_longest,
 )
 )
 from pathlib import Path
 from pathlib import Path
 from typing import (
 from typing import (
     Any,
     Any,
     Dict,
     Dict,
-    List,
 )
 )
 from PySide6.QtWidgets import (
 from PySide6.QtWidgets import (
     QFormLayout,
     QFormLayout,
@@ -22,7 +20,6 @@ from datalad.interface.common_opts import eval_params
 from datalad.support.constraints import EnsureChoice
 from datalad.support.constraints import EnsureChoice
 from datalad.support.param import Parameter
 from datalad.support.param import Parameter
 from datalad.utils import (
 from datalad.utils import (
-    getargspec,
     get_wrapped_class,
     get_wrapped_class,
 )
 )
 
 
@@ -34,6 +31,8 @@ from .active_api import (
     exclude_parameters,
     exclude_parameters,
     parameter_display_names,
     parameter_display_names,
 )
 )
+from .api_utils import get_cmd_params
+from .utils import _NoValue
 
 
 __all__ = ['populate_form_w_params']
 __all__ = ['populate_form_w_params']
 
 
@@ -64,7 +63,7 @@ def populate_form_w_params(
 
 
     # loop over all parameters of the command (with their defaults)
     # loop over all parameters of the command (with their defaults)
     def _specific_params():
     def _specific_params():
-        for pname, pdefault in _get_params(cmd):
+        for pname, pdefault in get_cmd_params(cmd):
             yield pname, pdefault, cmd_cls._params_[pname]
             yield pname, pdefault, cmd_cls._params_[pname]
 
 
     # loop over all generic
     # loop over all generic
@@ -72,7 +71,7 @@ def populate_form_w_params(
         for pname, param in eval_params.items():
         for pname, param in eval_params.items():
             yield (
             yield (
                 pname,
                 pname,
-                param.cmd_kwargs.get('default', pw._NoValue), \
+                param.cmd_kwargs.get('default', _NoValue), \
                 param,
                 param,
             )
             )
     for pname, pdefault, param_spec in sorted(
     for pname, pdefault, param_spec in sorted(
@@ -133,27 +132,6 @@ def populate_form_w_params(
 # Internal helpers
 # Internal helpers
 #
 #
 
 
-def _get_params(cmd) -> List:
-    """Take a callable and return a list of parameter names, and their defaults
-
-    Parameter names and defaults are returned as 2-tuples. If a parameter has
-    no default, the special value `_NoValue` is used.
-    """
-    # lifted from setup_parser_for_interface()
-    args, varargs, varkw, defaults = getargspec(cmd, include_kwonlyargs=True)
-    return list(
-        zip_longest(
-            # fuse parameters from the back, to match with their respective
-            # defaults -- if soem have no defaults, they would be the first
-            args[::-1],
-            defaults[::-1],
-            # pad with a dedicate type, to be able to tell if there was a
-            # default or not
-            fillvalue=pw._NoValue)
-    # reverse the order again to match the original order in the signature
-    )[::-1]
-
-
 def _get_parameter_widget(
 def _get_parameter_widget(
         basedir: Path,
         basedir: Path,
         parent: QWidget,
         parent: QWidget,

+ 1 - 1
datalad_gooey/param_multival_widget.py

@@ -18,8 +18,8 @@ from datalad.utils import ensure_list
 from .param_widgets import (
 from .param_widgets import (
     GooeyParamWidgetMixin,
     GooeyParamWidgetMixin,
     load_parameter_widget,
     load_parameter_widget,
-    _NoValue,
 )
 )
+from .utils import _NoValue
 
 
 
 
 class MyItemDelegate(QStyledItemDelegate):
 class MyItemDelegate(QStyledItemDelegate):

+ 1 - 10
datalad_gooey/param_widgets.py

@@ -23,16 +23,7 @@ from PySide6.QtWidgets import (
 from datalad import cfg as dlcfg
 from datalad import cfg as dlcfg
 
 
 from .resource_provider import gooey_resources
 from .resource_provider import gooey_resources
-
-
-class _NoValue:
-    """Type to annotate the absence of a value
-
-    For example in a list of parameter defaults. In general `None` cannot
-    be used, as it may be an actual value, hence we use a local, private
-    type.
-    """
-    pass
+from .utils import _NoValue
 
 
 
 
 class GooeyParamWidgetMixin:
 class GooeyParamWidgetMixin:

+ 11 - 0
datalad_gooey/utils.py

@@ -1,4 +1,5 @@
 from pathlib import Path
 from pathlib import Path
+
 from PySide6.QtUiTools import QUiLoader
 from PySide6.QtUiTools import QUiLoader
 from PySide6.QtCore import (
 from PySide6.QtCore import (
     QFile,
     QFile,
@@ -6,6 +7,16 @@ from PySide6.QtCore import (
 )
 )
 
 
 
 
+class _NoValue:
+    """Type to annotate the absence of a value
+
+    For example in a list of parameter defaults. In general `None` cannot
+    be used, as it may be an actual value, hence we use a local, private
+    type.
+    """
+    pass
+
+
 def load_ui(name, parent=None):
 def load_ui(name, parent=None):
     ui_file_name = Path(__file__).parent / 'resources' / 'ui' / f"{name}.ui"
     ui_file_name = Path(__file__).parent / 'resources' / 'ui' / f"{name}.ui"
     ui_file = QFile(ui_file_name)
     ui_file = QFile(ui_file_name)