From 2a8b84c7f8570cc76bec774c43045402d958f3ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Fri, 1 Oct 2021 21:46:11 +0200 Subject: [PATCH] [watchmedo] Lazy loading of the PyYAML module The module is loaded only when strictly necessary. Simple usages of watchmedo are possible without the module being installed. --- changelog.rst | 4 ++-- src/watchdog/watchmedo.py | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/changelog.rst b/changelog.rst index 8ad95138b..8c3ec5375 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. (`# `_) +- 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.