From 75fe2d251bc07d947e3d52ba14b357da41ebaa76 Mon Sep 17 00:00:00 2001 From: yunik maharjan Date: Sun, 22 Aug 2021 18:56:56 +0545 Subject: [PATCH] Convert regexes of type str to list (#831) * convert regexes of type str to list * > tests added > updated changelog.rst --- changelog.rst | 4 ++-- src/watchdog/events.py | 2 ++ tests/test_regex_matching_event_handler.py | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/changelog.rst b/changelog.rst index c16de1df..abc1cb0b 100644 --- a/changelog.rst +++ b/changelog.rst @@ -8,8 +8,8 @@ Changelog 2021-xx-xx • `full history `__ -- -- Thanks to our beloved contributors: @ +- convert regexes of type str to list. (`#831 `_) +- Thanks to our beloved contributors: @unique1o1 2.1.4 ~~~~~ diff --git a/src/watchdog/events.py b/src/watchdog/events.py index 2682366d..a3baab6b 100755 --- a/src/watchdog/events.py +++ b/src/watchdog/events.py @@ -418,6 +418,8 @@ def __init__(self, regexes=None, ignore_regexes=None, if regexes is None: regexes = [r".*"] + elif isinstance(regexes, str): + regexes = [regexes] if ignore_regexes is None: ignore_regexes = [] if case_sensitive: diff --git a/tests/test_regex_matching_event_handler.py b/tests/test_regex_matching_event_handler.py index b265f088..811edf38 100644 --- a/tests/test_regex_matching_event_handler.py +++ b/tests/test_regex_matching_event_handler.py @@ -35,6 +35,7 @@ path_1 = '/path/xyz' path_2 = '/path/abc' g_allowed_regexes = [r".*\.py", r".*\.txt"] +g_allowed_str_regexes = r".*\.py" g_ignore_regexes = [r".*\.pyc"] @@ -186,6 +187,12 @@ def test_regexes(): assert [r.pattern for r in handler1.regexes] == g_allowed_regexes +def test_str_regexes(): + handler1 = RegexMatchingEventHandler(g_allowed_str_regexes, + g_ignore_regexes, True) + assert [r.pattern for r in handler1.regexes] == [g_allowed_str_regexes] + + def test_logging_event_handler_dispatch(): class _TestableEventHandler(LoggingEventHandler):