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
|
2024-06-17 11:51:03 +00:00
|
|
|
import structlog
|
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
|
2023-11-12 20:39:41 +00:00
|
|
|
from ansibledoctor.exception import YAMLError
|
2021-01-01 12:50:41 +00:00
|
|
|
from ansibledoctor.file_registry import Registry
|
2024-06-17 11:51:03 +00:00
|
|
|
from ansibledoctor.utils import flatten, sysexit_with_message
|
2023-11-12 20:39:41 +00:00
|
|
|
from ansibledoctor.utils.yamlhelper import parse_yaml, parse_yaml_ansible
|
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()
|
2024-06-17 11:51:03 +00:00
|
|
|
self.log = structlog.get_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()
|
|
|
|
|
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):
|
2023-11-12 20:39:41 +00:00
|
|
|
with open(rfile, encoding="utf8") as yamlfile:
|
2019-10-07 06:52:00 +00:00
|
|
|
try:
|
2023-11-12 20:39:41 +00:00
|
|
|
raw = parse_yaml(yamlfile)
|
|
|
|
except YAMLError as e:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message("Failed to read yaml file", path=rfile, error=e)
|
2023-11-12 20:39:41 +00:00
|
|
|
|
|
|
|
data = defaultdict(dict, raw or {})
|
|
|
|
|
|
|
|
for key, value in data.items():
|
|
|
|
self._data["var"][key] = {"value": {key: value}}
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
def _parse_meta_file(self):
|
2023-08-09 07:46:15 +00:00
|
|
|
self._data["meta"]["name"] = {"value": self.config.config["role_name"]}
|
|
|
|
|
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("meta/main." + ext in rfile for ext in YAML_EXTENSIONS):
|
2023-11-12 20:39:41 +00:00
|
|
|
with open(rfile, encoding="utf8") as yamlfile:
|
2019-10-07 06:52:00 +00:00
|
|
|
try:
|
2023-11-12 20:39:41 +00:00
|
|
|
raw = parse_yaml(yamlfile)
|
|
|
|
except YAMLError as e:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message("Failed to read yaml file", path=rfile, error=e)
|
2023-11-12 20:39:41 +00:00
|
|
|
|
|
|
|
data = defaultdict(dict, raw)
|
|
|
|
if data.get("galaxy_info"):
|
|
|
|
for key, value in data.get("galaxy_info").items():
|
|
|
|
self._data["meta"][key] = {"value": value}
|
|
|
|
|
|
|
|
if data.get("dependencies") is not None:
|
|
|
|
self._data["meta"]["dependencies"] = {"value": data.get("dependencies")}
|
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):
|
2023-11-12 20:39:41 +00:00
|
|
|
with open(rfile, encoding="utf8") as yamlfile:
|
2019-10-15 07:54:03 +00:00
|
|
|
try:
|
2023-11-12 20:39:41 +00:00
|
|
|
raw = parse_yaml_ansible(yamlfile)
|
|
|
|
except YAMLError as e:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message("Failed to read yaml file", path=rfile, error=e)
|
2023-11-12 20:39:41 +00:00
|
|
|
|
2024-06-07 19:30:10 +00:00
|
|
|
tags = []
|
|
|
|
for task in raw:
|
2024-06-22 18:49:16 +00:00
|
|
|
task_tags = task.get("tags", [])
|
2024-06-07 19:30:10 +00:00
|
|
|
if isinstance(task_tags, str):
|
|
|
|
task_tags = [task_tags]
|
|
|
|
|
|
|
|
for tag in task_tags:
|
|
|
|
if tag not in self.config.config["exclude_tags"]:
|
|
|
|
tags.append(tag)
|
2023-11-12 20:39:41 +00:00
|
|
|
|
|
|
|
for tag in flatten(tags):
|
|
|
|
self._data["tag"][tag] = {"value": tag}
|
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):
|
2024-06-17 11:51:03 +00:00
|
|
|
self.log.info(f"Lookup annotation @{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:
|
2024-06-17 11:51:03 +00:00
|
|
|
sysexit_with_message("Failed to merge annotation values", error=e)
|
2019-10-07 06:52:00 +00:00
|
|
|
|
|
|
|
def get_data(self):
|
|
|
|
return self._data
|