-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes # ## Proposed Changes * Refactor input types code to make it easier to manage and extend * Allow dynamic import of input types ## Docs and Tests * [x] Tests added * [x] Updated documentation
- Loading branch information
Showing
23 changed files
with
854 additions
and
810 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,36 @@ | ||
# SPDX-FileCopyrightText: 2020 The Kapitan Authors <[email protected]> | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import functools # Import functools for caching | ||
import importlib | ||
from typing import Type | ||
|
||
from kapitan.inventory.model.input_types import InputTypes | ||
|
||
from .base import InputType | ||
|
||
|
||
@functools.lru_cache(maxsize=None) # Use lru_cache for caching | ||
def get_compiler(input_type: InputType) -> Type[InputType]: | ||
"""Dynamically imports and returns the compiler class based on input_type.""" | ||
|
||
module_map = { | ||
InputTypes.JINJA2: "jinja2", | ||
InputTypes.HELM: "helm", | ||
InputTypes.JSONNET: "jsonnet", | ||
InputTypes.KADET: "kadet", | ||
InputTypes.COPY: "copy", | ||
InputTypes.EXTERNAL: "external", | ||
InputTypes.REMOVE: "remove", | ||
} | ||
|
||
module_name = module_map.get(input_type) | ||
if module_name: | ||
try: | ||
module = importlib.import_module(f".{module_name}", package=__name__) | ||
return getattr(module, module_name.capitalize()) # Capitalize to get class name | ||
except (ImportError, AttributeError) as e: | ||
raise ImportError(f"Could not import module or class for {input_type}: {e}") from e | ||
else: | ||
return None # Or raise an appropriate error for unknown input_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.