move config loading metho into Pyinotifyd class
This commit is contained in:
@@ -14,6 +14,11 @@
|
|||||||
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
|
# along with pyinotifyd. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
"Pyinotifyd",
|
||||||
|
"daemon_from_config",
|
||||||
|
]
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
@@ -28,6 +33,8 @@ __version__ = "0.0.2"
|
|||||||
|
|
||||||
|
|
||||||
class Pyinotifyd:
|
class Pyinotifyd:
|
||||||
|
name = "pyinotifyd"
|
||||||
|
|
||||||
def __init__(self, watches=[], shutdown_timeout=30, logname="daemon"):
|
def __init__(self, watches=[], shutdown_timeout=30, logname="daemon"):
|
||||||
self.set_watches(watches)
|
self.set_watches(watches)
|
||||||
self.set_shutdown_timeout(shutdown_timeout)
|
self.set_shutdown_timeout(shutdown_timeout)
|
||||||
@@ -35,6 +42,21 @@ class Pyinotifyd:
|
|||||||
self._log = logging.getLogger(logname)
|
self._log = logging.getLogger(logname)
|
||||||
self._loop = asyncio.get_event_loop()
|
self._loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_cfg_file(config_file):
|
||||||
|
config = {}
|
||||||
|
name = Pyinotifyd.name
|
||||||
|
exec(f"from {name} import Pyinotifyd", {}, config)
|
||||||
|
exec(f"from {name}.scheduler import *", {}, config)
|
||||||
|
exec(f"from {name}.watch import EventMap, Watch", {}, config)
|
||||||
|
with open(config_file, "r") as fh:
|
||||||
|
exec(fh.read(), {}, config)
|
||||||
|
instance = config[f"{name}"]
|
||||||
|
assert isinstance(instance, Pyinotifyd), \
|
||||||
|
f"{name}: expected {type(Pyinotifyd)}, " \
|
||||||
|
f"got {type(instance)}"
|
||||||
|
return instance
|
||||||
|
|
||||||
def set_watches(self, watches):
|
def set_watches(self, watches):
|
||||||
if not isinstance(watches, list):
|
if not isinstance(watches, list):
|
||||||
watches = [watches]
|
watches = [watches]
|
||||||
@@ -70,26 +92,13 @@ class Pyinotifyd:
|
|||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
for watch in self._watches:
|
for watch in self._watches:
|
||||||
self._log.info(f"stop listening for inotify events on '{watch.path()}'")
|
self._log.info(
|
||||||
|
f"stop listening for inotify events on '{watch.path()}'")
|
||||||
watch.stop()
|
watch.stop()
|
||||||
|
|
||||||
return self._shutdown_timeout
|
return self._shutdown_timeout
|
||||||
|
|
||||||
|
|
||||||
def get_pyinotifyd_from_config(name, config_file):
|
|
||||||
config = {}
|
|
||||||
exec(f"from {name} import Pyinotifyd", {}, config)
|
|
||||||
exec(f"from {name}.scheduler import *", {}, config)
|
|
||||||
exec(f"from {name}.watch import EventMap, Watch", {}, config)
|
|
||||||
with open(config_file, "r") as c:
|
|
||||||
exec(c.read(), {}, config)
|
|
||||||
daemon = config[f"{name}"]
|
|
||||||
assert isinstance(daemon, Pyinotifyd), \
|
|
||||||
f"{name}: expected {type(Pyinotifyd)}, " \
|
|
||||||
f"got {type(daemon)}"
|
|
||||||
return daemon
|
|
||||||
|
|
||||||
|
|
||||||
class DaemonInstance:
|
class DaemonInstance:
|
||||||
def __init__(self, instance, logname="daemon"):
|
def __init__(self, instance, logname="daemon"):
|
||||||
self._instance = instance
|
self._instance = instance
|
||||||
@@ -147,17 +156,18 @@ class DaemonInstance:
|
|||||||
self._shutdown = False
|
self._shutdown = False
|
||||||
self._log.info("shutdown complete")
|
self._log.info("shutdown complete")
|
||||||
|
|
||||||
async def reload(self, signame, name, config, debug=False):
|
async def reload(self, signame, config_file, debug=False):
|
||||||
if self._shutdown:
|
if self._shutdown:
|
||||||
self._log.info(
|
self._log.info(
|
||||||
f"got signal {signame}, but shutdown already in progress")
|
f"got signal {signame}, but shutdown already in progress")
|
||||||
return
|
return
|
||||||
|
|
||||||
self._log.info(f"got signal {signame}, reload config")
|
self._log.info(f"got signal {signame}, reload config file")
|
||||||
try:
|
try:
|
||||||
instance = get_pyinotifyd_from_config(name, config)
|
instance = Pyinotifyd.from_cfg_file(config_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.exception(f"unable to reload config '{config}': {e}")
|
logging.exception(
|
||||||
|
f"unable to reload config file '{config_file}': {e}")
|
||||||
else:
|
else:
|
||||||
if debug:
|
if debug:
|
||||||
logging.getLogger().setLevel(logging.DEBUG)
|
logging.getLogger().setLevel(logging.DEBUG)
|
||||||
@@ -167,7 +177,7 @@ class DaemonInstance:
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
myname = "pyinotifyd"
|
myname = Pyinotifyd.name
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description=myname,
|
description=myname,
|
||||||
@@ -242,7 +252,7 @@ def main():
|
|||||||
sys.exit(uninstall(myname))
|
sys.exit(uninstall(myname))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
pyinotifyd = get_pyinotifyd_from_config(myname, args.config)
|
pyinotifyd = Pyinotifyd.from_cfg_file(args.config)
|
||||||
daemon = DaemonInstance(pyinotifyd)
|
daemon = DaemonInstance(pyinotifyd)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if args.debug:
|
if args.debug:
|
||||||
|
|||||||
Reference in New Issue
Block a user