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