Skip to content

Commit 40ff80c

Browse files
authored
Watch patterns (#598)
* CLI watcher support include, exclude patterns. Fixes: #594 * Update contributors * Fix accidental change to CONTRIBUTORS
1 parent a30b787 commit 40ff80c

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

CONTRIBUTORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ of those changes to CLEARTYPE SRL.
6262
| [@h3nnn4n](https://github.com/h3nnn4n/) | Renan S Silva |
6363
| [@DiegoPomares](https://github.com/DiegoPomares/) | Diego Pomares |
6464
| [@pahrohfit](https://github.com/pahrohfit/) | Robert Dailey |
65+
| [@nhairs](https://github.com/nhairs) | Nicholas Hairs |

docs/source/changelog.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ Fixed
1818
.. _#593: https://github.com/Bogdanp/dramatiq/pull/593
1919
.. _@pahrohfit: https://github.com/pahrohfit
2020

21+
Added
22+
^^^^^
23+
24+
* CLI watcher now supports setting include and exclude patterns (`#594`_,
25+
`@nhairs`_)
26+
27+
.. _#594: https://github.com/Bogdanp/dramatiq/issues/594
28+
.. _@nhairs: https://github.com/nhairs
29+
2130
`1.15.0`_ -- 2023-10-23
2231
-----------------------
2332

dramatiq/cli.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,24 @@ def make_argument_parser():
213213
"system-dependent filesystem event emitter"
214214
)
215215
)
216+
parser.add_argument(
217+
"-i",
218+
"--watch-include",
219+
action="append",
220+
dest="include_patterns",
221+
default=["**.py"],
222+
help=(
223+
"Patterns to include when watching for changes. "
224+
"Always includes all python files (*.py)."
225+
),
226+
)
227+
parser.add_argument(
228+
"-x",
229+
"--watch-exclude",
230+
action="append",
231+
dest="exclude_patterns",
232+
help="Patterns to ignore when watching for changes",
233+
)
216234

217235
parser.add_argument("--version", action="version", version=__version__)
218236
parser.add_argument("--verbose", "-v", action="count", default=0, help="turn on verbose log output")
@@ -536,7 +554,9 @@ def main(args=None): # noqa
536554
if HAS_WATCHDOG and args.watch:
537555
if not hasattr(signal, "SIGHUP"):
538556
raise RuntimeError("Watching for source changes is not supported on %s." % sys.platform)
539-
file_watcher = setup_file_watcher(args.watch, args.watch_use_polling)
557+
file_watcher = setup_file_watcher(
558+
args.watch, args.watch_use_polling, args.include_patterns, args.exclude_patterns
559+
)
540560

541561
log_watcher_stop_event = Event()
542562
log_watcher = Thread(

dramatiq/watcher.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
EVENTED_OBSERVER = watchdog.observers.Observer
1414

1515

16-
def setup_file_watcher(path, use_polling=False):
16+
def setup_file_watcher(path, use_polling=False, include_patterns=None, exclude_patterns=None):
1717
"""Sets up a background thread that watches for source changes and
1818
automatically sends SIGHUP to the current process whenever a file
1919
changes.
@@ -23,7 +23,12 @@ def setup_file_watcher(path, use_polling=False):
2323
else:
2424
observer_class = EVENTED_OBSERVER
2525

26-
file_event_handler = _SourceChangesHandler(patterns=["*.py"])
26+
if include_patterns is None:
27+
include_patterns = ["*.py"]
28+
29+
file_event_handler = _SourceChangesHandler(
30+
patterns=include_patterns, ignore_patterns=exclude_patterns
31+
)
2732
file_watcher = observer_class()
2833
file_watcher.schedule(file_event_handler, path, recursive=True)
2934
file_watcher.start()

0 commit comments

Comments
 (0)