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 c18393e
Show file tree
Hide file tree
Showing 2 changed files with 38 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()
32 changes: 27 additions & 5 deletions src/watchdog/watchmedo.py
Expand Up @@ -60,7 +60,8 @@ 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
Expand All @@ -69,6 +70,7 @@ def _split_lines(self, text, width):
cli = ArgumentParser(epilog=epilog, formatter_class=HelpFormatter)
cli.add_argument('--version', action='version', version=VERSION_STRING)
subparsers = cli.add_subparsers(dest='top_command')
command_parsers = {}


def argument(*name_or_flags, **kwargs):
Expand All @@ -94,6 +96,11 @@ def decorator(func):
description=desc,
aliases=cmd_aliases,
formatter_class=HelpFormatter)
verbosity_group = parser.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)
for arg in args:
parser.add_argument(*arg[0], **arg[1])
parser.set_defaults(func=func)
Expand Down Expand Up @@ -397,7 +404,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 +645,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)
command_parsers[args.top_command].print_help()
return 1
if verbosity > 2:
print("Error: -v/--verbose may be specified up to 2 times.", file=sys.stderr)
command_parsers[args.top_command].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 c18393e

Please sign in to comment.