batrachian-toad 0.5.22__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.
- batrachian_toad-0.5.22.dist-info/METADATA +197 -0
- batrachian_toad-0.5.22.dist-info/RECORD +120 -0
- batrachian_toad-0.5.22.dist-info/WHEEL +4 -0
- batrachian_toad-0.5.22.dist-info/entry_points.txt +2 -0
- batrachian_toad-0.5.22.dist-info/licenses/LICENSE +661 -0
- toad/__init__.py +46 -0
- toad/__main__.py +4 -0
- toad/_loop.py +86 -0
- toad/about.py +90 -0
- toad/acp/agent.py +671 -0
- toad/acp/api.py +47 -0
- toad/acp/encode_tool_call_id.py +12 -0
- toad/acp/messages.py +138 -0
- toad/acp/prompt.py +54 -0
- toad/acp/protocol.py +426 -0
- toad/agent.py +62 -0
- toad/agent_schema.py +70 -0
- toad/agents.py +45 -0
- toad/ansi/__init__.py +1 -0
- toad/ansi/_ansi.py +1612 -0
- toad/ansi/_ansi_colors.py +264 -0
- toad/ansi/_control_codes.py +37 -0
- toad/ansi/_keys.py +251 -0
- toad/ansi/_sgr_styles.py +64 -0
- toad/ansi/_stream_parser.py +418 -0
- toad/answer.py +22 -0
- toad/app.py +557 -0
- toad/atomic.py +37 -0
- toad/cli.py +257 -0
- toad/code_analyze.py +28 -0
- toad/complete.py +34 -0
- toad/constants.py +58 -0
- toad/conversation_markdown.py +19 -0
- toad/danger.py +371 -0
- toad/data/agents/ampcode.com.toml +51 -0
- toad/data/agents/augmentcode.com.toml +40 -0
- toad/data/agents/claude.com.toml +41 -0
- toad/data/agents/docker.com.toml +59 -0
- toad/data/agents/geminicli.com.toml +28 -0
- toad/data/agents/goose.ai.toml +51 -0
- toad/data/agents/inference.huggingface.co.toml +33 -0
- toad/data/agents/kimi.com.toml +35 -0
- toad/data/agents/openai.com.toml +53 -0
- toad/data/agents/opencode.ai.toml +61 -0
- toad/data/agents/openhands.dev.toml +44 -0
- toad/data/agents/stakpak.dev.toml +61 -0
- toad/data/agents/vibe.mistral.ai.toml +27 -0
- toad/data/agents/vtcode.dev.toml +62 -0
- toad/data/images/frog.png +0 -0
- toad/data/sounds/turn-over.wav +0 -0
- toad/db.py +5 -0
- toad/dec.py +332 -0
- toad/directory.py +234 -0
- toad/directory_watcher.py +96 -0
- toad/fuzzy.py +140 -0
- toad/gist.py +2 -0
- toad/history.py +138 -0
- toad/jsonrpc.py +576 -0
- toad/menus.py +14 -0
- toad/messages.py +74 -0
- toad/option_content.py +51 -0
- toad/os.py +0 -0
- toad/path_complete.py +145 -0
- toad/path_filter.py +124 -0
- toad/paths.py +71 -0
- toad/pill.py +23 -0
- toad/prompt/extract.py +19 -0
- toad/prompt/resource.py +68 -0
- toad/protocol.py +28 -0
- toad/screens/action_modal.py +94 -0
- toad/screens/agent_modal.py +172 -0
- toad/screens/command_edit_modal.py +58 -0
- toad/screens/main.py +192 -0
- toad/screens/permissions.py +390 -0
- toad/screens/permissions.tcss +72 -0
- toad/screens/settings.py +254 -0
- toad/screens/settings.tcss +101 -0
- toad/screens/store.py +476 -0
- toad/screens/store.tcss +261 -0
- toad/settings.py +354 -0
- toad/settings_schema.py +318 -0
- toad/shell.py +263 -0
- toad/shell_read.py +42 -0
- toad/slash_command.py +34 -0
- toad/toad.tcss +752 -0
- toad/version.py +80 -0
- toad/visuals/columns.py +273 -0
- toad/widgets/agent_response.py +79 -0
- toad/widgets/agent_thought.py +41 -0
- toad/widgets/command_pane.py +224 -0
- toad/widgets/condensed_path.py +93 -0
- toad/widgets/conversation.py +1626 -0
- toad/widgets/danger_warning.py +65 -0
- toad/widgets/diff_view.py +709 -0
- toad/widgets/flash.py +81 -0
- toad/widgets/future_text.py +126 -0
- toad/widgets/grid_select.py +223 -0
- toad/widgets/highlighted_textarea.py +180 -0
- toad/widgets/mandelbrot.py +294 -0
- toad/widgets/markdown_note.py +13 -0
- toad/widgets/menu.py +147 -0
- toad/widgets/non_selectable_label.py +5 -0
- toad/widgets/note.py +18 -0
- toad/widgets/path_search.py +381 -0
- toad/widgets/plan.py +180 -0
- toad/widgets/project_directory_tree.py +74 -0
- toad/widgets/prompt.py +741 -0
- toad/widgets/question.py +337 -0
- toad/widgets/shell_result.py +35 -0
- toad/widgets/shell_terminal.py +18 -0
- toad/widgets/side_bar.py +74 -0
- toad/widgets/slash_complete.py +211 -0
- toad/widgets/strike_text.py +66 -0
- toad/widgets/terminal.py +526 -0
- toad/widgets/terminal_tool.py +338 -0
- toad/widgets/throbber.py +90 -0
- toad/widgets/tool_call.py +303 -0
- toad/widgets/user_input.py +23 -0
- toad/widgets/version.py +5 -0
- toad/widgets/welcome.py +31 -0
toad/screens/store.tcss
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
StoreScreen {
|
|
2
|
+
|
|
3
|
+
#container {
|
|
4
|
+
hatch: right $primary 15%;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
.instruction-text {
|
|
8
|
+
text-style: dim italic;
|
|
9
|
+
padding: 1 2;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
.heading {
|
|
13
|
+
margin: 0 0 0 0;
|
|
14
|
+
padding: 1 1 0 2;
|
|
15
|
+
text-style: underline ;
|
|
16
|
+
color: $text-warning;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
Footer {
|
|
20
|
+
.footer-key--key {
|
|
21
|
+
background: transparent;
|
|
22
|
+
padding: 0 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
#title-container {
|
|
27
|
+
align: center top;
|
|
28
|
+
dock: top;
|
|
29
|
+
|
|
30
|
+
#title-grid {
|
|
31
|
+
margin: 0 1;
|
|
32
|
+
border: block black 20%;
|
|
33
|
+
background: black 20%;
|
|
34
|
+
grid-size: 2 1;
|
|
35
|
+
height: auto;
|
|
36
|
+
grid-columns: 24 1fr;
|
|
37
|
+
grid-rows: auto;
|
|
38
|
+
grid-gutter: 1 2;
|
|
39
|
+
min-width: 40;
|
|
40
|
+
|
|
41
|
+
#info {
|
|
42
|
+
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
Mandelbrot {
|
|
46
|
+
border: none;
|
|
47
|
+
height: 100%;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
LoadingIndicator {
|
|
53
|
+
height: 3;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
GridSelect {
|
|
57
|
+
height: auto;
|
|
58
|
+
padding: 0;
|
|
59
|
+
margin: 0 0;
|
|
60
|
+
|
|
61
|
+
&.-highlight {
|
|
62
|
+
background: $panel;
|
|
63
|
+
border: tall $primary 30%;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
&:focus * {
|
|
67
|
+
&.-highlight {
|
|
68
|
+
background: $panel;
|
|
69
|
+
border: tall $primary;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
#sponsored-agents {
|
|
76
|
+
GridSelect {
|
|
77
|
+
margin:0 1;
|
|
78
|
+
grid-gutter: 1;
|
|
79
|
+
keyline: thin white 10%;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
AgentItem {
|
|
85
|
+
height: auto;
|
|
86
|
+
border: tall transparent;
|
|
87
|
+
padding: 0 1;
|
|
88
|
+
&:hover {
|
|
89
|
+
background: $panel;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
Grid {
|
|
93
|
+
grid-size: 2 1;
|
|
94
|
+
grid-columns: 1fr auto;
|
|
95
|
+
height: auto;
|
|
96
|
+
#type {
|
|
97
|
+
text-align: right;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
#description {
|
|
101
|
+
text-style: dim;
|
|
102
|
+
}
|
|
103
|
+
#author {
|
|
104
|
+
text-style: italic;
|
|
105
|
+
color: $text-secondary;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
Launcher {
|
|
109
|
+
GridSelect {
|
|
110
|
+
width: 1fr;
|
|
111
|
+
}
|
|
112
|
+
&.-empty {
|
|
113
|
+
LauncherGridSelect {
|
|
114
|
+
visibility: hidden;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
.no-agents {
|
|
118
|
+
text-style: dim italic;
|
|
119
|
+
padding: 1 2;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
LauncherItem {
|
|
125
|
+
# width: 40;
|
|
126
|
+
# max-width: 40;
|
|
127
|
+
height: auto;
|
|
128
|
+
border: tall transparent;
|
|
129
|
+
padding: 0 1;
|
|
130
|
+
&:hover {
|
|
131
|
+
background: $panel;
|
|
132
|
+
}
|
|
133
|
+
Digits {
|
|
134
|
+
width: auto;
|
|
135
|
+
padding: 0 1 0 0;
|
|
136
|
+
color: $text-success;
|
|
137
|
+
# text-style: bold;
|
|
138
|
+
}
|
|
139
|
+
#description {
|
|
140
|
+
text-style: dim;
|
|
141
|
+
text-wrap: nowrap;
|
|
142
|
+
text-overflow: ellipsis;
|
|
143
|
+
}
|
|
144
|
+
#author {
|
|
145
|
+
text-style: italic;
|
|
146
|
+
color: $text-secondary;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
AgentModal {
|
|
153
|
+
align: center middle;
|
|
154
|
+
# background: $surface 80%;
|
|
155
|
+
|
|
156
|
+
.acp-warning {
|
|
157
|
+
# background: $success-muted 50%;
|
|
158
|
+
color: $text-success;
|
|
159
|
+
margin-left: 1;
|
|
160
|
+
padding: 0 1;
|
|
161
|
+
margin-bottom: 1;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#container {
|
|
165
|
+
|
|
166
|
+
margin: 2 4 1 4;
|
|
167
|
+
padding: 0 1 0 0;
|
|
168
|
+
max-width: 100;
|
|
169
|
+
height: auto;
|
|
170
|
+
border: thick $primary 20%;
|
|
171
|
+
|
|
172
|
+
DescriptionContainer {
|
|
173
|
+
height: auto;
|
|
174
|
+
padding: 0 0 0 0;
|
|
175
|
+
margin: 0 0 1 1;
|
|
176
|
+
max-height: 20;
|
|
177
|
+
overflow-y: auto;
|
|
178
|
+
Markdown {
|
|
179
|
+
padding: 0 1;
|
|
180
|
+
MarkdownH1 {
|
|
181
|
+
margin: 1 1 1 0
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
&:focus {
|
|
186
|
+
background: $primary 7%;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
Select {
|
|
191
|
+
width: 1fr;
|
|
192
|
+
margin-right: 1;
|
|
193
|
+
}
|
|
194
|
+
Checkbox {
|
|
195
|
+
margin: 0;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
Footer {
|
|
199
|
+
# margin: 0 0 0 0;
|
|
200
|
+
opacity: 0.0;
|
|
201
|
+
FooterKey {
|
|
202
|
+
.footer-key--key {
|
|
203
|
+
color: $accent !important;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
CommandEditModal {
|
|
210
|
+
align: center middle;
|
|
211
|
+
|
|
212
|
+
#container {
|
|
213
|
+
padding: 1 1 0 1;
|
|
214
|
+
border: thick $primary 20%;
|
|
215
|
+
border-title-color: $text;
|
|
216
|
+
margin: 3 6;
|
|
217
|
+
height: 1fr;
|
|
218
|
+
height: auto;
|
|
219
|
+
max-width: 100;
|
|
220
|
+
|
|
221
|
+
.instructions {
|
|
222
|
+
color: $text-success;
|
|
223
|
+
padding: 0 1 1 1;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
TextArea {
|
|
227
|
+
height: 8
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
#button-container {
|
|
231
|
+
padding: 1 1 0 1;
|
|
232
|
+
width: 1fr;
|
|
233
|
+
align: right top;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
ActionModal {
|
|
241
|
+
align: center middle;
|
|
242
|
+
#container {
|
|
243
|
+
margin: 3 6;
|
|
244
|
+
height: 1fr;
|
|
245
|
+
max-height: 48;
|
|
246
|
+
CommandPane {
|
|
247
|
+
padding: 1 1;
|
|
248
|
+
background: black 10%;
|
|
249
|
+
border: tab $primary;
|
|
250
|
+
&.-success {
|
|
251
|
+
border: tab $text-success 70%;
|
|
252
|
+
}
|
|
253
|
+
&.-fail {
|
|
254
|
+
border: tab $text-error 50%;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
Button {
|
|
258
|
+
width: 100%;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
toad/settings.py
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
import copy
|
|
5
|
+
from functools import cached_property
|
|
6
|
+
from json import dumps
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from typing import Callable, Iterable, KeysView, Sequence, TypedDict, Required
|
|
9
|
+
|
|
10
|
+
from toad._loop import loop_last
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass
|
|
14
|
+
class Setting:
|
|
15
|
+
"""A setting or group of setting."""
|
|
16
|
+
|
|
17
|
+
key: str
|
|
18
|
+
title: str
|
|
19
|
+
type: str = "object"
|
|
20
|
+
help: str = ""
|
|
21
|
+
choices: list[str] | None = None
|
|
22
|
+
default: object | None = None
|
|
23
|
+
validate: list[dict] | None = None
|
|
24
|
+
children: dict[str, Setting] | None = None
|
|
25
|
+
editable: bool = True
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SchemaDict(TypedDict, total=False):
|
|
29
|
+
"""Typing for schema data structure."""
|
|
30
|
+
|
|
31
|
+
key: Required[str]
|
|
32
|
+
title: Required[str]
|
|
33
|
+
type: Required[str]
|
|
34
|
+
help: str
|
|
35
|
+
choices: list[str] | list[tuple[str, str]] | None
|
|
36
|
+
default: object
|
|
37
|
+
fields: list[SchemaDict]
|
|
38
|
+
validate: list[dict]
|
|
39
|
+
editable: bool
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
type SettingsType = dict[str, object]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
INPUT_TYPES = {"boolean", "integer", "number", "string", "choices", "text"}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class SettingsError(Exception):
|
|
49
|
+
"""Base class for settings related errors."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class InvalidKey(SettingsError):
|
|
53
|
+
"""The key is not in the schema."""
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class InvalidValue(SettingsError):
|
|
57
|
+
"""The value was not of the expected type."""
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def parse_key(key: str) -> Sequence[str]:
|
|
61
|
+
return key.split(".")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def get_setting[ExpectType](
|
|
65
|
+
settings: dict[str, object], key: str, expect_type: type[ExpectType] = object
|
|
66
|
+
) -> ExpectType:
|
|
67
|
+
"""Get a key from a settings structure.
|
|
68
|
+
|
|
69
|
+
Args:
|
|
70
|
+
settings: A settings dictionary.
|
|
71
|
+
key: A dot delimited key, e.g. "ui.column"
|
|
72
|
+
expect_type: The expected type of the value.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
InvalidValue: If the value is not the expected type.
|
|
76
|
+
KeyError: If the key doesn't exist in settings.
|
|
77
|
+
|
|
78
|
+
Returns:
|
|
79
|
+
The value matching they key.
|
|
80
|
+
"""
|
|
81
|
+
for last, key_component in loop_last(parse_key(key)):
|
|
82
|
+
if last:
|
|
83
|
+
result = settings[key_component]
|
|
84
|
+
if not isinstance(result, expect_type):
|
|
85
|
+
raise InvalidValue(
|
|
86
|
+
f"Expected {expect_type.__name__} type; found {result!r}"
|
|
87
|
+
)
|
|
88
|
+
return result
|
|
89
|
+
else:
|
|
90
|
+
sub_settings = settings.setdefault(key_component, {})
|
|
91
|
+
assert isinstance(sub_settings, dict)
|
|
92
|
+
settings = sub_settings
|
|
93
|
+
raise KeyError(key)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class Schema:
|
|
97
|
+
def __init__(self, schema: list[SchemaDict]) -> None:
|
|
98
|
+
self.schema = schema
|
|
99
|
+
|
|
100
|
+
def set_value(self, settings: SettingsType, key: str, value: object) -> None:
|
|
101
|
+
schema = self.schema
|
|
102
|
+
keys = parse_key(key)
|
|
103
|
+
for last, key in loop_last(keys):
|
|
104
|
+
if last:
|
|
105
|
+
settings[key] = value
|
|
106
|
+
if key not in schema:
|
|
107
|
+
raise InvalidKey()
|
|
108
|
+
schema = schema[key]
|
|
109
|
+
assert isinstance(schema, dict)
|
|
110
|
+
if key not in settings:
|
|
111
|
+
settings = settings[key] = {}
|
|
112
|
+
|
|
113
|
+
def get_default(self, key: str) -> object | None:
|
|
114
|
+
"""Get a default for the given key.
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
key: Key in dotted notation
|
|
118
|
+
|
|
119
|
+
Returns:
|
|
120
|
+
Default, or `None`.
|
|
121
|
+
"""
|
|
122
|
+
defaults = self.defaults
|
|
123
|
+
|
|
124
|
+
schema_object = defaults
|
|
125
|
+
for last, sub_key in loop_last(parse_key(key)):
|
|
126
|
+
if last:
|
|
127
|
+
return schema_object.get(sub_key, None)
|
|
128
|
+
else:
|
|
129
|
+
if isinstance(schema_object, dict):
|
|
130
|
+
schema_object = schema_object.get(sub_key, {})
|
|
131
|
+
else:
|
|
132
|
+
return None
|
|
133
|
+
return None
|
|
134
|
+
|
|
135
|
+
@cached_property
|
|
136
|
+
def defaults(self) -> dict[str, object]:
|
|
137
|
+
settings: dict[str, object] = {}
|
|
138
|
+
|
|
139
|
+
def set_defaults(schema: list[SchemaDict], settings: dict[str, object]) -> None:
|
|
140
|
+
sub_settings: SettingsType
|
|
141
|
+
for sub_schema in schema:
|
|
142
|
+
key = sub_schema["key"]
|
|
143
|
+
assert isinstance(sub_schema, dict)
|
|
144
|
+
type = sub_schema["type"]
|
|
145
|
+
|
|
146
|
+
if type == "object":
|
|
147
|
+
if fields := sub_schema.get("fields"):
|
|
148
|
+
sub_settings = settings[key] = {}
|
|
149
|
+
set_defaults(fields, sub_settings)
|
|
150
|
+
|
|
151
|
+
else:
|
|
152
|
+
if (default := sub_schema.get("default")) is not None:
|
|
153
|
+
settings[key] = default
|
|
154
|
+
|
|
155
|
+
set_defaults(self.schema, settings)
|
|
156
|
+
return settings
|
|
157
|
+
|
|
158
|
+
@cached_property
|
|
159
|
+
def key_to_type(self) -> Mapping[str, type]:
|
|
160
|
+
TYPE_MAP = {
|
|
161
|
+
"object": SchemaDict,
|
|
162
|
+
"string": str,
|
|
163
|
+
"integer": int,
|
|
164
|
+
"number": float,
|
|
165
|
+
"boolean": bool,
|
|
166
|
+
"choices": str,
|
|
167
|
+
"text": str,
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
def get_keys(setting: Setting) -> Iterable[tuple[str, type]]:
|
|
171
|
+
if setting.type == "object" and setting.children:
|
|
172
|
+
for child in setting.children.values():
|
|
173
|
+
yield from get_keys(child)
|
|
174
|
+
else:
|
|
175
|
+
yield (setting.key, TYPE_MAP[setting.type])
|
|
176
|
+
|
|
177
|
+
keys = {
|
|
178
|
+
key: value_type
|
|
179
|
+
for setting in self.settings_map.values()
|
|
180
|
+
for key, value_type in get_keys(setting)
|
|
181
|
+
}
|
|
182
|
+
return keys
|
|
183
|
+
|
|
184
|
+
@property
|
|
185
|
+
def keys(self) -> KeysView:
|
|
186
|
+
return self.key_to_type.keys()
|
|
187
|
+
|
|
188
|
+
@cached_property
|
|
189
|
+
def settings_map(self) -> dict[str, Setting]:
|
|
190
|
+
form_settings: dict[str, Setting] = {}
|
|
191
|
+
|
|
192
|
+
def build_settings(
|
|
193
|
+
name: str, schema: SchemaDict, default: object = None
|
|
194
|
+
) -> Setting:
|
|
195
|
+
schema_type = schema.get("type")
|
|
196
|
+
assert schema_type is not None
|
|
197
|
+
if schema_type == "object":
|
|
198
|
+
return Setting(
|
|
199
|
+
name,
|
|
200
|
+
schema["title"],
|
|
201
|
+
schema_type,
|
|
202
|
+
help=schema.get("help") or "",
|
|
203
|
+
default=schema.get("default", default),
|
|
204
|
+
validate=schema.get("validate"),
|
|
205
|
+
children={
|
|
206
|
+
schema["key"]: build_settings(f"{name}.{schema['key']}", schema)
|
|
207
|
+
for schema in schema.get("fields", [])
|
|
208
|
+
},
|
|
209
|
+
editable=schema.get("editable", True),
|
|
210
|
+
)
|
|
211
|
+
else:
|
|
212
|
+
return Setting(
|
|
213
|
+
name,
|
|
214
|
+
schema["title"],
|
|
215
|
+
schema_type,
|
|
216
|
+
choices=schema.get("choices"),
|
|
217
|
+
help=schema.get("help") or "",
|
|
218
|
+
default=schema.get("default", default),
|
|
219
|
+
validate=schema.get("validate"),
|
|
220
|
+
editable=schema.get("editable", True),
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
for sub_schema in self.schema:
|
|
224
|
+
form_settings[sub_schema["key"]] = build_settings(
|
|
225
|
+
sub_schema["key"], sub_schema
|
|
226
|
+
)
|
|
227
|
+
return form_settings
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class Settings:
|
|
231
|
+
"""Stores schema backed settings."""
|
|
232
|
+
|
|
233
|
+
def __init__(
|
|
234
|
+
self,
|
|
235
|
+
schema: Schema,
|
|
236
|
+
settings: dict[str, object],
|
|
237
|
+
on_set_callback: Callable[[str, object]] | None = None,
|
|
238
|
+
) -> None:
|
|
239
|
+
self._schema = schema
|
|
240
|
+
self._settings = settings
|
|
241
|
+
self._on_set_callback = on_set_callback
|
|
242
|
+
self._changed: bool = False
|
|
243
|
+
|
|
244
|
+
@property
|
|
245
|
+
def changed(self) -> bool:
|
|
246
|
+
return self._changed
|
|
247
|
+
|
|
248
|
+
@property
|
|
249
|
+
def schema(self) -> Schema:
|
|
250
|
+
return self._schema
|
|
251
|
+
|
|
252
|
+
def up_to_date(self) -> None:
|
|
253
|
+
"""Set settings as up to date (clears changed flag)."""
|
|
254
|
+
self._changed = False
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def json(self) -> str:
|
|
258
|
+
"""Settings in JSON form."""
|
|
259
|
+
settings_json = dumps(self._settings, indent=4, separators=(", ", ": "))
|
|
260
|
+
return settings_json
|
|
261
|
+
|
|
262
|
+
def set_all(self) -> None:
|
|
263
|
+
if self._on_set_callback is not None:
|
|
264
|
+
for key in self._schema.keys:
|
|
265
|
+
self._on_set_callback(key, self.get(key))
|
|
266
|
+
|
|
267
|
+
def get[ExpectType](
|
|
268
|
+
self,
|
|
269
|
+
key: str,
|
|
270
|
+
expect_type: type[ExpectType] = object,
|
|
271
|
+
*,
|
|
272
|
+
expand: bool = True,
|
|
273
|
+
) -> ExpectType:
|
|
274
|
+
from os.path import expandvars
|
|
275
|
+
|
|
276
|
+
sub_settings = self._settings
|
|
277
|
+
|
|
278
|
+
for last, sub_key in loop_last(parse_key(key)):
|
|
279
|
+
if last:
|
|
280
|
+
if (value := sub_settings.get(sub_key)) is None:
|
|
281
|
+
default = self._schema.get_default(key)
|
|
282
|
+
if default is None:
|
|
283
|
+
default = expect_type()
|
|
284
|
+
if not isinstance(default, expect_type):
|
|
285
|
+
default = expect_type(default)
|
|
286
|
+
assert isinstance(default, expect_type)
|
|
287
|
+
return default
|
|
288
|
+
|
|
289
|
+
if isinstance(value, str) and expand:
|
|
290
|
+
value = expandvars(value)
|
|
291
|
+
if not isinstance(value, expect_type):
|
|
292
|
+
value = expect_type(value)
|
|
293
|
+
if not isinstance(value, expect_type):
|
|
294
|
+
raise InvalidValue(
|
|
295
|
+
f"key {sub_key!r} is not of expected type {expect_type.__name__}"
|
|
296
|
+
)
|
|
297
|
+
return value
|
|
298
|
+
if not isinstance((sub_settings := sub_settings.get(sub_key, {})), dict):
|
|
299
|
+
default = self._schema.get_default(key)
|
|
300
|
+
if default is None:
|
|
301
|
+
default = expect_type()
|
|
302
|
+
if not isinstance(default, expect_type):
|
|
303
|
+
default = expect_type(default)
|
|
304
|
+
assert isinstance(default, expect_type)
|
|
305
|
+
return default
|
|
306
|
+
assert False, "Can't get here"
|
|
307
|
+
|
|
308
|
+
def set(self, key: str, value: object) -> None:
|
|
309
|
+
"""Set a setting value.
|
|
310
|
+
|
|
311
|
+
Args:
|
|
312
|
+
key: Key in dot notation.
|
|
313
|
+
value: New value.
|
|
314
|
+
"""
|
|
315
|
+
current_value = self.get(key, expand=False)
|
|
316
|
+
|
|
317
|
+
updated_settings = copy.deepcopy(self._settings)
|
|
318
|
+
|
|
319
|
+
setting = updated_settings
|
|
320
|
+
for last, sub_key in loop_last(parse_key(key)):
|
|
321
|
+
if last:
|
|
322
|
+
if current_value != value:
|
|
323
|
+
self._changed = True
|
|
324
|
+
self._settings = updated_settings
|
|
325
|
+
assert isinstance(setting, dict)
|
|
326
|
+
setting[sub_key] = value
|
|
327
|
+
else:
|
|
328
|
+
setting_node = setting.setdefault(sub_key, {})
|
|
329
|
+
if isinstance(setting_node, dict):
|
|
330
|
+
setting = setting_node
|
|
331
|
+
else:
|
|
332
|
+
assert isinstance(setting, dict)
|
|
333
|
+
setting[sub_key] = {}
|
|
334
|
+
setting = setting[sub_key]
|
|
335
|
+
|
|
336
|
+
if self._on_set_callback is not None:
|
|
337
|
+
self._on_set_callback(key, value)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
if __name__ == "__main__":
|
|
341
|
+
from rich import print
|
|
342
|
+
from rich.traceback import install
|
|
343
|
+
|
|
344
|
+
from toad.settings_schema import SCHEMA
|
|
345
|
+
|
|
346
|
+
install(show_locals=True, width=None)
|
|
347
|
+
|
|
348
|
+
schema = Schema(SCHEMA)
|
|
349
|
+
settings = schema.defaults
|
|
350
|
+
print(settings)
|
|
351
|
+
|
|
352
|
+
print(schema.settings_map)
|
|
353
|
+
|
|
354
|
+
print(schema.key_to_type)
|