Skip to content

Commit 80dceb0

Browse files
authored
Add Stringify Filter for Jinja2 Rendering (#9)
This pull request introduces a new `stringify` filter to the Jinja2 templating engine, enhancing the flexibility and cleanliness of variable rendering within templates. #### Changes: - **Added `stringify` filter**: - Implemented a function that converts strings to lowercase, replaces spaces with hyphens, and removes non-alphanumeric characters. - This filter was added to `filters.py` and integrated into the Jinja2 environment in `template_renderer.py`. #### Justification: The `stringify` filter ensures that dynamically generated variable names are formatted in a consistent, URL-safe, and readable way, which is especially useful when rendering template values for deployment configurations. #### Impact: - Allows for better variable handling and formatting in Jinja2 templates, improving the clarity and reliability of dynamically generated content.
1 parent 8f6888d commit 80dceb0

6 files changed

Lines changed: 50 additions & 23 deletions

File tree

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
structure:
22
- README.md:
33
content: |
4-
# Project Name
4+
# {{% project_name %}}
55
66
Brief description of the project.
77
@@ -18,21 +18,16 @@ structure:
1818
Guidelines for contributing to the project.
1919
- CONTRIBUTING.md:
2020
content: |
21-
# Contributing to Project Name
21+
# Contributing to {{% project_name %}}
22+
2223
## How to Contribute
2324
Guidelines for how to contribute to the project.
25+
2426
## Code of Conduct
2527
Code of conduct for contributors.
2628
- CODE_OF_CONDUCT.md:
2729
content: |
2830
# Code of Conduct
2931
Our standards for how to behave within our community.
3032
- LICENSE.md:
31-
content: |
32-
MIT License
33-
Permission is hereby granted, free of charge, to any person obtaining a copy
34-
of this software and associated documentation files (the "Software"), to deal
35-
in the Software without restriction, including without limitation the rights
36-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
37-
copies of the Software, and to permit persons to whom the Software is
38-
furnished to do so, subject to the following conditions:
33+
file: https://raw.githubusercontent.com/git/git-scm.com/main/MIT-LICENSE.txt

contribs/kubernetes-manifests.yaml

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ structure:
44
apiVersion: apps/v1
55
kind: Deployment
66
metadata:
7-
name: example-deployment
7+
name: {{% deployment_name | stringify %}}
88
labels:
9-
app: example-app
9+
app: {{% app_name | stringify %}}
1010
spec:
1111
replicas: 2
1212
selector:
1313
matchLabels:
14-
app: example-app
14+
app: {{% app_name | stringify %}}
1515
template:
1616
metadata:
1717
labels:
18-
app: example-app
18+
app: {{% app_name | stringify %}}
1919
spec:
2020
containers:
21-
- name: example-container
22-
image: nginx:latest
21+
- name: {{% app_name | stringify %}}
22+
image: {{% image_name %}}:{{% image_tag %}}
2323
ports:
2424
- containerPort: 80
2525
- service.yaml:
@@ -30,7 +30,7 @@ structure:
3030
name: example-service
3131
spec:
3232
selector:
33-
app: example-app
33+
app: {{% app_name | stringify %}}
3434
ports:
3535
- protocol: TCP
3636
port: 80
@@ -82,3 +82,20 @@ structure:
8282
kubectl apply -f configmap.yaml
8383
kubectl apply -f secrets.yaml
8484
```
85+
86+
variables:
87+
app_name:
88+
description: "The name of the application."
89+
type: string
90+
default: "example-app"
91+
deployment_name:
92+
description: "The name of the deployment"
93+
type: string
94+
default: "example-app-deployment"
95+
image_name:
96+
description: "The name of the Docker image."
97+
type: string
98+
image_tag:
99+
description: "The tag of the Docker image."
100+
type: string
101+
default: "latest"

contribs/terraform-app.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
structure:
22
- main.tf:
33
content: |
4-
# This is the main Terraform configuration file.
5-
touch_file: 2024-07-17
4+
# This is the main Terraform app main file.
5+
touch_file: {{% now().strftime('%Y-%m-%d') %}}
66
- variables.tf:
77
content: "# This is the Terraform variables file."
88
- outputs.tf:

contribs/terraform-module.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ structure:
1919
}
2020
- README.md:
2121
content: |
22-
# Module Name
22+
# {{% module_name %}}
2323
This module provisions an EC2 instance on AWS.
2424
## Usage
2525
```hcl
2626
module "example" {
27-
source = "./module-name"
27+
source = "./path/to/module/{{% module_name | stringify %}}"
2828
instance_type = "t2.micro"
2929
}
3030
```

struct_module/filters.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import re
23
from github import Github
34

45
def get_latest_release(repo_name):
@@ -23,3 +24,12 @@ def get_latest_release(repo_name):
2324
return default_branch
2425
except Exception as e:
2526
return "LATEST_RELEASE_ERROR"
27+
28+
def stringify(value):
29+
# Convert to lowercase
30+
value = value.lower()
31+
# Replace spaces with hyphens
32+
value = re.sub(r'\s+', '-', value)
33+
# Remove any non-alphanumeric characters (except hyphens)
34+
value = re.sub(r'[^a-z0-9-]', '', value)
35+
return value

struct_module/template_renderer.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# FILE: template_renderer.py
22
import logging
33
from jinja2 import Environment, meta
4-
from struct_module.filters import get_latest_release
4+
from struct_module.filters import get_latest_release, stringify
55

66
class TemplateRenderer:
77
def __init__(self, config_variables):
@@ -15,7 +15,12 @@ def __init__(self, config_variables):
1515
comment_start_string='{#@',
1616
comment_end_string='@#}'
1717
)
18-
self.env.filters['latest_release'] = get_latest_release
18+
19+
custom_filters = {
20+
'latest_release': get_latest_release,
21+
'stringify': stringify,
22+
}
23+
self.env.filters.update(custom_filters)
1924
self.logger = logging.getLogger(__name__)
2025

2126
# Get the config variables from the list and create a dictionary that has

0 commit comments

Comments
 (0)