diff --git a/.github/workflows/build-and-publish-container-image.yaml b/.github/workflows/build-and-publish-container-image.yaml index 5b6c28e..fa17b74 100644 --- a/.github/workflows/build-and-publish-container-image.yaml +++ b/.github/workflows/build-and-publish-container-image.yaml @@ -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 diff --git a/src/utils.py b/src/utils.py index 6213eef..0047d1f 100644 --- a/src/utils.py +++ b/src/utils.py @@ -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( @@ -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]) @@ -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 \ No newline at end of file