hyperlight-cocoa 0.1.1__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.
- cocoa/cli/__init__.py +14 -0
- cocoa/cli/arg_types/__init__.py +18 -0
- cocoa/cli/arg_types/base/__init__.py +8 -0
- cocoa/cli/arg_types/base/keyword_arg.py +249 -0
- cocoa/cli/arg_types/base/positional_arg.py +143 -0
- cocoa/cli/arg_types/data_types/__init__.py +9 -0
- cocoa/cli/arg_types/data_types/assert_set.py +49 -0
- cocoa/cli/arg_types/data_types/context.py +16 -0
- cocoa/cli/arg_types/data_types/env.py +76 -0
- cocoa/cli/arg_types/data_types/import_file.py +107 -0
- cocoa/cli/arg_types/data_types/json_data.py +63 -0
- cocoa/cli/arg_types/data_types/json_file.py +102 -0
- cocoa/cli/arg_types/data_types/paths.py +124 -0
- cocoa/cli/arg_types/data_types/pattern.py +129 -0
- cocoa/cli/arg_types/data_types/raw_file.py +92 -0
- cocoa/cli/arg_types/data_types/reduce_pattern_type.py +30 -0
- cocoa/cli/arg_types/operators/__init__.py +3 -0
- cocoa/cli/arg_types/operators/chain.py +83 -0
- cocoa/cli/arg_types/operators/map.py +90 -0
- cocoa/cli/arg_types/operators/operator.py +63 -0
- cocoa/cli/cli.py +138 -0
- cocoa/cli/cli_protocol.py +20 -0
- cocoa/cli/command.py +449 -0
- cocoa/cli/group.py +578 -0
- cocoa/cli/help_message/__init__.py +3 -0
- cocoa/cli/help_message/cli_style.py +97 -0
- cocoa/cli/help_message/create_help_string.py +49 -0
- cocoa/cli/help_message/description_help_message.py +76 -0
- cocoa/cli/help_message/help_message.py +208 -0
- cocoa/cli/help_message/line_parsing.py +3 -0
- cocoa/cli/help_message/options_help_message.py +242 -0
- cocoa/cli/help_message/title_help_message.py +129 -0
- cocoa/cli/inspect_wrapped.py +139 -0
- cocoa/ui/__init__.py +28 -0
- cocoa/ui/components/__init__.py +28 -0
- cocoa/ui/components/animated_status_bar/__init__.py +4 -0
- cocoa/ui/components/animated_status_bar/animated_status_bar.py +935 -0
- cocoa/ui/components/animated_status_bar/animated_status_bar_config.py +55 -0
- cocoa/ui/components/counter/__init__.py +2 -0
- cocoa/ui/components/counter/counter.py +238 -0
- cocoa/ui/components/counter/counter_config.py +22 -0
- cocoa/ui/components/empty/__init__.py +1 -0
- cocoa/ui/components/empty/empty.py +9 -0
- cocoa/ui/components/header/__init__.py +2 -0
- cocoa/ui/components/header/font/__init__.py +4 -0
- cocoa/ui/components/header/font/formatted_letter.py +8 -0
- cocoa/ui/components/header/font/formatted_word.py +10 -0
- cocoa/ui/components/header/font/letters.py +286 -0
- cocoa/ui/components/header/font/word.py +87 -0
- cocoa/ui/components/header/header.py +182 -0
- cocoa/ui/components/header/header_config.py +21 -0
- cocoa/ui/components/link/__init__.py +2 -0
- cocoa/ui/components/link/link.py +178 -0
- cocoa/ui/components/link/link_config.py +14 -0
- cocoa/ui/components/link/link_validator.py +5 -0
- cocoa/ui/components/multiline_text/__init__.py +2 -0
- cocoa/ui/components/multiline_text/multiline_text.py +216 -0
- cocoa/ui/components/multiline_text/multiline_text_config.py +23 -0
- cocoa/ui/components/progress_bar/__init__.py +2 -0
- cocoa/ui/components/progress_bar/background_char.py +72 -0
- cocoa/ui/components/progress_bar/end_char.py +54 -0
- cocoa/ui/components/progress_bar/fill_char.py +80 -0
- cocoa/ui/components/progress_bar/progress_bar.py +374 -0
- cocoa/ui/components/progress_bar/progress_bar_config.py +65 -0
- cocoa/ui/components/progress_bar/progress_bar_status.py +8 -0
- cocoa/ui/components/progress_bar/start_char.py +54 -0
- cocoa/ui/components/scatter_plot/__init__.py +2 -0
- cocoa/ui/components/scatter_plot/plot_config.py +29 -0
- cocoa/ui/components/scatter_plot/plot_point.py +6 -0
- cocoa/ui/components/scatter_plot/plotille/__init__.py +31 -0
- cocoa/ui/components/scatter_plot/plotille/_canvas.py +401 -0
- cocoa/ui/components/scatter_plot/plotille/_cmaps.py +121 -0
- cocoa/ui/components/scatter_plot/plotille/_cmaps_data.py +1604 -0
- cocoa/ui/components/scatter_plot/plotille/_colors.py +324 -0
- cocoa/ui/components/scatter_plot/plotille/_dots.py +190 -0
- cocoa/ui/components/scatter_plot/plotille/_figure.py +621 -0
- cocoa/ui/components/scatter_plot/plotille/_figure_data.py +229 -0
- cocoa/ui/components/scatter_plot/plotille/_graphs.py +335 -0
- cocoa/ui/components/scatter_plot/plotille/_input_formatter.py +234 -0
- cocoa/ui/components/scatter_plot/plotille/_util.py +119 -0
- cocoa/ui/components/scatter_plot/point_char.py +64 -0
- cocoa/ui/components/scatter_plot/scatter_plot.py +311 -0
- cocoa/ui/components/spinner/__init__.py +2 -0
- cocoa/ui/components/spinner/spinner.py +159 -0
- cocoa/ui/components/spinner/spinner_config.py +23 -0
- cocoa/ui/components/spinner/spinner_data.py +1109 -0
- cocoa/ui/components/spinner/spinner_factory.py +1199 -0
- cocoa/ui/components/spinner/spinner_frames_set.py +12 -0
- cocoa/ui/components/spinner/spinner_status.py +8 -0
- cocoa/ui/components/spinner/spinner_types.py +175 -0
- cocoa/ui/components/status_bar/__init__.py +2 -0
- cocoa/ui/components/status_bar/status_bar.py +174 -0
- cocoa/ui/components/status_bar/status_bar_config.py +24 -0
- cocoa/ui/components/table/__init__.py +2 -0
- cocoa/ui/components/table/table.py +328 -0
- cocoa/ui/components/table/table_config.py +40 -0
- cocoa/ui/components/table/tabulate/__init__.py +4 -0
- cocoa/ui/components/table/tabulate/cell_alignment.py +32 -0
- cocoa/ui/components/table/tabulate/charset_position_type.py +29 -0
- cocoa/ui/components/table/tabulate/table_assembler.py +1506 -0
- cocoa/ui/components/table/tabulate/table_border_lines.py +45 -0
- cocoa/ui/components/terminal/__init__.py +6 -0
- cocoa/ui/components/terminal/action.py +11 -0
- cocoa/ui/components/terminal/canvas.py +254 -0
- cocoa/ui/components/terminal/engine_config.py +9 -0
- cocoa/ui/components/terminal/refresh_rate.py +24 -0
- cocoa/ui/components/terminal/section.py +466 -0
- cocoa/ui/components/terminal/section_config.py +55 -0
- cocoa/ui/components/terminal/terminal.py +540 -0
- cocoa/ui/components/terminal/terminal_protocol.py +20 -0
- cocoa/ui/components/text/__init__.py +2 -0
- cocoa/ui/components/text/text.py +155 -0
- cocoa/ui/components/text/text_config.py +22 -0
- cocoa/ui/components/timer/__init__.py +2 -0
- cocoa/ui/components/timer/timer.py +344 -0
- cocoa/ui/components/timer/timer_config.py +21 -0
- cocoa/ui/components/timer/timer_status.py +8 -0
- cocoa/ui/components/total_rate/__init__.py +2 -0
- cocoa/ui/components/total_rate/total_rate.py +293 -0
- cocoa/ui/components/total_rate/total_rate_config.py +23 -0
- cocoa/ui/components/total_rate/total_rate_status.py +7 -0
- cocoa/ui/components/windowed_rate/__init__.py +2 -0
- cocoa/ui/components/windowed_rate/windowed_rate.py +256 -0
- cocoa/ui/components/windowed_rate/windowed_rate_config.py +22 -0
- cocoa/ui/config/__init__.py +0 -0
- cocoa/ui/config/mode.py +15 -0
- cocoa/ui/config/widget_fit_dimensions.py +25 -0
- cocoa/ui/state/__init__.py +4 -0
- cocoa/ui/state/observe.py +56 -0
- cocoa/ui/state/state_types.py +27 -0
- cocoa/ui/state/subscription_set.py +70 -0
- cocoa/ui/styling/__init__.py +2 -0
- cocoa/ui/styling/attributes/__init__.py +4 -0
- cocoa/ui/styling/attributes/attribute.py +45 -0
- cocoa/ui/styling/attributes/attributizer.py +17 -0
- cocoa/ui/styling/colors/__init__.py +10 -0
- cocoa/ui/styling/colors/color.py +107 -0
- cocoa/ui/styling/colors/colorizer.py +36 -0
- cocoa/ui/styling/colors/extended_color.py +521 -0
- cocoa/ui/styling/colors/highlight.py +107 -0
- cocoa/ui/styling/stylize.py +101 -0
- cocoa/ui/styling/stylizer.py +21 -0
- hyperlight_cocoa-0.1.1.dist-info/LICENSE +21 -0
- hyperlight_cocoa-0.1.1.dist-info/METADATA +43 -0
- hyperlight_cocoa-0.1.1.dist-info/RECORD +147 -0
- hyperlight_cocoa-0.1.1.dist-info/WHEEL +5 -0
- hyperlight_cocoa-0.1.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
import io
|
|
4
|
+
import json
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
from typing import Any, TypeVar, Generic
|
|
7
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
T = TypeVar("T", bound=BaseModel)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class JsonData(Generic[T]):
|
|
14
|
+
def __init__(self, data_type: JsonData[T]):
|
|
15
|
+
super().__init__()
|
|
16
|
+
|
|
17
|
+
self.data: T | None = None
|
|
18
|
+
|
|
19
|
+
conversion_types: list[T] = reduce_pattern_type(data_type)
|
|
20
|
+
|
|
21
|
+
self._data_types = [
|
|
22
|
+
conversion_type.__name__
|
|
23
|
+
if hasattr(conversion_type, "__name__")
|
|
24
|
+
else type(conversion_type).__name__
|
|
25
|
+
for conversion_type in conversion_types
|
|
26
|
+
]
|
|
27
|
+
self._types = conversion_types
|
|
28
|
+
|
|
29
|
+
self._loop = asyncio.get_event_loop()
|
|
30
|
+
|
|
31
|
+
def __contains__(self, value: Any):
|
|
32
|
+
return type(value) in [self._types]
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def data_type(self):
|
|
36
|
+
return ", ".join(self._data_types)
|
|
37
|
+
|
|
38
|
+
async def parse(self, arg: str | list[str] | None = None):
|
|
39
|
+
result = await self._load_json(arg)
|
|
40
|
+
if isinstance(result, Exception):
|
|
41
|
+
return result
|
|
42
|
+
|
|
43
|
+
self.data = result
|
|
44
|
+
|
|
45
|
+
return self
|
|
46
|
+
|
|
47
|
+
async def _load_json(self, arg: str):
|
|
48
|
+
json_data: dict[str, Any] = json.loads(arg)
|
|
49
|
+
|
|
50
|
+
for conversion_type in self._types:
|
|
51
|
+
try:
|
|
52
|
+
if conversion_type == list and isinstance(json_data, list):
|
|
53
|
+
return json_data
|
|
54
|
+
|
|
55
|
+
elif conversion_type == bytes:
|
|
56
|
+
return bytes(json_data, encoding="utf-8")
|
|
57
|
+
|
|
58
|
+
return conversion_type(**json_data)
|
|
59
|
+
|
|
60
|
+
except Exception:
|
|
61
|
+
pass
|
|
62
|
+
|
|
63
|
+
return Exception(f"could not parse {arg} specified types")
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
import io
|
|
4
|
+
import json
|
|
5
|
+
from pydantic import BaseModel
|
|
6
|
+
from typing import Any, TypeVar, Generic
|
|
7
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
T = TypeVar("T", bound=BaseModel)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class JsonFile(Generic[T]):
|
|
14
|
+
def __init__(self, data_type: JsonFile[T]):
|
|
15
|
+
super().__init__()
|
|
16
|
+
|
|
17
|
+
self.data: T | None = None
|
|
18
|
+
|
|
19
|
+
conversion_types: list[T] = reduce_pattern_type(data_type)
|
|
20
|
+
|
|
21
|
+
self._data_types = [
|
|
22
|
+
conversion_type.__name__
|
|
23
|
+
if hasattr(conversion_type, "__name__")
|
|
24
|
+
else type(conversion_type).__name__
|
|
25
|
+
for conversion_type in conversion_types
|
|
26
|
+
]
|
|
27
|
+
self._types = conversion_types
|
|
28
|
+
|
|
29
|
+
self._loop = asyncio.get_event_loop()
|
|
30
|
+
|
|
31
|
+
def __contains__(self, value: Any):
|
|
32
|
+
return type(value) in [self._types]
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def data_type(self):
|
|
36
|
+
return ", ".join(self._data_types)
|
|
37
|
+
|
|
38
|
+
async def parse(self, arg: str | None = None):
|
|
39
|
+
result = await self._load_json_file(arg)
|
|
40
|
+
if isinstance(result, Exception):
|
|
41
|
+
return result
|
|
42
|
+
|
|
43
|
+
self.data = result
|
|
44
|
+
|
|
45
|
+
return self
|
|
46
|
+
|
|
47
|
+
async def _load_json_file(self, arg: str | None = None):
|
|
48
|
+
try:
|
|
49
|
+
if arg is None:
|
|
50
|
+
return Exception("no argument passed for filepath")
|
|
51
|
+
|
|
52
|
+
file_data = await self._load_file(arg)
|
|
53
|
+
if isinstance(file_data, Exception):
|
|
54
|
+
return file_data
|
|
55
|
+
|
|
56
|
+
json_data: dict[str, Any] = json.loads(file_data)
|
|
57
|
+
|
|
58
|
+
return self._parse_type(json_data, arg)
|
|
59
|
+
|
|
60
|
+
except Exception as e:
|
|
61
|
+
return Exception(
|
|
62
|
+
f"encountered error {str(e)} parsing file at {arg} to JSON"
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
async def _load_file(self, arg: str):
|
|
66
|
+
try:
|
|
67
|
+
file_handle: io.TextIOWrapper = await self._loop.run_in_executor(
|
|
68
|
+
None,
|
|
69
|
+
open,
|
|
70
|
+
arg,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
file_data = await self._loop.run_in_executor(None, file_handle.read)
|
|
74
|
+
|
|
75
|
+
await self._loop.run_in_executor(None, file_handle.close)
|
|
76
|
+
|
|
77
|
+
return file_data
|
|
78
|
+
|
|
79
|
+
except Exception as e:
|
|
80
|
+
return Exception(f"encountered error {str(e)} opening file at {arg}")
|
|
81
|
+
|
|
82
|
+
def _parse_type(
|
|
83
|
+
self,
|
|
84
|
+
json_data: dict[str, Any],
|
|
85
|
+
arg: str,
|
|
86
|
+
):
|
|
87
|
+
last_error: Exception | None = None
|
|
88
|
+
|
|
89
|
+
for conversion_type in self._types:
|
|
90
|
+
try:
|
|
91
|
+
if conversion_type == list and isinstance(json_data, list):
|
|
92
|
+
return json_data
|
|
93
|
+
|
|
94
|
+
elif conversion_type == bytes:
|
|
95
|
+
return bytes(json_data, encoding="utf-8")
|
|
96
|
+
|
|
97
|
+
return conversion_type(**json_data)
|
|
98
|
+
|
|
99
|
+
except Exception:
|
|
100
|
+
pass
|
|
101
|
+
|
|
102
|
+
return Exception(f"could not parse file at {arg} from JSON to specified types")
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import functools
|
|
5
|
+
import operator
|
|
6
|
+
import os
|
|
7
|
+
import pathlib
|
|
8
|
+
|
|
9
|
+
from typing import Generic, TypeVarTuple, Any, get_args, TypeVar
|
|
10
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
T = TypeVar("T")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Paths(Generic[T]):
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
data_type: Paths[T],
|
|
20
|
+
):
|
|
21
|
+
super().__init__()
|
|
22
|
+
|
|
23
|
+
self.data: list[str] | None = None
|
|
24
|
+
|
|
25
|
+
args = get_args(data_type)
|
|
26
|
+
|
|
27
|
+
search_patterns: list[T] = reduce_pattern_type(data_type)
|
|
28
|
+
self._data_types = [
|
|
29
|
+
search_pattern.__name__
|
|
30
|
+
if hasattr(search_pattern, "__name__")
|
|
31
|
+
else type(search_pattern).__name__
|
|
32
|
+
for search_pattern in search_patterns
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
self._patterns = search_patterns
|
|
36
|
+
|
|
37
|
+
self._types = [type(pattern) for pattern in self._patterns]
|
|
38
|
+
|
|
39
|
+
self._loop = asyncio.get_event_loop()
|
|
40
|
+
|
|
41
|
+
def __contains__(self, value: Any):
|
|
42
|
+
return type(value) in [self._types]
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def data_type(self):
|
|
46
|
+
return ", ".join(self._data_types)
|
|
47
|
+
|
|
48
|
+
async def parse(self, arg: str | None = None) -> T | Exception:
|
|
49
|
+
result = await self._find_paths(arg)
|
|
50
|
+
if isinstance(result, Exception):
|
|
51
|
+
return result
|
|
52
|
+
|
|
53
|
+
self.data = result
|
|
54
|
+
|
|
55
|
+
return self
|
|
56
|
+
|
|
57
|
+
async def _find_paths(self, arg: str | None):
|
|
58
|
+
results: list[str] = []
|
|
59
|
+
errors: list[Exception] = []
|
|
60
|
+
|
|
61
|
+
matches = await asyncio.gather(
|
|
62
|
+
*[
|
|
63
|
+
self._find_path_with_pattern(
|
|
64
|
+
search_pattern,
|
|
65
|
+
arg,
|
|
66
|
+
)
|
|
67
|
+
for search_pattern in self._patterns
|
|
68
|
+
]
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
for match_set in matches:
|
|
72
|
+
if not isinstance(match_set, Exception):
|
|
73
|
+
results.extend(match_set)
|
|
74
|
+
|
|
75
|
+
else:
|
|
76
|
+
errors.append(match_set)
|
|
77
|
+
|
|
78
|
+
if len(results) < 1 and len(errors) > 0:
|
|
79
|
+
return errors[0]
|
|
80
|
+
|
|
81
|
+
elif len(results) < 1 and len(errors) < 1:
|
|
82
|
+
return Exception("no matches found")
|
|
83
|
+
|
|
84
|
+
return results
|
|
85
|
+
|
|
86
|
+
async def _find_path_with_pattern(
|
|
87
|
+
self,
|
|
88
|
+
search_pattern: str,
|
|
89
|
+
arg: str,
|
|
90
|
+
):
|
|
91
|
+
if search_pattern and arg:
|
|
92
|
+
search_pattern = await self._loop.run_in_executor(
|
|
93
|
+
None,
|
|
94
|
+
os.path.join,
|
|
95
|
+
search_pattern,
|
|
96
|
+
arg,
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
elif search_pattern is None and arg is not None:
|
|
100
|
+
search_pattern = arg
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
path: pathlib.Path = await self._loop.run_in_executor(
|
|
104
|
+
None,
|
|
105
|
+
pathlib.Path,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
path_matches = await self._loop.run_in_executor(
|
|
109
|
+
None,
|
|
110
|
+
path.rglob,
|
|
111
|
+
search_pattern,
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
matches: list[str] = [str(path) for path in path_matches]
|
|
115
|
+
|
|
116
|
+
if len(matches) < 1:
|
|
117
|
+
return Exception("no matches found")
|
|
118
|
+
|
|
119
|
+
return matches
|
|
120
|
+
|
|
121
|
+
except Exception as e:
|
|
122
|
+
return Exception(
|
|
123
|
+
f"encountered unexpected error {str(e)} searching pattern {search_pattern}"
|
|
124
|
+
)
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
import re
|
|
4
|
+
from typing import Generic, TypeVar, Any, get_args, get_origin
|
|
5
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
K = TypeVar("K")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Pattern(Generic[T, K]):
|
|
13
|
+
def __init__(self, pattern: Pattern[T, K]):
|
|
14
|
+
super().__init__()
|
|
15
|
+
|
|
16
|
+
self.data: K | None = None
|
|
17
|
+
|
|
18
|
+
pattern_types, conversion_type = get_args(pattern)
|
|
19
|
+
patterns = reduce_pattern_type(pattern_types)
|
|
20
|
+
|
|
21
|
+
self._patterns = [re.compile(pattern) for pattern in patterns]
|
|
22
|
+
|
|
23
|
+
self._data_type = (
|
|
24
|
+
conversion_type.__name__
|
|
25
|
+
if hasattr(conversion_type, "__name__")
|
|
26
|
+
else type(conversion_type).__name__
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
self._type = conversion_type
|
|
30
|
+
|
|
31
|
+
self._loop = asyncio.get_event_loop()
|
|
32
|
+
|
|
33
|
+
def __contains__(self, value: Any):
|
|
34
|
+
return type(value) in [self._type]
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def data_type(self):
|
|
38
|
+
return self._data_type
|
|
39
|
+
|
|
40
|
+
async def parse(self, arg: str | None = None):
|
|
41
|
+
results: Any | Exception = await self._try_match(arg)
|
|
42
|
+
|
|
43
|
+
if isinstance(results, Exception):
|
|
44
|
+
return results
|
|
45
|
+
|
|
46
|
+
self.data: K = results
|
|
47
|
+
|
|
48
|
+
return self
|
|
49
|
+
|
|
50
|
+
async def _try_match(self, arg: str):
|
|
51
|
+
try:
|
|
52
|
+
matches: list[str] = []
|
|
53
|
+
|
|
54
|
+
if get_origin(self._type) == list:
|
|
55
|
+
args_type = get_args(self._type)
|
|
56
|
+
parse_type = args_type[0] if len(args_type) > 0 else None
|
|
57
|
+
|
|
58
|
+
for pattern in self._patterns:
|
|
59
|
+
matches.extend(
|
|
60
|
+
self._parse_match_all(
|
|
61
|
+
re.findall(
|
|
62
|
+
pattern,
|
|
63
|
+
arg,
|
|
64
|
+
),
|
|
65
|
+
parse_type=parse_type,
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
return matches
|
|
70
|
+
|
|
71
|
+
for pattern in self._patterns:
|
|
72
|
+
matches.append(
|
|
73
|
+
self._match_pattern(
|
|
74
|
+
pattern,
|
|
75
|
+
arg,
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
results = [match for match in matches if not isinstance(match, Exception)]
|
|
80
|
+
|
|
81
|
+
errors = [error for error in matches if isinstance(error, Exception)]
|
|
82
|
+
|
|
83
|
+
if len(results) < 1 and len(errors) > 0:
|
|
84
|
+
return errors[0]
|
|
85
|
+
|
|
86
|
+
elif len(results) < 1 and len(errors) < 1:
|
|
87
|
+
return Exception("no matches found")
|
|
88
|
+
|
|
89
|
+
return results
|
|
90
|
+
|
|
91
|
+
except Exception as e:
|
|
92
|
+
return Exception(f"encountered unexpected error {str(e)} parsing patterns")
|
|
93
|
+
|
|
94
|
+
def _match_pattern(self, pattern: str, arg: str):
|
|
95
|
+
if value := re.match(pattern, arg):
|
|
96
|
+
return self._parse_match(value.group(0))
|
|
97
|
+
|
|
98
|
+
return Exception("no matches found")
|
|
99
|
+
|
|
100
|
+
def _parse_match_all(self, value: list[str], parse_type: type[Any] | None = None):
|
|
101
|
+
results: list[K] = []
|
|
102
|
+
|
|
103
|
+
for item in value:
|
|
104
|
+
result = self._parse_match(
|
|
105
|
+
item,
|
|
106
|
+
parse_type=parse_type,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
if not isinstance(results, Exception):
|
|
110
|
+
results.append(result)
|
|
111
|
+
|
|
112
|
+
if len(results) < 1:
|
|
113
|
+
return Exception("no matches found")
|
|
114
|
+
|
|
115
|
+
return results
|
|
116
|
+
|
|
117
|
+
def _parse_match(self, value: str, parse_type: type[Any] | None = None):
|
|
118
|
+
parser = self._type
|
|
119
|
+
if parse_type:
|
|
120
|
+
parser = parse_type
|
|
121
|
+
|
|
122
|
+
try:
|
|
123
|
+
if parser == bytes:
|
|
124
|
+
return bytes(value, encoding="utf-8")
|
|
125
|
+
|
|
126
|
+
return parser(value)
|
|
127
|
+
|
|
128
|
+
except Exception:
|
|
129
|
+
return Exception(f"could not parse {value} to {parse_type.__name__}")
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
import io
|
|
4
|
+
from typing import Generic, TypeVar, Any
|
|
5
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class RawFile(Generic[T]):
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
data_type: RawFile[T],
|
|
15
|
+
):
|
|
16
|
+
super().__init__()
|
|
17
|
+
|
|
18
|
+
self.data: T | None = None
|
|
19
|
+
|
|
20
|
+
conversion_types: list[T] = reduce_pattern_type(data_type)
|
|
21
|
+
self._data_types = [
|
|
22
|
+
conversion_type.__name__
|
|
23
|
+
if hasattr(conversion_type, "__name__")
|
|
24
|
+
else type(conversion_type).__name__
|
|
25
|
+
for conversion_type in conversion_types
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
self._types = conversion_types
|
|
29
|
+
|
|
30
|
+
self._loop = asyncio.get_event_loop()
|
|
31
|
+
|
|
32
|
+
def __contains__(self, value: Any):
|
|
33
|
+
return type(value) in self._types
|
|
34
|
+
|
|
35
|
+
@property
|
|
36
|
+
def data_type(self):
|
|
37
|
+
return ", ".join(self._data_types)
|
|
38
|
+
|
|
39
|
+
async def parse(self, arg: str | None = None) -> T | Exception:
|
|
40
|
+
if arg is None:
|
|
41
|
+
return Exception("Err. - No match found.")
|
|
42
|
+
|
|
43
|
+
result = await self._read_file(arg)
|
|
44
|
+
if isinstance(result, Exception):
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
self.data = result
|
|
48
|
+
|
|
49
|
+
return self
|
|
50
|
+
|
|
51
|
+
async def _read_file(self, arg: str | None = None):
|
|
52
|
+
try:
|
|
53
|
+
file_data = await self._load_file(arg)
|
|
54
|
+
if isinstance(file_data, Exception):
|
|
55
|
+
return file_data
|
|
56
|
+
|
|
57
|
+
return self._parse_type(file_data)
|
|
58
|
+
|
|
59
|
+
except Exception as e:
|
|
60
|
+
return Exception(
|
|
61
|
+
f"encountered unexpected error {str(e)} loading file {arg}"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
async def _load_file(self, arg: str):
|
|
65
|
+
try:
|
|
66
|
+
file_handle: io.TextIOWrapper = await self._loop.run_in_executor(
|
|
67
|
+
None,
|
|
68
|
+
open,
|
|
69
|
+
arg,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
file_data = await self._loop.run_in_executor(None, file_handle.read)
|
|
73
|
+
|
|
74
|
+
await self._loop.run_in_executor(None, file_handle.close)
|
|
75
|
+
|
|
76
|
+
return file_data
|
|
77
|
+
|
|
78
|
+
except Exception as e:
|
|
79
|
+
return Exception(f"encountered error {str(e)} opening file at {arg}")
|
|
80
|
+
|
|
81
|
+
def _parse_type(self, file_data: str):
|
|
82
|
+
for conversion_type in self._types:
|
|
83
|
+
try:
|
|
84
|
+
if conversion_type == bytes:
|
|
85
|
+
return bytes(file_data, encoding="utf-8")
|
|
86
|
+
|
|
87
|
+
return conversion_type(file_data)
|
|
88
|
+
|
|
89
|
+
except Exception:
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
return Exception(f"could not parse {file_data} specified types")
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import functools
|
|
2
|
+
import operator
|
|
3
|
+
from typing import Any, get_args, TypeVar, get_origin
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
T = TypeVar("T", bound=list)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _concat_pattern_types(pattern: Any):
|
|
10
|
+
if isinstance(pattern, tuple) and len(pattern) > 0:
|
|
11
|
+
return [_concat_pattern_types(arg) for arg in pattern]
|
|
12
|
+
|
|
13
|
+
elif (args := get_args(pattern)) and len(args) > 0:
|
|
14
|
+
return [_concat_pattern_types(arg) for arg in args]
|
|
15
|
+
|
|
16
|
+
else:
|
|
17
|
+
return pattern
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def reduce_pattern_type(pattern: Any):
|
|
21
|
+
try:
|
|
22
|
+
results = _concat_pattern_types(pattern)
|
|
23
|
+
return (
|
|
24
|
+
functools.reduce(operator.concat, results, [])
|
|
25
|
+
if len(results) > 0 and isinstance(results[0], list)
|
|
26
|
+
else results
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
except Exception:
|
|
30
|
+
pass
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
from typing import Generic, TypeVarTuple, Any, get_args, get_origin, TypeVar, Callable
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
T = TypeVarTuple("T")
|
|
7
|
+
K = TypeVar("K")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
from cocoa.cli.arg_types.data_types import (
|
|
11
|
+
AssertSet,
|
|
12
|
+
Env,
|
|
13
|
+
ImportFile,
|
|
14
|
+
JsonData,
|
|
15
|
+
JsonFile,
|
|
16
|
+
Paths,
|
|
17
|
+
Pattern,
|
|
18
|
+
RawFile,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Chain(Generic[*T]):
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
name: str,
|
|
26
|
+
data_type: Chain[tuple[*T]],
|
|
27
|
+
):
|
|
28
|
+
super().__init__()
|
|
29
|
+
|
|
30
|
+
self.name = name
|
|
31
|
+
self._types: tuple[*T] = get_args(data_type)
|
|
32
|
+
|
|
33
|
+
self._data_type = [
|
|
34
|
+
subtype_type.__name__
|
|
35
|
+
for subtype_type in self._types
|
|
36
|
+
if hasattr(data_type, "__name__")
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
self.data: Any | None = None
|
|
40
|
+
|
|
41
|
+
self._complex_types: dict[
|
|
42
|
+
AssertSet | Env | ImportFile | JsonData | JsonData | Pattern | RawFile,
|
|
43
|
+
Callable[
|
|
44
|
+
[str, type[Any]],
|
|
45
|
+
AssertSet | Env | ImportFile | JsonData | JsonData | Pattern | RawFile,
|
|
46
|
+
],
|
|
47
|
+
] = {
|
|
48
|
+
AssertSet: lambda name, subtype: AssertSet(name, subtype),
|
|
49
|
+
Env: lambda envar, subtype: Env(envar, subtype),
|
|
50
|
+
ImportFile: lambda _, subtype: ImportFile(subtype),
|
|
51
|
+
JsonFile: lambda _, subtype: JsonFile(subtype),
|
|
52
|
+
JsonData: lambda _, subtype: JsonData(subtype),
|
|
53
|
+
Paths: lambda _, subtype: Paths(subtype),
|
|
54
|
+
Pattern: lambda _, subtype: Pattern(subtype),
|
|
55
|
+
RawFile: lambda _, subtype: RawFile(subtype),
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
self._loop = asyncio.get_event_loop()
|
|
59
|
+
|
|
60
|
+
def __contains__(self, value: Any):
|
|
61
|
+
return type(value) in self._types
|
|
62
|
+
|
|
63
|
+
@property
|
|
64
|
+
def data_type(self):
|
|
65
|
+
return ", ".join(self._data_type)
|
|
66
|
+
|
|
67
|
+
async def parse(self, arg: str | None = None):
|
|
68
|
+
result: Any | Exception | str = arg
|
|
69
|
+
err: Exception | None = None
|
|
70
|
+
|
|
71
|
+
for subtype in self._types:
|
|
72
|
+
if complex_type_factory := self._complex_types.get(get_origin(subtype)):
|
|
73
|
+
complex_type = complex_type_factory(self.name, subtype)
|
|
74
|
+
|
|
75
|
+
err = await complex_type.parse(result)
|
|
76
|
+
result = complex_type.data
|
|
77
|
+
|
|
78
|
+
if isinstance(err, Exception):
|
|
79
|
+
return err
|
|
80
|
+
|
|
81
|
+
self.data = result
|
|
82
|
+
|
|
83
|
+
return self
|