Modernize code
* Add pyproject.toml * Remove pylint/flake8 linting configuration * Add ruff configuration * Multiple code refactor * Add prints when watch option is enable
This commit is contained in:
parent
ce74e6135e
commit
933dea2b9e
6 changed files with 197 additions and 179 deletions
16
watch.py
16
watch.py
|
|
@ -68,15 +68,15 @@ class Watcher:
|
|||
libc_path = 'libc.so.6'
|
||||
self._instance = ctypes.cdll.LoadLibrary(libc_path)
|
||||
|
||||
self._inotify_init: ctypes.CDLL._FuncPtr = self._instance.inotify_init
|
||||
self._inotify_init = self._instance.inotify_init
|
||||
self._inotify_init.argtypes = []
|
||||
self._inotify_init.restype = self._check_nonnegative
|
||||
|
||||
self._inotify_add_watch: ctypes.CDLL._FuncPtr = self._instance.inotify_add_watch
|
||||
self._inotify_add_watch = self._instance.inotify_add_watch
|
||||
self._inotify_add_watch.argtypes = [ctypes.c_int, ctypes.c_char_p, ctypes.c_uint32]
|
||||
self._inotify_add_watch.restype = self._check_nonnegative
|
||||
|
||||
self._inotify_rm_watch: ctypes.CDLL._FuncPtr = self._instance.inotify_rm_watch
|
||||
self._inotify_rm_watch = self._instance.inotify_rm_watch
|
||||
self._inotify_rm_watch.argtypes = [ctypes.c_int, ctypes.c_int]
|
||||
self._inotify_rm_watch.restype = self._check_nonnegative
|
||||
|
||||
|
|
@ -105,17 +105,17 @@ class Watcher:
|
|||
def watch(self):
|
||||
while True:
|
||||
results = self._poll.poll()
|
||||
for poll_fd, _event in results:
|
||||
for poll_fd, _ in results:
|
||||
watch_buffer = os.read(poll_fd, self._EVENT_SIZE)
|
||||
if len(watch_buffer) < self._EVENT_SIZE:
|
||||
continue
|
||||
watch_fd, mask, _cookie, _namesize = unpack(self._EVENT_FORMAT, watch_buffer)
|
||||
watch_fd, mask, _, _ = unpack(self._EVENT_FORMAT, watch_buffer)
|
||||
watch_path, callback = self._watch_info[watch_fd]
|
||||
callback(watch_path, WatchFlag(mask))
|
||||
|
||||
def __del__(self):
|
||||
self._poll.unregister(self._inotify_fd)
|
||||
for _watch_path, watch_fd in self._watch_fds.items():
|
||||
for watch_fd in self._watch_fds.values():
|
||||
self._inotify_rm_watch(self._inotify_fd, watch_fd)
|
||||
os.close(self._inotify_fd)
|
||||
|
||||
|
|
@ -127,7 +127,7 @@ class Watcher:
|
|||
|
||||
|
||||
def main():
|
||||
from argparse import ArgumentParser # pylint: disable=import-outside-toplevel
|
||||
from argparse import ArgumentParser # ruff:ignore[import-outside-top-level]
|
||||
|
||||
parser = ArgumentParser()
|
||||
parser.add_argument('paths', nargs='*', type=Path)
|
||||
|
|
@ -135,7 +135,7 @@ def main():
|
|||
|
||||
paths: list[Path] = arguments.paths
|
||||
|
||||
def callback(path: Path, _flags: WatchFlag):
|
||||
def callback(path: Path, _: WatchFlag):
|
||||
print(f'{path} has been modified')
|
||||
|
||||
watcher = Watcher()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue