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
cocoa/cli/__init__.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from .cli import CLI as CLI
|
|
2
|
+
from .arg_types import AssertSet as AssertSet
|
|
3
|
+
from .arg_types import Chain as Chain
|
|
4
|
+
from .arg_types import Context as Context
|
|
5
|
+
from .arg_types import Env as Env
|
|
6
|
+
from .arg_types import ImportFile as ImportFile
|
|
7
|
+
from .arg_types import JsonData as JsonData
|
|
8
|
+
from .arg_types import JsonFile as JsonFile
|
|
9
|
+
from .arg_types import Map as Map
|
|
10
|
+
from .arg_types import Operator as Operator
|
|
11
|
+
from .arg_types import Paths as Paths
|
|
12
|
+
from .arg_types import Pattern as Pattern
|
|
13
|
+
from .arg_types import RawFile as RawFile
|
|
14
|
+
from .help_message import CLIStyle as CLIStyle
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from .base import KeywordArg as KeywordArg
|
|
2
|
+
from .base import is_defaultable as is_defaultable
|
|
3
|
+
from .base import is_env_defaultable as is_env_defaultable
|
|
4
|
+
from .base import is_required_missing_keyword_arg as is_required_missing_keyword_arg
|
|
5
|
+
from .base import is_unsupported_keyword_arg as is_unsupported_keyword_arg
|
|
6
|
+
from .base import PositionalArg as PositionalArg
|
|
7
|
+
from .data_types import AssertSet as AssertSet
|
|
8
|
+
from .data_types import Context as Context
|
|
9
|
+
from .data_types import Env as Env
|
|
10
|
+
from .data_types import ImportFile as ImportFile
|
|
11
|
+
from .data_types import JsonData as JsonData
|
|
12
|
+
from .data_types import JsonFile as JsonFile
|
|
13
|
+
from .data_types import Paths as Paths
|
|
14
|
+
from .data_types import Pattern as Pattern
|
|
15
|
+
from .data_types import RawFile as RawFile
|
|
16
|
+
from .operators import Chain as Chain
|
|
17
|
+
from .operators import Map as Map
|
|
18
|
+
from .operators import Operator as Operator
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from .keyword_arg import KeywordArg as KeywordArg
|
|
2
|
+
from .keyword_arg import is_defaultable as is_defaultable
|
|
3
|
+
from .keyword_arg import is_env_defaultable as is_env_defaultable
|
|
4
|
+
from .keyword_arg import (
|
|
5
|
+
is_required_missing_keyword_arg as is_required_missing_keyword_arg,
|
|
6
|
+
)
|
|
7
|
+
from .keyword_arg import is_unsupported_keyword_arg as is_unsupported_keyword_arg
|
|
8
|
+
from .positional_arg import PositionalArg as PositionalArg
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
from cocoa.cli.arg_types.data_types import (
|
|
3
|
+
AssertSet,
|
|
4
|
+
Context,
|
|
5
|
+
Env,
|
|
6
|
+
ImportFile,
|
|
7
|
+
JsonData,
|
|
8
|
+
JsonFile,
|
|
9
|
+
Paths,
|
|
10
|
+
Pattern,
|
|
11
|
+
RawFile,
|
|
12
|
+
)
|
|
13
|
+
from cocoa.cli.arg_types.data_types.reduce_pattern_type import reduce_pattern_type
|
|
14
|
+
from cocoa.cli.arg_types.operators import Operator
|
|
15
|
+
from typing import (
|
|
16
|
+
Literal,
|
|
17
|
+
Generic,
|
|
18
|
+
TypeVar,
|
|
19
|
+
Any,
|
|
20
|
+
Callable,
|
|
21
|
+
get_args,
|
|
22
|
+
get_origin,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
KeywordArgType = Literal["keyword", "flag"]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
T = TypeVar("T")
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class KeywordArg(Generic[T]):
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
name: str,
|
|
36
|
+
data_type: type[T],
|
|
37
|
+
short_name: str | None = None,
|
|
38
|
+
default: T | None = None,
|
|
39
|
+
group: str | None = None,
|
|
40
|
+
required: bool = True,
|
|
41
|
+
arg_type: KeywordArgType = "keyword",
|
|
42
|
+
description: str | None = None,
|
|
43
|
+
is_context_arg: bool = False,
|
|
44
|
+
):
|
|
45
|
+
if short_name is None:
|
|
46
|
+
short_name = name[:1]
|
|
47
|
+
|
|
48
|
+
self.name = name
|
|
49
|
+
self.short_name = short_name
|
|
50
|
+
self.is_context_arg = is_context_arg
|
|
51
|
+
|
|
52
|
+
full_flag = "-".join(name.split("_"))
|
|
53
|
+
|
|
54
|
+
self.full_flag = f"--{full_flag}"
|
|
55
|
+
self.short_flag = f"-{short_name}"
|
|
56
|
+
|
|
57
|
+
args = get_args(data_type)
|
|
58
|
+
self._complex_types: dict[
|
|
59
|
+
AssertSet
|
|
60
|
+
| Context
|
|
61
|
+
| Env
|
|
62
|
+
| ImportFile
|
|
63
|
+
| JsonData
|
|
64
|
+
| JsonData
|
|
65
|
+
| Operator
|
|
66
|
+
| Paths
|
|
67
|
+
| Pattern
|
|
68
|
+
| RawFile,
|
|
69
|
+
Callable[
|
|
70
|
+
[str, type[Any]],
|
|
71
|
+
AssertSet
|
|
72
|
+
| Context
|
|
73
|
+
| Env
|
|
74
|
+
| ImportFile
|
|
75
|
+
| JsonData
|
|
76
|
+
| JsonData
|
|
77
|
+
| Operator
|
|
78
|
+
| Paths
|
|
79
|
+
| Pattern
|
|
80
|
+
| RawFile,
|
|
81
|
+
],
|
|
82
|
+
] = {
|
|
83
|
+
AssertSet: lambda name, subtype: AssertSet(name, subtype),
|
|
84
|
+
Context: lambda _, __: Context(),
|
|
85
|
+
Env: lambda envar, subtype: Env(envar, subtype),
|
|
86
|
+
ImportFile: lambda _, subtype: ImportFile(subtype),
|
|
87
|
+
JsonFile: lambda _, subtype: JsonFile(subtype),
|
|
88
|
+
JsonData: lambda _, subtype: JsonData(subtype),
|
|
89
|
+
Operator: lambda name, subtype: Operator(name, subtype),
|
|
90
|
+
Paths: lambda _, subtype: Paths(subtype),
|
|
91
|
+
Pattern: lambda _, subtype: Pattern(subtype),
|
|
92
|
+
RawFile: lambda _, subtype: RawFile(subtype),
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
self._is_complex_type = (
|
|
96
|
+
get_origin(
|
|
97
|
+
data_type,
|
|
98
|
+
)
|
|
99
|
+
in self._complex_types.keys()
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
if len(args) > 0 and self._is_complex_type is False:
|
|
103
|
+
self._value_type = args
|
|
104
|
+
|
|
105
|
+
else:
|
|
106
|
+
self._value_type = tuple([data_type])
|
|
107
|
+
|
|
108
|
+
self.loads_from_envar = (
|
|
109
|
+
len([subtype for subtype in self._value_type if get_origin(subtype) == Env])
|
|
110
|
+
> 0
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
self.required = required
|
|
114
|
+
|
|
115
|
+
self.default_is_awaitable = False
|
|
116
|
+
if inspect.isawaitable(default) or inspect.iscoroutinefunction(default):
|
|
117
|
+
self.default_is_awaitable = True
|
|
118
|
+
|
|
119
|
+
self.default_is_callable = False
|
|
120
|
+
if inspect.isfunction(default) or inspect.ismethod(default):
|
|
121
|
+
self.default_is_callable = True
|
|
122
|
+
|
|
123
|
+
base_type = [data_type]
|
|
124
|
+
if self._is_complex_type:
|
|
125
|
+
base_type = reduce_pattern_type(data_type)
|
|
126
|
+
|
|
127
|
+
self.default = default
|
|
128
|
+
self.group = group
|
|
129
|
+
self.arg_type: KeywordArgType = arg_type
|
|
130
|
+
self._data_type = [
|
|
131
|
+
subtype_type.__name__
|
|
132
|
+
if hasattr(subtype_type, '__name__')
|
|
133
|
+
else subtype_type
|
|
134
|
+
for subtype_type in base_type
|
|
135
|
+
]
|
|
136
|
+
|
|
137
|
+
self.description = description
|
|
138
|
+
|
|
139
|
+
self.consumed_next_positions_count = 1 if arg_type == "keyword" else 0
|
|
140
|
+
|
|
141
|
+
@property
|
|
142
|
+
def data_type(self):
|
|
143
|
+
return ", ".join(self._data_type)
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def data_type_list(self):
|
|
147
|
+
return self._data_type
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def value_type(self):
|
|
151
|
+
return (
|
|
152
|
+
self._value_type
|
|
153
|
+
if self._is_complex_type is False
|
|
154
|
+
else tuple([get_origin(data_type) for data_type in self._value_type])
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
async def to_default(self):
|
|
158
|
+
if self.default_is_awaitable:
|
|
159
|
+
default_value = await self.default()
|
|
160
|
+
|
|
161
|
+
elif self.default_is_callable:
|
|
162
|
+
default_value = self.default()
|
|
163
|
+
|
|
164
|
+
else:
|
|
165
|
+
default_value = self.default
|
|
166
|
+
|
|
167
|
+
for subtype in self._value_type:
|
|
168
|
+
if complex_type_factory := self._complex_types.get(get_origin(subtype)):
|
|
169
|
+
complex_type = complex_type_factory(self.name, subtype)
|
|
170
|
+
|
|
171
|
+
complex_type.data = default_value
|
|
172
|
+
|
|
173
|
+
return complex_type
|
|
174
|
+
|
|
175
|
+
return default_value
|
|
176
|
+
|
|
177
|
+
def to_flag(self):
|
|
178
|
+
return f"--{self.name}"
|
|
179
|
+
|
|
180
|
+
async def parse(self, value: str | None = None):
|
|
181
|
+
parse_error: Exception | None = None
|
|
182
|
+
|
|
183
|
+
for subtype in self._value_type:
|
|
184
|
+
try:
|
|
185
|
+
if complex_type_factory := self._complex_types.get(get_origin(subtype)):
|
|
186
|
+
complex_type = complex_type_factory(self.name, subtype)
|
|
187
|
+
|
|
188
|
+
return await complex_type.parse(value)
|
|
189
|
+
|
|
190
|
+
elif subtype == bytes:
|
|
191
|
+
return bytes(value, encoding="utf-8")
|
|
192
|
+
|
|
193
|
+
return subtype(value)
|
|
194
|
+
|
|
195
|
+
except Exception as e:
|
|
196
|
+
parse_error = e
|
|
197
|
+
|
|
198
|
+
return parse_error
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def is_required_missing_keyword_arg(
|
|
202
|
+
flag: str, keyword_arg: KeywordArg, keyword_args: dict[str, KeywordArg]
|
|
203
|
+
):
|
|
204
|
+
if keyword_args.get(keyword_arg.name):
|
|
205
|
+
return False
|
|
206
|
+
|
|
207
|
+
if flag != keyword_arg.full_flag:
|
|
208
|
+
return False
|
|
209
|
+
|
|
210
|
+
if keyword_arg.arg_type == "flag":
|
|
211
|
+
return False
|
|
212
|
+
|
|
213
|
+
return keyword_arg.required
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def is_defaultable(
|
|
217
|
+
flag: str, keyword_arg: KeywordArg, keyword_args: dict[str, KeywordArg]
|
|
218
|
+
):
|
|
219
|
+
if keyword_args.get(keyword_arg.name):
|
|
220
|
+
return False
|
|
221
|
+
|
|
222
|
+
if flag == keyword_arg.short_flag:
|
|
223
|
+
return False
|
|
224
|
+
|
|
225
|
+
if keyword_arg.arg_type == "flag":
|
|
226
|
+
return False
|
|
227
|
+
|
|
228
|
+
return keyword_arg.required is False and keyword_arg.loads_from_envar is False
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def is_env_defaultable(
|
|
232
|
+
flag: str, keyword_arg: KeywordArg, keyword_args: dict[str, KeywordArg]
|
|
233
|
+
):
|
|
234
|
+
if keyword_args.get(keyword_arg.name):
|
|
235
|
+
return False
|
|
236
|
+
|
|
237
|
+
if flag == keyword_arg.short_flag:
|
|
238
|
+
return False
|
|
239
|
+
|
|
240
|
+
if keyword_arg.arg_type == "flag":
|
|
241
|
+
return False
|
|
242
|
+
|
|
243
|
+
return keyword_arg.loads_from_envar
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def is_unsupported_keyword_arg(arg: str, keyword_args: dict[str, KeywordArg]):
|
|
247
|
+
return (arg.startswith("--") or arg.startswith("-")) and arg.replace(
|
|
248
|
+
"-", ""
|
|
249
|
+
) not in keyword_args
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
from cocoa.cli.arg_types.data_types import (
|
|
2
|
+
AssertSet,
|
|
3
|
+
Context,
|
|
4
|
+
Env,
|
|
5
|
+
ImportFile,
|
|
6
|
+
JsonData,
|
|
7
|
+
JsonFile,
|
|
8
|
+
Paths,
|
|
9
|
+
Pattern,
|
|
10
|
+
RawFile,
|
|
11
|
+
)
|
|
12
|
+
from cocoa.cli.arg_types.operators import Operator
|
|
13
|
+
from typing import (
|
|
14
|
+
Literal,
|
|
15
|
+
Generic,
|
|
16
|
+
TypeVar,
|
|
17
|
+
Any,
|
|
18
|
+
Callable,
|
|
19
|
+
get_args,
|
|
20
|
+
get_origin,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
T = TypeVar("T")
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class PositionalArg(Generic[T]):
|
|
28
|
+
def __init__(
|
|
29
|
+
self,
|
|
30
|
+
name: str,
|
|
31
|
+
index: int,
|
|
32
|
+
data_type: type[T],
|
|
33
|
+
description: str | None = None,
|
|
34
|
+
is_context_arg: bool = False,
|
|
35
|
+
):
|
|
36
|
+
self.name = name
|
|
37
|
+
self.index = index
|
|
38
|
+
self.is_context_arg = is_context_arg
|
|
39
|
+
|
|
40
|
+
self.is_envar: bool = False
|
|
41
|
+
|
|
42
|
+
args = get_args(data_type)
|
|
43
|
+
self._complex_types: dict[
|
|
44
|
+
AssertSet
|
|
45
|
+
| Context
|
|
46
|
+
| Env
|
|
47
|
+
| ImportFile
|
|
48
|
+
| JsonData
|
|
49
|
+
| JsonData
|
|
50
|
+
| Operator
|
|
51
|
+
| Paths
|
|
52
|
+
| Pattern
|
|
53
|
+
| RawFile,
|
|
54
|
+
Callable[
|
|
55
|
+
[str, type[Any]],
|
|
56
|
+
AssertSet
|
|
57
|
+
| Context
|
|
58
|
+
| Env
|
|
59
|
+
| ImportFile
|
|
60
|
+
| JsonData
|
|
61
|
+
| JsonData
|
|
62
|
+
| Operator
|
|
63
|
+
| Paths
|
|
64
|
+
| Pattern
|
|
65
|
+
| RawFile,
|
|
66
|
+
],
|
|
67
|
+
] = {
|
|
68
|
+
AssertSet: lambda name, subtype: AssertSet(name, subtype),
|
|
69
|
+
Context: lambda _, __: Context(),
|
|
70
|
+
Env: lambda envar, subtype: Env(envar, subtype),
|
|
71
|
+
ImportFile: lambda _, subtype: ImportFile(subtype),
|
|
72
|
+
JsonFile: lambda _, subtype: JsonFile(subtype),
|
|
73
|
+
JsonData: lambda _, subtype: JsonData(subtype),
|
|
74
|
+
Operator: lambda name, subtype: Operator(name, subtype),
|
|
75
|
+
Paths: lambda _, subtype: Paths(subtype),
|
|
76
|
+
Pattern: lambda _, subtype: Pattern(subtype),
|
|
77
|
+
RawFile: lambda _, subtype: RawFile(subtype),
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
self._is_complex_type = (
|
|
81
|
+
get_origin(
|
|
82
|
+
data_type,
|
|
83
|
+
)
|
|
84
|
+
in self._complex_types.keys()
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
if len(args) > 0 and self._is_complex_type is False:
|
|
88
|
+
self._value_type = args
|
|
89
|
+
|
|
90
|
+
elif self._is_complex_type:
|
|
91
|
+
self._value_type = tuple([data_type])
|
|
92
|
+
|
|
93
|
+
else:
|
|
94
|
+
self._value_type = tuple([data_type])
|
|
95
|
+
|
|
96
|
+
self.loads_from_envar = (
|
|
97
|
+
len([subtype for subtype in self.value_type if get_origin(subtype) == Env])
|
|
98
|
+
> 0
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
self._data_type = [subtype_type.__name__ for subtype_type in self.value_type]
|
|
102
|
+
|
|
103
|
+
self.description = description
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def data_type(self):
|
|
107
|
+
return ", ".join(self._data_type)
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def value_type(self):
|
|
111
|
+
return (
|
|
112
|
+
self._value_type
|
|
113
|
+
if self._is_complex_type is False
|
|
114
|
+
else tuple([get_origin(data_type) for data_type in self._value_type])
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
def to_help_string(self):
|
|
118
|
+
help_string = f"{self.name}: [{self.data_type}]"
|
|
119
|
+
|
|
120
|
+
if self.description:
|
|
121
|
+
help_string = f"{help_string} {self.description}"
|
|
122
|
+
|
|
123
|
+
return help_string
|
|
124
|
+
|
|
125
|
+
async def parse(self, value: str | None = None):
|
|
126
|
+
parse_error: Exception | None = None
|
|
127
|
+
|
|
128
|
+
for subtype in self._value_type:
|
|
129
|
+
try:
|
|
130
|
+
if complex_type_factory := self._complex_types.get(get_origin(subtype)):
|
|
131
|
+
complex_type = complex_type_factory(self.name, subtype)
|
|
132
|
+
|
|
133
|
+
return await complex_type.parse(value)
|
|
134
|
+
|
|
135
|
+
elif subtype == bytes:
|
|
136
|
+
return bytes(value, encoding="utf-8")
|
|
137
|
+
|
|
138
|
+
return subtype(value)
|
|
139
|
+
|
|
140
|
+
except Exception as e:
|
|
141
|
+
parse_error = e
|
|
142
|
+
|
|
143
|
+
return parse_error
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
from .assert_set import AssertSet as AssertSet
|
|
2
|
+
from .context import Context as Context
|
|
3
|
+
from .env import Env as Env
|
|
4
|
+
from .import_file import ImportFile as ImportFile
|
|
5
|
+
from .json_data import JsonData as JsonData
|
|
6
|
+
from .json_file import JsonFile as JsonFile
|
|
7
|
+
from .paths import Paths as Paths
|
|
8
|
+
from .pattern import Pattern as Pattern
|
|
9
|
+
from .raw_file import RawFile as RawFile
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
from typing import TypeVar, Generic, Any
|
|
4
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
T = TypeVar("T")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AssertSet(Generic[T]):
|
|
11
|
+
def __init__(self, name: str, data_type: AssertSet[T]):
|
|
12
|
+
super().__init__()
|
|
13
|
+
self.name = name
|
|
14
|
+
self.data: T | None = None
|
|
15
|
+
|
|
16
|
+
conversion_types: list[T] = reduce_pattern_type(data_type)
|
|
17
|
+
|
|
18
|
+
self._data_types = [
|
|
19
|
+
conversion_type.__name__
|
|
20
|
+
if hasattr(conversion_type, "__name__")
|
|
21
|
+
else conversion_type
|
|
22
|
+
for conversion_type in conversion_types
|
|
23
|
+
]
|
|
24
|
+
self._types = conversion_types
|
|
25
|
+
|
|
26
|
+
self._loop = asyncio.get_event_loop()
|
|
27
|
+
|
|
28
|
+
def __contains__(self, value: Any):
|
|
29
|
+
return type(value) in [self._types]
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def data_type(self):
|
|
33
|
+
return ", ".join(self._data_types)
|
|
34
|
+
|
|
35
|
+
async def parse(self, arg: str | None = None):
|
|
36
|
+
if arg is None:
|
|
37
|
+
return Exception("no argument passed")
|
|
38
|
+
|
|
39
|
+
try:
|
|
40
|
+
assert arg in self._types, (
|
|
41
|
+
f"{arg} is not a supported value for {self.name} - please pass one of {self.data_type}"
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
self.data = arg
|
|
45
|
+
|
|
46
|
+
return self
|
|
47
|
+
|
|
48
|
+
except Exception as e:
|
|
49
|
+
return e
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from typing import TypeVar, Generic
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
K = TypeVar("K")
|
|
5
|
+
V = TypeVar("V")
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Context(Generic[K, V]):
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self._context: dict[K, V] = {}
|
|
11
|
+
|
|
12
|
+
def __setitem__(self, key: K, value: V):
|
|
13
|
+
self._context[key] = value
|
|
14
|
+
|
|
15
|
+
def __getitem__(self, key: K):
|
|
16
|
+
return self._context.get(key)
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import os
|
|
5
|
+
from typing import Generic, TypeVar, Any
|
|
6
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
T = TypeVar("T")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Env(Generic[T]):
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
envar: str,
|
|
16
|
+
data_type: Env[T],
|
|
17
|
+
):
|
|
18
|
+
super().__init__()
|
|
19
|
+
self._envar = envar
|
|
20
|
+
|
|
21
|
+
self.data: T | None = None
|
|
22
|
+
|
|
23
|
+
data_types = reduce_pattern_type(data_type)
|
|
24
|
+
self._types = data_types
|
|
25
|
+
|
|
26
|
+
self._data_types = [
|
|
27
|
+
conversion_type.__name__
|
|
28
|
+
if hasattr(conversion_type, "__name__")
|
|
29
|
+
else type(conversion_type).__name__
|
|
30
|
+
for conversion_type in self._types
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
self._loop = asyncio.get_event_loop()
|
|
34
|
+
|
|
35
|
+
def __contains__(self, value: Any):
|
|
36
|
+
return type(value) in [self._types]
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def data_type(self):
|
|
40
|
+
return self._data_types
|
|
41
|
+
|
|
42
|
+
async def parse(self, _: str | None = None):
|
|
43
|
+
result = await self._load_envar()
|
|
44
|
+
if isinstance(result, Exception):
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
self.data = result
|
|
48
|
+
|
|
49
|
+
return self
|
|
50
|
+
|
|
51
|
+
async def _load_envar(self):
|
|
52
|
+
value = await self._load()
|
|
53
|
+
if isinstance(value, None):
|
|
54
|
+
return Exception(f"no envar matching {self._envar.upper()} found")
|
|
55
|
+
|
|
56
|
+
for conversion_type in self._types:
|
|
57
|
+
try:
|
|
58
|
+
if conversion_type == bytes:
|
|
59
|
+
return bytes(value, encoding="utf-8")
|
|
60
|
+
|
|
61
|
+
else:
|
|
62
|
+
return conversion_type(value)
|
|
63
|
+
|
|
64
|
+
except Exception:
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
return Exception(f"could not parse {value} specified types")
|
|
68
|
+
|
|
69
|
+
async def _load(self):
|
|
70
|
+
value = await self._loop.run_in_executor(
|
|
71
|
+
None,
|
|
72
|
+
os.getenv,
|
|
73
|
+
self._envar.upper(),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
return value
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
import asyncio
|
|
3
|
+
import importlib.util
|
|
4
|
+
import pathlib
|
|
5
|
+
import sys
|
|
6
|
+
import importlib
|
|
7
|
+
import ntpath
|
|
8
|
+
from typing import Generic, TypeVar, Any
|
|
9
|
+
from .reduce_pattern_type import reduce_pattern_type
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ImportFile(Generic[T]):
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
data_type: ImportFile[T],
|
|
19
|
+
):
|
|
20
|
+
super().__init__()
|
|
21
|
+
self.data: dict[str, T] | None = None
|
|
22
|
+
|
|
23
|
+
conversion_types: list[T] = reduce_pattern_type(data_type)
|
|
24
|
+
|
|
25
|
+
self._data_types = [
|
|
26
|
+
conversion_type.__name__
|
|
27
|
+
if hasattr(conversion_type, "__name__")
|
|
28
|
+
else type(conversion_type).__name__
|
|
29
|
+
for conversion_type in conversion_types
|
|
30
|
+
]
|
|
31
|
+
self._types = conversion_types
|
|
32
|
+
|
|
33
|
+
self._loop = asyncio.get_event_loop()
|
|
34
|
+
|
|
35
|
+
def __contains__(self, value: Any):
|
|
36
|
+
return type(value) in [self._types]
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def data_type(self):
|
|
40
|
+
return ", ".join(self._data_types)
|
|
41
|
+
|
|
42
|
+
async def parse(self, arg: str | None = None):
|
|
43
|
+
result = await self._import_file_objects(arg)
|
|
44
|
+
if isinstance(result, Exception):
|
|
45
|
+
return result
|
|
46
|
+
|
|
47
|
+
self.data = result
|
|
48
|
+
|
|
49
|
+
return self
|
|
50
|
+
|
|
51
|
+
async def _import_file_objects(self, arg: str | None):
|
|
52
|
+
try:
|
|
53
|
+
if arg is None:
|
|
54
|
+
return Exception("no argument passed for filepath")
|
|
55
|
+
|
|
56
|
+
path = await self._loop.run_in_executor(None, pathlib.Path, arg)
|
|
57
|
+
|
|
58
|
+
resolved_path = await self._loop.run_in_executor(None, path.resolve)
|
|
59
|
+
|
|
60
|
+
package_dir = resolved_path.parent
|
|
61
|
+
package_dir_path = str(package_dir)
|
|
62
|
+
package_dir_module = package_dir_path.split("/")[-1]
|
|
63
|
+
|
|
64
|
+
package = ntpath.basename(path)
|
|
65
|
+
package_slug = package.split(".")[0]
|
|
66
|
+
|
|
67
|
+
spec = await self._loop.run_in_executor(
|
|
68
|
+
None,
|
|
69
|
+
importlib.util.spec_from_file_location,
|
|
70
|
+
f"{package_dir_module}.{package_slug}",
|
|
71
|
+
arg,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
if arg not in sys.path:
|
|
75
|
+
await self._loop.run_in_executor(
|
|
76
|
+
None, sys.path.append, str(package_dir.parent)
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
module = await self._loop.run_in_executor(
|
|
80
|
+
None,
|
|
81
|
+
importlib.util.module_from_spec,
|
|
82
|
+
spec,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
sys.modules[module.__name__] = module
|
|
86
|
+
|
|
87
|
+
await self._loop.run_in_executor(
|
|
88
|
+
None,
|
|
89
|
+
spec.loader.exec_module,
|
|
90
|
+
module,
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
imported_types: dict[str, T] = {}
|
|
94
|
+
|
|
95
|
+
for conversion_type in self._types:
|
|
96
|
+
discovered = list(
|
|
97
|
+
{
|
|
98
|
+
cls.__name__: cls for cls in conversion_type.__subclasses__()
|
|
99
|
+
}.values()
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
imported_types.update({item.__name__: item for item in discovered})
|
|
103
|
+
|
|
104
|
+
return imported_types
|
|
105
|
+
|
|
106
|
+
except Exception as e:
|
|
107
|
+
return Exception(f"could not import objects from file {arg}\n\t {str(e)}")
|