lockss-pybasic 0.1.0.dev9__tar.gz → 0.1.0.dev10__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: lockss-pybasic
3
- Version: 0.1.0.dev9
3
+ Version: 0.1.0.dev10
4
4
  Summary: Basic Python utilities
5
5
  License: BSD-3-Clause
6
6
  Author: Thib Guicherd-Callin
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "lockss-pybasic"
3
- version = "0.1.0-dev9"
3
+ version = "0.1.0-dev10"
4
4
  description = "Basic Python utilities"
5
5
  authors = [
6
6
  { name = "Thib Guicherd-Callin", email = "thib@cs.stanford.edu" }
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env python3
2
2
 
3
+ """
4
+ Basic Python utilities.
5
+ """
6
+
3
7
  __copyright__ = '''
4
8
  Copyright (c) 2000-2025, Board of Trustees of Leland Stanford Jr. University
5
9
  '''.strip()
@@ -32,4 +36,4 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
36
  POSSIBILITY OF SUCH DAMAGE.
33
37
  '''.strip()
34
38
 
35
- __version__ = '0.1.0-dev9'
39
+ __version__ = '0.1.0-dev10'
@@ -28,13 +28,13 @@
28
28
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
29
  # POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
- '''
31
+ """
32
32
  Command line utilities.
33
- '''
33
+ """
34
34
 
35
35
  from abc import ABC, abstractmethod
36
36
  import sys
37
- from typing import Any, Dict, Generic, List, TypeVar
37
+ from typing import Any, Dict, Generic, List, Optional, TypeVar
38
38
 
39
39
  from pydantic.v1 import BaseModel, Field
40
40
  from pydantic_argparse import ArgumentParser
@@ -96,13 +96,12 @@ ModelT = TypeVar('ModelT')
96
96
 
97
97
 
98
98
  class BaseCli(Generic[ModelT], ABC):
99
- args: ModelT
100
- extra: Dict[str, Any]
101
- parser: ArgumentParser
102
99
 
103
100
  def __init__(self, **extra):
104
101
  super().__init__()
105
- self.extra = dict(**extra)
102
+ self.args: ModelT = None
103
+ self.parser: ArgumentParser = None
104
+ self.extra: Dict[str, Any] = dict(**extra)
106
105
 
107
106
  def run(self):
108
107
  self.parser: ArgumentParser = ArgumentParser(model=self.extra.get('model'),
@@ -116,14 +115,24 @@ class BaseCli(Generic[ModelT], ABC):
116
115
  pass
117
116
 
118
117
 
118
+ def _matchy_length(values: Dict[str, Any], *names: str) -> int:
119
+ return len([values[name] for name in names if values[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
+
119
128
  def exactly_one(values: Dict[str, Any], *names: str):
120
- if (length := len([values[name] for name in names if values[name]])) != 1:
129
+ if (length := _matchy_length(values, names)) != 1:
121
130
  raise ValueError(f'exactly one of {', '.join([option_name(name) for name in names])} is required, got {length}')
122
131
  return values
123
132
 
124
133
 
125
134
  def one_or_more(values: Dict[str, Any], *names: str):
126
- if len([values[name] for name in names if values[name]]) == 0:
135
+ if _matchy_length(values, names) == 0:
127
136
  raise ValueError(f'one or more of {', '.join([option_name(name) for name in names])} is required')
128
137
  return values
129
138
 
@@ -28,9 +28,9 @@
28
28
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
29
  # POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
- '''
31
+ """
32
32
  Error and exception utilities.
33
- '''
33
+ """
34
34
 
35
35
  class InternalError(RuntimeError):
36
36
 
@@ -28,9 +28,9 @@
28
28
  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
29
  # POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
- '''
31
+ """
32
32
  File and path utilities.
33
- '''
33
+ """
34
34
 
35
35
  from pathlib import Path, PurePath
36
36
  import sys