1111import mdit as _mdit
1212import pyserials as _ps
1313import pylinks as _pl
14+ import jinja2 as _jinja
15+ from loggerman import logger as _logger
1416
1517if _TYPE_CHECKING :
1618 from typing import Literal , Callable , Any , Sequence
@@ -149,7 +151,7 @@ def make_env_file(manager: Literal["apt", "brew", "conda", "pip", "pwsh"], deps:
149151
150152
151153def create_dynamic_file (
152- file_type : Literal ["yaml" , "json" , "toml" , "txt" ],
154+ file_type : Literal ["yaml" , "json" , "toml" , "txt" , "exec" ],
153155 content ,
154156 filters : Sequence [tuple [str , Callable [[Any ], bool ], bool ]] = (),
155157 content_item_separator : str = "\n " ,
@@ -179,7 +181,14 @@ def create_dynamic_file(
179181 if not inplace :
180182 jsonpath = _jpath .parse (jsonpath_str )
181183 content = jsonpath .filter (filter_func , content )
182- if file_type == "txt" :
184+ if order :
185+ key_priority = {key : index for index , key in enumerate (order )}
186+ sorted_items = sorted (
187+ content .items (),
188+ key = lambda item : key_priority .get (item [0 ], len (order ))
189+ )
190+ content = dict (sorted_items )
191+ if file_type in ("txt" , "exec" ):
183192 if isinstance (content , str ):
184193 return content
185194 elif isinstance (content , (list , dict )):
@@ -188,13 +197,6 @@ def create_dynamic_file(
188197 for content_item in (content if isinstance (content , list ) else content .values ())
189198 )
190199 raise ValueError (f"Content type { type (content )} not supported for dynamic files." )
191- if order :
192- key_priority = {key : index for index , key in enumerate (order )}
193- sorted_items = sorted (
194- content .items (),
195- key = lambda item : key_priority .get (item [0 ], len (order ))
196- )
197- content = dict (sorted_items )
198200 return _ps .write .to_string (
199201 data = content ,
200202 data_type = file_type ,
@@ -226,6 +228,35 @@ def create_md_content(file: dict, repo_path: str | _Path) -> str:
226228 return doc_str
227229
228230
231+ def fill_jinja_templates (templates : dict | list | str , jsonpath : str , env_vars : dict | None = None ) -> dict :
232+
233+ def recursive_fill (template , path ):
234+ if isinstance (template , dict ):
235+ filled = {}
236+ for key , value in template .items ():
237+ new_path = f"{ path } .{ key } "
238+ filled [recursive_fill (key , new_path )] = recursive_fill (value , new_path )
239+ return filled
240+ if isinstance (template , list ):
241+ filled = []
242+ for idx , value in enumerate (template ):
243+ new_path = f"{ path } [{ idx } ]"
244+ filled .append (recursive_fill (value , new_path ))
245+ return filled
246+ if isinstance (template , str ):
247+ try :
248+ filled = _jinja .Template (template ).render (env_vars )
249+ except Exception as e :
250+ _logger .critical (
251+ "Jinja Templating" ,
252+ f"Failed to render Jinja template at '{ path } ': { e } " ,
253+ _logger .traceback ()
254+ )
255+ raise ValueError (f"Failed to render Jinja template at '{ path } '" ) from e
256+ return filled
257+ return template
258+
259+ return recursive_fill (templates , jsonpath )
229260
230261
231262
0 commit comments