Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion nettacker/core/template.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import copy
import re

import yaml

from nettacker.config import Config
from nettacker.core.utils import common as common_utils


class TemplateLoader:
Expand All @@ -27,6 +29,26 @@ def parse(module_content, module_inputs):

return module_content

def _apply_dynamic_placeholders(self, content: str) -> str:
"""
Handle runtime placeholders like:
- {rand_str(10)}
"""

def _rand_str_replacer(match):
length = min(
int(match.group(1)),
256, # maximum length allowed in rand_str
)
return common_utils.generate_random_token(length)

content = re.sub(
r"\{rand_str\((\d+)\)\}",
_rand_str_replacer,
content,
)
return content
Comment thread
coderabbitai[bot] marked this conversation as resolved.

def open(self):
module_name_parts = self.name.split("_")
action = module_name_parts[-1]
Expand All @@ -36,7 +58,9 @@ def open(self):
return yaml_file.read()

def format(self):
return self.open().format(**self.inputs)
content = self.open()
content = self._apply_dynamic_placeholders(content)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Add tests for dynamic placeholder expansion

This introduces a new template-preprocessing path without any test coverage, so regressions involving replacement, the 256-character cap, multiple placeholders, or coexistence with ordinary placeholders such as {target} can pass unnoticed. Add focused tests under tests/core/ for the new formatting behavior.

AGENTS.md reference: AGENTS.md:L27-L30

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add tests once this is merged

return content.format(**self.inputs)

def load(self):
return self.parse(yaml.safe_load(self.format()), self.inputs)
Loading