How to run the RegexMatchingEventHandler of Watchdog correctly?

爷,独闯天下 提交于 2020-05-31 07:41:12

问题


I'm working on a small tool for a GameApi. This Api works with .log-files. They are provided in a specific location. I want to observe this location with with watchdog and it is working fine, if I use the PatternMatchingEventHandler. But if I use the RegexMatchingEventHandler it fails. I want to use Regex because there a many .log-files and I only want to check the files from today.

Extension: I use Watchdog with the functions:

on_created
on_deleted
on_moved
on_modified

This Website shows the code I am using: https://www.thepythoncorner.com/2019/01/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/

I tested my Regex function normaly with re. This is doing absolutly fine. But even if I try the Regex Entry: ['\w+.log'] it did not work.

I provide you my Regex to understand what I want to do:

regexes = ["^Journal\.190901\d{6}\.\d{2}\.log$"]

I expected a message everytime I change one of my .log-files but this is only happend i f i use the PatternMatchingEventHAndle

EDIT: Now I present you you my minimal Example:

import time
from watchdog.observers import Observer
from watchdog.events import RegexMatchingEventHandler

if __name__ == "__main__":
    regexes = ["^Journal\.190901\d{6}\.\d{2}\.log$"]
    ignore_regexes= []
    ignore_directories = False
    case_sensitive = True
    my_event_handler = RegexMatchingEventHandler(regexes,ignore_regexes,ignore_directories,case_sensitive)

    def on_modified(event):
        print(f"hey buddy, {event.src_path} has been modified")

    my_event_handler.on_modified = on_modified

# Observer
    path = "."
    go_recursively = True
    my_observer = Observer()
    my_observer.schedule(my_event_handler, path, recursive=go_recursively)

    my_observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        my_observer.stop()
        my_observer.join()

回答1:


This is just about the pattern of the path returned by the event handler, it begins by the path to the root of the folder specified in path (path = "." in your example) .

  • It can help to inspect any patterns of the paths returned, and check what you need exactly:

    regexes = ['.+']  # will match everything 
    
  • To track files in the current directory with path = ".":

    # linux (example path : ./Journal[...].log)
    regexes = ['^\./Journal\.190901\d{6}\.\d{2}\.log$']
    
    # windows (example path : .\Journal[...].log)
    regexes = ["^\.\\\\Journal\.190901\d{6}\.\d{2}\.log$"]
    
    # both
    regexes = ["^\.(/|\\\\)Journal\.190901\d{6}\.\d{2}\.log$"]
    
  • if you define a subfolder named logs and you specify the root like path = "logs"

    # (example path : logs/Journal[...].log or logs\Journal[...].log
    regexes = ['^logs(/|\\\\)logs/Journal\.190901\d{6}\.\d{2}\.log$']
    


来源:https://stackoverflow.com/questions/57746487/how-to-run-the-regexmatchingeventhandler-of-watchdog-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!