diff --git a/ansible_collections/arista/avd/plugins/filter/markdown_rendering.py b/ansible_collections/arista/avd/plugins/filter/markdown_rendering.py index 8e4531656e7..c9754f55660 100644 --- a/ansible_collections/arista/avd/plugins/filter/markdown_rendering.py +++ b/ansible_collections/arista/avd/plugins/filter/markdown_rendering.py @@ -5,6 +5,21 @@ __metaclass__ = type +from ansible.errors import AnsibleFilterError + +from ansible_collections.arista.avd.plugins.plugin_utils.pyavd_wrappers import RaiseOnUse, wrap_filter + +PLUGIN_NAME = "arista.avd.status_render" + +try: + from pyavd.j2filters.markdown_rendering import status_render +except ImportError as e: + status_render = RaiseOnUse( + AnsibleFilterError( + f"The '{PLUGIN_NAME}' plugin requires the 'pyavd' Python library. Got import error", + orig_exc=e, + ) + ) DOCUMENTATION = r""" --- @@ -38,33 +53,6 @@ class FilterModule(object): - # STATIC EMOJI CODE - GH_CODE = {} - # Github MD code for Emoji checked box - GH_CODE["PASS"] = ":white_check_mark:" - # GH MD code for Emoji Fail - GH_CODE["FAIL"] = ":x:" - - def status_render(self, state_string, rendering): - """ - status_render Convert Text to EMOJI code - - Parameters - ---------- - state_string : str - Text to convert in EMOJI - rendering : string - Markdown Flavor to use for Emoji rendering. - - Returns - ------- - str - Value to render in markdown - """ - if rendering == "github": - return self.GH_CODE[state_string.upper()] - else: - return state_string def filters(self): - return {"status_render": self.status_render} + return {"status_render": wrap_filter(PLUGIN_NAME)(status_render)} diff --git a/ansible_collections/arista/avd/tests/unit/filters/filter_utils.py b/ansible_collections/arista/avd/tests/unit/filters/filter_utils.py deleted file mode 100644 index 3538fbf5df0..00000000000 --- a/ansible_collections/arista/avd/tests/unit/filters/filter_utils.py +++ /dev/null @@ -1,12 +0,0 @@ -# Copyright (c) 2023-2024 Arista Networks, Inc. -# Use of this source code is governed by the Apache License 2.0 -# that can be found in the LICENSE file. -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -from natsort import os_sorted - - -def natural_sort(item_to_natural_sort): - return os_sorted(item_to_natural_sort) diff --git a/ansible_collections/arista/avd/tests/unit/filters/test_markdown_rendering.py b/ansible_collections/arista/avd/tests/unit/filters/test_markdown_rendering.py deleted file mode 100644 index 81980752d61..00000000000 --- a/ansible_collections/arista/avd/tests/unit/filters/test_markdown_rendering.py +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright (c) 2023-2024 Arista Networks, Inc. -# Use of this source code is governed by the Apache License 2.0 -# that can be found in the LICENSE file. -from __future__ import absolute_import, division, print_function - -__metaclass__ = type - -import pytest - -from ansible_collections.arista.avd.plugins.filter.markdown_rendering import FilterModule - -STATE_STRINGS = ["PASS", "FAIL"] -RENDERING_VALID = "github" -RENDERING_INVALID = "test" - -GH_CODE = {} -# Github MD code for Emoji checked box -GH_CODE["PASS"] = ":white_check_mark:" -# GH MD code for Emoji Fail -GH_CODE["FAIL"] = ":x:" - -f = FilterModule() - - -class TestMarkdownRenderingFilter: - @pytest.mark.parametrize("STATE_STRING", STATE_STRINGS) - def test_status_render_valid(self, STATE_STRING): - resp = f.status_render(STATE_STRING, RENDERING_VALID) - assert resp == GH_CODE[STATE_STRING] - - @pytest.mark.parametrize("STATE_STRING", STATE_STRINGS) - def test_status_render_invalid(self, STATE_STRING): - resp = f.status_render(STATE_STRING, RENDERING_INVALID) - assert resp == STATE_STRING - - def test_markdown_rendering_filter(self): - resp = f.filters() - assert isinstance(resp, dict) - assert "status_render" in resp.keys() diff --git a/python-avd/Makefile b/python-avd/Makefile index 0086bb3848c..cf76bf6dc25 100644 --- a/python-avd/Makefile +++ b/python-avd/Makefile @@ -95,6 +95,7 @@ fix-libs: ## Fix/remove various Ansible specifics things from python files find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.generate_route_target/$(PYAVD_FILTER_IMPORT)\.generate_route_target/g' {} + find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.hide_passwords/$(PYAVD_FILTER_IMPORT)\.hide_passwords/g' {} + find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.list_compress/$(PYAVD_FILTER_IMPORT)\.list_compress/g' {} + + find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter.markdown_rendering/$(PYAVD_FILTER_IMPORT)\.markdown_rendering/g' {} + find $(PACKAGE_DIR) -name '*.py' -exec sed -i -e 's/ansible_collections\.arista\.avd\.plugins\.filter/$(VENDOR_IMPORT)\.j2\.filter/g' {} + diff --git a/python-avd/pyavd/j2filters/markdown_rendering.py b/python-avd/pyavd/j2filters/markdown_rendering.py new file mode 100644 index 00000000000..9583279afe6 --- /dev/null +++ b/python-avd/pyavd/j2filters/markdown_rendering.py @@ -0,0 +1,32 @@ +# Copyright (c) 2023-2024 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the LICENSE file. +from __future__ import annotations + + +def status_render(state_string, rendering): + """ + status_render Convert Text to EMOJI code + + Parameters + ---------- + state_string : str + Text to convert in EMOJI + rendering : string + Markdown Flavor to use for Emoji rendering. + + Returns + ------- + str + Value to render in markdown + """ + # STATIC EMOJI CODE + GH_CODE = {} + # Github MD code for Emoji checked box + GH_CODE["PASS"] = ":white_check_mark:" + # GH MD code for Emoji Fail + GH_CODE["FAIL"] = ":x:" + + if rendering == "github": + return GH_CODE[state_string.upper()] + return state_string diff --git a/python-avd/pyavd/templater.py b/python-avd/pyavd/templater.py index f43c003ba6c..79144680b07 100644 --- a/python-avd/pyavd/templater.py +++ b/python-avd/pyavd/templater.py @@ -77,6 +77,7 @@ def import_filters_and_tests(self) -> None: from .j2filters.hide_passwords import hide_passwords from .j2filters.is_in_filter import is_in_filter from .j2filters.list_compress import list_compress + from .j2filters.markdown_rendering import status_render from .j2filters.natural_sort import natural_sort from .j2filters.snmp_hash import snmp_hash from .j2tests.contains import contains @@ -101,6 +102,7 @@ def import_filters_and_tests(self) -> None: "arista.avd.natural_sort": natural_sort, "arista.avd.range_expand": range_expand, "arista.avd.snmp_hash": snmp_hash, + "arista.avd.status_render": status_render, } ) self.environment.tests.update( diff --git a/python-avd/tests/pyavd/j2filters/test_markdown_rendering.py b/python-avd/tests/pyavd/j2filters/test_markdown_rendering.py new file mode 100644 index 00000000000..abd1c65725b --- /dev/null +++ b/python-avd/tests/pyavd/j2filters/test_markdown_rendering.py @@ -0,0 +1,18 @@ +# Copyright (c) 2023-2024 Arista Networks, Inc. +# Use of this source code is governed by the Apache License 2.0 +# that can be found in the LICENSE file. +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +import pytest +from pyavd.j2filters.markdown_rendering import status_render + +STATE_STRINGS = [("PASS", "github", ":white_check_mark:"), ("FAIL", "test", "FAIL")] + + +class TestMarkdownRenderingFilter: + @pytest.mark.parametrize("state_string, rendering, markdown_code", STATE_STRINGS) + def test_status_render_valid(self, state_string, rendering, markdown_code): + resp = status_render(state_string, rendering) + assert resp == markdown_code