Skip to content

Commit

Permalink
support setting output verbosity
Browse files Browse the repository at this point in the history
This changes the internal use of echo to use a logger instead of sys.stdout.write.

This uses the stdlib logging library, with the common convention of logger = logging.getLogger(__name__), so that all watchdog logs are under the "watchdog" logging context.

Finally, this adds -q/--quiet and -v/--verbose command line options to watchmedo.
  • Loading branch information
taleinat committed May 13, 2022
1 parent a60ab4f commit c9793cf
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
16 changes: 11 additions & 5 deletions src/watchdog/tricks/__init__.py
Expand Up @@ -41,6 +41,8 @@
"""

import functools
import logging
import os
import signal
import subprocess
Expand All @@ -50,6 +52,10 @@
from watchdog.events import PatternMatchingEventHandler


logger = logging.getLogger(__name__)
echo_events = functools.partial(echo.echo, write=lambda msg: logger.info(msg))


class Trick(PatternMatchingEventHandler):

"""Your tricks should subclass this class."""
Expand Down Expand Up @@ -80,19 +86,19 @@ class LoggerTrick(Trick):
def on_any_event(self, event):
pass

@echo.echo
@echo_events
def on_modified(self, event):
pass

@echo.echo
@echo_events
def on_deleted(self, event):
pass

@echo.echo
@echo_events
def on_created(self, event):
pass

@echo.echo
@echo_events
def on_moved(self, event):
pass

Expand Down Expand Up @@ -202,7 +208,7 @@ def kill_process(stop_signal):
pass
self.process = None

@echo.echo
@echo_events
def on_any_event(self, event):
self.stop()
self.start()
29 changes: 24 additions & 5 deletions src/watchdog/watchmedo.py
Expand Up @@ -60,14 +60,18 @@ def _split_lines(self, text, width):
return text.splitlines()


epilog = '''Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>.
epilog = '''\
Copyright 2011 Yesudeep Mangalapilly <yesudeep@gmail.com>.
Copyright 2012 Google, Inc & contributors.
Licensed under the terms of the Apache license, version 2.0. Please see
LICENSE in the source code for more information.'''

cli = ArgumentParser(epilog=epilog, formatter_class=HelpFormatter)
cli.add_argument('--version', action='version', version=VERSION_STRING)
verbosity_group = cli.add_mutually_exclusive_group()
verbosity_group.add_argument('-q', '--quiet', dest='verbosity', action='append_const', const=-1)
verbosity_group.add_argument('-v', '--verbose', dest='verbosity', action='append_const', const=1)
subparsers = cli.add_subparsers(dest='top_command')


Expand Down Expand Up @@ -397,7 +401,8 @@ def log(args):
from watchdog.tricks import LoggerTrick

if args.trace:
echo.echo_class(LoggerTrick)
class_module_logger = logging.getLogger(LoggerTrick.__module__)
echo.echo_class(LoggerTrick, write=lambda msg: class_module_logger.info(msg))

patterns, ignore_patterns =\
parse_patterns(args.patterns, args.ignore_patterns)
Expand Down Expand Up @@ -637,9 +642,23 @@ def main():
args = cli.parse_args()
if args.command is None:
cli.print_help()
else:
args.func(args)
return 1

verbosity = sum(args.verbosity)
if verbosity < -1:
print("Error: -q/--quiet may be specified only once.", file=sys.stderr)
cli.print_help()
return 1
if verbosity > 2:
print("Error: -v/--verbose may be specified up to 2 times.", file=sys.stderr)
cli.print_help()
return 1
log_level = ['ERROR', 'WARNING', 'INFO', 'DEBUG'][1 + verbosity]
logging.getLogger('watchdog').setLevel(log_level)

args.func(args)
return 0


if __name__ == '__main__':
main()
sys.exit(main())

0 comments on commit c9793cf

Please sign in to comment.