mirror of
https://github.com/kovidgoyal/kitty
synced 2026-06-08 14:18:26 +02:00
Add more type annotations
This commit is contained in:
18
glfw/glfw.py
18
glfw/glfw.py
@@ -23,10 +23,9 @@ class Command(NamedTuple):
|
|||||||
desc: str
|
desc: str
|
||||||
cmd: Sequence[str]
|
cmd: Sequence[str]
|
||||||
is_newer_func: Callable[[], bool]
|
is_newer_func: Callable[[], bool]
|
||||||
on_success: Callable[[], None]
|
on_success: Callable[[], None] = lambda: None
|
||||||
key: Optional[CompileKey]
|
key: Optional[CompileKey] = None
|
||||||
keyfile: Optional[str]
|
keyfile: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Env:
|
class Env:
|
||||||
@@ -140,7 +139,13 @@ def init_env(
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def build_wayland_protocols(env: Env, Command: Callable, parallel_run: Callable, emphasis: Callable, newer: Callable, dest_dir: str) -> None:
|
def build_wayland_protocols(
|
||||||
|
env: Env,
|
||||||
|
parallel_run: Callable[[List[Command]], None],
|
||||||
|
emphasis: Callable[[str], str],
|
||||||
|
newer: Callable[..., bool],
|
||||||
|
dest_dir: str
|
||||||
|
) -> None:
|
||||||
items = []
|
items = []
|
||||||
for protocol in env.wayland_protocols:
|
for protocol in env.wayland_protocols:
|
||||||
src = os.path.join(env.wayland_packagedir, protocol)
|
src = os.path.join(env.wayland_packagedir, protocol)
|
||||||
@@ -153,8 +158,7 @@ def build_wayland_protocols(env: Env, Command: Callable, parallel_run: Callable,
|
|||||||
q = 'client-header' if ext == 'h' else env.wayland_scanner_code
|
q = 'client-header' if ext == 'h' else env.wayland_scanner_code
|
||||||
items.append(Command(
|
items.append(Command(
|
||||||
'Generating {} ...'.format(emphasis(os.path.basename(dest))),
|
'Generating {} ...'.format(emphasis(os.path.basename(dest))),
|
||||||
[env.wayland_scanner, q, src, dest],
|
[env.wayland_scanner, q, src, dest], lambda: True))
|
||||||
lambda: True, None, None, None))
|
|
||||||
if items:
|
if items:
|
||||||
parallel_run(items)
|
parallel_run(items)
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,10 @@ from typing import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from kitty.conf.types import Definition, MultiOption, Option, unset
|
from kitty.conf.types import Definition, MultiOption, Option, unset
|
||||||
|
from kitty.types import _T
|
||||||
|
|
||||||
|
|
||||||
def chunks(lst: List, n: int) -> Iterator[List]:
|
def chunks(lst: List[_T], n: int) -> Iterator[List[_T]]:
|
||||||
for i in range(0, len(lst), n):
|
for i in range(0, len(lst), n):
|
||||||
yield lst[i:i + n]
|
yield lst[i:i + n]
|
||||||
|
|
||||||
@@ -51,7 +52,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
|
|||||||
ans = ans.replace('NoneType', 'None')
|
ans = ans.replace('NoneType', 'None')
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def option_type_data(option: Union[Option, MultiOption]) -> Tuple[Callable, str]:
|
def option_type_data(option: Union[Option, MultiOption]) -> Tuple[Callable[[Any], Any], str]:
|
||||||
func = option.parser_func
|
func = option.parser_func
|
||||||
if func.__module__ == 'builtins':
|
if func.__module__ == 'builtins':
|
||||||
return func, func.__name__
|
return func, func.__name__
|
||||||
@@ -270,7 +271,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
|
|||||||
|
|
||||||
for aname, func in action_parsers.items():
|
for aname, func in action_parsers.items():
|
||||||
a(f'defaults.{aname} = [')
|
a(f'defaults.{aname} = [')
|
||||||
only: Dict[str, List[Tuple[str, Callable]]] = {}
|
only: Dict[str, List[Tuple[str, Callable[..., Any]]]] = {}
|
||||||
for sc in defn.iter_all_maps(aname):
|
for sc in defn.iter_all_maps(aname):
|
||||||
if not sc.add_to_default:
|
if not sc.add_to_default:
|
||||||
continue
|
continue
|
||||||
@@ -333,7 +334,7 @@ def generate_class(defn: Definition, loc: str) -> Tuple[str, str]:
|
|||||||
preamble = ['# generated by gen-config.py DO NOT edit', '']
|
preamble = ['# generated by gen-config.py DO NOT edit', '']
|
||||||
a = preamble.append
|
a = preamble.append
|
||||||
|
|
||||||
def output_imports(imports: Set, add_module_imports: bool = True) -> None:
|
def output_imports(imports: Set[Tuple[str, str]], add_module_imports: bool = True) -> None:
|
||||||
a('import typing')
|
a('import typing')
|
||||||
seen_mods = {'typing'}
|
seen_mods = {'typing'}
|
||||||
mmap: Dict[str, List[str]] = {}
|
mmap: Dict[str, List[str]] = {}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
from contextlib import suppress
|
from contextlib import suppress
|
||||||
from typing import (
|
from typing import (
|
||||||
TYPE_CHECKING, Any, Callable, Dict, FrozenSet, Generator, Iterable, List,
|
TYPE_CHECKING, Any, Callable, Dict, FrozenSet, Iterable, Iterator, List,
|
||||||
NoReturn, Optional, Tuple, Type, Union, cast
|
NoReturn, Optional, Tuple, Type, Union, cast
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -64,9 +64,9 @@ class PayloadGetter:
|
|||||||
|
|
||||||
no_response = NoResponse()
|
no_response = NoResponse()
|
||||||
payload_get = object()
|
payload_get = object()
|
||||||
ResponseType = Optional[Union[bool, str]]
|
ResponseType = Union[bool, str, None]
|
||||||
CmdReturnType = Union[Dict[str, Any], List, Tuple, str, int, float, bool]
|
CmdReturnType = Union[Dict[str, Any], List[Any], Tuple[Any, ...], str, int, float, bool]
|
||||||
CmdGenerator = Generator[CmdReturnType, None, None]
|
CmdGenerator = Iterator[CmdReturnType]
|
||||||
PayloadType = Optional[Union[CmdReturnType, CmdGenerator]]
|
PayloadType = Optional[Union[CmdReturnType, CmdGenerator]]
|
||||||
PayloadGetType = PayloadGetter
|
PayloadGetType = PayloadGetter
|
||||||
ArgsType = List[str]
|
ArgsType = List[str]
|
||||||
@@ -120,7 +120,7 @@ class RemoteCommand:
|
|||||||
args_count: Optional[int] = None
|
args_count: Optional[int] = None
|
||||||
args_completion: Optional[Dict[str, Tuple[str, Union[Callable[[], Iterable[str]], Tuple[str, ...]]]]] = None
|
args_completion: Optional[Dict[str, Tuple[str, Union[Callable[[], Iterable[str]], Tuple[str, ...]]]]] = None
|
||||||
defaults: Optional[Dict[str, Any]] = None
|
defaults: Optional[Dict[str, Any]] = None
|
||||||
options_class: Type = RCOptions
|
options_class: Type[RCOptions] = RCOptions
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.desc = self.desc or self.short_desc
|
self.desc = self.desc or self.short_desc
|
||||||
|
|||||||
@@ -4,15 +4,16 @@
|
|||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
from typing import TYPE_CHECKING, Dict, Generator, List, Optional, Union
|
from typing import TYPE_CHECKING, List, Optional, Union
|
||||||
|
|
||||||
from kitty.fast_data_types import KeyEvent as WindowSystemKeyEvent
|
from kitty.fast_data_types import KeyEvent as WindowSystemKeyEvent
|
||||||
from kitty.key_encoding import decode_key_event_as_window_system_key
|
from kitty.key_encoding import decode_key_event_as_window_system_key
|
||||||
from kitty.options.utils import parse_send_text_bytes
|
from kitty.options.utils import parse_send_text_bytes
|
||||||
|
|
||||||
from .base import (
|
from .base import (
|
||||||
MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, MatchError,
|
MATCH_TAB_OPTION, MATCH_WINDOW_OPTION, ArgsType, Boss, CmdGenerator,
|
||||||
PayloadGetType, PayloadType, RCOptions, RemoteCommand, ResponseType, Window
|
MatchError, PayloadGetType, PayloadType, RCOptions, RemoteCommand,
|
||||||
|
ResponseType, Window
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -63,7 +64,7 @@ Do not send text to the active window, even if it is one of the matched windows.
|
|||||||
limit = 1024
|
limit = 1024
|
||||||
ret = {'match': opts.match, 'data': '', 'match_tab': opts.match_tab, 'all': opts.all, 'exclude_active': opts.exclude_active}
|
ret = {'match': opts.match, 'data': '', 'match_tab': opts.match_tab, 'all': opts.all, 'exclude_active': opts.exclude_active}
|
||||||
|
|
||||||
def pipe() -> Generator[Dict, None, None]:
|
def pipe() -> CmdGenerator:
|
||||||
if sys.stdin.isatty():
|
if sys.stdin.isatty():
|
||||||
ret['exclude_active'] = True
|
ret['exclude_active'] = True
|
||||||
import select
|
import select
|
||||||
@@ -90,14 +91,14 @@ Do not send text to the active window, even if it is one of the matched windows.
|
|||||||
ret['data'] = 'base64:' + base64.standard_b64encode(data).decode('ascii')
|
ret['data'] = 'base64:' + base64.standard_b64encode(data).decode('ascii')
|
||||||
yield ret
|
yield ret
|
||||||
|
|
||||||
def chunks(text: str) -> Generator[Dict, None, None]:
|
def chunks(text: str) -> CmdGenerator:
|
||||||
data = parse_send_text_bytes(text).decode('utf-8')
|
data = parse_send_text_bytes(text).decode('utf-8')
|
||||||
while data:
|
while data:
|
||||||
ret['data'] = 'text:' + data[:limit]
|
ret['data'] = 'text:' + data[:limit]
|
||||||
yield ret
|
yield ret
|
||||||
data = data[limit:]
|
data = data[limit:]
|
||||||
|
|
||||||
def file_pipe(path: str) -> Generator[Dict, None, None]:
|
def file_pipe(path: str) -> CmdGenerator:
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
while True:
|
while True:
|
||||||
data = f.read(limit)
|
data = f.read(limit)
|
||||||
@@ -116,7 +117,7 @@ Do not send text to the active window, even if it is one of the matched windows.
|
|||||||
text = ' '.join(args)
|
text = ' '.join(args)
|
||||||
sources.append(chunks(text))
|
sources.append(chunks(text))
|
||||||
|
|
||||||
def chain() -> Generator[Dict, None, None]:
|
def chain() -> CmdGenerator:
|
||||||
for src in sources:
|
for src in sources:
|
||||||
yield from src
|
yield from src
|
||||||
return chain()
|
return chain()
|
||||||
|
|||||||
@@ -4,12 +4,12 @@
|
|||||||
import imghdr
|
import imghdr
|
||||||
import tempfile
|
import tempfile
|
||||||
from base64 import standard_b64decode, standard_b64encode
|
from base64 import standard_b64decode, standard_b64encode
|
||||||
from typing import IO, TYPE_CHECKING, Dict, Generator, Optional
|
from typing import IO, TYPE_CHECKING, Optional
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
from .base import (
|
from .base import (
|
||||||
MATCH_WINDOW_OPTION, ArgsType, Boss, PayloadGetType, PayloadType,
|
MATCH_WINDOW_OPTION, ArgsType, Boss, CmdGenerator, PayloadGetType,
|
||||||
RCOptions, RemoteCommand, ResponseType, Window
|
PayloadType, RCOptions, RemoteCommand, ResponseType, Window
|
||||||
)
|
)
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -83,7 +83,7 @@ failed, the command will exit with a success code.
|
|||||||
if imghdr.what(path) != 'png':
|
if imghdr.what(path) != 'png':
|
||||||
self.fatal(f'{path} is not a PNG image')
|
self.fatal(f'{path} is not a PNG image')
|
||||||
|
|
||||||
def file_pipe(path: str) -> Generator[Dict, None, None]:
|
def file_pipe(path: str) -> CmdGenerator:
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
while True:
|
while True:
|
||||||
data = f.read(512)
|
data = f.read(512)
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -748,7 +748,7 @@ def compile_glfw(compilation_database: CompilationDatabase) -> None:
|
|||||||
all_headers = [os.path.join('glfw', x) for x in genv.all_headers]
|
all_headers = [os.path.join('glfw', x) for x in genv.all_headers]
|
||||||
if module == 'wayland':
|
if module == 'wayland':
|
||||||
try:
|
try:
|
||||||
glfw.build_wayland_protocols(genv, Command, parallel_run, emphasis, newer, 'glfw')
|
glfw.build_wayland_protocols(genv, parallel_run, emphasis, newer, 'glfw')
|
||||||
except SystemExit as err:
|
except SystemExit as err:
|
||||||
print(err, file=sys.stderr)
|
print(err, file=sys.stderr)
|
||||||
print(error('Disabling building of wayland backend'), file=sys.stderr)
|
print(error('Disabling building of wayland backend'), file=sys.stderr)
|
||||||
|
|||||||
Reference in New Issue
Block a user