Compare commits

...

2 commits

Author SHA1 Message Date
d3d4530cfd
Decrease final_workspace_delay from 1s to 100ms. 2022-12-13 16:40:14 +01:00
4e1e241bf1
Switch to logging module.
Add hint that it might be necessary to increase the timeout
if programs are left after execution.

Turn off the new window callback to avoid asyncio exceptions.
2022-12-13 16:35:14 +01:00

View file

@ -10,6 +10,7 @@ import os
import time import time
import functools import functools
import json import json
import logging
import yaml import yaml
import click import click
@ -22,6 +23,7 @@ try:
except ImportError: except ImportError:
from yaml import SafeLoader from yaml import SafeLoader
LOGGER = logging.getLogger('i3toolwait' if __name__ == '__main__' else __name__)
def lazy_fc_if(env, local, a, b, c): def lazy_fc_if(env, local, a, b, c):
a.reduce(env, local) a.reduce(env, local)
@ -572,6 +574,7 @@ class Config(pydantic.BaseModel):
init: typing.Optional[Filter] = None init: typing.Optional[Filter] = None
programs: typing.List[ProgramConfig] programs: typing.List[ProgramConfig]
final_workspace: typing.Optional[str] = None final_workspace: typing.Optional[str] = None
final_workspace_delay: int = 100
class RuntimeData(pydantic.BaseModel): class RuntimeData(pydantic.BaseModel):
init: typing.Optional[str] init: typing.Optional[str]
@ -583,8 +586,7 @@ class RuntimeData(pydantic.BaseModel):
def window_new(runtime_data: RuntimeData, *, debug): def window_new(runtime_data: RuntimeData, *, debug):
async def callback(ipc: i3ipc.aio.Connection, e: i3ipc.WorkspaceEvent): async def callback(ipc: i3ipc.aio.Connection, e: i3ipc.WorkspaceEvent):
assert e.change == 'new' assert e.change == 'new'
if debug: LOGGER.debug('New window: %s', json.dumps(e.ipc_data))
print(json.dumps(e.ipc_data))
async with runtime_data.lock: async with runtime_data.lock:
env = Environment(e.ipc_data) env = Environment(e.ipc_data)
local = LocalEnvironment() local = LocalEnvironment()
@ -592,8 +594,7 @@ def window_new(runtime_data: RuntimeData, *, debug):
parse(runtime_data.init).reduce(env, local) parse(runtime_data.init).reduce(env, local)
for i, cfg in enumerate(runtime_data.programs): for i, cfg in enumerate(runtime_data.programs):
cfg.match.reduce(env, local) cfg.match.reduce(env, local)
if debug: LOGGER.debug('Tried to match %s, result: %s', cfg.match, cfg.match.reduced)
print(i, cfg.match, cfg.match.reduced)
if cfg.match.reduced: if cfg.match.reduced:
container_id = e.ipc_data['container']['id'] container_id = e.ipc_data['container']['id']
await ipc.command(f'for_window [con_id="{container_id}"] focus') await ipc.command(f'for_window [con_id="{container_id}"] focus')
@ -619,6 +620,11 @@ async def init(config: Config, *, debug: bool) -> RuntimeData:
event=Event(), event=Event(),
ipc=Connection(), ipc=Connection(),
) )
logging.basicConfig(level=logging.WARNING)
if debug:
LOGGER.setLevel(logging.DEBUG)
else:
LOGGER.setLevel(logging.INFO)
if config.signal is not None: if config.signal is not None:
asyncio.get_running_loop().add_signal_handler(config.signal, lambda: rd.event.set()) asyncio.get_running_loop().add_signal_handler(config.signal, lambda: rd.event.set())
return rd return rd
@ -626,7 +632,8 @@ async def init(config: Config, *, debug: bool) -> RuntimeData:
async def run(config: Config, *, debug: bool): async def run(config: Config, *, debug: bool):
runtime_data = await init(config, debug=debug) runtime_data = await init(config, debug=debug)
await runtime_data.ipc.connect() await runtime_data.ipc.connect()
runtime_data.ipc.on('window::new', window_new(runtime_data, debug=debug)) handler = window_new(runtime_data, debug=debug)
runtime_data.ipc.on('window::new', handler)
variables = { variables = {
'pid': os.getpid(), 'pid': os.getpid(),
@ -655,9 +662,14 @@ async def run(config: Config, *, debug: bool):
new_timeout = max(timeout - diff, 0) new_timeout = max(timeout - diff, 0)
await asyncio.wait_for(runtime_data.ipc.main(), timeout=new_timeout/1000) await asyncio.wait_for(runtime_data.ipc.main(), timeout=new_timeout/1000)
except asyncio.TimeoutError: except asyncio.TimeoutError:
runtime_data.ipc.off(handler)
if runtime_data.programs:
LOGGER.debug('Not all programs consumed: %s', runtime_data.programs)
LOGGER.debug('Maybe the timeouts are too short?')
return 1 return 1
finally: finally:
if config.final_workspace is not None: if config.final_workspace is not None:
await asyncio.sleep(config.final_workspace_delay/1000)
await runtime_data.ipc.command(f'workspace {config.final_workspace}') await runtime_data.ipc.command(f'workspace {config.final_workspace}')
return 0 return 0