Skip to content

Commit

Permalink
Type hint Talon cheatsheet get_list (#1850)
Browse files Browse the repository at this point in the history
  • Loading branch information
auscompgeek authored and cursorless-bot committed Sep 15, 2023
1 parent 84bcab3 commit b2ca68b
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/cheatsheet/get_list.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
import re
from collections.abc import Mapping, Sequence
from typing import Optional, TypedDict

from talon import registry

from ..conventions import get_cursorless_list_name


def get_list(name, type, descriptions=None):
class Variation(TypedDict):
spokenForm: str
description: str


class ListItemDescriptor(TypedDict):
id: str
type: str
variations: list[Variation]


def get_list(
name: str, type: str, descriptions: Optional[Mapping[str, str]] = None
) -> list[ListItemDescriptor]:
if descriptions is None:
descriptions = {}

items = get_raw_list(name)
item_dict = items if isinstance(items, dict) else {item: item for item in items}

return make_dict_readable(type, item_dict, descriptions)
return make_dict_readable(type, items, descriptions)


def get_lists(names: list[str], type: str, descriptions=None):
def get_lists(
names: Sequence[str], type: str, descriptions: Optional[Mapping[str, str]] = None
) -> list[ListItemDescriptor]:
return [item for name in names for item in get_list(name, type, descriptions)]


def get_raw_list(name):
def get_raw_list(name: str) -> Mapping[str, str]:
cursorless_list_name = get_cursorless_list_name(name)
return registry.lists[cursorless_list_name][0].copy()


def make_dict_readable(type: str, dict, descriptions=None):
if descriptions is None:
descriptions = {}

def make_dict_readable(
type: str, dict: Mapping[str, str], descriptions: Mapping[str, str]
) -> list[ListItemDescriptor]:
return [
{
"id": value,
Expand All @@ -43,7 +58,7 @@ def make_dict_readable(type: str, dict, descriptions=None):
]


def make_readable(text):
def make_readable(text: str) -> str:
text = text.replace(".", " ")
return de_camel(text).lower().capitalize()

Expand Down

0 comments on commit b2ca68b

Please sign in to comment.