Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If the pattern contains "[]" square brackets in it, no event was triggered in PatternMatchingEventHandler #991

Open
hariharan605 opened this issue Jun 5, 2023 · 1 comment

Comments

@hariharan605
Copy link

hariharan605 commented Jun 5, 2023

Hello people,
I came across a strange issue. I'm using PatternMatchingEventHandler class to detect any changes occurring in a certain path.

If the pattern contains "[]" in it, no event was triggered. Please provide me any workaround or a solution on this.

Sample pattern : C:\Users\Test\AppData\Local\zwd\data\files\647971177\74j1a3a8020fba7934ecfb6579acd2de88c8c\[test].txt

Watchdog version : 0.9.0

Platform: Windows OS

@pree-dew
Copy link

pree-dew commented Jul 7, 2023

I tried to reproduce the issue, I was able to reproduce this by using the underlying library (pathlib) used by PatternMatchingEventHandler.
pathlib library treat square bracket([]) as a pattern specified for allowing certain characters/letters etc. Ref: https://github.com/python/cpython/blob/003ba71dcbe94f0d5cb1d0c56d7f1d5a6dae56f7/Lib/fnmatch.py#L26

Adding an example:

>>> from pathlib import PurePosixPath
>>> PurePosixPath("a/b.py").match("[ab].py")
True
>>> PurePosixPath("a/a.py").match("[ab].py")
True
>>> PurePosixPath("a/ab.py").match("[ab].py")
False

Syntax of PurePosixPath: PurePosixPath(path).match(pattern)

As per that the pattern specified C:\Users\Test\AppData\Local\zwd\data\files\647971177\74j1a3a8020fba7934ecfb6579acd2de88c8c\[test].txt will trigger event for these scenarios:

C:\Users\Test\AppData\Local\zwd\data\files\647971177\74j1a3a8020fba7934ecfb6579acd2de88c8c\t.txt
C:\Users\Test\AppData\Local\zwd\data\files\647971177\74j1a3a8020fba7934ecfb6579acd2de88c8c\e.txt
C:\Users\Test\AppData\Local\zwd\data\files\647971177\74j1a3a8020fba7934ecfb6579acd2de88c8c\s.txt

Suggested Approach

  • If the use case is that square bracket ([]) will be part of the filename then the following pattern can be used:
>>> from pathlib import PurePosixPath
>>> PurePosixPath("a/[ab].py").match("[[]ab[]].py")
True
  • If the use case is that square bracket ([]) should match along with character matching functionality of pathlib then the following pattern can be used:
>>> from pathlib import PurePosixPath
>>> PurePosixPath("a/[a].py").match("[[][ab][]].py")
True
>>> PurePosixPath("a/[b].py").match("[[][ab][]].py")
True
>>> PurePosixPath("a/[ab].py").match("[[][ab][]].py")
False

Feel free to add details if there is a gap in understanding the use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants