From c3daa969fa019dab8c84dd3b7a02d9d8804f864c Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 30 Oct 2021 12:20:37 +0530 Subject: [PATCH] Allow individual commands to control the response timeout --- kitty/rc/base.py | 1 + kitty/remote_control.py | 9 ++++++--- kitty/shell.py | 5 ++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/kitty/rc/base.py b/kitty/rc/base.py index c9711fc2e..b9e8af99d 100644 --- a/kitty/rc/base.py +++ b/kitty/rc/base.py @@ -116,6 +116,7 @@ class RemoteCommand: argspec: str = '...' options_spec: Optional[str] = None no_response: bool = False + response_timeout: float = 10. # seconds string_return_is_error: bool = False args_count: Optional[int] = None args_completion: Optional[Dict[str, Tuple[str, Union[Callable[[], Iterable[str]], Tuple[str, ...]]]]] = None diff --git a/kitty/remote_control.py b/kitty/remote_control.py index 1a80ccdae..1fd9c5733 100644 --- a/kitty/remote_control.py +++ b/kitty/remote_control.py @@ -114,7 +114,7 @@ class RCIO(TTYIO): return b''.join(ans) -def do_io(to: Optional[str], send: Dict[str, Any], no_response: bool) -> Dict[str, Any]: +def do_io(to: Optional[str], send: Dict[str, Any], no_response: bool, response_timeout: float) -> Dict[str, Any]: payload = send.get('payload') if not isinstance(payload, types.GeneratorType): send_data: Union[bytes, Iterable[bytes]] = encode_send(send) @@ -131,7 +131,7 @@ def do_io(to: Optional[str], send: Dict[str, Any], no_response: bool) -> Dict[st io.send(send_data) if no_response: return {'ok': True} - received = io.simple_recv(timeout=10) + received = io.simple_recv(timeout=response_timeout) return cast(Dict[str, Any], json.loads(received.decode('ascii'))) @@ -181,10 +181,13 @@ def main(args: List[str]) -> None: no_response = c.no_response if hasattr(opts, 'no_response'): no_response = opts.no_response + response_timeout = c.response_timeout + if hasattr(opts, 'response_timeout'): + response_timeout = opts.response_timeout send = create_basic_command(cmd, payload=payload, no_response=no_response) if not global_opts.to and 'KITTY_LISTEN_ON' in os.environ: global_opts.to = os.environ['KITTY_LISTEN_ON'] - response = do_io(global_opts.to, send, no_response) + response = do_io(global_opts.to, send, no_response, response_timeout) if no_response: return if not response.get('ok'): diff --git a/kitty/shell.py b/kitty/shell.py index 27e30c9f6..182495377 100644 --- a/kitty/shell.py +++ b/kitty/shell.py @@ -152,7 +152,10 @@ def run_cmd(global_opts: RCOptions, cmd: str, func: RemoteCommand, opts: Any, it } if payload is not None: send['payload'] = payload - response = do_io(global_opts.to, send, func.no_response) + response_timeout = func.response_timeout + if hasattr(opts, 'response_timeout'): + response_timeout = opts.response_timeout + response = do_io(global_opts.to, send, func.no_response, response_timeout) if not response.get('ok'): if response.get('tb'): print_err(response['tb'])