diff --git a/changelog.rst b/changelog.rst index 8ad95138b..cd9a78c3c 100644 --- a/changelog.rst +++ b/changelog.rst @@ -8,8 +8,8 @@ Changelog 2021-xx-xx • `full history `__ -- -- Thanks to our beloved contributors: @ +- [watchmedo] ``PyYAML`` is loaded only when strictly necessary. Simple usages of ``watchmedo`` are possible without the module being installed. (`#847 `_) +- Thanks to our beloved contributors: @BoboTiG 2.1.6 ~~~~~ diff --git a/src/watchdog/watchmedo.py b/src/watchdog/watchmedo.py index 8c2341e8a..a1c2a33f4 100755 --- a/src/watchdog/watchmedo.py +++ b/src/watchdog/watchmedo.py @@ -31,7 +31,6 @@ from io import StringIO from textwrap import dedent -import yaml from watchdog.utils import WatchdogShutdown, load_class from watchdog.version import VERSION_STRING @@ -136,8 +135,10 @@ def load_config(tricks_file_pathname): :returns: A dictionary of configuration information. """ + from yaml import safe_load + with open(tricks_file_pathname, 'rb') as f: - return yaml.safe_load(f.read()) + return safe_load(f.read()) def parse_patterns(patterns_spec, ignore_patterns_spec, separator=';'): @@ -309,6 +310,8 @@ def tricks_generate_yaml(args): """ Command to generate Yaml configuration for tricks named on the command line. """ + from yaml import dump + python_paths = path_split(args.python_path) add_to_sys_path(python_paths) output = StringIO() @@ -320,7 +323,7 @@ def tricks_generate_yaml(args): content = output.getvalue() output.close() - header = yaml.dump({CONFIG_KEY_PYTHON_PATH: python_paths}) + header = dump({CONFIG_KEY_PYTHON_PATH: python_paths}) header += "%s:\n" % CONFIG_KEY_TRICKS if args.append_to_file is None: # Output to standard output.