lockss-pybasic 0.1.0.dev14__tar.gz → 0.1.0.dev16__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.dev14
3
+ Version: 0.1.0.dev16
4
4
  Summary: Basic Python utilities
5
5
  License: BSD-3-Clause
6
6
  Author: Thib Guicherd-Callin
@@ -15,6 +15,7 @@ Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
16
  Requires-Dist: pydantic (>=2.11.0,<3.0.0)
17
17
  Requires-Dist: pydantic-argparse (>=0.10.0,<0.11.0)
18
+ Requires-Dist: tabulate (>=0.9.0,<0.10.0)
18
19
  Description-Content-Type: text/x-rst
19
20
 
20
21
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "lockss-pybasic"
3
- version = "0.1.0-dev14"
3
+ version = "0.1.0-dev16"
4
4
  description = "Basic Python utilities"
5
5
  authors = [
6
6
  { name = "Thib Guicherd-Callin", email = "thib@cs.stanford.edu" }
@@ -10,7 +10,8 @@ readme = "README.rst"
10
10
  requires-python = ">=3.9,<4.0"
11
11
  dependencies = [
12
12
  "pydantic (>=2.11.0,<3.0.0)",
13
- "pydantic-argparse (>=0.10.0,<0.11.0)"
13
+ "pydantic-argparse (>=0.10.0,<0.11.0)",
14
+ "tabulate (>=0.9.0,<0.10.0)"
14
15
  ]
15
16
 
16
17
  [tool.poetry]
@@ -36,4 +36,4 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
36
  POSSIBILITY OF SUCH DAMAGE.
37
37
  '''.strip()
38
38
 
39
- __version__ = '0.1.0-dev14'
39
+ __version__ = '0.1.0-dev16'
@@ -89,8 +89,8 @@ class BaseCli(Generic[BaseModelT]):
89
89
  the program.
90
90
  """
91
91
  super().__init__()
92
- self.args: Optional[BaseModelT] = None
93
- self.parser: Optional[ArgumentParser] = None
92
+ self._args: Optional[BaseModelT] = None
93
+ self._parser: Optional[ArgumentParser] = None
94
94
  self.extra: Dict[str, Any] = dict(**extra)
95
95
 
96
96
  def run(self) -> None:
@@ -107,10 +107,10 @@ class BaseCli(Generic[BaseModelT]):
107
107
 
108
108
  :return: Nothing.
109
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()
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
114
  self.dispatch()
115
115
 
116
116
  def dispatch(self) -> None:
@@ -121,18 +121,18 @@ class BaseCli(Generic[BaseModelT]):
121
121
 
122
122
  :return: Nothing.
123
123
  """
124
- field_names = self.args.__class__.__fields__.keys()
124
+ field_names = self._args.__class__.__fields__.keys()
125
125
  for field_name in field_names:
126
- field_value = getattr(self.args, field_name)
126
+ field_value = getattr(self._args, field_name)
127
127
  if issubclass(type(field_value), BaseModel):
128
128
  func = getattr(self, f'_{field_name}')
129
129
  if callable(func):
130
130
  func(field_value)
131
131
  else:
132
- self.parser.exit(1, f'internal error: no _{field_name} callable for the {field_name} command')
132
+ self._parser.exit(1, f'internal error: no _{field_name} callable for the {field_name} command')
133
133
  break
134
134
  else:
135
- self.parser.error(f'unknown command; expected one of {', '.join(field_names)}')
135
+ self._parser.error(f'unknown command; expected one of {', '.join(field_names)}')
136
136
 
137
137
 
138
138
  def at_most_one_from_enum(model_cls, values: Dict[str, Any], enum_cls) -> Dict[str, Any]:
@@ -0,0 +1,59 @@
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
+ Utilities to work with 'tabulate'.
33
+ """
34
+
35
+
36
+ from enum import Enum
37
+
38
+
39
+ from pydantic.v1 import BaseModel, Field, validator
40
+ import tabulate
41
+ from typing import Optional
42
+
43
+
44
+ OutputFormat = Enum('OutputFormat', tabulate.tabulate_formats)
45
+
46
+
47
+ DEFAULT_OUTPUT_FORMAT = 'simple' # from tabulate
48
+
49
+
50
+ class OutputFormatOptions(BaseModel):
51
+ output_format: Optional[str] = Field(DEFAULT_OUTPUT_FORMAT, description=f'[output] set the output format; choices: {', '.join(OutputFormat.__members__.keys())}')
52
+
53
+ @validator('output_format')
54
+ def _validate_output_format(cls, val: str):
55
+ try:
56
+ _ = OutputFormat[val]
57
+ return val
58
+ except KeyError:
59
+ raise ValueError(f'must be one of {', '.join(OutputFormat.__members__.keys())}; got {val}')