Skip to content

Latest commit

 

History

History
70 lines (50 loc) · 1.77 KB

README.md

File metadata and controls

70 lines (50 loc) · 1.77 KB

Data Watcher

The Data Watcher class is a PyQt system tray application with three main components:

  • Ability to specify a background task to run (i.e. watchdog observer)
  • Ability to manually upload a file using the context menu of the tray icon.
  • Logging display

Example Usage

Using the watchdog library combined with the FileUploadDialog class, you can create a program that watches a directory for new files and runs any tasks in the parse_file function.

You can use the pyinstaller library to create a frozen exe to distribute.

import logging
import sys
from pathlib import Path

from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer

from data_watcher import DataWatcher, FileUploadDialog

logger = logging.getLogger()
icon_path = Path("icon.png").absolute()
app_name = "Data Logger"


# Main parsing method
def parse_file(path: Path):
    logger.info(path)


class CreatedHandler(FileSystemEventHandler):
    def on_created(self, event):
        if not event.is_directory and ".txt" in event.src_path:
            parse_file(event.src_path)


def init_watcher():
    event_handler = CreatedHandler()
    observer = Observer()
    try:
        observer.schedule(event_handler, path=str(Path.cwd()), recursive=False)
        observer.start()
    except FileNotFoundError:
        logging.error('Directory not found')
    logging.info('Watcher Started')

    return observer


def upload_file():
    FileUploadDialog(
        app_name=app_name,
        on_select=parse_file,
        allowed_files_filter='Text Files (*.txt);;All Files (*)'
    )


app = DataWatcher(
    app_name=app_name,
    icon_path=icon_path,
    background_task=init_watcher,
    upload_callback=upload_file
)

sys.exit(app.start())