From 2416be044bd9cfed812662fa77524cff3004d8fb Mon Sep 17 00:00:00 2001 From: Adam Byczkowski Date: Mon, 25 Apr 2022 00:37:16 -0500 Subject: [PATCH 1/2] Updated lib mapping docs --- docs/source/conf.py | 23 ++++- docs/source/netutils/lib_mapping/index.rst | 108 +++++---------------- docs/source/table_template.j2 | 9 ++ netutils/lib_mapper.py | 16 +++ 4 files changed, 71 insertions(+), 85 deletions(-) create mode 100644 docs/source/table_template.j2 diff --git a/docs/source/conf.py b/docs/source/conf.py index e8ba9e1e..1b4266e8 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -13,12 +13,15 @@ import os import sys import toml +from jinja2 import Environment, FileSystemLoader + sys.path.insert(0, os.path.abspath("../..")) +from netutils import lib_mapper # noqa: E402 + sys.path.append(os.path.abspath("sphinxext")) toml_dict = toml.load("../../pyproject.toml") - # -- Project information ----------------------------------------------------- project = toml_dict["tool"]["poetry"]["name"] @@ -63,3 +66,21 @@ # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ["_static"] + + +def build_mapping_tables(app): + """Build the library mappings tables.""" + env = Environment(loader=FileSystemLoader("docs/source")) + template_file = env.get_template("table_template.j2") + for dict_name in lib_mapper._LIST_OF_MAP_DICTS: + lib_name = dict_name.split("_")[0] + filename = f"{lib_name}_reverse" if "REVERSE" in dict_name else lib_name + headers = ["NETMIKO", lib_name] if "REVERSE" in dict_name else [lib_name, "NETMIKO"] + rendered_template = template_file.render(lib_names=headers, mappings=getattr(lib_mapper, dict_name)) + with open(f"docs/source/netutils/lib_mapping/{filename}_table.rst", "w") as table_file: + table_file.write(rendered_template) + + +def setup(app): + """Call methods during builder initiated.""" + app.connect("builder-inited", build_mapping_tables) diff --git a/docs/source/netutils/lib_mapping/index.rst b/docs/source/netutils/lib_mapping/index.rst index 4953717e..52af9f92 100644 --- a/docs/source/netutils/lib_mapping/index.rst +++ b/docs/source/netutils/lib_mapping/index.rst @@ -41,111 +41,51 @@ a python script to leverage the backup capabilities of pyntc? Here's an example Another use case could be using an example like the above in an Ansible filter. That would allow you to write a filter utilizing whichever automation library you needed without having to store the driver for each one in your Source of Truth. Napalm Mapper -=================== -.. exec:: - import json - from netutils.lib_mapper import NAPALM_LIB_MAPPER - json_obj = json.dumps(NAPALM_LIB_MAPPER, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: NAPALM_table.rst Reverse Napalm Mapper -===================== -.. exec:: - import json - from netutils.lib_mapper import NAPALM_LIB_MAPPER_REVERSE - json_obj = json.dumps(NAPALM_LIB_MAPPER_REVERSE, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: NAPALM_reverse_table.rst PyNTC Mapper -============== -.. exec:: - import json - from netutils.lib_mapper import PYNTC_LIB_MAPPER - json_obj = json.dumps(PYNTC_LIB_MAPPER, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: PYNTC_table.rst Reverse PyNTC Mapper -==================== -.. exec:: - import json - from netutils.lib_mapper import PYNTC_LIB_MAPPER_REVERSE - json_obj = json.dumps(PYNTC_LIB_MAPPER_REVERSE, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: PYNTC_reverse_table.rst Ansible Mapper -============== -.. exec:: - import json - from netutils.lib_mapper import ANSIBLE_LIB_MAPPER - json_obj = json.dumps(ANSIBLE_LIB_MAPPER, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: ANSIBLE_table.rst Reverse Ansible Mapper -====================== -.. exec:: - import json - from netutils.lib_mapper import ANSIBLE_LIB_MAPPER_REVERSE - json_obj = json.dumps(ANSIBLE_LIB_MAPPER_REVERSE, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: ANSIBLE_reverse_table.rst PyATS Mapper -============== -.. exec:: - import json - from netutils.lib_mapper import PYATS_LIB_MAPPER - json_obj = json.dumps(PYATS_LIB_MAPPER, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: PYATS_table.rst Reverse PyATS Mapper -==================== -.. exec:: - import json - from netutils.lib_mapper import PYATS_LIB_MAPPER_REVERSE - json_obj = json.dumps(PYATS_LIB_MAPPER_REVERSE, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: PYATS_reverse_table.rst Scrapli Mapper -============== -.. exec:: - import json - from netutils.lib_mapper import SCRAPLI_LIB_MAPPER - json_obj = json.dumps(SCRAPLI_LIB_MAPPER, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: SCRAPLI_table.rst Reverse Scrapli Mapper -====================== -.. exec:: - import json - from netutils.lib_mapper import SCRAPLI_LIB_MAPPER_REVERSE - json_obj = json.dumps(SCRAPLI_LIB_MAPPER_REVERSE, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: SCRAPLI_reverse_table.rst NTC Templates Mapper -==================== -.. exec:: - import json - from netutils.lib_mapper import NTCTEMPLATES_LIB_MAPPER - json_obj = json.dumps(NTCTEMPLATES_LIB_MAPPER, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: NTCTEMPLATES_table.rst Reverse NTC Templates Mapper -============================ -.. exec:: - import json - from netutils.lib_mapper import NTCTEMPLATES_LIB_MAPPER_REVERSE - json_obj = json.dumps(NTCTEMPLATES_LIB_MAPPER_REVERSE, sort_keys=True, indent=4) - json_obj = json_obj[:-1] + " }" - print(f".. code-block:: JavaScript\n\n {json_obj}\n\n") +============================== +.. include:: NTCTEMPLATES_reverse_table.rst diff --git a/docs/source/table_template.j2 b/docs/source/table_template.j2 new file mode 100644 index 00000000..b9394d03 --- /dev/null +++ b/docs/source/table_template.j2 @@ -0,0 +1,9 @@ +.. raw:: html + + + {% for lib_name in lib_names %}{% endfor %} + {% for specific_driver, normalized_driver in mappings.items() %} + + + {% endfor %} +
{{lib_name|upper}}
{{ specific_driver }}{{ normalized_driver }}
\ No newline at end of file diff --git a/netutils/lib_mapper.py b/netutils/lib_mapper.py index c55c7c9a..d7567d91 100644 --- a/netutils/lib_mapper.py +++ b/netutils/lib_mapper.py @@ -283,3 +283,19 @@ MAIN_LIB_MAPPER["ruckus_icx"] = "ruckus_icx" MAIN_LIB_MAPPER["vmware_nsxv"] = "vmware_nsxv" MAIN_LIB_MAPPER["watchguard_firebox"] = "watchguard_firebox" + +# Used for dynamic creation of html library tables +_LIST_OF_MAP_DICTS = [ + "NAPALM_LIB_MAPPER", + "NAPALM_LIB_MAPPER_REVERSE", + "PYNTC_LIB_MAPPER", + "PYNTC_LIB_MAPPER_REVERSE", + "ANSIBLE_LIB_MAPPER", + "ANSIBLE_LIB_MAPPER_REVERSE", + "PYATS_LIB_MAPPER", + "PYATS_LIB_MAPPER_REVERSE", + "SCRAPLI_LIB_MAPPER", + "SCRAPLI_LIB_MAPPER_REVERSE", + "NTCTEMPLATES_LIB_MAPPER", + "NTCTEMPLATES_LIB_MAPPER_REVERSE", +] From 9966751056ed5862b348f7ea19e9a5e21d79eb68 Mon Sep 17 00:00:00 2001 From: Adam Byczkowski Date: Mon, 25 Apr 2022 19:36:58 -0500 Subject: [PATCH 2/2] Updated per review suggestions --- docs/source/conf.py | 12 ++++++++++-- netutils/lib_mapper.py | 16 ---------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/docs/source/conf.py b/docs/source/conf.py index 1b4266e8..5682ad7d 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -72,10 +72,18 @@ def build_mapping_tables(app): """Build the library mappings tables.""" env = Environment(loader=FileSystemLoader("docs/source")) template_file = env.get_template("table_template.j2") - for dict_name in lib_mapper._LIST_OF_MAP_DICTS: + + LIST_OF_MAP_DICTS = [] + for attr in dir(lib_mapper): + if (attr.endswith("MAPPER_REVERSE") or attr.endswith("_MAPPER")) and not ( + attr.startswith("_") or attr.startswith("NETMIKO") or attr.startswith("MAIN") + ): + LIST_OF_MAP_DICTS.append(attr) + + for dict_name in LIST_OF_MAP_DICTS: lib_name = dict_name.split("_")[0] filename = f"{lib_name}_reverse" if "REVERSE" in dict_name else lib_name - headers = ["NETMIKO", lib_name] if "REVERSE" in dict_name else [lib_name, "NETMIKO"] + headers = ["NORMALIZED", lib_name] if "REVERSE" in dict_name else [lib_name, "NORMALIZED"] rendered_template = template_file.render(lib_names=headers, mappings=getattr(lib_mapper, dict_name)) with open(f"docs/source/netutils/lib_mapping/{filename}_table.rst", "w") as table_file: table_file.write(rendered_template) diff --git a/netutils/lib_mapper.py b/netutils/lib_mapper.py index d7567d91..c55c7c9a 100644 --- a/netutils/lib_mapper.py +++ b/netutils/lib_mapper.py @@ -283,19 +283,3 @@ MAIN_LIB_MAPPER["ruckus_icx"] = "ruckus_icx" MAIN_LIB_MAPPER["vmware_nsxv"] = "vmware_nsxv" MAIN_LIB_MAPPER["watchguard_firebox"] = "watchguard_firebox" - -# Used for dynamic creation of html library tables -_LIST_OF_MAP_DICTS = [ - "NAPALM_LIB_MAPPER", - "NAPALM_LIB_MAPPER_REVERSE", - "PYNTC_LIB_MAPPER", - "PYNTC_LIB_MAPPER_REVERSE", - "ANSIBLE_LIB_MAPPER", - "ANSIBLE_LIB_MAPPER_REVERSE", - "PYATS_LIB_MAPPER", - "PYATS_LIB_MAPPER_REVERSE", - "SCRAPLI_LIB_MAPPER", - "SCRAPLI_LIB_MAPPER_REVERSE", - "NTCTEMPLATES_LIB_MAPPER", - "NTCTEMPLATES_LIB_MAPPER_REVERSE", -]