|
9 | 9 |
|
10 | 10 | LGPL-2.1 or LGPL-3.0. See file COPYING. |
11 | 11 | """ |
12 | | -from abc import ABC, abstractmethod |
13 | 12 | import copy |
14 | 13 | import enum |
15 | 14 | import math |
16 | | -import itertools |
17 | 15 | import json |
18 | 16 | import os |
19 | 17 | import pprint |
|
25 | 23 | import uuid |
26 | 24 |
|
27 | 25 | from collections import abc |
28 | | -from typing import cast, Any, Callable, Dict, Generic, List, Optional, Sequence, Tuple, Union, \ |
29 | | - Annotated, Generic, TypeVar |
30 | | - |
31 | | -S = TypeVar('S') # Input type |
32 | | -T = TypeVar('T') # Output type |
| 26 | +from typing import cast, Any, Callable, Dict, Generic, List, Optional, Sequence, Tuple, Union |
33 | 27 |
|
34 | 28 | if sys.version_info >= (3, 8): |
35 | 29 | from typing import get_args, get_origin |
@@ -119,20 +113,6 @@ class JsonFormat(Exception): |
119 | 113 | pass |
120 | 114 |
|
121 | 115 |
|
122 | | -class Converter(ABC, Generic[S, T]): |
123 | | - @abstractmethod |
124 | | - def convert(self, value: S) -> T: pass |
125 | | - |
126 | | - |
127 | | -def _get_annotation_metadata(tp): |
128 | | - if get_origin(tp) is Annotated: |
129 | | - annotated_args = get_args(tp) |
130 | | - try: |
131 | | - return annotated_args[1] |
132 | | - except IndexError: |
133 | | - return None |
134 | | - |
135 | | - |
136 | 116 | class CephArgtype(object): |
137 | 117 | """ |
138 | 118 | Base class for all Ceph argument types |
@@ -213,10 +193,6 @@ def to_argdesc(tp, attrs, has_default=False, positional=True): |
213 | 193 | attrs['req'] = 'false' |
214 | 194 | if not positional: |
215 | 195 | attrs['positional'] = 'false' |
216 | | - if annotation := _get_annotation_metadata(tp): |
217 | | - if isinstance(annotation, CephArgtype): |
218 | | - return annotation.argdesc(attrs) |
219 | | - |
220 | 196 | CEPH_ARG_TYPES = { |
221 | 197 | str: CephString, |
222 | 198 | int: CephInt, |
@@ -256,10 +232,6 @@ def _cast_to_compound_type(tp, v): |
256 | 232 |
|
257 | 233 | @staticmethod |
258 | 234 | def cast_to(tp, v): |
259 | | - if annotation := _get_annotation_metadata(tp): |
260 | | - if isinstance(annotation, Converter): |
261 | | - return annotation.convert(v) |
262 | | - |
263 | 235 | PYTHON_TYPES = ( |
264 | 236 | str, |
265 | 237 | int, |
@@ -392,72 +364,6 @@ def argdesc(self, attrs): |
392 | 364 | return super().argdesc(attrs) |
393 | 365 |
|
394 | 366 |
|
395 | | -class CephSizeBytes(CephArgtype, Converter[str, int]): |
396 | | - """ |
397 | | - Size in bytes. e.g. 1024, 1KB, 100MB, etc.. |
398 | | - """ |
399 | | - MULTIPLES = ['', "K", "M", "G", "T", "P"] |
400 | | - UNITS = { |
401 | | - f"{prefix}{suffix}": 1024 ** mult |
402 | | - for mult, prefix in enumerate(MULTIPLES) |
403 | | - for suffix in ['', 'B', 'iB'] |
404 | | - if not (prefix == '' and suffix == 'iB') |
405 | | - } |
406 | | - |
407 | | - def __init__(self, units_types: set = None): |
408 | | - self.units = set(CephSizeBytes.UNITS.keys()) |
409 | | - if units_types : |
410 | | - self.units &= units_types |
411 | | - self.re_exp = re.compile(f"r'^\d+({'|'.join(self.units)})?$'") |
412 | | - self.number_re_exp = re.compile(r'^\d+') |
413 | | - self.num_and_unit_re_exp = re.compile(r'(\d+)([A-Za-z]*)$') |
414 | | - |
415 | | - |
416 | | - def valid(self, s: str, partial: bool = False) -> None: |
417 | | - if not s: |
418 | | - raise ArgumentValid("Size string not provided.") |
419 | | - |
420 | | - number_str = ''.join(itertools.takewhile(str.isdigit, s)) |
421 | | - if not number_str: |
422 | | - raise ArgumentFormat("Size must start with a positive number.") |
423 | | - |
424 | | - unit = s[len(number_str):] |
425 | | - if unit and not unit.isalpha(): |
426 | | - raise ArgumentFormat("Invalid format. Expected format: <number>[KB|MB|GB] (e.g., 100MB, 10KB, 1000).") |
427 | | - |
428 | | - if unit and unit not in self.units: |
429 | | - raise ArgumentValid(f'{unit} is not a valid size unit. Supported units: {self.units}') |
430 | | - self.val = s |
431 | | - |
432 | | - def __str__(self) -> str: |
433 | | - b = '|'.join(self.units) |
434 | | - return '<sizebytes({0})>'.format(b) |
435 | | - |
436 | | - def argdesc(self, attrs): |
437 | | - return super().argdesc(attrs) |
438 | | - |
439 | | - @staticmethod |
440 | | - def _convert_to_bytes(size: Union[int, str], default_unit=None): |
441 | | - if isinstance(size, int): |
442 | | - number = size |
443 | | - size = str(size) |
444 | | - else: |
445 | | - num_str = ''.join(filter(str.isdigit, size)) |
446 | | - number = int(num_str) |
447 | | - unit_str = ''.join(filter(str.isalpha, size)) |
448 | | - if not unit_str: |
449 | | - if not default_unit: |
450 | | - raise ValueError("No size unit was provided") |
451 | | - unit_str = default_unit |
452 | | - |
453 | | - if unit_str in CephSizeBytes.UNITS: |
454 | | - return number * CephSizeBytes.UNITS[unit_str] |
455 | | - raise ValueError(f"Invalid unit: {unit_str}") |
456 | | - |
457 | | - def convert(self, value: str) -> int: |
458 | | - return CephSizeBytes._convert_to_bytes(value, default_unit="B") |
459 | | - |
460 | | - |
461 | 367 | class CephSocketpath(CephArgtype): |
462 | 368 | """ |
463 | 369 | Admin socket path; check that it's readable and S_ISSOCK |
|
0 commit comments