Skip to content

Commit

Permalink
add review improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
peters-david committed Jan 30, 2025
1 parent 71756f4 commit c9f27e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-and-publish-container-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
dockerfile: Dockerfile
- uses: ricardochaves/python-lint@v1.4.0
with:
args: --max-line-length=120
use-pylint: false
use-flake8: false
use-black: false
Expand Down
56 changes: 19 additions & 37 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,36 +70,22 @@ def get_member_id(members: [ProjectMemberEntity], username: str) -> int | None:


async def fill_template(client, path: str, logger) -> str:
"""Takes the path to a template file and returns its content with the
replaced ids
"""
"""Reads a template file, replaces placeholders with fetched IDs, and returns the updated content."""
with open(path, 'r') as file:
config = file.read()
placeholders = re.findall(
r'{{[ ]*(?:project|registry):[A-z,0-9,.,\-,_]+[ ]*}}', config
)
logger.info("Found id templates", extra={"placeholders": placeholders})
placeholders = [
placeholder.replace('{{', '').replace(' ', '').replace('}}', '')
for placeholder in placeholders
]
replacements = {}
for placeholder in placeholders:
placeholder_type, placeholder_value = placeholder.split(':')
replacement_value = await fetch_id(
client, placeholder_type, placeholder_value
)
# The mustache specification, which the chevron library builds
# on top of, does not allow for dots in keys. Instead, keys with
# dots are meant to reference nested objects. In order to have
# the right objects to reference, nested objects / dictionaries
# are created for keys with dots.
replacement_last_part = str(replacement_value)
replacement_keys = placeholder.split('.')
replacement_keys.append(replacement_last_part)
insert_into_dict(replacements, replacement_keys)
config = chevron.render(config, replacements)
return config

placeholders = re.findall(r'{{\s*(?:project|registry):[\w.\-_]+\s*}}', config)
logger.info("Found id templates", extra={"placeholders": placeholders})

replacements = {}
for placeholder in (p.strip(" {}") for p in placeholders):
placeholder_type, placeholder_value = placeholder.split(':')
replacement_value = await fetch_id(client, placeholder_type, placeholder_value)

# Create nested dictionary structure for replacements
insert_into_dict(replacements, placeholder.split('.') + [str(replacement_value)])

return chevron.render(config, replacements)


async def fetch_id(
Expand All @@ -123,7 +109,7 @@ async def fetch_id(


def insert_into_dict(d: dict, parts: [str]) -> None:
"""Takes a dictionary and insert nested objects given as list.
"""Inserts nested keys and value into a dictionary.
>>> d = {}
>>> insert_into_dict(d, ["a", "b", 1])
Expand All @@ -133,11 +119,7 @@ def insert_into_dict(d: dict, parts: [str]) -> None:
>>> d
{"a": {"b": 1, c: "2"}}
"""
assert len(parts) >= 2
for part in parts[:-2]:
if part not in d:
d[part] = {}
d = d[part]
last_key = parts[-2]
value = parts[-1]
d[last_key] = value
*keys, last_key, value = parts
for key in keys:
d = d.setdefault(key, {})
d[last_key] = value

0 comments on commit c9f27e6

Please sign in to comment.