lockss-pybasic 0.3.0.dev2__tar.gz → 0.3.0.dev4__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.4
2
2
  Name: lockss-pybasic
3
- Version: 0.3.0.dev2
3
+ Version: 0.3.0.dev4
4
4
  Summary: Basic Python utilities
5
5
  License: BSD-3-Clause
6
6
  License-File: LICENSE
@@ -16,7 +16,8 @@ Classifier: Intended Audience :: Developers
16
16
  Classifier: License :: OSI Approved :: BSD License
17
17
  Classifier: Programming Language :: Python
18
18
  Classifier: Topic :: Software Development :: Libraries
19
- Requires-Dist: click-extra (>=7.16.0,<7.17.0)
19
+ Requires-Dist: annotated-types (>=0.7.0,<0.8.0)
20
+ Requires-Dist: click-extra (>=7.17.0,<7.18.0)
20
21
  Requires-Dist: pydantic (>=2.13.0,<2.14.0)
21
22
  Project-URL: Repository, https://github.com/lockss/lockss-pybasic
22
23
  Description-Content-Type: text/x-rst
@@ -25,7 +26,7 @@ Description-Content-Type: text/x-rst
25
26
  lockss-pybasic
26
27
  ==============
27
28
 
28
- .. |RELEASE| replace:: 0.3.0-dev2
29
+ .. |RELEASE| replace:: 0.3.0-dev4
29
30
  .. |RELEASE_DATE| replace:: NOT YET RELEASED
30
31
 
31
32
  **Latest release:** |RELEASE| (|RELEASE_DATE|)
@@ -2,7 +2,7 @@
2
2
  lockss-pybasic
3
3
  ==============
4
4
 
5
- .. |RELEASE| replace:: 0.3.0-dev2
5
+ .. |RELEASE| replace:: 0.3.0-dev4
6
6
  .. |RELEASE_DATE| replace:: NOT YET RELEASED
7
7
 
8
8
  **Latest release:** |RELEASE| (|RELEASE_DATE|)
@@ -28,7 +28,7 @@
28
28
 
29
29
  [project]
30
30
  name = "lockss-pybasic"
31
- version = "0.3.0-dev2" # Always change in __init__.py, and at release time in README.rst and CHANGELOG.rst
31
+ version = "0.3.0-dev4" # Always change in __init__.py, and at release time in README.rst and CHANGELOG.rst
32
32
  description = "Basic Python utilities"
33
33
  license = { text = "BSD-3-Clause" }
34
34
  readme = "README.rst"
@@ -40,7 +40,8 @@ maintainers = [
40
40
  { name = "Thib Guicherd-Callin", email = "thib@cs.stanford.edu" },
41
41
  ]
42
42
  dependencies = [
43
- "click-extra (>=7.16.0,<7.17.0)",
43
+ "annotated-types (>=0.7.0,<0.8.0)", # from Pydantic
44
+ "click-extra (>=7.17.0,<7.18.0)",
44
45
  "pydantic (>=2.13.0,<2.14.0)",
45
46
  ]
46
47
  classifiers = [
@@ -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.3.0-dev2'
39
+ __version__ = '0.3.0-dev4'
@@ -0,0 +1,216 @@
1
+ #!/usr/bin/env python3
2
+
3
+ # Copyright (c) 2000-2026, 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
+ LOCKSS node utilities.
33
+ """
34
+
35
+ from enum import Enum
36
+ from re import Match, Pattern
37
+ import re
38
+ from typing import Annotated, Any, ClassVar, Literal, Optional, Union
39
+
40
+ from annotated_types import Ge, Le
41
+ from pydantic import BaseModel, BeforeValidator, Field, TypeAdapter, model_validator
42
+
43
+
44
+ #: An annotated type for port numbers (0-65535)
45
+ PortNumber = Annotated[int, Ge(0), Le(65535)]
46
+
47
+
48
+ class NodeTypeEnum(Enum):
49
+ """An enumerated type representing LOCKSS node types."""
50
+ #: An enumerated constant representing LOCKSS 1.x nodes.
51
+ V1 = 'v1'
52
+ #: An enumerated constant representing LOCKSS 2.x nodes.
53
+ V2 = 'v2'
54
+
55
+
56
+ class NodeProtocolEnum(Enum):
57
+ """An enumerated type representing protocols for reaching LOCKSS nodes."""
58
+ #: An enumerated constant representing HTTP.
59
+ HTTP = 'http'
60
+ #: An enumerated constant representing HTTPS.
61
+ HTTPS = 'https'
62
+
63
+
64
+ class BaseNodeSpec(BaseModel):
65
+
66
+ DEFAULT_PROTOCOL: ClassVar[NodeProtocolEnum] = NodeProtocolEnum.HTTP
67
+
68
+ TYPE_FIELD: ClassVar[dict[str, str]] = dict(title='Type',
69
+ description="The node's type")
70
+
71
+ protocol: NodeProtocolEnum = Field(default=DEFAULT_PROTOCOL,
72
+ title='Protocol',
73
+ description="The protocol for reaching the node")
74
+
75
+ host: str = Field(title='Host',
76
+ description="The node's host")
77
+
78
+
79
+ class NodeSpec1(BaseNodeSpec):
80
+
81
+ DEFAULT_UI_PORT_V1: ClassVar[int] = 8081
82
+
83
+ type: Literal['v1'] = Field(**BaseNodeSpec.TYPE_FIELD)
84
+
85
+ ui: PortNumber = Field(default=DEFAULT_UI_PORT_V1,
86
+ title='UI Port',
87
+ description="The LOCKSS 1.x node's Web user interface port")
88
+
89
+
90
+ class NodeSpec2(BaseNodeSpec):
91
+
92
+ DEFAULT_REPO_PORT: ClassVar[int] = 24611
93
+
94
+ DEFAULT_CFG_PORT: ClassVar[int] = 24612
95
+
96
+ DEFAULT_POL_PORT: ClassVar[int] = 24613
97
+
98
+ DEFAULT_CRW_PORT: ClassVar[int] = 24614
99
+
100
+ DEFAULT_MD_PORT: ClassVar[int] = 24615
101
+
102
+ DEFAULT_SOAP_PORT: ClassVar[int] = 24616
103
+
104
+ type: Literal['v2'] = Field(**BaseNodeSpec.TYPE_FIELD)
105
+
106
+ repository: PortNumber = Field(default=DEFAULT_REPO_PORT,
107
+ title='Repository Port',
108
+ description="The node's Repository Service REST API Port")
109
+
110
+ configuration: PortNumber = Field(default=DEFAULT_CFG_PORT,
111
+ title='Configuration Port',
112
+ description="The node's Configuration Service REST API Port")
113
+
114
+ poller: PortNumber = Field(default=DEFAULT_POL_PORT,
115
+ title='Poller Port',
116
+ description="The node's Poller Service REST API Port")
117
+
118
+ crawler: PortNumber = Field(default=DEFAULT_CRW_PORT,
119
+ title='Crawler Port',
120
+ description="The node's Crawler Service REST API Port")
121
+
122
+ metadata: PortNumber = Field(default=DEFAULT_MD_PORT,
123
+ title='Metadata Port',
124
+ description="The node's Metadata Service REST API Port")
125
+
126
+ soap: PortNumber = Field(default=DEFAULT_SOAP_PORT,
127
+ title='SOAP Port',
128
+ description="The node's SOAP Compatibility Service REST API Port")
129
+
130
+
131
+ _RE_NODE_REFERENCE: Pattern[str] = re.compile(r'((?P<protocol>https?)://)?(?P<host>[^:]+)(:(?P<repository>\d+|(?=:))(:(?P<configuration>\d+|(?=:))(:(?P<poller>\d+|(?=:))(:(?P<crawler>\d+|(?=:))(:(?P<metadata>\d+|(?=:))(:(?P<soap>\d+))?)?)?)?)?)?')
132
+
133
+
134
+ #: A type for LOCKSS node specification strings.
135
+ NodeSpecStr = str
136
+
137
+
138
+ def _parse_node_spec_string(node_spec_string: NodeSpecStr) -> dict[str, str]:
139
+ mat: Optional[Match[str]] = _RE_NODE_REFERENCE.fullmatch(node_spec_string)
140
+ if mat is None:
141
+ raise ValueError(f'Invalid node specification string: {node_spec_string}')
142
+ d = dict(host=mat.group('host'))
143
+ if prot := mat.group('protocol'):
144
+ d['protocol'] = prot
145
+ five = ('configuration', 'poller', 'crawler', 'metadata', 'soap')
146
+ if repo_or_ui := mat.group('repository'):
147
+ if any(mat.group(x) for x in five) or len(repo_or_ui) >= 5:
148
+ # 10000 or larger: assume V2
149
+ d['type'] = NodeTypeEnum.V2.value
150
+ d['repository'] = repo_or_ui
151
+ elif len(repo_or_ui) == 4:
152
+ # 1000 through 9999: assume V1
153
+ d['type'] = NodeTypeEnum.V1.value
154
+ d['ui'] = repo_or_ui
155
+ else:
156
+ raise ValueError(f'Invalid repository/UI port in node specification string: {repo_or_ui}')
157
+ else:
158
+ # Assume V2
159
+ d['type'] = NodeTypeEnum.V2.value
160
+ for k in five:
161
+ if p := mat.group(k):
162
+ d[k] = p # string okay, will be coerced to int
163
+ return d
164
+
165
+
166
+ def _maybe_deserialize_node_spec_string(value: Any) -> Any:
167
+ if isinstance(value, NodeSpecStr) and not value.startswith('{'):
168
+ return _parse_node_spec_string(value)
169
+ return value
170
+
171
+
172
+ #: A type for LOCKSS node specifications, that also accepts a compact LOCKSS
173
+ #: node specification string.
174
+ NodeSpec = Annotated[
175
+ Annotated[Union[NodeSpec1, NodeSpec2], Field(discriminator='type')],
176
+ BeforeValidator(_maybe_deserialize_node_spec_string)
177
+ ]
178
+
179
+
180
+ #: A type adapter for the NodeSpec type.
181
+ _node_spec_adapter: TypeAdapter[NodeSpec] = TypeAdapter(NodeSpec)
182
+
183
+
184
+ def get_node_spec_adapter() -> TypeAdapter[NodeSpec]:
185
+ """
186
+ Gets a type adapter for the NodeSpec type, which is a union type and cannot
187
+ be instantiated directly.
188
+
189
+ :return: A type adapter for the NodeSpec type.
190
+ """
191
+ return _node_spec_adapter
192
+
193
+
194
+ NodeSetKind = Literal['NodeSet']
195
+
196
+
197
+ NodeSetIdentifier = str
198
+
199
+
200
+ NodeIdentifier = str
201
+
202
+
203
+ class NodeSet(BaseModel):
204
+
205
+ kind: NodeSetKind = Field(title='Kind',
206
+ description="This object's kind")
207
+
208
+ id: NodeSetIdentifier = Field(title='Node Set Identifier',
209
+ description='An identifier for the node set')
210
+
211
+ name: str = Field(title='Node Set Name',
212
+ description='A name for the node set')
213
+
214
+ nodes: dict[NodeIdentifier, NodeSpec] = Field(min_length=1,
215
+ title='Nodes',
216
+ description='A non-empty list of nodes')
@@ -1,121 +0,0 @@
1
- #!/usr/bin/env python3
2
-
3
- # Copyright (c) 2000-2026, 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
- LOCKSS node utilities.
33
- """
34
-
35
- from enum import Enum
36
- from re import Pattern
37
- import re
38
- from typing import Any, Optional
39
-
40
- from pydantic import BaseModel, Field, model_validator
41
-
42
- from .errorutil import InternalError
43
-
44
-
45
- RE_NODE_REFERENCE: Pattern = re.compile(r'((?P<protocol>https?)://)?(?P<host>[^:]+)(:(?P<repository>\d+|(?=:))(:(?P<configuration>\d+|(?=:))(:(?P<poller>\d+|(?=:))(:(?P<crawler>\d+|(?=:))(:(?P<metadata>\d+|(?=:))(:(?P<soap>\d+))?)?)?)?)?)?')
46
-
47
-
48
- class LockssNodeTypeEnum(Enum):
49
- V1 = 'v1'
50
- V2 = 'v2'
51
-
52
-
53
- class LockssNodeProtocolEnum(Enum):
54
- HTTP = 'http'
55
- HTTPS = 'https'
56
-
57
-
58
- DEFAULT_UI_PORT_V1: int = 8081
59
-
60
- DEFAULT_REPO_PORT: int = 24611
61
-
62
- DEFAULT_CFG_PORT: int = 24612
63
-
64
- DEFAULT_POL_PORT: int = 24613
65
-
66
- DEFAULT_CRW_PORT: int = 24614
67
-
68
- DEFAULT_MD_PORT: int = 24615
69
-
70
- DEFAULT_SOAP_PORT: int = 24616
71
-
72
-
73
- class LockssNodeModel(BaseModel):
74
- host: str = Field(title='Host', description="The LOCKSS node's host")
75
- type: LockssNodeTypeEnum = Field(default=LockssNodeTypeEnum.V2, title='Type', description='LOCKSS node type')
76
- protocol: LockssNodeProtocolEnum = Field(default=LockssNodeProtocolEnum.HTTPS, title='Protocol', description="The protocol for reaching the node")
77
- repository: int = Field(default=DEFAULT_REPO_PORT, title='Repository Port', description="The node's Repository Service REST API Port")
78
- configuration: int = Field(default=DEFAULT_CFG_PORT, title='Configuration Port', description="The node's Configuration Service REST API Port")
79
- poller: int = Field(default=DEFAULT_POL_PORT, title='Poller Port', description="The node's Poller Service REST API Port")
80
- crawler: int = Field(default=DEFAULT_CRW_PORT, title='Crawler Port', description="The node's Crawler Service REST API Port")
81
- metadata: int = Field(default=DEFAULT_MD_PORT, title='Metadata Port', description="The node's Metadata Service REST API Port")
82
- soap: int = Field(default=DEFAULT_SOAP_PORT, title='SOAP Port', description="The node's SOAP Compatibility Service REST API Port")
83
- ui: int = Field(default=DEFAULT_UI_PORT_V1, title='UI Port', description="The LOCKSS 1.x node's Web user interface port")
84
-
85
- @model_validator(mode='before')
86
- @classmethod
87
- def _validate_model_type(cls, data: Any) -> Any:
88
- _has, _get, _set = (dict.__contains__, dict.get, dict.__setitem__) if isinstance(data, dict) else (hasattr, getattr, setattr)
89
- if (host_orig := _get(data, 'host', None)) is not None and isinstance(host_orig, str):
90
- host_ref: str = host_orig
91
- mat = RE_NODE_REFERENCE.fullmatch(host_ref)
92
- if mat is None:
93
- raise ValueError(f'Invalid node reference: {host_ref}')
94
- # Avoid conflicting definitions
95
- five = ('configuration', 'poller', 'crawler', 'metadata', 'soap')
96
- for k in ('protocol', *five):
97
- if h_val := mat.group(k):
98
- if _get(data, k, None) is not None:
99
- raise ValueError(f"Node reference conflicts with '{k}' definition: {h_val}")
100
- _set(data, k, h_val)
101
- # 'repository' + 'ui' is more complicated
102
- if h_repo_or_ui := mat.group('repository'):
103
- for k in ('repository', 'ui'):
104
- if _get(data, k, None) is not None:
105
- raise ValueError(f"Node reference conflicts with '{k}' definition: {h_repo_or_ui}")
106
- try:
107
- repo_or_ui_int: int = int(h_repo_or_ui)
108
- except ValueError as ve:
109
- raise InternalError from ve # shouldn't happen, should be \d+
110
- if any(mat.group(x) for x in five) or repo_or_ui_int >= 10000:
111
- _set(data, 'repository', h_repo_or_ui) # Assume v2
112
- elif 1000 <= repo_or_ui_int < 10000:
113
- _set(data, 'ui', h_repo_or_ui) # Assume v1
114
- if _get(data, 'type', None) is None:
115
- _set(data, 'type', LockssNodeTypeEnum.V1.value)
116
- else:
117
- raise ValueError(f'Invalid repository/UI port in node reference: {repo_or_ui_int}')
118
- # Finally, reset 'host'
119
- if any(mat.group(x) for x in ('protocol', 'repository', *five)):
120
- _set(data, 'host', mat.group('host'))
121
- return data