Compare commits

..

No commits in common. "d3d4530cfdf91e7c31fea3274e0f6f877e1373db" and "7b8c6df478643a1e34102afc1f713b903019d49a" have entirely different histories.

View file

@ -10,7 +10,6 @@ import os
import time import time
import functools import functools
import json import json
import logging
import yaml import yaml
import click import click
@ -23,7 +22,6 @@ 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)
@ -574,7 +572,6 @@ 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]
@ -586,7 +583,8 @@ 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'
LOGGER.debug('New window: %s', json.dumps(e.ipc_data)) if debug:
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()
@ -594,7 +592,8 @@ 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)
LOGGER.debug('Tried to match %s, result: %s', cfg.match, cfg.match.reduced) if debug:
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')
@ -620,11 +619,6 @@ 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
@ -632,8 +626,7 @@ 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()
handler = window_new(runtime_data, debug=debug) runtime_data.ipc.on('window::new', window_new(runtime_data, debug=debug))
runtime_data.ipc.on('window::new', handler)
variables = { variables = {
'pid': os.getpid(), 'pid': os.getpid(),
@ -662,14 +655,9 @@ 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