From f139b23f442cdc964bfbf27b97be64dd777872cd Mon Sep 17 00:00:00 2001 From: Matteo Voges Date: Mon, 30 Oct 2023 17:13:19 +0100 Subject: [PATCH] wip --- examples/kubernetes/i.py | 20 +++++++++++++++++++ examples/kubernetes/resolvers/__init__.py | 7 +++++++ examples/kubernetes/resolvers/sub.py | 5 +++++ examples/kubernetes/resolvers/sub2.py | 2 ++ kapitan/inventory/resolvers.py | 24 +++++++++++++++++------ 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 examples/kubernetes/i.py create mode 100644 examples/kubernetes/resolvers/__init__.py create mode 100644 examples/kubernetes/resolvers/sub.py create mode 100644 examples/kubernetes/resolvers/sub2.py diff --git a/examples/kubernetes/i.py b/examples/kubernetes/i.py new file mode 100644 index 000000000..762f6d4be --- /dev/null +++ b/examples/kubernetes/i.py @@ -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) diff --git a/examples/kubernetes/resolvers/__init__.py b/examples/kubernetes/resolvers/__init__.py new file mode 100644 index 000000000..69cdb9e9e --- /dev/null +++ b/examples/kubernetes/resolvers/__init__.py @@ -0,0 +1,7 @@ +from . import sub + +a = "hello" + + +def test(): + return sub.test2() diff --git a/examples/kubernetes/resolvers/sub.py b/examples/kubernetes/resolvers/sub.py new file mode 100644 index 000000000..dd18334d1 --- /dev/null +++ b/examples/kubernetes/resolvers/sub.py @@ -0,0 +1,5 @@ +# from resolvers.sub2 import * + + +def test2(): + return "success2" diff --git a/examples/kubernetes/resolvers/sub2.py b/examples/kubernetes/resolvers/sub2.py new file mode 100644 index 000000000..2c5c7a77e --- /dev/null +++ b/examples/kubernetes/resolvers/sub2.py @@ -0,0 +1,2 @@ +def test3(): + return "success3" diff --git a/kapitan/inventory/resolvers.py b/kapitan/inventory/resolvers.py index 9280031e5..dd4f2cac1 100644 --- a/kapitan/inventory/resolvers.py +++ b/kapitan/inventory/resolvers.py @@ -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 @@ -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)