fully migrate to ruamel.yaml instead of pyyaml

This commit is contained in:
Robert Kaussow 2019-10-15 10:48:18 +02:00
parent cf6905b9a0
commit cb76507159
5 changed files with 20 additions and 18 deletions

View File

@ -7,7 +7,6 @@ import re
from collections import defaultdict from collections import defaultdict
import anyconfig import anyconfig
import yaml
from ansibledoctor.Config import SingleConfig from ansibledoctor.Config import SingleConfig
from ansibledoctor.FileRegistry import Registry from ansibledoctor.FileRegistry import Registry

View File

@ -8,7 +8,7 @@ import sys
import anyconfig import anyconfig
import environs import environs
import jsonschema.exceptions import jsonschema.exceptions
import yaml import ruamel.yaml
from appdirs import AppDirs from appdirs import AppDirs
from jsonschema._utils import format_as_index from jsonschema._utils import format_as_index
from pkg_resources import resource_filename from pkg_resources import resource_filename
@ -215,10 +215,12 @@ class Config():
with open(config, "r", encoding="utf8") as stream: with open(config, "r", encoding="utf8") as stream:
s = stream.read() s = stream.read()
try: try:
file_dict = yaml.safe_load(s) file_dict = ruamel.yaml.safe_load(s)
except yaml.parser.ParserError as e: except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e:
message = "{}\n{}".format(e.problem, str(e.problem_mark)) message = "{} {}".format(e.context, e.problem)
raise ansibledoctor.Exception.ConfigError("Unable to read file", message) raise ansibledoctor.Exception.ConfigError(
"Unable to read config file {}".format(config), message
)
if self._validate(file_dict): if self._validate(file_dict):
anyconfig.merge(defaults, file_dict, ac_merge=anyconfig.MS_DICTS) anyconfig.merge(defaults, file_dict, ac_merge=anyconfig.MS_DICTS)

View File

@ -7,7 +7,7 @@ import os
from collections import defaultdict from collections import defaultdict
import anyconfig import anyconfig
import yaml import ruamel.yaml
from nested_lookup import nested_lookup from nested_lookup import nested_lookup
from ansibledoctor.Annotation import Annotation from ansibledoctor.Annotation import Annotation
@ -35,18 +35,19 @@ class Parser:
if any(fnmatch.fnmatch(rfile, "*/defaults/*." + ext) for ext in YAML_EXTENSIONS): if any(fnmatch.fnmatch(rfile, "*/defaults/*." + ext) for ext in YAML_EXTENSIONS):
with open(rfile, "r", encoding="utf8") as yaml_file: with open(rfile, "r", encoding="utf8") as yaml_file:
try: 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(): for key, value in data.items():
self._data["var"][key] = {"value": {key: value}} self._data["var"][key] = {"value": {key: value}}
except yaml.YAMLError as exc: except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e:
print(exc) 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): def _parse_meta_file(self):
for rfile in self._files_registry.get_files(): for rfile in self._files_registry.get_files():
if any("meta/main." + ext in rfile for ext in YAML_EXTENSIONS): if any("meta/main." + ext in rfile for ext in YAML_EXTENSIONS):
with open(rfile, "r", encoding="utf8") as yaml_file: with open(rfile, "r", encoding="utf8") as yaml_file:
try: try:
data = defaultdict(dict, yaml.safe_load(yaml_file)) data = defaultdict(dict, ruamel.yaml.safe_load(yaml_file))
if data.get("galaxy_info"): if data.get("galaxy_info"):
for key, value in data.get("galaxy_info").items(): for key, value in data.get("galaxy_info").items():
self._data["meta"][key] = {"value": value} self._data["meta"][key] = {"value": value}
@ -54,17 +55,19 @@ class Parser:
if data.get("dependencies") is not None: if data.get("dependencies") is not None:
self._data["meta"]["dependencies"] = {"value": data.get("dependencies")} self._data["meta"]["dependencies"] = {"value": data.get("dependencies")}
self._data["meta"]["name"] = {"value": os.path.basename(self.config.role_dir)} self._data["meta"]["name"] = {"value": os.path.basename(self.config.role_dir)}
except yaml.YAMLError as exc: except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e:
print(exc) 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): def _parse_task_tags(self):
for rfile in self._files_registry.get_files(): for rfile in self._files_registry.get_files():
if any(fnmatch.fnmatch(rfile, "*/tasks/*." + ext) for ext in YAML_EXTENSIONS): if any(fnmatch.fnmatch(rfile, "*/tasks/*." + ext) for ext in YAML_EXTENSIONS):
with open(rfile, "r", encoding="utf8") as yaml_file: with open(rfile, "r", encoding="utf8") as yaml_file:
try: try:
data = yaml.safe_load(yaml_file) data = ruamel.yaml.safe_load(yaml_file)
except yaml.YAMLError as exc: except (ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError) as e:
print(exc) 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) tags_found = nested_lookup("tags", data)
for tag in tags_found: for tag in tags_found:

View File

@ -8,7 +8,6 @@ import sys
from distutils.util import strtobool from distutils.util import strtobool
import colorama import colorama
import yaml
from pythonjsonlogger import jsonlogger from pythonjsonlogger import jsonlogger
import ansibledoctor.Exception import ansibledoctor.Exception

View File

@ -58,7 +58,6 @@ setup(
"Topic :: Software Development :: Documentation", "Topic :: Software Development :: Documentation",
], ],
install_requires=[ install_requires=[
"pyyaml",
"ruamel.yaml", "ruamel.yaml",
"appdirs", "appdirs",
"colorama", "colorama",