Skip to content

Commit

Permalink
Watch patterns (#598)
Browse files Browse the repository at this point in the history
* CLI watcher support include, exclude patterns.

Fixes: #594

* Update contributors

* Fix accidental change to CONTRIBUTORS
  • Loading branch information
nhairs authored Jan 13, 2024
1 parent a30b787 commit 40ff80c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ of those changes to CLEARTYPE SRL.
| [@h3nnn4n](https://github.com/h3nnn4n/) | Renan S Silva |
| [@DiegoPomares](https://github.com/DiegoPomares/) | Diego Pomares |
| [@pahrohfit](https://github.com/pahrohfit/) | Robert Dailey |
| [@nhairs](https://github.com/nhairs) | Nicholas Hairs |
9 changes: 9 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ Fixed
.. _#593: https://github.com/Bogdanp/dramatiq/pull/593
.. _@pahrohfit: https://github.com/pahrohfit

Added
^^^^^

* CLI watcher now supports setting include and exclude patterns (`#594`_,
`@nhairs`_)

.. _#594: https://github.com/Bogdanp/dramatiq/issues/594
.. _@nhairs: https://github.com/nhairs

`1.15.0`_ -- 2023-10-23
-----------------------

Expand Down
22 changes: 21 additions & 1 deletion dramatiq/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,24 @@ def make_argument_parser():
"system-dependent filesystem event emitter"
)
)
parser.add_argument(
"-i",
"--watch-include",
action="append",
dest="include_patterns",
default=["**.py"],
help=(
"Patterns to include when watching for changes. "
"Always includes all python files (*.py)."
),
)
parser.add_argument(
"-x",
"--watch-exclude",
action="append",
dest="exclude_patterns",
help="Patterns to ignore when watching for changes",
)

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

log_watcher_stop_event = Event()
log_watcher = Thread(
Expand Down
9 changes: 7 additions & 2 deletions dramatiq/watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
EVENTED_OBSERVER = watchdog.observers.Observer


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

file_event_handler = _SourceChangesHandler(patterns=["*.py"])
if include_patterns is None:
include_patterns = ["*.py"]

file_event_handler = _SourceChangesHandler(
patterns=include_patterns, ignore_patterns=exclude_patterns
)
file_watcher = observer_class()
file_watcher.schedule(file_event_handler, path, recursive=True)
file_watcher.start()
Expand Down

0 comments on commit 40ff80c

Please sign in to comment.