Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[internal] feat/resolvers as module #23

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
Loading