add feature to load static custom header from file

This commit is contained in:
Robert Kaussow 2019-10-08 14:57:46 +02:00
parent 80a1b8e33a
commit 669bb9442a
8 changed files with 38 additions and 11 deletions

View File

@ -45,6 +45,7 @@ class Config():
self.dry_run = self._set_dry_run() or False self.dry_run = self._set_dry_run() or False
self.config = self._get_config() self.config = self._get_config()
self._annotations = self._set_annotations() self._annotations = self._set_annotations()
self._post_processing()
def _set_args(self, args): def _set_args(self, args):
defaults = self._get_defaults() defaults = self._get_defaults()
@ -81,6 +82,7 @@ class Config():
"template_dir": default_template, "template_dir": default_template,
"template": "readme", "template": "readme",
"force_overwrite": False, "force_overwrite": False,
"appent_to_file": "",
"exclude_files": [], "exclude_files": [],
} }
@ -90,7 +92,6 @@ class Config():
def _get_config(self): def _get_config(self):
defaults = self._get_defaults() defaults = self._get_defaults()
source_files = [] source_files = []
source_files.append(self.config_file) source_files.append(self.config_file)
source_files.append(os.path.join(self.base_dir, ".ansibledoctor")) source_files.append(os.path.join(self.base_dir, ".ansibledoctor"))
source_files.append(os.path.join(self.base_dir, ".ansibledoctor.yml")) source_files.append(os.path.join(self.base_dir, ".ansibledoctor.yml"))
@ -156,6 +157,15 @@ class Config():
if self.args.get("dry_run"): if self.args.get("dry_run"):
return True return True
def _post_processing(self):
# Override append file path
append_file = self.config.get("append_to_file")
if append_file:
if not os.path.isabs(os.path.expanduser(os.path.expandvars(append_file))):
append_file = os.path.join(self.base_dir, append_file)
self.config["append_to_file"] = os.path.abspath(os.path.expanduser(os.path.expandvars(append_file)))
def _validate(self, config): def _validate(self, config):
try: try:
anyconfig.validate(config, self.schema, ac_schema_safe=False) anyconfig.validate(config, self.schema, ac_schema_safe=False)

View File

@ -79,6 +79,14 @@ class Generator:
for file in self.template_files: for file in self.template_files:
doc_file = os.path.join(self.config.config.get("output_dir"), os.path.splitext(file)[0]) doc_file = os.path.join(self.config.config.get("output_dir"), os.path.splitext(file)[0])
source_file = self.config.get_template() + "/" + file source_file = self.config.get_template() + "/" + file
append_file = self.config.config.get("append_to_file")
role_data = self._parser.get_data()
custom_header = ""
if append_file:
role_data["internal"]["append"] = True
with open(append_file, "r") as a:
custom_header = a.read()
self.logger.debug("Writing doc output to: " + doc_file + " from: " + source_file) self.logger.debug("Writing doc output to: " + doc_file + " from: " + source_file)
@ -90,13 +98,14 @@ class Generator:
data = template.read() data = template.read()
if data is not None: if data is not None:
try: try:
# print(json.dumps(self._parser.get_data(), indent=4, sort_keys=True)) # print(json.dumps(role_data, indent=4, sort_keys=True))
jenv = Environment(loader=FileSystemLoader(self.config.get_template()), lstrip_blocks=True, trim_blocks=True) jenv = Environment(loader=FileSystemLoader(self.config.get_template()), lstrip_blocks=True, trim_blocks=True)
jenv.filters["to_nice_yaml"] = self._to_nice_yaml jenv.filters["to_nice_yaml"] = self._to_nice_yaml
jenv.filters["deep_get"] = self._deep_get jenv.filters["deep_get"] = self._deep_get
data = jenv.from_string(data).render(self._parser.get_data(), role=self._parser.get_data()) data = jenv.from_string(data).render(role_data, role=role_data)
if not self.config.dry_run: if not self.config.dry_run:
with open(doc_file, "wb") as outfile: with open(doc_file, "wb") as outfile:
outfile.write(custom_header.encode("utf-8"))
outfile.write(data.encode("utf-8")) outfile.write(data.encode("utf-8"))
self.logger.info("Writing to: " + doc_file) self.logger.info("Writing to: " + doc_file)
else: else:

View File

@ -1,5 +1,7 @@
{% if not append | deep_get(role, "internal.append") %}
{% set meta = role.meta | default({}) %} {% set meta = role.meta | default({}) %}
# {{ name | deep_get(meta, "name.value") | default("_undefined_") }} # {{ name | deep_get(meta, "name.value") | default("_undefined_") }}
{% endif %}
{% if description | deep_get(meta, "description.value") %} {% if description | deep_get(meta, "description.value") %}
{{ description | deep_get(meta, "description.value") }} {{ description | deep_get(meta, "description.value") }}

View File

@ -6,9 +6,7 @@
### {{ key }} ### {{ key }}
{% if item.description is defined and item.description %} {% if item.description is defined and item.description %}
{% for desc_line in item.description %} {{ item.description | join(" ") | striptags }}
{{ desc_line }}
{% endfor %}
{% endif %} {% endif %}
{% if item.value is defined and item.value %} {% if item.value is defined and item.value %}

View File

@ -1,4 +1,7 @@
# demo-role # demo-role-custom-header
[![Build Status](https://cloud.drone.io/api/badges/xoxys/ansible-later/status.svg)](https://cloud.drone.io/xoxys/ansible-later)
![License](https://img.shields.io/pypi/l/ansible-later)
Role to demonstrate ansible-doctor Role to demonstrate ansible-doctor
@ -97,8 +100,7 @@ demo_role_dict:
### demo_role_other_tags ### demo_role_other_tags
The role requires epel repository and pip to work. If a variable need some more explanation, this is a good place to do so.
You can use dockerengine_packages_extra to install these dependencys.
#### Default value #### Default value

View File

@ -0,0 +1,2 @@
---
append_to_file: HEADER.md

View File

@ -0,0 +1,4 @@
# demo-role-custom-header
[![Build Status](https://cloud.drone.io/api/badges/xoxys/ansible-later/status.svg)](https://cloud.drone.io/xoxys/ansible-later)
![License](https://img.shields.io/pypi/l/ansible-later)

View File

@ -20,8 +20,8 @@ demo_role_dict:
sub: some value sub: some value
# @var demo_role_other_tags:description: > # @var demo_role_other_tags:description: >
# The role requires epel repository and pip to work. # If a variable need some more explanation,
# You can use dockerengine_packages_extra to install these dependencys. # this is a good place to do so.
# @end # @end
# @var dockerengine_packages_extra:example: ["package1", "package2"] # @var dockerengine_packages_extra:example: ["package1", "package2"]