sway-context-manager/sway_context_manager/workspace_config_watcher.py

41 lines
1.3 KiB
Python

from asyncinotify import Inotify, Mask
from .swayipc import SwayConnection
from .interface import ContextMngrInterface
from .workspace_tree import WorkspaceTree
from pathlib import Path
class Watcher:
def __init__(
self,
filepath: Path,
sway: SwayConnection,
tree: WorkspaceTree,
interface: ContextMngrInterface,
):
self.sway = sway
self.tree = tree
self.interface = interface
self.filepath = filepath
if self.filepath.is_symlink():
# Resolve the symlink so we're tracking the actual file
self.filepath = self.filepath.resolve()
file_dir = self.filepath.parent
self.inotify = Inotify()
# Watch for changes in the file
self.watch = self.inotify.add_watch(
file_dir,
Mask.CREATE | Mask.DELETE | Mask.MOVED_TO | Mask.MOVED_FROM | Mask.MODIFY,
)
async def run(self):
async for event in self.inotify:
if event.path == self.filepath:
print(f"Config file changed, reloading", flush=True)
self.tree.load_file(str(self.filepath))
await self.tree.update_context(self.sway.connection, True)
await self.interface.on_tree_changed(self.tree)