From 1d115038004d055450aecff52bfea6bd0218e6cb Mon Sep 17 00:00:00 2001 From: badra001 Date: Fri, 16 Jan 2026 10:08:19 -0500 Subject: [PATCH] expose jinja2 variables --- .../sso-tools/sso-create-sc-group.py | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/local-app/python-tools/sso-tools/sso-create-sc-group.py b/local-app/python-tools/sso-tools/sso-create-sc-group.py index dd138b8d..b9652082 100755 --- a/local-app/python-tools/sso-tools/sso-create-sc-group.py +++ b/local-app/python-tools/sso-tools/sso-create-sc-group.py @@ -1,11 +1,14 @@ -#!/bin/env python +#!/usr/bin/env python import argparse import shutil from pathlib import Path from jinja2 import Environment, FileSystemLoader -__version__ = "1.0.0" +# Version History: +# 1.0.0: Initial release +# 1.0.1: Added shebang, exposed variables to Jinja2, updated versioning +__version__ = "1.0.1" def create_sc_group(): parser = argparse.ArgumentParser(description="Automate file creation from templates.") @@ -16,16 +19,16 @@ def create_sc_group(): args = parser.parse_args() - # 1. Logic for group name and folder naming + # 1. Determine the 'group' name (from arg or folder name) group_name = args.group if args.group else Path.cwd().name - # Construct created_group name based on presence of application_label + # 2. Define the 'created_group' string if args.application_label: created_group = f"{args.business_label}-{args.application_label}-{group_name}" else: created_group = f"{args.business_label}-{group_name}" - # 2. Setup paths + # 3. Setup paths template_dir = Path("TEMPLATE") target_dir = Path(created_group) @@ -35,11 +38,10 @@ def create_sc_group(): target_dir.mkdir(parents=True, exist_ok=True) - # 3. Initialize Jinja2 Environment + # 4. Initialize Jinja2 Environment env = Environment(loader=FileSystemLoader(str(template_dir))) - # 4. Expose all arguments as variables for rendering - # This dictionary maps exactly to what you can use in your {{ }} tags + # 5. Expose variables to Jinja2 templates render_vars = { "business_label": args.business_label, "application_label": args.application_label, @@ -47,19 +49,19 @@ def create_sc_group(): "created_group": created_group } - print(f"Processing templates for: {created_group}...") + print(f"Creating files for: {created_group}") - # 5. File Processing Loop + # 6. Process files in TEMPLATE/ for item in template_dir.iterdir(): if item.is_dir(): continue # Skips subdirectories if item.suffix == ".j2": - # Target logic: Replace 'GROUP' with created_group and strip '.j2' + # Rule: Rename GROUP.extension.j2 to {created_group}.extension new_filename = item.name.replace("GROUP", created_group).replace(".j2", "") target_path = target_dir / new_filename - # Load and render + # Render the template with variables template = env.get_template(item.name) output_content = template.render(render_vars) @@ -67,11 +69,11 @@ def create_sc_group(): print(f" [Rendered] {item.name} -> {new_filename}") else: - # Standard copy for non-template files + # Rule: Copy files not ending in .j2 directly shutil.copy2(item, target_dir / item.name) print(f" [Copied] {item.name}") - print(f"\nSuccessfully created group directory: {created_group}") + print(f"\nDone. Files located in: ./{created_group}") if __name__ == "__main__": create_sc_group()