Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More type fixes #86

Merged
merged 13 commits into from
Feb 4, 2024
24 changes: 12 additions & 12 deletions kalamine/cli_xkb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
import sys
import tempfile
from pathlib import Path
from typing import List
from typing import List, Dict, Union, Optional

import click

from .layout import KeyboardLayout
from .xkb_manager import WAYLAND, XKBManager
from .xkb_manager import WAYLAND, XKBManager, Index


@click.group()
def cli():
def cli() -> None:
if platform.system() != "Linux":
sys.exit("This command is only compatible with GNU/Linux, sorry.")

Expand All @@ -22,7 +22,7 @@ def cli():
@click.argument(
"filepath", type=click.Path(exists=True, dir_okay=False, path_type=Path)
)
def apply(filepath: Path):
def apply(filepath: Path) -> None:
"""Apply a Kalamine layout."""

if WAYLAND:
Expand All @@ -42,7 +42,7 @@ def apply(filepath: Path):
@click.argument(
"layouts", nargs=-1, type=click.Path(exists=True, dir_okay=False, path_type=Path)
)
def install(layouts: List[Path]):
def install(layouts: List[Path]) -> None:
"""Install a list of Kalamine layouts."""

if not layouts:
Expand All @@ -55,22 +55,22 @@ def install(layouts: List[Path]):
kb_layouts.append(layout)
kb_locales.add(layout.meta["locale"])

def xkb_install(xkb):
def xkb_install(xkb: XKBManager) -> Index:
for layout in kb_layouts:
xkb.add(layout)
index = xkb.index # gets erased with xkb.update()
xkb.clean()
xkb.update()
print()
print("Successfully installed.")
return index
return dict(index)

# EAFP (Easier to Ask Forgiveness than Permission)
try:
xkb_root = XKBManager(root=True)
xkb_index = xkb_install(xkb_root)
print(f"On XOrg, you can try the layout{'s' if len(layouts) > 1 else ''} with:")
for locale, variants in xkb_index:
for locale, variants in xkb_index.items():
for name in variants.keys():
print(f" setxkbmap {locale} -variant {name}")
print()
Expand All @@ -87,10 +87,10 @@ def xkb_install(xkb):

@cli.command()
@click.argument("mask") # [locale]/[name]
def remove(mask: str):
def remove(mask: str) -> None:
"""Remove a list of Kalamine layouts."""

def xkb_remove(root=False):
def xkb_remove(root:bool = False) -> None:
xkb = XKBManager(root=root)
xkb.clean()
for locale, variants in xkb.list(mask).items():
Expand All @@ -108,11 +108,11 @@ def xkb_remove(root=False):
@cli.command(name="list")
@click.option("-a", "--all", "all_flag", is_flag=True)
@click.argument("mask", default="*")
def list_command(mask, all_flag):
def list_command(mask: str, all_flag: bool) -> None:
"""List installed Kalamine layouts."""

for root in [True, False]:
filtered = {}
filtered: Dict[str, Union[Optional[KeyboardLayout], str]] = {} # Very weird type...
fabi1cazenave marked this conversation as resolved.
Show resolved Hide resolved

xkb = XKBManager(root=root)
layouts = xkb.list_all(mask) if all_flag else xkb.list(mask)
Expand Down