From dd93754295dfe6362465b60a5b4b7d23874425f3 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 31 Jul 2024 18:33:45 +0000 Subject: [PATCH] Add logic that detects test scans and real scans I just don't do anything with them yet. --- scan_uploader/__init__.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/scan_uploader/__init__.py b/scan_uploader/__init__.py index ccffde4..2d722d6 100644 --- a/scan_uploader/__init__.py +++ b/scan_uploader/__init__.py @@ -1,2 +1,31 @@ +import argparse +import inotify.adapters +import logging +from pathlib import Path + +LOGGER = logging.getLogger(__name__) + def main(): - print("hey there") + parser = argparse.ArgumentParser() + parser.add_argument("--verbose", "-v", action="store_true", help="Enable verbose logging.") + args = parser.parse_args() + + logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) + try: + run(Path("/mnt/shares/scans")) + except KeyboardInterrupt: + LOGGER.info("Quitting") + +def run(path: Path) -> None: + i = inotify.adapters.Inotify() + i.add_watch(str(path)) + for event in i.event_gen(yield_nones=False): + (_, type_names, path, filename) = event + if "IN_CLOSE_WRITE" in type_names: + _handle_write_complete(Path(filename)) + +def _handle_write_complete(path: Path) -> None: + if path.suffix == ".test": + LOGGER.info("Ignoring scanner test write '%s'", path) + return + LOGGER.info("Pretend I uploaded %s", path)