remove loop parameter (fix for python 3.10)
This commit is contained in:
@@ -36,7 +36,7 @@ from pyinotify import ProcessEvent, ExcludeFilter
|
|||||||
from pyinotifyd._install import install, uninstall
|
from pyinotifyd._install import install, uninstall
|
||||||
from pyinotifyd.scheduler import TaskScheduler, Cancel
|
from pyinotifyd.scheduler import TaskScheduler, Cancel
|
||||||
|
|
||||||
__version__ = "0.0.9"
|
__version__ = "0.0.8"
|
||||||
|
|
||||||
|
|
||||||
def setLoglevel(loglevel, logname=None):
|
def setLoglevel(loglevel, logname=None):
|
||||||
@@ -56,16 +56,15 @@ def enableSyslog(loglevel=None, address="/dev/log", logname=None):
|
|||||||
|
|
||||||
|
|
||||||
class _SchedulerList:
|
class _SchedulerList:
|
||||||
def __init__(self, schedulers=[], loop=None):
|
def __init__(self, schedulers=[]):
|
||||||
if not isinstance(schedulers, list):
|
if not isinstance(schedulers, list):
|
||||||
schedulers = [schedulers]
|
schedulers = [schedulers]
|
||||||
|
|
||||||
self._schedulers = schedulers
|
self._schedulers = schedulers
|
||||||
self._loop = (loop or asyncio.get_event_loop())
|
|
||||||
|
|
||||||
def process_event(self, event):
|
def process_event(self, event):
|
||||||
for scheduler in self._schedulers:
|
for scheduler in self._schedulers:
|
||||||
self._loop.create_task(scheduler.process_event(event))
|
asyncio.create_task(scheduler.process_event(event))
|
||||||
|
|
||||||
def schedulers(self):
|
def schedulers(self):
|
||||||
return self._schedulers
|
return self._schedulers
|
||||||
@@ -76,11 +75,10 @@ class EventMap(ProcessEvent):
|
|||||||
**pyinotify.EventsCodes.OP_FLAGS,
|
**pyinotify.EventsCodes.OP_FLAGS,
|
||||||
**pyinotify.EventsCodes.EVENT_FLAGS}
|
**pyinotify.EventsCodes.EVENT_FLAGS}
|
||||||
|
|
||||||
def my_init(self, event_map=None, default_sched=None, exclude_filter=None, loop=None,
|
def my_init(self, event_map=None, default_sched=None, exclude_filter=None,
|
||||||
logname="eventmap"):
|
logname="eventmap"):
|
||||||
self._map = {}
|
self._map = {}
|
||||||
self._exclude_filter = None
|
self._exclude_filter = None
|
||||||
self._loop = (loop or asyncio.get_event_loop())
|
|
||||||
|
|
||||||
if default_sched is not None:
|
if default_sched is not None:
|
||||||
for flag in EventMap.flags:
|
for flag in EventMap.flags:
|
||||||
@@ -108,10 +106,9 @@ class EventMap(ProcessEvent):
|
|||||||
isinstance(scheduler, Cancel):
|
isinstance(scheduler, Cancel):
|
||||||
instances.append(scheduler)
|
instances.append(scheduler)
|
||||||
else:
|
else:
|
||||||
instances.append(
|
instances.append(TaskScheduler(scheduler))
|
||||||
TaskScheduler(scheduler, loop=self._loop))
|
|
||||||
|
|
||||||
self._map[flag] = _SchedulerList(instances, loop=self._loop)
|
self._map[flag] = _SchedulerList(instances)
|
||||||
|
|
||||||
elif flag in self._map:
|
elif flag in self._map:
|
||||||
del self._map[flag]
|
del self._map[flag]
|
||||||
@@ -160,7 +157,7 @@ class EventMap(ProcessEvent):
|
|||||||
class Watch:
|
class Watch:
|
||||||
def __init__(self, path, event_map=None, default_sched=None,
|
def __init__(self, path, event_map=None, default_sched=None,
|
||||||
rec=False, auto_add=False, exclude_filter=None,
|
rec=False, auto_add=False, exclude_filter=None,
|
||||||
logname="watch", loop=None):
|
logname="watch"):
|
||||||
assert (isinstance(path, str) or isinstance(path, list)), \
|
assert (isinstance(path, str) or isinstance(path, list)), \
|
||||||
f"path: expected {type('')} or {type([])}, got {type(path)}"
|
f"path: expected {type('')} or {type([])}, got {type(path)}"
|
||||||
|
|
||||||
@@ -184,7 +181,6 @@ class Watch:
|
|||||||
self._exclude_filter = exclude_filter
|
self._exclude_filter = exclude_filter
|
||||||
|
|
||||||
logname = (logname or __name__)
|
logname = (logname or __name__)
|
||||||
self._loop = loop
|
|
||||||
|
|
||||||
self._path = path
|
self._path = path
|
||||||
self._rec = rec
|
self._rec = rec
|
||||||
@@ -200,15 +196,14 @@ class Watch:
|
|||||||
def event_map(self):
|
def event_map(self):
|
||||||
return self._event_map
|
return self._event_map
|
||||||
|
|
||||||
def start(self, loop=None):
|
def start(self):
|
||||||
loop = (loop or self._loop)
|
|
||||||
self._watch_manager.add_watch(self._path, pyinotify.ALL_EVENTS,
|
self._watch_manager.add_watch(self._path, pyinotify.ALL_EVENTS,
|
||||||
rec=self._rec, auto_add=self._auto_add,
|
rec=self._rec, auto_add=self._auto_add,
|
||||||
exclude_filter=self._exclude_filter,
|
exclude_filter=self._exclude_filter,
|
||||||
do_glob=True)
|
do_glob=True)
|
||||||
|
|
||||||
self._notifier = pyinotify.AsyncioNotifier(
|
self._notifier = pyinotify.AsyncioNotifier(
|
||||||
self._watch_manager, loop, default_proc_fun=self._event_map)
|
self._watch_manager, asyncio.get_event_loop(), default_proc_fun=self._event_map)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self._notifier.stop()
|
self._notifier.stop()
|
||||||
@@ -219,12 +214,10 @@ class Watch:
|
|||||||
class Pyinotifyd:
|
class Pyinotifyd:
|
||||||
name = "pyinotifyd"
|
name = "pyinotifyd"
|
||||||
|
|
||||||
def __init__(self, watches=[], shutdown_timeout=30, logname="daemon",
|
def __init__(self, watches=[], shutdown_timeout=30, logname="daemon"):
|
||||||
loop=None):
|
|
||||||
self.set_watches(watches)
|
self.set_watches(watches)
|
||||||
self.set_shutdown_timeout(shutdown_timeout)
|
self.set_shutdown_timeout(shutdown_timeout)
|
||||||
logname = (logname or __name__)
|
logname = (logname or __name__)
|
||||||
self._loop = (loop or asyncio.get_event_loop())
|
|
||||||
|
|
||||||
self._log = logging.getLogger(logname)
|
self._log = logging.getLogger(logname)
|
||||||
|
|
||||||
@@ -276,9 +269,7 @@ class Pyinotifyd:
|
|||||||
schedulers.extend(w.event_map().schedulers())
|
schedulers.extend(w.event_map().schedulers())
|
||||||
return list(set(schedulers))
|
return list(set(schedulers))
|
||||||
|
|
||||||
def start(self, loop=None):
|
def start(self):
|
||||||
loop = (loop or self._loop)
|
|
||||||
|
|
||||||
if len(self._watches) == 0:
|
if len(self._watches) == 0:
|
||||||
self._log.warning(
|
self._log.warning(
|
||||||
"no watches configured, the daemon will not do anything")
|
"no watches configured, the daemon will not do anything")
|
||||||
@@ -286,7 +277,7 @@ class Pyinotifyd:
|
|||||||
for watch in self._watches:
|
for watch in self._watches:
|
||||||
self._log.info(
|
self._log.info(
|
||||||
f"start listening for inotify events on '{watch.path()}'")
|
f"start listening for inotify events on '{watch.path()}'")
|
||||||
watch.start(loop)
|
watch.start()
|
||||||
|
|
||||||
def pause(self):
|
def pause(self):
|
||||||
for scheduler in self.schedulers():
|
for scheduler in self.schedulers():
|
||||||
|
|||||||
16
pyinotifyd/scheduler.py
Executable file → Normal file
16
pyinotifyd/scheduler.py
Executable file → Normal file
@@ -52,7 +52,7 @@ class TaskScheduler:
|
|||||||
self.cancelable = cancelable
|
self.cancelable = cancelable
|
||||||
|
|
||||||
def __init__(self, job, files=True, dirs=False, delay=0, logname="sched",
|
def __init__(self, job, files=True, dirs=False, delay=0, logname="sched",
|
||||||
loop=None, global_vars={}, singlejob=False):
|
global_vars={}, singlejob=False):
|
||||||
assert iscoroutinefunction(job), \
|
assert iscoroutinefunction(job), \
|
||||||
f"job: expected coroutine, got {type(job)}"
|
f"job: expected coroutine, got {type(job)}"
|
||||||
assert isinstance(files, bool), \
|
assert isinstance(files, bool), \
|
||||||
@@ -69,7 +69,6 @@ class TaskScheduler:
|
|||||||
self._dirs = dirs
|
self._dirs = dirs
|
||||||
self._delay = delay
|
self._delay = delay
|
||||||
self._log = logging.getLogger((logname or __name__))
|
self._log = logging.getLogger((logname or __name__))
|
||||||
self._loop = (loop or asyncio.get_event_loop())
|
|
||||||
self._globals = global_vars
|
self._globals = global_vars
|
||||||
self._singlejob = singlejob
|
self._singlejob = singlejob
|
||||||
self._tasks = {}
|
self._tasks = {}
|
||||||
@@ -91,8 +90,7 @@ class TaskScheduler:
|
|||||||
self._log.info(
|
self._log.info(
|
||||||
f"wait {timeout} seconds for {len(pending)} "
|
f"wait {timeout} seconds for {len(pending)} "
|
||||||
f"remaining task(s) to complete")
|
f"remaining task(s) to complete")
|
||||||
done, pending = await asyncio.wait([*pending], timeout=timeout,
|
done, pending = await asyncio.wait([*pending], timeout=timeout)
|
||||||
loop=self._loop)
|
|
||||||
if pending:
|
if pending:
|
||||||
self._log.warning(
|
self._log.warning(
|
||||||
f"shutdown timeout exceeded, "
|
f"shutdown timeout exceeded, "
|
||||||
@@ -100,7 +98,7 @@ class TaskScheduler:
|
|||||||
for task in pending:
|
for task in pending:
|
||||||
task.cancel()
|
task.cancel()
|
||||||
try:
|
try:
|
||||||
await asyncio.gather(*pending, loop=self._loop)
|
await asyncio.gather(*pending)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
@@ -115,7 +113,7 @@ class TaskScheduler:
|
|||||||
"id": task_state.id})
|
"id": task_state.id})
|
||||||
|
|
||||||
if self._delay > 0:
|
if self._delay > 0:
|
||||||
task_state.task = self._loop.create_task(
|
task_state.task = asyncio.create_task(
|
||||||
asyncio.sleep(self._delay))
|
asyncio.sleep(self._delay))
|
||||||
try:
|
try:
|
||||||
if restart:
|
if restart:
|
||||||
@@ -134,11 +132,11 @@ class TaskScheduler:
|
|||||||
local_vars = {"self": self,
|
local_vars = {"self": self,
|
||||||
"event": event,
|
"event": event,
|
||||||
"task_id": task_state.id}
|
"task_id": task_state.id}
|
||||||
task_state.task = self._loop.create_task(
|
task_state.task = asyncio.create_task(
|
||||||
eval("self._job(event, task_id)", self._globals, local_vars))
|
eval("self._job(event, task_id)", self._globals, local_vars))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
task_state.task = self._loop.create_task(
|
task_state.task = asyncio.create_task(
|
||||||
self._job(event, task_state.id))
|
self._job(event, task_state.id))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -244,7 +242,7 @@ class ShellScheduler(TaskScheduler):
|
|||||||
|
|
||||||
logger.info(f"execute shell command, cmd={cmd}")
|
logger.info(f"execute shell command, cmd={cmd}")
|
||||||
try:
|
try:
|
||||||
proc = await asyncio.create_subprocess_shell(cmd, loop=self._loop)
|
proc = await asyncio.create_subprocess_shell(cmd)
|
||||||
await proc.communicate()
|
await proc.communicate()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
|
|||||||
Reference in New Issue
Block a user