|
6 | 6 |
|
7 | 7 | from __future__ import annotations |
8 | 8 | import pathlib |
9 | | -import typing |
10 | 9 |
|
11 | 10 | from loam.manager import ConfOpt as Conf |
12 | 11 | from loam import tools |
13 | 12 | from loam.tools import switch_opt, command_flag |
| 13 | +import loam.types |
14 | 14 |
|
15 | | -if typing.TYPE_CHECKING: |
16 | | - from typing import Union, Callable, TypeVar, Tuple |
17 | | - T = TypeVar('T') |
18 | 15 |
|
19 | | - |
20 | | -def _slice_or_int(arg: str) -> Union[slice, int]: |
21 | | - """Parse a string into an integer or slice.""" |
22 | | - if ':' in arg: |
23 | | - idxs = arg.split(':') |
24 | | - if len(idxs) > 3: |
25 | | - raise ValueError(f'{arg} is an invalid slice') |
26 | | - slice_parts = [int(idxs[0]) if idxs[0] else None, |
27 | | - int(idxs[1]) if idxs[1] else None] |
28 | | - if len(idxs) == 3: |
29 | | - slice_parts.append(int(idxs[2]) if idxs[2] else None) |
30 | | - else: |
31 | | - slice_parts.append(1) |
32 | | - return slice(*slice_parts) |
33 | | - return int(arg) |
34 | | - |
35 | | - |
36 | | -def _list_of(from_str: Callable[[str], T]) -> Callable[[str], Tuple[T, ...]]: |
37 | | - """Return fn parsing a str as a comma-separated list of given type.""" |
38 | | - def parser(arg: str) -> Tuple[T, ...]: |
39 | | - return tuple(from_str(v) for v in map(str.strip, arg.split(',')) if v) |
40 | | - return parser |
41 | | - |
42 | | - |
43 | | -_index_collection = _list_of(_slice_or_int) |
44 | | -_float_list = _list_of(float) |
| 16 | +_index_collection = loam.types.list_of(loam.types.slice_or_int_parser) |
| 17 | +_float_list = loam.types.list_of(float) |
45 | 18 |
|
46 | 19 | HOME_DIR = pathlib.Path.home() |
47 | 20 | CONFIG_DIR = HOME_DIR / '.config' / 'stagpy' |
|
0 commit comments