Skip to content

Commit

Permalink
[watchmedo] Lazy loading of the PyYAML module
Browse files Browse the repository at this point in the history
The module is loaded only when strictly necessary.
Simple usages of watchmedo are possible without the module being installed.
  • Loading branch information
BoboTiG committed Oct 1, 2021
1 parent 57e80a5 commit 175b7ea
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions changelog.rst
Expand Up @@ -8,8 +8,8 @@ Changelog

2021-xx-xx • `full history <https://github.com/gorakhargosh/watchdog/compare/v2.1.6...master>`__

-
- 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 <https://github.com/gorakhargosh/watchdog/pull/847>`_)
- Thanks to our beloved contributors: @BoboTiG

2.1.6
~~~~~
Expand Down
9 changes: 6 additions & 3 deletions src/watchdog/watchmedo.py
Expand Up @@ -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

Expand Down Expand Up @@ -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=';'):
Expand Down Expand Up @@ -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()
Expand All @@ -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.
Expand Down

0 comments on commit 175b7ea

Please sign in to comment.