Skip to content

Commit

Permalink
Refactor(plugins): Move jinja filter code for arista.avd.generate_lac…
Browse files Browse the repository at this point in the history
…p_id to PyAVD
  • Loading branch information
Shivani-gslab committed Jun 20, 2024
1 parent 55f18c4 commit 31d7a9d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 79 deletions.
44 changes: 16 additions & 28 deletions ansible_collections/arista/avd/plugins/filter/markdown_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
---
Expand Down Expand Up @@ -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)}
12 changes: 0 additions & 12 deletions ansible_collections/arista/avd/tests/unit/filters/filter_utils.py

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions python-avd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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' {} +

Expand Down
32 changes: 32 additions & 0 deletions python-avd/pyavd/j2filters/markdown_rendering.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions python-avd/pyavd/templater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
18 changes: 18 additions & 0 deletions python-avd/tests/pyavd/j2filters/test_markdown_rendering.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 31d7a9d

Please sign in to comment.