lockss-pybasic 0.1.0.dev12__tar.gz → 0.1.0.dev14__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/PKG-INFO +2 -2
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/pyproject.toml +2 -2
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/src/lockss/pybasic/__init__.py +1 -1
- lockss_pybasic-0.1.0.dev14/src/lockss/pybasic/cliutil.py +201 -0
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/src/lockss/pybasic/errorutil.py +4 -0
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/src/lockss/pybasic/fileutil.py +19 -3
- lockss_pybasic-0.1.0.dev12/src/lockss/pybasic/cliutil.py +0 -141
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/LICENSE +0 -0
- {lockss_pybasic-0.1.0.dev12 → lockss_pybasic-0.1.0.dev14}/README.rst +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: lockss-pybasic
|
|
3
|
-
Version: 0.1.0.
|
|
3
|
+
Version: 0.1.0.dev14
|
|
4
4
|
Summary: Basic Python utilities
|
|
5
5
|
License: BSD-3-Clause
|
|
6
6
|
Author: Thib Guicherd-Callin
|
|
@@ -13,7 +13,7 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
13
13
|
Classifier: Programming Language :: Python :: 3.11
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.12
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
-
Requires-Dist: pydantic (>=2.11.
|
|
16
|
+
Requires-Dist: pydantic (>=2.11.0,<3.0.0)
|
|
17
17
|
Requires-Dist: pydantic-argparse (>=0.10.0,<0.11.0)
|
|
18
18
|
Description-Content-Type: text/x-rst
|
|
19
19
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "lockss-pybasic"
|
|
3
|
-
version = "0.1.0-
|
|
3
|
+
version = "0.1.0-dev14"
|
|
4
4
|
description = "Basic Python utilities"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Thib Guicherd-Callin", email = "thib@cs.stanford.edu" }
|
|
@@ -9,7 +9,7 @@ license = { text = "BSD-3-Clause" }
|
|
|
9
9
|
readme = "README.rst"
|
|
10
10
|
requires-python = ">=3.9,<4.0"
|
|
11
11
|
dependencies = [
|
|
12
|
-
"pydantic (>=2.11.
|
|
12
|
+
"pydantic (>=2.11.0,<3.0.0)",
|
|
13
13
|
"pydantic-argparse (>=0.10.0,<0.11.0)"
|
|
14
14
|
]
|
|
15
15
|
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
|
|
3
|
+
# Copyright (c) 2000-2025, Board of Trustees of Leland Stanford Jr. University
|
|
4
|
+
#
|
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
|
6
|
+
# modification, are permitted provided that the following conditions are met:
|
|
7
|
+
#
|
|
8
|
+
# 1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
# this list of conditions and the following disclaimer.
|
|
10
|
+
#
|
|
11
|
+
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
# this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
# and/or other materials provided with the distribution.
|
|
14
|
+
#
|
|
15
|
+
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
+
# may be used to endorse or promote products derived from this software without
|
|
17
|
+
# specific prior written permission.
|
|
18
|
+
#
|
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
22
|
+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
23
|
+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
24
|
+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
25
|
+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
26
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
27
|
+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
28
|
+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
29
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
Command line utilities.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from abc import ABC, abstractmethod
|
|
36
|
+
from collections.abc import Callable
|
|
37
|
+
import sys
|
|
38
|
+
from typing import Any, Dict, Generic, Optional, TypeVar
|
|
39
|
+
|
|
40
|
+
from pydantic.v1 import BaseModel, Field
|
|
41
|
+
from pydantic_argparse import ArgumentParser
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class ActionCommand(Callable, BaseModel):
|
|
45
|
+
"""
|
|
46
|
+
Base class for a Pydantic-Argparse command
|
|
47
|
+
"""
|
|
48
|
+
pass
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class StringCommand(ActionCommand):
|
|
52
|
+
|
|
53
|
+
@staticmethod
|
|
54
|
+
def type(display_str: str):
|
|
55
|
+
class _StringCommand(StringCommand):
|
|
56
|
+
def __call__(self, file=sys.stdout):
|
|
57
|
+
print(display_str, file=file)
|
|
58
|
+
return _StringCommand
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
COPYRIGHT_DESCRIPTION = 'print the copyright and exit'
|
|
62
|
+
LICENSE_DESCRIPTION = 'print the software license and exit'
|
|
63
|
+
VERSION_DESCRIPTION = 'print the version number and exit'
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
BaseModelT = TypeVar('BaseModelT', bound=BaseModel)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class BaseCli(Generic[BaseModelT]):
|
|
70
|
+
"""
|
|
71
|
+
Base class for general CLI functionality.
|
|
72
|
+
|
|
73
|
+
``BaseModelT`` represents a Pydantic-Argparse model type deriving from
|
|
74
|
+
``BaseModel``.
|
|
75
|
+
|
|
76
|
+
For each command ``x-y-z`` induced by a Pydantic-Argparse field named
|
|
77
|
+
``x_y_z`` deriving from ``BaseModel`` , the ``dispatch()`` method expects a
|
|
78
|
+
method named ``_x_y_z``.
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
def __init__(self, **extra):
|
|
82
|
+
"""
|
|
83
|
+
Makes a new BaseCli instance.
|
|
84
|
+
|
|
85
|
+
:param extra: Keyword arguments. Must include ``model``, the
|
|
86
|
+
``BaseModel`` type corresponding to ``BaseModelT``. Must
|
|
87
|
+
include ``prog``, the command-line name of the program.
|
|
88
|
+
Must include ``description``, the description string of
|
|
89
|
+
the program.
|
|
90
|
+
"""
|
|
91
|
+
super().__init__()
|
|
92
|
+
self.args: Optional[BaseModelT] = None
|
|
93
|
+
self.parser: Optional[ArgumentParser] = None
|
|
94
|
+
self.extra: Dict[str, Any] = dict(**extra)
|
|
95
|
+
|
|
96
|
+
def run(self) -> None:
|
|
97
|
+
"""
|
|
98
|
+
Runs the command line tool:
|
|
99
|
+
|
|
100
|
+
* Creates a Pydantic-Argparse ``ArgumentParser`` with ``model``,
|
|
101
|
+
``prog`` and ``description`` from the constructor keyword
|
|
102
|
+
arguments in ``self.parser``.
|
|
103
|
+
|
|
104
|
+
* Stores the Pydantic-Argparse parsed arguemnts in ``self.args``.
|
|
105
|
+
|
|
106
|
+
* Calls ``dispatch()``.
|
|
107
|
+
|
|
108
|
+
:return: Nothing.
|
|
109
|
+
"""
|
|
110
|
+
self.parser: ArgumentParser = ArgumentParser(model=self.extra.get('model'),
|
|
111
|
+
prog=self.extra.get('prog'),
|
|
112
|
+
description=self.extra.get('description'))
|
|
113
|
+
self.args = self.parser.parse_typed_args()
|
|
114
|
+
self.dispatch()
|
|
115
|
+
|
|
116
|
+
def dispatch(self) -> None:
|
|
117
|
+
"""
|
|
118
|
+
Dispatches from the first field ``x_y_z`` in ``self.args`` that is a
|
|
119
|
+
command (i.e. whose value derives from ``BaseModel``) to a method
|
|
120
|
+
called ``_x_y_z``.
|
|
121
|
+
|
|
122
|
+
:return: Nothing.
|
|
123
|
+
"""
|
|
124
|
+
field_names = self.args.__class__.__fields__.keys()
|
|
125
|
+
for field_name in field_names:
|
|
126
|
+
field_value = getattr(self.args, field_name)
|
|
127
|
+
if issubclass(type(field_value), BaseModel):
|
|
128
|
+
func = getattr(self, f'_{field_name}')
|
|
129
|
+
if callable(func):
|
|
130
|
+
func(field_value)
|
|
131
|
+
else:
|
|
132
|
+
self.parser.exit(1, f'internal error: no _{field_name} callable for the {field_name} command')
|
|
133
|
+
break
|
|
134
|
+
else:
|
|
135
|
+
self.parser.error(f'unknown command; expected one of {', '.join(field_names)}')
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def at_most_one_from_enum(model_cls, values: Dict[str, Any], enum_cls) -> Dict[str, Any]:
|
|
139
|
+
"""
|
|
140
|
+
Among the fields of a Pydantic-Argparse model whose ``Field`` definition is
|
|
141
|
+
tagged with the ``enum`` keyword set to the given ``Enum`` type, ensures
|
|
142
|
+
that at most one of them has a true value in the given Pydantic-Argparse
|
|
143
|
+
validator ``values``, or raises a ``ValueError`` otherwise.
|
|
144
|
+
|
|
145
|
+
:param model_cls: A Pydantic-Argparse model class.
|
|
146
|
+
:param values: The Pydantic-Argparse validator ``values``.
|
|
147
|
+
:param enum_cls: The ``Enum`` class the fields of the Pydantic-Argparse
|
|
148
|
+
model are tagged with (using the ``enum`` keyword).
|
|
149
|
+
:return: The ``values`` argument, if no ``ValueError`` has been raised.
|
|
150
|
+
"""
|
|
151
|
+
enum_names = [field_name for field_name, model_field in model_cls.__fields__.items() if model_field.field_info.extra.get('enum') == enum_cls]
|
|
152
|
+
ret = [field_name for field_name in enum_names if values.get(field_name)]
|
|
153
|
+
if (length := len(ret)) > 1:
|
|
154
|
+
raise ValueError(f'at most one of {', '.join([option_name(enum_name) for enum_name in enum_names])} is allowed, got {length} ({', '.join([option_name(enum_name) for enum_name in ret])})')
|
|
155
|
+
return values
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def get_from_enum(model_inst, enum_cls, default=None):
|
|
159
|
+
"""
|
|
160
|
+
Among the fields of a Pydantic-Argparse model whose ``Field`` definition is
|
|
161
|
+
tagged with the ``enum`` keyword set to the given ``Enum`` type, gets the
|
|
162
|
+
corresponding enum value of the first with a true value in the model, or
|
|
163
|
+
returns the given default value. Assumes the existence of a
|
|
164
|
+
``from_member()`` static method in the ``Enum`` class.
|
|
165
|
+
|
|
166
|
+
:param model_inst:
|
|
167
|
+
:param enum_cls:
|
|
168
|
+
:param default:
|
|
169
|
+
:return:
|
|
170
|
+
"""
|
|
171
|
+
enum_names = [field_name for field_name, model_field in type(model_inst).__fields__.items() if model_field.field_info.extra.get('enum') == enum_cls]
|
|
172
|
+
for field_name in enum_names:
|
|
173
|
+
if getattr(model_inst, field_name):
|
|
174
|
+
return enum_cls[field_name]
|
|
175
|
+
return default
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def at_most_one(values: Dict[str, Any], *names: str):
|
|
179
|
+
if (length := _matchy_length(values, *names)) > 1:
|
|
180
|
+
raise ValueError(f'at most one of {', '.join([option_name(name) for name in names])} is allowed, got {length}')
|
|
181
|
+
return values
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def exactly_one(values: Dict[str, Any], *names: str):
|
|
185
|
+
if (length := _matchy_length(values, *names)) != 1:
|
|
186
|
+
raise ValueError(f'exactly one of {', '.join([option_name(name) for name in names])} is required, got {length}')
|
|
187
|
+
return values
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def one_or_more(values: Dict[str, Any], *names: str):
|
|
191
|
+
if _matchy_length(values, *names) == 0:
|
|
192
|
+
raise ValueError(f'one or more of {', '.join([option_name(name) for name in names])} is required')
|
|
193
|
+
return values
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def option_name(name: str):
|
|
197
|
+
return f'{('-' if len(name) == 1 else '--')}{name.replace('_', '-')}'
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def _matchy_length(values: Dict[str, Any], *names: str) -> int:
|
|
201
|
+
return len([name for name in names if values.get(name)])
|
|
@@ -33,6 +33,10 @@ Error and exception utilities.
|
|
|
33
33
|
"""
|
|
34
34
|
|
|
35
35
|
class InternalError(RuntimeError):
|
|
36
|
+
"""
|
|
37
|
+
A simple ``RuntimeError`` with the message ``internal error``, intended for
|
|
38
|
+
situations that should never occur.
|
|
39
|
+
"""
|
|
36
40
|
|
|
37
41
|
def __init__(self, *args: object) -> None:
|
|
38
42
|
super().__init__('internal error')
|
|
@@ -34,10 +34,17 @@ File and path utilities.
|
|
|
34
34
|
|
|
35
35
|
from pathlib import Path, PurePath
|
|
36
36
|
import sys
|
|
37
|
-
from typing import Union
|
|
37
|
+
from typing import List, Union
|
|
38
38
|
|
|
39
39
|
|
|
40
|
-
def file_lines(fpath: Path):
|
|
40
|
+
def file_lines(fpath: Path) -> List[str]:
|
|
41
|
+
"""
|
|
42
|
+
Returns a list of lines from the given file, with '#' comments and leading
|
|
43
|
+
trailing whitespace removed, ignoring empty lines.
|
|
44
|
+
|
|
45
|
+
:param fpath: A ``Path`` to a file (``-`` for ``sys.stdin``).
|
|
46
|
+
:return: A list of lines.
|
|
47
|
+
"""
|
|
41
48
|
f = None
|
|
42
49
|
try:
|
|
43
50
|
f = open(path(fpath), 'r') if fpath != '-' else sys.stdin
|
|
@@ -47,7 +54,16 @@ def file_lines(fpath: Path):
|
|
|
47
54
|
f.close()
|
|
48
55
|
|
|
49
56
|
|
|
50
|
-
def path(purepath_or_string: Union[PurePath, str]):
|
|
57
|
+
def path(purepath_or_string: Union[PurePath, str]) -> Path:
|
|
58
|
+
"""
|
|
59
|
+
Returns the given ``PurePath`` (or if given a string, the ``Path`` created
|
|
60
|
+
from that string), expanded with ``expanduser()`` and resolved with
|
|
61
|
+
``resolve()``.
|
|
62
|
+
|
|
63
|
+
:param purepath_or_string: A ``PurePath`` (or a ``str`` from which to create
|
|
64
|
+
a ``Path``).
|
|
65
|
+
:return: An expanded and resolved ``Path``.
|
|
66
|
+
"""
|
|
51
67
|
if not issubclass(type(purepath_or_string), PurePath):
|
|
52
68
|
purepath_or_string = Path(purepath_or_string)
|
|
53
69
|
return purepath_or_string.expanduser().resolve()
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
|
|
3
|
-
# Copyright (c) 2000-2025, Board of Trustees of Leland Stanford Jr. University
|
|
4
|
-
#
|
|
5
|
-
# Redistribution and use in source and binary forms, with or without
|
|
6
|
-
# modification, are permitted provided that the following conditions are met:
|
|
7
|
-
#
|
|
8
|
-
# 1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
-
# this list of conditions and the following disclaimer.
|
|
10
|
-
#
|
|
11
|
-
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
-
# this list of conditions and the following disclaimer in the documentation
|
|
13
|
-
# and/or other materials provided with the distribution.
|
|
14
|
-
#
|
|
15
|
-
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
16
|
-
# may be used to endorse or promote products derived from this software without
|
|
17
|
-
# specific prior written permission.
|
|
18
|
-
#
|
|
19
|
-
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
-
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
-
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
22
|
-
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
23
|
-
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
24
|
-
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
25
|
-
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
26
|
-
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
27
|
-
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
28
|
-
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
29
|
-
# POSSIBILITY OF SUCH DAMAGE.
|
|
30
|
-
|
|
31
|
-
"""
|
|
32
|
-
Command line utilities.
|
|
33
|
-
"""
|
|
34
|
-
|
|
35
|
-
from abc import ABC, abstractmethod
|
|
36
|
-
import sys
|
|
37
|
-
from typing import Any, Dict, Generic, List, Optional, TypeVar
|
|
38
|
-
|
|
39
|
-
from pydantic.v1 import BaseModel, Field
|
|
40
|
-
from pydantic_argparse import ArgumentParser
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
class Printable(ABC):
|
|
44
|
-
|
|
45
|
-
def print(self, file=sys.stdout):
|
|
46
|
-
print(self.get_display(), file=file)
|
|
47
|
-
|
|
48
|
-
@abstractmethod
|
|
49
|
-
def get_display(self):
|
|
50
|
-
pass
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
class CopyrightCommand:
|
|
54
|
-
|
|
55
|
-
@staticmethod
|
|
56
|
-
def make(copyright):
|
|
57
|
-
class CopyrightModel(Printable, BaseModel):
|
|
58
|
-
def get_display(self):
|
|
59
|
-
return copyright
|
|
60
|
-
return CopyrightModel
|
|
61
|
-
|
|
62
|
-
@staticmethod
|
|
63
|
-
def field():
|
|
64
|
-
return Field(description='print the copyright and exit')
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
class LicenseCommand:
|
|
68
|
-
|
|
69
|
-
@staticmethod
|
|
70
|
-
def make(license):
|
|
71
|
-
class LicenseModel(Printable, BaseModel):
|
|
72
|
-
def get_display(self):
|
|
73
|
-
return license
|
|
74
|
-
return LicenseModel
|
|
75
|
-
|
|
76
|
-
@staticmethod
|
|
77
|
-
def field():
|
|
78
|
-
return Field(description='print the software license and exit')
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
class VersionCommand:
|
|
82
|
-
|
|
83
|
-
@staticmethod
|
|
84
|
-
def make(version):
|
|
85
|
-
class VersionModel(Printable, BaseModel):
|
|
86
|
-
def get_display(self):
|
|
87
|
-
return version
|
|
88
|
-
return VersionModel
|
|
89
|
-
|
|
90
|
-
@staticmethod
|
|
91
|
-
def field():
|
|
92
|
-
return Field(description='print the version number and exit')
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
ModelT = TypeVar('ModelT')
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
class BaseCli(Generic[ModelT], ABC):
|
|
99
|
-
|
|
100
|
-
def __init__(self, **extra):
|
|
101
|
-
super().__init__()
|
|
102
|
-
self.args: ModelT = None
|
|
103
|
-
self.parser: ArgumentParser = None
|
|
104
|
-
self.extra: Dict[str, Any] = dict(**extra)
|
|
105
|
-
|
|
106
|
-
def run(self):
|
|
107
|
-
self.parser: ArgumentParser = ArgumentParser(model=self.extra.get('model'),
|
|
108
|
-
prog=self.extra.get('prog'),
|
|
109
|
-
description=self.extra.get('description'))
|
|
110
|
-
self.args = self.parser.parse_typed_args()
|
|
111
|
-
self.dispatch()
|
|
112
|
-
|
|
113
|
-
@abstractmethod
|
|
114
|
-
def dispatch(self):
|
|
115
|
-
pass
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
def _matchy_length(values: Dict[str, Any], *names: str) -> int:
|
|
119
|
-
return len([name for name in names if values.get(name)])
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
def at_most_one(values: Dict[str, Any], *names: str):
|
|
123
|
-
if (length := _matchy_length(values, names)) > 1:
|
|
124
|
-
raise ValueError(f'at most one of {', '.join([option_name(name) for name in names])} is allowed, got {length}')
|
|
125
|
-
return values
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
def exactly_one(values: Dict[str, Any], *names: str):
|
|
129
|
-
if (length := _matchy_length(values, names)) != 1:
|
|
130
|
-
raise ValueError(f'exactly one of {', '.join([option_name(name) for name in names])} is required, got {length}')
|
|
131
|
-
return values
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
def one_or_more(values: Dict[str, Any], *names: str):
|
|
135
|
-
if _matchy_length(values, names) == 0:
|
|
136
|
-
raise ValueError(f'one or more of {', '.join([option_name(name) for name in names])} is required')
|
|
137
|
-
return values
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
def option_name(name: str):
|
|
141
|
-
return f'{('-' if len(name) == 1 else '--')}{name.replace('_', '-')}'
|
|
File without changes
|
|
File without changes
|