|
1 | 1 | """Wrapper of ``Gimp.get_pdb()`` to simplify invoking GIMP PDB procedures."""
|
2 | 2 |
|
3 | 3 | import abc
|
| 4 | +import keyword |
4 | 5 | from typing import Optional
|
5 | 6 |
|
6 | 7 | import gi
|
@@ -137,9 +138,32 @@ def __call__(self, **kwargs):
|
137 | 138 |
|
138 | 139 | All underscore characters (``_``) in argument names are automatically
|
139 | 140 | replaced by ``-``.
|
| 141 | +
|
| 142 | + Arguments whose names match a Python keyword (``if``, ``lambda``, |
| 143 | + etc.) can be specified by appending a ``_``, e.g. ``lambda_=<value>``. |
140 | 144 | """
|
141 | 145 | pass
|
142 | 146 |
|
| 147 | + @staticmethod |
| 148 | + def _process_arg_name(arg_name): |
| 149 | + """Transforms the given Python argument name to the name of a property |
| 150 | + defined for this procedure. |
| 151 | +
|
| 152 | + For example, ``'run_mode'`` is transformed to ``'run-mode'``. |
| 153 | +
|
| 154 | + Argument names matching a Python keyword can be specified with a trailing |
| 155 | + ``_``, e.g. ``lambda_``. This is transformed such that the trailing `_`` |
| 156 | + is removed, e.g. ``lambda``. |
| 157 | + """ |
| 158 | + processed_arg_name = arg_name.replace('_', '-') |
| 159 | + |
| 160 | + # This allows passing arguments with a trailing '_' to avoid name clashes |
| 161 | + # with Python keywords. |
| 162 | + if processed_arg_name.endswith('-') and keyword.iskeyword(processed_arg_name[:-1]): |
| 163 | + processed_arg_name = processed_arg_name[:-1] |
| 164 | + |
| 165 | + return processed_arg_name |
| 166 | + |
143 | 167 | @property
|
144 | 168 | @abc.abstractmethod
|
145 | 169 | def arguments(self):
|
@@ -310,7 +334,7 @@ def _create_config_for_call(self, **proc_kwargs):
|
310 | 334 | args_and_names = {arg.name: arg for arg in args}
|
311 | 335 |
|
312 | 336 | for arg_name, arg_value in proc_kwargs.items():
|
313 |
| - processed_arg_name = arg_name.replace('_', '-') |
| 337 | + processed_arg_name = self._process_arg_name(arg_name) |
314 | 338 |
|
315 | 339 | try:
|
316 | 340 | arg = args_and_names[processed_arg_name]
|
@@ -379,7 +403,7 @@ def __call__(self, *args, **kwargs):
|
379 | 403 | ``drawable_`` argument, which may be specified as the first and the only
|
380 | 404 | positional argument.
|
381 | 405 | """
|
382 |
| - processed_kwargs = {name.replace('_', '-'): value for name, value in kwargs.items()} |
| 406 | + processed_kwargs = {self._process_arg_name(name): value for name, value in kwargs.items()} |
383 | 407 |
|
384 | 408 | if 'drawable-' in processed_kwargs:
|
385 | 409 | drawable = processed_kwargs.pop('drawable-')
|
|
0 commit comments