22
33from collections .abc import Mapping
44from pathlib import Path
5- from typing import Any
65
76import jinja2
87
1615 NodeSchemaAPI ,
1716 ProfileSchemaAPI ,
1817 RelationshipSchemaAPI ,
18+ TemplateSchemaAPI ,
1919)
2020from .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 :
0 commit comments