Rewrite incompatible option checking for more readability
Marco Ricci

Marco Ricci commited on 2024-12-19 15:47:42
Zeige 1 geänderte Dateien mit 20 Einfügungen und 22 Löschungen.

... ...
@@ -12,6 +12,7 @@ import base64
12 12
 import collections
13 13
 import copy
14 14
 import enum
15
+import functools
15 16
 import importlib
16 17
 import inspect
17 18
 import json
... ...
@@ -1788,28 +1789,27 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
1788 1789
         for name in param.opts + param.secondary_opts:
1789 1790
             params_by_str[name] = param
1790 1791
 
1792
+    @functools.cache
1791 1793
     def is_param_set(param: click.Parameter) -> bool:
1792 1794
         return bool(ctx.params.get(param.human_readable_name))
1793 1795
 
1794 1796
     def check_incompatible_options(
1795
-        param: click.Parameter | str,
1796
-        *incompatible: click.Parameter | str,
1797
+        param1: click.Parameter | str,
1798
+        param2: click.Parameter | str,
1797 1799
     ) -> None:
1798
-        if isinstance(param, str):
1799
-            param = params_by_str[param]
1800
-        assert isinstance(param, click.Parameter)
1801
-        if not is_param_set(param):
1800
+        param1 = params_by_str[param1] if isinstance(param1, str) else param1
1801
+        param2 = params_by_str[param2] if isinstance(param2, str) else param2
1802
+        if param1 == param2:
1802 1803
             return
1803
-        for other in incompatible:
1804
-            if isinstance(other, str):
1805
-                other = params_by_str[other]  # noqa: PLW2901
1806
-            assert isinstance(other, click.Parameter)
1807
-            if other != param and is_param_set(other):
1808
-                opt_str = param.opts[0]
1809
-                other_str = other.opts[0]
1804
+        if not is_param_set(param1):
1805
+            return
1806
+        if is_param_set(param2):
1807
+            param1_str = param1.human_readable_name
1808
+            param2_str = param2.human_readable_name
1810 1809
             raise click.BadOptionUsage(
1811
-                    opt_str, f'mutually exclusive with {other_str}', ctx=ctx
1810
+                param1_str, f'mutually exclusive with {param2_str}', ctx=ctx
1812 1811
             )
1812
+        return
1813 1813
 
1814 1814
     def err(msg: Any, *args: Any, **kwargs: Any) -> NoReturn:  # noqa: ANN401
1815 1815
         stacklevel = kwargs.pop('stacklevel', 1)
... ...
@@ -1878,17 +1878,15 @@ def derivepassphrase_vault(  # noqa: C901,PLR0912,PLR0913,PLR0914,PLR0915
1878 1878
     for group in (ConfigurationOption, StorageManagementOption):
1879 1879
         for opt in options_in_group[group]:
1880 1880
             if opt != params_by_str['--config']:
1881
-                check_incompatible_options(
1882
-                    opt, *options_in_group[PasswordGenerationOption]
1883
-                )
1881
+                for other_opt in options_in_group[PasswordGenerationOption]:
1882
+                    check_incompatible_options(opt, other_opt)
1884 1883
 
1885 1884
     for group in (ConfigurationOption, StorageManagementOption):
1886 1885
         for opt in options_in_group[group]:
1887
-            check_incompatible_options(
1888
-                opt,
1889
-                *options_in_group[ConfigurationOption],
1890
-                *options_in_group[StorageManagementOption],
1891
-            )
1886
+            for other_opt in options_in_group[ConfigurationOption]:
1887
+                check_incompatible_options(opt, other_opt)
1888
+            for other_opt in options_in_group[StorageManagementOption]:
1889
+                check_incompatible_options(opt, other_opt)
1892 1890
     sv_or_global_options = options_in_group[PasswordGenerationOption]
1893 1891
     for param in sv_or_global_options:
1894 1892
         if is_param_set(param) and not (
1895 1893