Skip to content

Commit

Permalink
expose jinja2 variables
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Jan 16, 2026
1 parent 32457e7 commit 1d11503
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions local-app/python-tools/sso-tools/sso-create-sc-group.py
Original file line number Diff line number Diff line change
@@ -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.")
Expand All @@ -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)

Expand All @@ -35,43 +38,42 @@ 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,
"group": group_name,
"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)

target_path.write_text(output_content)
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()

0 comments on commit 1d11503

Please sign in to comment.