Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
MatteoVoges committed Oct 30, 2023
1 parent 49eceea commit f139b23
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 6 deletions.
20 changes: 20 additions & 0 deletions examples/kubernetes/i.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from importlib.util import module_from_spec, spec_from_file_location
from inspect import getmembers, isfunction

from omegaconf import OmegaConf

resolvers_path = "resolvers/__init__.py"

resolver_spec = spec_from_file_location(name="resolvers", location=resolvers_path)

resolver_module = module_from_spec(resolver_spec)
resolver_spec.loader.exec_module(resolver_module)


for name, func in getmembers(resolver_module, isfunction):
print("registered", name)
OmegaConf.register_new_resolver(name, func, replace=True)

config = OmegaConf.create({"a": "${test2:}"})
OmegaConf.resolve(config)
print(config.a)
7 changes: 7 additions & 0 deletions examples/kubernetes/resolvers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from . import sub

a = "hello"


def test():
return sub.test2()
5 changes: 5 additions & 0 deletions examples/kubernetes/resolvers/sub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# from resolvers.sub2 import *


def test2():
return "success2"
2 changes: 2 additions & 0 deletions examples/kubernetes/resolvers/sub2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test3():
return "success3"
24 changes: 18 additions & 6 deletions kapitan/inventory/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import logging
import os
import sys
# from importlib.util import module_from_spec, spec_from_file_location
# from inspect import getmembers, isfunction

from omegaconf import Container, ListMergeMode, Node, OmegaConf

Expand Down Expand Up @@ -268,16 +270,26 @@ def register_resolvers(inventory_path: str) -> None:
OmegaConf.register_new_resolver("helm_input", helm_input, replace=replace)

# user defined resolvers
user_resolver_file = os.path.join(inventory_path, "resolvers.py")
if os.path.exists(user_resolver_file):
try:
register_user_resolvers(inventory_path)
except:
logger.warning(f"Couldn't import {os.path.join(inventory_path, 'resolvers.py')}")
register_user_resolvers(inventory_path)
# logger.warning(f"Couldn't import {os.path.join(inventory_path, 'resolvers.py')}")


def register_user_resolvers(inventory_path: str) -> None:
"""import user resolvers specified in inventory/resolvers.py"""
# resolvers_path = "resolvers"
# abs_path = os.path.join(os.getcwd(), resolvers_path)
# sys.path.append(abs_path)
# import resolvers

# resolver_spec = spec_from_file_location("resolvers", os.path.join(resolvers_path, "__init__.py"))

# resolver_module = module_from_spec(resolver_spec)
# resolver_spec.loader.exec_module(resolver_module)

# for name, func in getmembers(resolver_module, isfunction):
# print("registered", name)
# OmegaConf.register_new_resolver(name, func, replace=True)

try:
import_path = os.path.join(os.getcwd(), inventory_path)
sys.path.append(import_path)
Expand Down

0 comments on commit f139b23

Please sign in to comment.