Skip to content

Commit 3c532be

Browse files
committed
Add support for template in protocols
1 parent b9a7ab5 commit 3c532be

File tree

5 files changed

+24669
-16
lines changed

5 files changed

+24669
-16
lines changed

infrahub_sdk/protocols_generator/generator.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from collections.abc import Mapping
44
from pathlib import Path
5-
from typing import Any
65

76
import jinja2
87

@@ -16,6 +15,7 @@
1615
NodeSchemaAPI,
1716
ProfileSchemaAPI,
1817
RelationshipSchemaAPI,
18+
TemplateSchemaAPI,
1919
)
2020
from .constants import ATTRIBUTE_KIND_MAP, TEMPLATE_FILE_NAME
2121

@@ -30,6 +30,7 @@ def __init__(self, schema: dict[str, MainSchemaTypesAll]):
3030
self.generics: dict[str, GenericSchemaAPI | GenericSchema] = {}
3131
self.nodes: dict[str, NodeSchemaAPI | NodeSchema] = {}
3232
self.profiles: dict[str, ProfileSchemaAPI] = {}
33+
self.templates: dict[str, TemplateSchemaAPI] = {}
3334

3435
for name, schema_type in schema.items():
3536
if isinstance(schema_type, (GenericSchemaAPI, GenericSchema)):
@@ -38,6 +39,8 @@ def __init__(self, schema: dict[str, MainSchemaTypesAll]):
3839
self.nodes[name] = schema_type
3940
if isinstance(schema_type, ProfileSchemaAPI):
4041
self.profiles[name] = schema_type
42+
if isinstance(schema_type, TemplateSchemaAPI):
43+
self.templates[name] = schema_type
4144

4245
self.base_protocols = [
4346
e
@@ -53,30 +56,44 @@ def __init__(self, schema: dict[str, MainSchemaTypesAll]):
5356
self.sorted_profiles = self._sort_and_filter_models(
5457
self.profiles, filters=["CoreProfile"] + self.base_protocols
5558
)
59+
self.sorted_templates = self._sort_and_filter_models(
60+
self.templates, filters=["CoreObjectTemplate"] + self.base_protocols
61+
)
5662

5763
def render(self, sync: bool = True) -> str:
5864
jinja2_env = jinja2.Environment(loader=jinja2.BaseLoader(), trim_blocks=True, lstrip_blocks=True)
59-
jinja2_env.filters["inheritance"] = self._jinja2_filter_inheritance
6065
jinja2_env.filters["render_attribute"] = self._jinja2_filter_render_attribute
6166
jinja2_env.filters["render_relationship"] = self._jinja2_filter_render_relationship
67+
jinja2_env.filters["syncify"] = self._jinja2_filter_syncify
6268

6369
template = jinja2_env.from_string(load_template())
6470
return template.render(
6571
generics=self.sorted_generics,
6672
nodes=self.sorted_nodes,
6773
profiles=self.sorted_profiles,
74+
templates=self.sorted_templates,
6875
base_protocols=self.base_protocols,
6976
core_node_name="CoreNodeSync" if sync else "CoreNode",
7077
sync=sync,
7178
)
7279

7380
@staticmethod
74-
def _jinja2_filter_inheritance(value: dict[str, Any]) -> str:
75-
inherit_from: list[str] = value.get("inherit_from", [])
81+
def _jinja2_filter_syncify(value: str | list, sync: bool = False) -> str | list:
82+
"""Filter to help with the convertion to sync
83+
84+
If a string is provided, append Sync to the end of the string
85+
If a list is provided, search for CoreNode and replace it with CoreNodeSync
86+
"""
87+
if not sync:
88+
return value
89+
90+
if isinstance(value, str):
91+
return f"{value}Sync"
92+
93+
if isinstance(value, list):
94+
return [f"{item}Sync" if item == "CoreNode" else item for item in value]
7695

77-
if not inherit_from:
78-
return "CoreNode"
79-
return ", ".join(inherit_from)
96+
return value
8097

8198
@staticmethod
8299
def _jinja2_filter_render_attribute(value: AttributeSchemaAPI) -> str:

infrahub_sdk/protocols_generator/template.j2

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class {{ generic.namespace + generic.name }}({{core_node_name}}):
6868
{% for node in nodes %}
6969

7070

71-
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or core_node_name }}):
71+
class {{ node.namespace + node.name }}({{ node.inherit_from | syncify(sync) | join(", ") or core_node_name }}):
7272
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
7373
pass
7474
{% endif %}
@@ -91,7 +91,7 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or core
9191
{% for node in profiles %}
9292

9393

94-
class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or core_node_name }}):
94+
class {{ node.namespace + node.name }}({{ node.inherit_from | syncify(sync) | join(", ") or core_node_name }}):
9595
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
9696
pass
9797
{% endif %}
@@ -102,12 +102,21 @@ class {{ node.namespace + node.name }}({{ node.inherit_from | join(", ") or core
102102
{{ relationship | render_relationship(sync) }}
103103
{% endfor %}
104104
{% if node.hierarchical | default(false) %}
105-
{% if sync %}
106-
parent: RelatedNodeSync
107-
children: RelationshipManagerSync
108-
{% else %}
109-
parent: RelatedNode
110-
children: RelationshipManager
111-
{% endif %}
105+
parent: {{ "RelatedNode" | sincify(sync)}}
106+
children: {{ "RelationshipManager" | sincify(sync)}}
112107
{% endif %}
113108
{% endfor %}
109+
{% for node in templates %}
110+
111+
112+
class {{ node.namespace + node.name }}({{ node.inherit_from | syncify(sync) | join(", ") or core_node_name }}):
113+
{% if not node.attributes|default([]) and not node.relationships|default([]) %}
114+
pass
115+
{% endif %}
116+
{% for attribute in node.attributes|default([]) %}
117+
{{ attribute | render_attribute }}
118+
{% endfor %}
119+
{% for relationship in node.relationships|default([]) %}
120+
{{ relationship | render_relationship(sync) }}
121+
{% endfor %}
122+
{% endfor %}

0 commit comments

Comments
 (0)