lockss-pybasic 0.1.0.dev6__py3-none-any.whl → 0.1.0.dev8__py3-none-any.whl

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.
@@ -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'
lockss/pybasic/cliutil.py CHANGED
@@ -28,22 +28,98 @@
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
+ '''
32
+ Command line utilities.
33
+ '''
34
+
35
+ from abc import ABC, abstractmethod
31
36
  import sys
37
+ from typing import Any, Dict, Generic, List, TypeVar
32
38
 
33
39
  from pydantic.v1 import BaseModel, Field
40
+ from pydantic_argparse import ArgumentParser
34
41
 
35
- class VersionCommand:
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:
36
54
 
37
55
  @staticmethod
38
- def make(version):
39
- class VersionModel(BaseModel):
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')
40
79
 
41
- def print_version(self, file=sys.stdout):
42
- print(version, file=file)
43
- sys.exit(0)
44
80
 
81
+ class VersionCommand:
82
+
83
+ @staticmethod
84
+ def make(version):
85
+ class VersionModel(Printable, BaseModel):
86
+ def get_display(self):
87
+ return version
45
88
  return VersionModel
46
89
 
47
90
  @staticmethod
48
91
  def field():
49
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('_', '-')}'
@@ -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()
@@ -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
 
@@ -0,0 +1,8 @@
1
+ lockss/pybasic/__init__.py,sha256=S_imeto8t47j8ETAJvdZ2Jl1WMSmdVlzt-0pDzD1SVg,1645
2
+ lockss/pybasic/cliutil.py,sha256=z85gaLAn4M4Y3uBQGrJDgvDEmJ51V2M5JP1CzRLExSc,3956
3
+ lockss/pybasic/errorutil.py,sha256=8MdEaIpbzotsEzTN4hW8_VPEZcVfoti1C-Fueord7JU,1746
4
+ lockss/pybasic/fileutil.py,sha256=FVKP7CXjtC69G9-wJ_3javZt1FO3NCc21o-6w6QNL-Y,2199
5
+ lockss_pybasic-0.1.0.dev8.dist-info/LICENSE,sha256=O9ONND4uDxY_jucI4jZDf2liAk05ScEJaYu-Al7EOdQ,1506
6
+ lockss_pybasic-0.1.0.dev8.dist-info/METADATA,sha256=23jhPGwyNi4I4OLRc-AggJp0Uq7Rj01FjvI4PP9TYPE,695
7
+ lockss_pybasic-0.1.0.dev8.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
8
+ lockss_pybasic-0.1.0.dev8.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.2
2
+ Generator: poetry-core 2.1.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,6 +0,0 @@
1
- lockss/pybasic/__init__.py,sha256=rwuIboTuiRE-y9wGaTQSXwI1Mywjjl2ZtuDJJcIZVeM,1645
2
- lockss/pybasic/cliutil.py,sha256=3mH20MHVrDe5SQp1zbVDPs2l0tF4xlPERpfIKbPATug,1997
3
- lockss_pybasic-0.1.0.dev6.dist-info/LICENSE,sha256=O9ONND4uDxY_jucI4jZDf2liAk05ScEJaYu-Al7EOdQ,1506
4
- lockss_pybasic-0.1.0.dev6.dist-info/METADATA,sha256=pkkJ5ldDFBYzEN5bUCU4mYNPGgOJ1m0crgY0vKKGerA,643
5
- lockss_pybasic-0.1.0.dev6.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
6
- lockss_pybasic-0.1.0.dev6.dist-info/RECORD,,