Set up inotify watch

This commit is contained in:
hygienic-books 2022-03-21 03:32:49 +01:00
parent 3b4b017322
commit 9e58429aae

View File

@ -1,6 +1,7 @@
import os
import logging
import sys
import time
from rich.logging import RichHandler
from rich.traceback import install
import configparser
@ -21,8 +22,7 @@ class CONST(object):
CFG_DEFAULT_ABS_PATH = os.path.join(CFG_THIS_FILE_DIRNAME, CFG_DEFAULT_FILENAME)
CFG_KNOWN_DEFAULTS = [
{"key": "self_name", "value": "kodi-nfo-feeder"},
{"key": "watch_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s/watch")},
{"key": "output_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s/output")}
{"key": "watch_dir", "value": os.path.join(CFG_THIS_FILE_DIRNAME, "data/var/lib/%(self_name)s/watch")}
]
CFG_KNOWN_SECTION = [
{"key": "watch_dir", "is_mandatory": True},
@ -136,6 +136,23 @@ def validate_config_sections(
log.debug(f"{kv_prefix} {key} = {config_obj[this_section][key]}")
def setup_watch(
watch_this: str) -> INotify:
if not os.path.exists(watch_this):
os.makedirs(watch_this, exist_ok=False)
inotify = INotify()
watch_flags = flags.MOVED_TO
try:
inotify.add_watch(watch_this, watch_flags)
log.debug(f"Watching for files moved to '{watch_this}' ...")
except FileNotFoundError:
log.error(f"Watch directory '{watch_this}' does not exist. Please create it. Exiting 3 ...")
sys.exit(4)
else:
return inotify
if __name__ == '__main__':
validate_default_section(config)
if config_has_valid_section(config):
@ -149,3 +166,24 @@ if __name__ == '__main__':
log.debug(f"Iterating over config sections ...")
for section in config.sections():
log.debug(f"Processing section '[{section}]' ...")
watch_dir = config.get(section, "watch_dir")
inotify_watch = setup_watch(watch_dir)
while True:
time.sleep(0.2)
for event in inotify_watch.read():
events = [str(flags) for flags in flags.from_mask(event.mask)]
if "flags.MOVED_TO" in events:
file_name = event.name
log.info(f"File '{file_name}' was moved to watch directory '{watch_dir}', processing ...")
# is_xauth = xauth_pattern.findall(event.name)
#if is_xauth and "flags.CREATE" in events:
# log.info(f"New .Xauthority file '{event.name}' detected")
# react_to_cec = True
# xauth_abs_path = xauth_dir + "/" + event.name
# set_xauth_env(xauth_abs_path)
#elif is_xauth and "flags.DELETE" in events:
# log.info(f".Xauthority file '{event.name}' was deleted")
# react_to_cec = False