Minor tweaks for typechecking purposes

This commit is contained in:
Cian Hughes
2023-11-14 12:23:38 +00:00
parent 9bacdebac4
commit 949bef9671
2 changed files with 32 additions and 18 deletions

View File

@@ -26,7 +26,7 @@ def cli_spinner(*spinner_args, **spinner_kwargs) -> Callable:
def decorator(f: Callable) -> Callable: def decorator(f: Callable) -> Callable:
# Indent the spinner to match its nesting level # Indent the spinner to match its nesting level
indent = len(inspect.stack()) - 1 indent = len(inspect.stack()) - 1
spinner_kwargs["indent"] = f"{""*indent}" spinner_kwargs["indent"] = f"{''*indent}" if indent else "├► "
@wraps(f) @wraps(f)
def wrapped(*func_args, **func_kwargs): def wrapped(*func_args, **func_kwargs):

View File

@@ -19,6 +19,15 @@ def _no_hotkey() -> Callable[[ft.KeyboardEvent], None]:
return dummy_func return dummy_func
def optional_num_floordiv(num: ft.OptionalNumber, denom: int) -> Optional[int]:
if num is None:
return None
elif isinstance(num, int):
return num // denom
else:
return int(num / denom)
HOTKEY_MAP: DefaultDict[str, Callable[[ft.KeyboardEvent], None]] = defaultdict(_no_hotkey) HOTKEY_MAP: DefaultDict[str, Callable[[ft.KeyboardEvent], None]] = defaultdict(_no_hotkey)
@@ -45,40 +54,45 @@ def main(page: ft.Page) -> None:
# These fields are used to get the parameters for the disk creation # These fields are used to get the parameters for the disk creation
disk, dd_element = disk_dropdown(tooltip="Select the disk to write to", label="Disk") disk, dd_element = disk_dropdown(tooltip="Select the disk to write to", label="Disk")
hostname = ft.TextField( hostname = ft.TextField(
value="host", label="Hostname", text_align=ft.TextAlign.LEFT, width=page.window_width // 3 value="host",
label="Hostname",
text_align=ft.TextAlign.LEFT,
width=optional_num_floordiv(page.window_width, 3)
) )
password = ft.TextField( password = ft.TextField(
label="Password", label="Password",
password=True, password=True,
can_reveal_password=True, can_reveal_password=True,
text_align=ft.TextAlign.LEFT, text_align=ft.TextAlign.LEFT,
width=page.window_width // 3, width=hostname.width,
) )
switch_ip = ft.TextField( switch_ip = ft.TextField(
label="Switch IP", text_align=ft.TextAlign.LEFT, width=page.window_width // 3 label="Switch IP", text_align=ft.TextAlign.LEFT, width=hostname.width
) )
switch_port = ft.TextField( switch_port = ft.TextField(
label="Switch Port", label="Switch Port",
value="4789", value="4789",
text_align=ft.TextAlign.LEFT, text_align=ft.TextAlign.LEFT,
width=page.window_width // 3, width=hostname.width,
) )
swarm_token = ft.TextField( swarm_token = ft.TextField(
label="Swarm Token", text_align=ft.TextAlign.LEFT, width=int((2 * page.window_width) // 3) label="Swarm Token",
text_align=ft.TextAlign.LEFT,
width=(2*hostname.width) if hostname.width else None,
) )
# Add varnames, as they will be useful for unpacking later # Map varnames, as they will be useful for unpacking later
disk.__varname__ = "disk" varnames = {
hostname.__varname__ = "hostname" disk: "disk",
password.__varname__ = "password" hostname: "hostname",
switch_ip.__varname__ = "switch_ip" password: "password",
switch_port.__varname__ = "switch_port" switch_ip: "switch_ip",
swarm_token.__varname__ = "swarm_token" switch_port: "switch_port",
swarm_token: "swarm_token",
}
# This wrapper validates the value of the field before passing it to the function # This wrapper validates the value of the field before passing it to the function
def validate_value[ def validate_value[F](func: Callable[[], F]) -> Callable[
F
](func: Callable[[], F]) -> Callable[
[], Optional[F] [], Optional[F]
]: # mypy PEP 695 support can't come quickly enough # noqa ]: # mypy PEP 695 support can't come quickly enough # noqa
#! It is important that bool(F) evaluates to False if the value is invalid #! It is important that bool(F) evaluates to False if the value is invalid
@@ -150,7 +164,7 @@ def main(page: ft.Page) -> None:
invalid_values = False invalid_values = False
for field, fetch_func in field_fetch_map: for field, fetch_func in field_fetch_map:
value = fetch_func() value = fetch_func()
varname: str = str(field.__varname__) varname: str = varnames[field]
if varname in CreateDiskArgs.__annotations__.keys(): if varname in CreateDiskArgs.__annotations__.keys():
target_type = CreateDiskArgs.__annotations__[varname] target_type = CreateDiskArgs.__annotations__[varname]
else: else:
@@ -242,7 +256,7 @@ def main(page: ft.Page) -> None:
disk_creation_dialog = ft.FilledButton( disk_creation_dialog = ft.FilledButton(
text="Create Ignition Disk", text="Create Ignition Disk",
on_click=confirm_disk_creation, on_click=confirm_disk_creation,
width=page.window_width // 3, width=hostname.width,
) )
# Then, we arrange the fields into rows and columns # Then, we arrange the fields into rows and columns