Skip to content

Commit

Permalink
feat(omegaconf): fix escaped interpolations
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoVoges committed Aug 9, 2023
1 parent b239ef2 commit a0ea50b
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 50 deletions.
24 changes: 13 additions & 11 deletions kapitan/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def migrate(input: str):
def migrate_dir(path: str):
"""migrates all .yml/.yaml files in the given path to omegaconfs syntax"""

# TODO: write migrations to temp dir and copy only if suceeded

for root, subdirs, files in os.walk(path):
for file in files:
file = os.path.join(root, file)
Expand Down Expand Up @@ -140,22 +142,22 @@ def migrate_file(file: str):

@staticmethod
def migrate_str(content: str):
# replace colons in tags and change _reclass_ to _meta_

# TODO: dont migrate custom resolvers
# TODO: migrate interpolations with '.' in the keyname

# search for interpolation pattern
updated_content = regex.sub(
r"(?<!\\)\${([^\${}]*+(?:(?R)[^\${}]*)*+)}",
lambda match: "${" + match.group(1).replace(":", ".").replace("_reclass_", "_meta_") + "}",
lambda match: "${" + match.group(1)
# .replace(".", ",") # support interpolations with '.' in keyname
.replace(":", ".",).replace( # migrate path delimiter
"_reclass_", "_meta_"
)
+ "}", # migrate meta data
content,
)

# replace escaped tags with specific resolver
excluded_chars = "!"
invalid = any(c in updated_content for c in excluded_chars)
updated_content = regex.sub(
r"\\\${([^\${}]*+(?:(?R)[^\${}]*)*+)}",
lambda match: ("${tag:" if not invalid else "\\\\\\${") + match.group(1) + "}",
updated_content,
)

return updated_content


Expand Down
6 changes: 3 additions & 3 deletions kapitan/omegaconf_inv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
# Copyright 2023 nexenio
import logging
import os
import time

import regex
from omegaconf import ListMergeMode, Node, OmegaConf, errors
from omegaconf import ListMergeMode, OmegaConf

from kapitan.errors import InventoryError
from kapitan.resolvers import register_resolvers
Expand Down Expand Up @@ -137,6 +135,8 @@ def load_target(
else:
target_config_parameters = class_config_parameters

logger.debug(f"{target_name}: merged class {class_name}")

if not target_config_parameters:
raise InventoryError("empty target")

Expand Down
17 changes: 8 additions & 9 deletions kapitan/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import sys

from omegaconf import ListMergeMode, Node, OmegaConf
from omegaconf import Container, ListMergeMode, Node, OmegaConf

logger = logging.getLogger(__name__)

Expand All @@ -26,14 +26,13 @@ def fullkey(_node_: Node):
return _node_._get_full_key("")


def escape_tag(input: str, _node_: Node):
"""resolver function, that returns an escaped tag with the input"""
if "\n" in str(_node_):
escape = "\\\\\\"
else:
escape = "\\"
def access_key_with_dots(*key: str, _root_: Container):
"""resolver function, that accesses a key with dots in it"""
value = _root_
for part in key:
value = value[part]

return escape + "${" + input + "}"
return value


def merge(*args):
Expand Down Expand Up @@ -120,7 +119,7 @@ def register_resolvers(inventory_path: str) -> None:
OmegaConf.register_new_resolver("relpath", relpath)

# yaml object utility functions
OmegaConf.register_new_resolver("tag", escape_tag)
OmegaConf.register_new_resolver("access", access_key_with_dots)
OmegaConf.register_new_resolver("merge", merge)
OmegaConf.register_new_resolver("dict", to_dict)
OmegaConf.register_new_resolver("list", to_list)
Expand Down
6 changes: 3 additions & 3 deletions kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def generate_inventory(args):
except Exception as e:
if not isinstance(e, KapitanError):
logger.exception("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
sys.exit(1)
logger.error(e)


def get_inventory(inventory_path, ignore_class_notfound=False, targets=[]):
Expand Down Expand Up @@ -335,8 +335,8 @@ def get_inventory(inventory_path, ignore_class_notfound=False, targets=[]):
try:
inventory = inventory_backend.inventory()
except Exception as e:
if not args.migrate:
logger.warning("Make sure to migrate your inventory using --migrate")
# if hasattr(args, "omegaconf") and args.omegaconf and not args.migrate:
# logger.warning("Make sure to migrate your inventory using --migrate if you are using omegaconf the first time")
raise InventoryError(e)

cached.inv = inventory
Expand Down
Loading

0 comments on commit a0ea50b

Please sign in to comment.