Watchdog

Watchdog

A lot of office automation operates at the level of Excel and files. I created a service/daemon that keeps track of these files. It detects changes to the files and allows you to perform various actions on top of these events. So, whenever you need a report generated from the latest Excel files, directory or database, my tool can assist you. No more manual copying of data. In fact, you could even save these files or synchronize them with a database.

Sounds trivial, right? Well, I have actually helped implement such a tool for BMW.

Code

Below you can see a short code snippet

 
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
 
    try:
        from colorama import Fore
        import time
        import watchdog
        import watchdog.events
        import watchdog.observers
 
    except (ImportError, ModuleNotFoundError) as exception:
        raise exception
 
 
    class Watcher:
        def __init__(self, path):
            self.observer = watchdog.observers.Observer()
            self.path = path
 
        def run(self, queue):
            event_handler = Handler(queue=queue)
            self.observer.schedule(event_handler, self.path, recursive=True)
            self.observer.start()
            try:
                while True:
                    time.sleep(1)
            except (Exception,):
                self.observer.stop()
            self.observer.join()
 
 
    class Handler(watchdog.events.PatternMatchingEventHandler):
        def __init__(self, queue):
            watchdog.events.PatternMatchingEventHandler.__init__(
                self,
                patterns=["*.csv"],
                ignore_directories=True,
                case_sensitive=False,
            )
            self.queue = queue
 
        def on_any_event(self, event):
            if event.event_type == ("created" or "modified"):
                print(Fore.YELLOW, "\003 DETECTED CHANGES IN: ", event.src_path)
                self.queue.put(event.src_path)

Get in touch with me via


August 10, 2023
finished
Watchdog, service, daemon, Excel files, directories, databases, automated actions, report generation