diff --git a/ansibledoctor/Annotation.py b/ansibledoctor/Annotation.py index d600c23..e77672d 100644 --- a/ansibledoctor/Annotation.py +++ b/ansibledoctor/Annotation.py @@ -7,7 +7,6 @@ import re from collections import defaultdict import anyconfig -import yaml from ansibledoctor.Config import SingleConfig from ansibledoctor.FileRegistry import Registry diff --git a/ansibledoctor/Config.py b/ansibledoctor/Config.py index 1f1d1d3..e1560b4 100644 --- a/ansibledoctor/Config.py +++ b/ansibledoctor/Config.py @@ -8,7 +8,7 @@ import sys import anyconfig import environs import jsonschema.exceptions -import yaml +import ruamel.yaml from appdirs import AppDirs from jsonschema._utils import format_as_index from pkg_resources import resource_filename @@ -215,10 +215,12 @@ class Config(): with open(config, "r", encoding="utf8") as stream: s = stream.read() try: - file_dict = yaml.safe_load(s) - except yaml.parser.ParserError as e: - message = "{}\n{}".format(e.problem, str(e.problem_mark)) - raise ansibledoctor.Exception.ConfigError("Unable to read file", message) + file_dict = ruamel.yaml.safe_load(s) + except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e: + message = "{} {}".format(e.context, e.problem) + raise ansibledoctor.Exception.ConfigError( + "Unable to read config file {}".format(config), message + ) if self._validate(file_dict): anyconfig.merge(defaults, file_dict, ac_merge=anyconfig.MS_DICTS) diff --git a/ansibledoctor/DocumentationParser.py b/ansibledoctor/DocumentationParser.py index e7fbf77..01fd252 100644 --- a/ansibledoctor/DocumentationParser.py +++ b/ansibledoctor/DocumentationParser.py @@ -7,7 +7,7 @@ import os from collections import defaultdict import anyconfig -import yaml +import ruamel.yaml from nested_lookup import nested_lookup from ansibledoctor.Annotation import Annotation @@ -35,18 +35,19 @@ class Parser: if any(fnmatch.fnmatch(rfile, "*/defaults/*." + ext) for ext in YAML_EXTENSIONS): with open(rfile, "r", encoding="utf8") as yaml_file: try: - data = defaultdict(dict, yaml.load(yaml_file, Loader=yaml.SafeLoader)) + data = defaultdict(dict, ruamel.yaml.safe_load(yaml_file)) for key, value in data.items(): self._data["var"][key] = {"value": {key: value}} - except yaml.YAMLError as exc: - print(exc) + except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e: + message = "{} {}".format(e.context, e.problem) + self.log.sysexit_with_message("Unable to read yaml file {}\n{}".format(rfile, message)) def _parse_meta_file(self): for rfile in self._files_registry.get_files(): if any("meta/main." + ext in rfile for ext in YAML_EXTENSIONS): with open(rfile, "r", encoding="utf8") as yaml_file: try: - data = defaultdict(dict, yaml.safe_load(yaml_file)) + data = defaultdict(dict, ruamel.yaml.safe_load(yaml_file)) if data.get("galaxy_info"): for key, value in data.get("galaxy_info").items(): self._data["meta"][key] = {"value": value} @@ -54,17 +55,19 @@ class Parser: if data.get("dependencies") is not None: self._data["meta"]["dependencies"] = {"value": data.get("dependencies")} self._data["meta"]["name"] = {"value": os.path.basename(self.config.role_dir)} - except yaml.YAMLError as exc: - print(exc) + except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e: + message = "{} {}".format(e.context, e.problem) + self.log.sysexit_with_message("Unable to read yaml file {}\n{}".format(rfile, message)) def _parse_task_tags(self): for rfile in self._files_registry.get_files(): if any(fnmatch.fnmatch(rfile, "*/tasks/*." + ext) for ext in YAML_EXTENSIONS): with open(rfile, "r", encoding="utf8") as yaml_file: try: - data = yaml.safe_load(yaml_file) - except yaml.YAMLError as exc: - print(exc) + data = ruamel.yaml.safe_load(yaml_file) + except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e: + message = "{} {}".format(e.context, e.problem) + self.log.sysexit_with_message("Unable to read yaml file {}\n{}".format(rfile, message)) tags_found = nested_lookup("tags", data) for tag in tags_found: diff --git a/ansibledoctor/Utils.py b/ansibledoctor/Utils.py index 3e8a5fc..7577c73 100644 --- a/ansibledoctor/Utils.py +++ b/ansibledoctor/Utils.py @@ -8,7 +8,6 @@ import sys from distutils.util import strtobool import colorama -import yaml from pythonjsonlogger import jsonlogger import ansibledoctor.Exception diff --git a/setup.py b/setup.py index 42a2bc7..c821269 100755 --- a/setup.py +++ b/setup.py @@ -58,7 +58,6 @@ setup( "Topic :: Software Development :: Documentation", ], install_requires=[ - "pyyaml", "ruamel.yaml", "appdirs", "colorama",