Skip to content

Commit 47345b5

Browse files
committed
bump
1 parent f8f5731 commit 47345b5

File tree

6 files changed

+219
-4
lines changed

6 files changed

+219
-4
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Python SDK Templating
3+
---
4+
5+
6+
## Builtin Jinja2 filters
7+
8+
The following filters are those that are shipped with Jinja2 and enabled within Infrahub. The trusted column indicates if the filter is allowed for use with Infrahub's computed attributes when the server is configured in strict mode.
9+
10+
| Name | Trusted |
11+
|----------|----------|
12+
{% for filter in builtin %}
13+
| {{ filter.name }} | {% if filter.trusted %}{% else %}{% endif %} |
14+
{% endfor %}
15+
16+
## Netutils filters
17+
18+
The following Jinja2 filters from <a href="https://netutils.readthedocs.io">netutils</a> are included within Infrahub.
19+
20+
| Name | Trusted |
21+
|----------|----------|
22+
{% for filter in netutils %}
23+
| {{ filter.name }} | {% if filter.trusted %}{% else %}{% endif %} |
24+
{% endfor %}
25+
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
---
2+
title: Python SDK Templating
3+
---
4+
5+
6+
## Builtin Jinja2 filters
7+
8+
The following filters are those that are shipped with Jinja2 and enabled within Infrahub. The trusted column indicates if the filter is allowed for use with Infrahub's computed attributes when the server is configured in strict mode.
9+
10+
| Name | Trusted |
11+
|----------|----------|
12+
| abs ||
13+
| attr ||
14+
| batch ||
15+
| capitalize ||
16+
| center ||
17+
| count ||
18+
| d ||
19+
| default ||
20+
| dictsort ||
21+
| e ||
22+
| escape ||
23+
| filesizeformat ||
24+
| first ||
25+
| float ||
26+
| forceescape ||
27+
| format ||
28+
| groupby ||
29+
| indent ||
30+
| int ||
31+
| items ||
32+
| join ||
33+
| last ||
34+
| length ||
35+
| list ||
36+
| lower ||
37+
| map ||
38+
| max ||
39+
| min ||
40+
| pprint ||
41+
| random ||
42+
| reject ||
43+
| rejectattr ||
44+
| replace ||
45+
| reverse ||
46+
| round ||
47+
| safe ||
48+
| select ||
49+
| selectattr ||
50+
| slice ||
51+
| sort ||
52+
| string ||
53+
| striptags ||
54+
| sum ||
55+
| title ||
56+
| tojson ||
57+
| trim ||
58+
| truncate ||
59+
| unique ||
60+
| upper ||
61+
| urlencode ||
62+
| urlize ||
63+
| wordcount ||
64+
| wordwrap ||
65+
| xmlattr ||
66+
67+
## Netutils filters
68+
69+
The following Jinja2 filters from <a href="https://netutils.readthedocs.io">netutils</a> are included within Infrahub.
70+
71+
| Name | Trusted |
72+
|----------|----------|
73+
| abbreviated_interface_name ||
74+
| abbreviated_interface_name_list ||
75+
| asn_to_int ||
76+
| bits_to_name ||
77+
| bytes_to_name ||
78+
| canonical_interface_name ||
79+
| canonical_interface_name_list ||
80+
| cidr_to_netmask ||
81+
| cidr_to_netmaskv6 ||
82+
| clean_config ||
83+
| compare_version_loose ||
84+
| compare_version_strict ||
85+
| config_compliance ||
86+
| config_section_not_parsed ||
87+
| delimiter_change ||
88+
| diff_network_config ||
89+
| feature_compliance ||
90+
| find_unordered_cfg_lines ||
91+
| fqdn_to_ip ||
92+
| get_all_host ||
93+
| get_broadcast_address ||
94+
| get_first_usable ||
95+
| get_ips_sorted ||
96+
| get_nist_urls ||
97+
| get_nist_vendor_platform_urls ||
98+
| get_oui ||
99+
| get_peer_ip ||
100+
| get_range_ips ||
101+
| get_upgrade_path ||
102+
| get_usable_range ||
103+
| hash_data ||
104+
| int_to_asdot ||
105+
| interface_range_compress ||
106+
| interface_range_expansion ||
107+
| ip_addition ||
108+
| ip_subtract ||
109+
| ip_to_bin ||
110+
| ip_to_hex ||
111+
| ipaddress_address ||
112+
| ipaddress_interface ||
113+
| ipaddress_network ||
114+
| is_classful ||
115+
| is_fqdn_resolvable ||
116+
| is_ip ||
117+
| is_ip_range ||
118+
| is_ip_within ||
119+
| is_netmask ||
120+
| is_network ||
121+
| is_reversible_wildcardmask ||
122+
| is_valid_mac ||
123+
| longest_prefix_match ||
124+
| mac_normalize ||
125+
| mac_to_format ||
126+
| mac_to_int ||
127+
| mac_type ||
128+
| name_to_bits ||
129+
| name_to_bytes ||
130+
| name_to_name ||
131+
| netmask_to_cidr ||
132+
| netmask_to_wildcardmask ||
133+
| normalise_delimiter_caret_c ||
134+
| paloalto_panos_brace_to_set ||
135+
| paloalto_panos_clean_newlines ||
136+
| regex_findall ||
137+
| regex_match ||
138+
| regex_search ||
139+
| regex_split ||
140+
| regex_sub ||
141+
| sanitize_config ||
142+
| section_config ||
143+
| sort_interface_list ||
144+
| split_interface ||
145+
| uptime_seconds_to_string ||
146+
| uptime_string_to_seconds ||
147+
| version_metadata ||
148+
| vlanconfig_to_list ||
149+
| vlanlist_to_config ||
150+
| wildcardmask_to_netmask ||

docs/sidebars-python-sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const sidebars: SidebarsConfig = {
3838
label: 'Reference',
3939
items: [
4040
'reference/config',
41+
'reference/templating',
4142
],
4243
},
4344
],

infrahub_sdk/template/filters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ class FilterDefinition:
2828
FilterDefinition(name="groupby", trusted=False, source="jinja2"),
2929
FilterDefinition(name="indent", trusted=True, source="jinja2"),
3030
FilterDefinition(name="int", trusted=True, source="jinja2"),
31+
FilterDefinition(name="items", trusted=False, source="jinja2"),
3132
FilterDefinition(name="join", trusted=True, source="jinja2"),
3233
FilterDefinition(name="last", trusted=True, source="jinja2"),
3334
FilterDefinition(name="length", trusted=True, source="jinja2"),
3435
FilterDefinition(name="list", trusted=True, source="jinja2"),
3536
FilterDefinition(name="lower", trusted=True, source="jinja2"),
36-
FilterDefinition(name="items", trusted=False, source="jinja2"),
3737
FilterDefinition(name="map", trusted=False, source="jinja2"),
38-
FilterDefinition(name="min", trusted=True, source="jinja2"),
3938
FilterDefinition(name="max", trusted=True, source="jinja2"),
39+
FilterDefinition(name="min", trusted=True, source="jinja2"),
4040
FilterDefinition(name="pprint", trusted=False, source="jinja2"),
4141
FilterDefinition(name="random", trusted=False, source="jinja2"),
4242
FilterDefinition(name="reject", trusted=False, source="jinja2"),
@@ -53,6 +53,7 @@ class FilterDefinition:
5353
FilterDefinition(name="striptags", trusted=True, source="jinja2"),
5454
FilterDefinition(name="sum", trusted=True, source="jinja2"),
5555
FilterDefinition(name="title", trusted=True, source="jinja2"),
56+
FilterDefinition(name="tojson", trusted=False, source="jinja2"),
5657
FilterDefinition(name="trim", trusted=True, source="jinja2"),
5758
FilterDefinition(name="truncate", trusted=True, source="jinja2"),
5859
FilterDefinition(name="unique", trusted=False, source="jinja2"),
@@ -62,7 +63,6 @@ class FilterDefinition:
6263
FilterDefinition(name="wordcount", trusted=True, source="jinja2"),
6364
FilterDefinition(name="wordwrap", trusted=True, source="jinja2"),
6465
FilterDefinition(name="xmlattr", trusted=False, source="jinja2"),
65-
FilterDefinition(name="tojson", trusted=False, source="jinja2"),
6666
]
6767

6868

tasks.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import sys
23
from pathlib import Path
34
from typing import Any
@@ -14,6 +15,7 @@ def _generate(context: Context) -> None:
1415
"""Generate documentation output from code."""
1516
_generate_infrahubctl_documentation(context=context)
1617
_generate_infrahub_sdk_configuration_documentation()
18+
_generate_infrahub_sdk_template_documentation()
1719

1820

1921
def _generate_infrahubctl_documentation(context: Context) -> None:
@@ -89,6 +91,24 @@ def _generate_infrahub_sdk_configuration_documentation() -> None:
8991
print(f"Docs saved to: {output_file}")
9092

9193

94+
def _generate_infrahub_sdk_template_documentation() -> None:
95+
"""Generate documentation for the Infrahub SDK template reference."""
96+
from infrahub_sdk.template import Jinja2Template
97+
from infrahub_sdk.template.filters import BUILTIN_FILTERS, NETUTILS_FILTERS
98+
99+
output_file = DOCUMENTATION_DIRECTORY / "docs" / "python-sdk" / "reference" / "templating.mdx"
100+
jinja2_template = Jinja2Template(
101+
template=Path("sdk_template_reference.j2"),
102+
template_directory=DOCUMENTATION_DIRECTORY / "_templates",
103+
)
104+
105+
rendered_file = asyncio.run(
106+
jinja2_template.render(variables={"builtin": BUILTIN_FILTERS, "netutils": NETUTILS_FILTERS})
107+
)
108+
output_file.write_text(rendered_file, encoding="utf-8")
109+
print(f"Docs saved to: {output_file}")
110+
111+
92112
def _get_env_vars() -> dict[str, list[str]]:
93113
"""Retrieve environment variables for Infrahub SDK configuration."""
94114
from collections import defaultdict
@@ -170,7 +190,7 @@ def docs_build(context: Context) -> None:
170190
with context.cd(DOCUMENTATION_DIRECTORY):
171191
output = context.run(exec_cmd)
172192

173-
if output.exited != 0:
193+
if output and output.exited != 0:
174194
sys.exit(-1)
175195

176196

@@ -184,3 +204,4 @@ def generate_infrahubctl(context: Context) -> None:
184204
def generate_python_sdk(context: Context) -> None: # noqa: ARG001
185205
"""Generate documentation for the Python SDK."""
186206
_generate_infrahub_sdk_configuration_documentation()
207+
_generate_infrahub_sdk_template_documentation()

tests/unit/sdk/test_template.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
JinjaTemplateSyntaxError,
1515
JinjaTemplateUndefinedError,
1616
)
17+
from infrahub_sdk.template.filters import (
18+
BUILTIN_FILTERS,
19+
NETUTILS_FILTERS,
20+
FilterDefinition,
21+
)
1722
from infrahub_sdk.template.models import UndefinedJinja2Error
1823

1924
CURRENT_DIRECTORY = Path(__file__).parent
@@ -256,6 +261,19 @@ async def test_validate_filter() -> None:
256261
assert exc.value.message == "The 'get_all_host' filter isn't allowed to be used"
257262

258263

264+
@pytest.mark.parametrize(
265+
"filter_collection",
266+
[
267+
pytest.param(BUILTIN_FILTERS, id="builtin-filters"),
268+
pytest.param(NETUTILS_FILTERS, id="netutils-filters"),
269+
],
270+
)
271+
def test_validate_filter_sorting(filter_collection: list[FilterDefinition]) -> None:
272+
"""Test to validate that the filter names are in alphabetical order, for the docs and general sanity."""
273+
names = [filter_definition.name for filter_definition in filter_collection]
274+
assert names == sorted(names)
275+
276+
259277
def _divide_by_zero(number: int) -> float:
260278
return number / 0
261279

0 commit comments

Comments
 (0)