2019-10-07 06:52:00 +00:00
|
|
|
#!/usr/bin/env python3
|
2019-10-08 09:39:27 +00:00
|
|
|
"""Parse static files."""
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
import fnmatch
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
import anyconfig
|
2019-10-15 08:48:18 +00:00
|
|
|
import ruamel.yaml
|
2019-10-15 07:54:03 +00:00
|
|
|
from nested_lookup import nested_lookup
|
2019-10-07 06:52:00 +00:00
|
|
|
|
2021-01-01 12:50:41 +00:00
|
|
|
from ansibledoctor.annotation import Annotation
|
|
|
|
from ansibledoctor.config import SingleConfig
|
|
|
|
from ansibledoctor.contstants import YAML_EXTENSIONS
|
|
|
|
from ansibledoctor.file_registry import Registry
|
|
|
|
from ansibledoctor.utils import SingleLog
|
|
|
|
from ansibledoctor.utils import UnsafeTag
|
2022-02-26 12:35:34 +00:00
|
|
|
from ansibledoctor.utils import flatten
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Parser:
|
2020-04-05 21:16:53 +00:00
|
|
|
"""Parse yaml files."""
|
|
|
|
|
2019-10-07 06:52:00 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._annotation_objs = {}
|
|
|
|
self._data = defaultdict(dict)
|
|
|
|
self.config = SingleConfig()
|
2019-10-15 07:54:03 +00:00
|
|
|
self.log = SingleLog()
|
|
|
|
self.logger = SingleLog().logger
|
2019-10-07 06:52:00 +00:00
|
|
|
self._files_registry = Registry()
|
|
|
|
self._parse_meta_file()
|
2019-10-15 07:54:03 +00:00
|
|
|
self._parse_var_files()
|
2022-02-26 12:35:34 +00:00
|
|
|
self._parse_task_tags()
|
2019-10-07 06:52:00 +00:00
|
|
|
self._populate_doc_data()
|
|
|
|
|
2022-02-21 20:38:47 +00:00
|
|
|
def _yaml_remove_comments(self, d):
|
|
|
|
if isinstance(d, dict):
|
|
|
|
for k, v in d.items():
|
|
|
|
self._yaml_remove_comments(k)
|
|
|
|
self._yaml_remove_comments(v)
|
|
|
|
elif isinstance(d, list):
|
|
|
|
for elem in d:
|
|
|
|
self._yaml_remove_comments(elem)
|
|
|
|
try:
|
|
|
|
attr = "comment" if isinstance(
|
|
|
|
d, ruamel.yaml.scalarstring.ScalarString
|
|
|
|
) else ruamel.yaml.comments.Comment.attrib
|
|
|
|
delattr(d, attr)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2019-10-15 07:54:03 +00:00
|
|
|
def _parse_var_files(self):
|
2019-10-07 06:52:00 +00:00
|
|
|
for rfile in self._files_registry.get_files():
|
2019-10-15 07:54:03 +00:00
|
|
|
if any(fnmatch.fnmatch(rfile, "*/defaults/*." + ext) for ext in YAML_EXTENSIONS):
|
2019-10-07 06:52:00 +00:00
|
|
|
with open(rfile, "r", encoding="utf8") as yaml_file:
|
|
|
|
try:
|
2020-04-05 21:16:53 +00:00
|
|
|
ruamel.yaml.add_constructor(
|
|
|
|
UnsafeTag.yaml_tag,
|
|
|
|
UnsafeTag.yaml_constructor,
|
|
|
|
constructor=ruamel.yaml.SafeConstructor
|
|
|
|
)
|
2022-02-21 20:38:47 +00:00
|
|
|
|
|
|
|
raw = ruamel.yaml.YAML(typ="rt").load(yaml_file)
|
|
|
|
self._yaml_remove_comments(raw)
|
|
|
|
|
|
|
|
data = defaultdict(dict, raw or {})
|
2019-10-07 06:52:00 +00:00
|
|
|
for key, value in data.items():
|
|
|
|
self._data["var"][key] = {"value": {key: value}}
|
2020-04-05 21:16:53 +00:00
|
|
|
except (
|
2022-03-02 21:37:13 +00:00
|
|
|
ruamel.yaml.composer.ComposerError,
|
|
|
|
ruamel.yaml.scanner.ScannerError,
|
|
|
|
ruamel.yaml.constructor.ConstructorError,
|
|
|
|
ruamel.yaml.constructor.DuplicateKeyError,
|
2020-04-05 21:16:53 +00:00
|
|
|
) as e:
|
2019-10-15 08:48:18 +00:00
|
|
|
message = "{} {}".format(e.context, e.problem)
|
2020-04-05 21:16:53 +00:00
|
|
|
self.log.sysexit_with_message(
|
|
|
|
"Unable to read yaml file {}\n{}".format(rfile, message)
|
|
|
|
)
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
def _parse_meta_file(self):
|
|
|
|
for rfile in self._files_registry.get_files():
|
2019-10-15 07:54:03 +00:00
|
|
|
if any("meta/main." + ext in rfile for ext in YAML_EXTENSIONS):
|
2019-10-07 06:52:00 +00:00
|
|
|
with open(rfile, "r", encoding="utf8") as yaml_file:
|
|
|
|
try:
|
2022-02-21 20:38:47 +00:00
|
|
|
raw = ruamel.yaml.YAML(typ="rt").load(yaml_file)
|
|
|
|
self._yaml_remove_comments(raw)
|
|
|
|
|
|
|
|
data = defaultdict(dict, raw)
|
2019-10-07 06:52:00 +00:00
|
|
|
if data.get("galaxy_info"):
|
|
|
|
for key, value in data.get("galaxy_info").items():
|
|
|
|
self._data["meta"][key] = {"value": value}
|
2019-10-08 09:30:31 +00:00
|
|
|
|
|
|
|
if data.get("dependencies") is not None:
|
2020-04-05 21:16:53 +00:00
|
|
|
self._data["meta"]["dependencies"] = {
|
|
|
|
"value": data.get("dependencies")
|
|
|
|
}
|
2019-11-07 08:10:20 +00:00
|
|
|
|
2020-01-22 12:07:05 +00:00
|
|
|
self._data["meta"]["name"] = {"value": self.config.config["role_name"]}
|
2020-04-05 21:16:53 +00:00
|
|
|
except (
|
|
|
|
ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError
|
|
|
|
) as e:
|
2019-10-15 08:48:18 +00:00
|
|
|
message = "{} {}".format(e.context, e.problem)
|
2020-04-05 21:16:53 +00:00
|
|
|
self.log.sysexit_with_message(
|
|
|
|
"Unable to read yaml file {}\n{}".format(rfile, message)
|
|
|
|
)
|
2019-11-07 08:13:39 +00:00
|
|
|
|
2019-10-15 07:54:03 +00:00
|
|
|
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:
|
2022-02-21 20:38:47 +00:00
|
|
|
raw = ruamel.yaml.YAML(typ="rt").load(yaml_file)
|
|
|
|
self._yaml_remove_comments(raw)
|
|
|
|
|
2022-02-26 12:35:34 +00:00
|
|
|
tags = list(set(flatten(nested_lookup("tags", raw))))
|
2022-04-29 11:19:35 +00:00
|
|
|
for tag in [
|
|
|
|
x for x in tags if x not in self.config.config["exclude_tags"]
|
|
|
|
]:
|
2022-02-26 12:35:34 +00:00
|
|
|
self._data["tag"][tag] = {"value": tag}
|
2020-04-05 21:16:53 +00:00
|
|
|
except (
|
|
|
|
ruamel.yaml.composer.ComposerError, ruamel.yaml.scanner.ScannerError
|
|
|
|
) as e:
|
2019-10-15 08:48:18 +00:00
|
|
|
message = "{} {}".format(e.context, e.problem)
|
2020-04-05 21:16:53 +00:00
|
|
|
self.log.sysexit_with_message(
|
|
|
|
"Unable to read yaml file {}\n{}".format(rfile, message)
|
|
|
|
)
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
def _populate_doc_data(self):
|
|
|
|
"""Generate the documentation data object."""
|
|
|
|
tags = defaultdict(dict)
|
2022-02-21 20:38:47 +00:00
|
|
|
for annotation in self.config.get_annotations_names(automatic=True):
|
2022-08-21 21:10:03 +00:00
|
|
|
self.logger.info(f"Finding annotations for: @{annotation}")
|
2022-02-21 20:38:47 +00:00
|
|
|
self._annotation_objs[annotation] = Annotation(
|
|
|
|
name=annotation, files_registry=self._files_registry
|
2020-04-05 21:16:53 +00:00
|
|
|
)
|
2022-02-21 20:38:47 +00:00
|
|
|
tags[annotation] = self._annotation_objs[annotation].get_details()
|
2019-10-15 07:54:03 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
anyconfig.merge(self._data, tags, ac_merge=anyconfig.MS_DICTS)
|
|
|
|
except ValueError as e:
|
|
|
|
self.log.sysexit_with_message("Unable to merge annotation values:\n{}".format(e))
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|