lockss-pybasic 0.1.0.dev7__tar.gz → 0.1.0.dev9__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.dev7
3
+ Version: 0.1.0.dev9
4
4
  Summary: Basic Python utilities
5
5
  License: BSD-3-Clause
6
6
  Author: Thib Guicherd-Callin
@@ -14,6 +14,7 @@ Classifier: Programming Language :: Python :: 3.11
14
14
  Classifier: Programming Language :: Python :: 3.12
15
15
  Classifier: Programming Language :: Python :: 3.13
16
16
  Requires-Dist: pydantic (>=2.11.3,<3.0.0)
17
+ Requires-Dist: pydantic-argparse (>=0.10.0,<0.11.0)
17
18
  Description-Content-Type: text/x-rst
18
19
 
19
20
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "lockss-pybasic"
3
- version = "0.1.0-dev7"
3
+ version = "0.1.0-dev9"
4
4
  description = "Basic Python utilities"
5
5
  authors = [
6
6
  { name = "Thib Guicherd-Callin", email = "thib@cs.stanford.edu" }
@@ -9,7 +9,8 @@ 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.3,<3.0.0)"
12
+ "pydantic (>=2.11.3,<3.0.0)",
13
+ "pydantic-argparse (>=0.10.0,<0.11.0)"
13
14
  ]
14
15
 
15
16
  [tool.poetry]
@@ -32,4 +32,4 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
32
  POSSIBILITY OF SUCH DAMAGE.
33
33
  '''.strip()
34
34
 
35
- __version__ = '0.1.0-dev7'
35
+ __version__ = '0.1.0-dev9'
@@ -29,13 +29,15 @@
29
29
  # POSSIBILITY OF SUCH DAMAGE.
30
30
 
31
31
  '''
32
- Utilities for command line processing.
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
38
 
38
39
  from pydantic.v1 import BaseModel, Field
40
+ from pydantic_argparse import ArgumentParser
39
41
 
40
42
 
41
43
  class Printable(ABC):
@@ -88,3 +90,43 @@ class VersionCommand:
88
90
  @staticmethod
89
91
  def field():
90
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
+ args: ModelT
100
+ extra: Dict[str, Any]
101
+ parser: ArgumentParser
102
+
103
+ def __init__(self, **extra):
104
+ super().__init__()
105
+ self.extra = dict(**extra)
106
+
107
+ def run(self):
108
+ self.parser: ArgumentParser = ArgumentParser(model=self.extra.get('model'),
109
+ prog=self.extra.get('prog'),
110
+ description=self.extra.get('description'))
111
+ self.args = self.parser.parse_typed_args()
112
+ self.dispatch()
113
+
114
+ @abstractmethod
115
+ def dispatch(self):
116
+ pass
117
+
118
+
119
+ def exactly_one(values: Dict[str, Any], *names: str):
120
+ if (length := len([values[name] for name in names if values[name]])) != 1:
121
+ raise ValueError(f'exactly one of {', '.join([option_name(name) for name in names])} is required, got {length}')
122
+ return values
123
+
124
+
125
+ def one_or_more(values: Dict[str, Any], *names: str):
126
+ if len([values[name] for name in names if values[name]]) == 0:
127
+ raise ValueError(f'one or more of {', '.join([option_name(name) for name in names])} is required')
128
+ return values
129
+
130
+
131
+ def option_name(name: str):
132
+ return f'{('-' if len(name) == 1 else '--')}{name.replace('_', '-')}'
@@ -0,0 +1,38 @@
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
+ Error and exception utilities.
33
+ '''
34
+
35
+ class InternalError(RuntimeError):
36
+
37
+ def __init__(self, *args: object) -> None:
38
+ super().__init__('internal error')
@@ -0,0 +1,53 @@
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
+ File and path utilities.
33
+ '''
34
+
35
+ from pathlib import Path, PurePath
36
+ import sys
37
+ from typing import Union
38
+
39
+
40
+ def file_lines(fpath: Path):
41
+ f = None
42
+ try:
43
+ f = open(path(fpath), 'r') if fpath != '-' else sys.stdin
44
+ return [line for line in [line.partition('#')[0].strip() for line in f] if len(line) > 0]
45
+ finally:
46
+ if f is not None and path != '-':
47
+ f.close()
48
+
49
+
50
+ def path(purepath_or_string: Union[PurePath, str]):
51
+ if not issubclass(type(purepath_or_string), PurePath):
52
+ purepath_or_string = Path(purepath_or_string)
53
+ return purepath_or_string.expanduser().resolve()