lockss-pybasic 0.1.0.dev6__tar.gz → 0.1.0.dev8__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.dev6
3
+ Version: 0.1.0.dev8
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-dev6"
3
+ version = "0.1.0-dev8"
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-dev6'
35
+ __version__ = '0.1.0-dev8'
@@ -0,0 +1,125 @@
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, 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
+ 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: List[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
+ def option_name(name: str):
125
+ return f'{('-' if len(name) == 1 else '--')}{name.replace('_', '-')}'
@@ -28,22 +28,11 @@
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
- import sys
31
+ '''
32
+ Error and exception utilities.
33
+ '''
32
34
 
33
- from pydantic.v1 import BaseModel, Field
35
+ class InternalError(RuntimeError):
34
36
 
35
- class VersionCommand:
36
-
37
- @staticmethod
38
- def make(version):
39
- class VersionModel(BaseModel):
40
-
41
- def print_version(self, file=sys.stdout):
42
- print(version, file=file)
43
- sys.exit(0)
44
-
45
- return VersionModel
46
-
47
- @staticmethod
48
- def field():
49
- return Field(description='print the version number and exit')
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()