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

Enable auto extension detection #1273

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions kapitan/inputs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def compile_file(self, config: CompileInputTypeConfig, input_path: str, compile_


class CompilingFile(object):
def __init__(self, context, fp, ref_controller, **kwargs):
def __init__(self, fp, ref_controller, **kwargs):
self.fp = fp
self.ref_controller = ref_controller
self.kwargs = kwargs
Expand Down Expand Up @@ -221,7 +221,7 @@ def __enter__(self):
os.makedirs(os.path.dirname(self.name), exist_ok=True)

self.fp = open(self.name, mode)
return CompilingFile(self, self.fp, self.ref_controller, **self.kwargs)
return CompilingFile(self.fp, self.ref_controller, **self.kwargs)

def __exit__(self, *args):
self.fp.close()
23 changes: 13 additions & 10 deletions kapitan/inputs/jsonnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

from kapitan.errors import CompileError
from kapitan.inputs.base import CompiledFile, InputType
from kapitan.inventory.model.input_types import KapitanInputTypeJsonnetConfig
from kapitan.inventory.model.input_types import (
KapitanInputTypeJsonnetConfig,
OutputType,
)
from kapitan.resources import resource_callbacks, search_imports
from kapitan.utils import prune_empty

Expand Down Expand Up @@ -143,9 +146,7 @@ def _search_imports(cwd, imp):
# Write each item in output_obj to a separate file.
for item_key, item_value in output_obj.items():
file_ext = output
if output in ["yml", "yaml"]:
file_ext = output
elif output == "plain":
if output == OutputType.PLAIN:
file_ext = "" # no extension for plain text

file_name = f"{item_key}.{file_ext}" if file_ext else item_key
Expand All @@ -159,13 +160,15 @@ def _search_imports(cwd, imp):
target_name=target_name,
indent=indent,
) as fp:
if output == "json":
if output == OutputType.JSON:
fp.write_json(item_value)
elif output in ["yml", "yaml"]:
elif output in [OutputType.YAML, OutputType.YML]:
fp.write_yaml(item_value)
elif output == "toml":
fp.write_toml(item_value)
elif output == "plain":
elif output == OutputType.PLAIN:
fp.write(item_value)
elif output == OutputType.TOML:
fp.write_toml(item_value)
else:
raise ValueError(f"Invalid output type: {output}")
raise ValueError(
f"Output type defined in inventory for {input_path} not supported: {output}: {OutputType}"
)
42 changes: 30 additions & 12 deletions kapitan/inputs/kadet.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from kapitan import cached
from kapitan.errors import CompileError
from kapitan.inputs.base import CompiledFile, InputType
from kapitan.inventory.model.input_types import KapitanInputTypeKadetConfig
from kapitan.inventory.model.input_types import KapitanInputTypeKadetConfig, OutputType
from kapitan.utils import prune_empty

# Set external kadet exception to kapitan.error.CompileError
Expand Down Expand Up @@ -100,7 +100,7 @@ def compile_file(self, config: KapitanInputTypeKadetConfig, input_path, compile_
"""

input_params = config.input_params
output = config.output_type

prune_output = config.prune
reveal = self.args.reveal
target_name = self.target_name
Expand Down Expand Up @@ -146,11 +146,29 @@ def compile_file(self, config: KapitanInputTypeKadetConfig, input_path, compile_
return None

for item_key, item_value in output_obj.items():
file_ext = output
if output in ["yml", "yaml"]:
file_ext = output
elif output == "plain":
file_ext = "" # no extension for plain text
output_type = config.output_type
file_ext = output_type

if output_type == OutputType.AUTO:
_, detected_type = os.path.splitext(item_key)
if detected_type:
# Remove . from the beginning of the extension
detected_type = detected_type[1:]

if detected_type in [OutputType.TOML, OutputType.JSON, OutputType.YAML, OutputType.YML]:
output_type = detected_type
file_ext = None
elif detected_type == "tf": # TODO(ademaria) Handle it better.
output_type = OutputType.JSON
file_ext = output_type
else:
# Extension is not handled, so trating it as YAML
logger.debug(f"could not detect extension for {item_key}, defaulting to YAML")
output_type = OutputType.YML
file_ext = output_type # no extension for plain text

if output_type == OutputType.PLAIN:
file_ext = None # no extension for plain text

file_name = f"{item_key}.{file_ext}" if file_ext else item_key
file_path = os.path.join(compile_path, file_name)
Expand All @@ -167,17 +185,17 @@ def compile_file(self, config: KapitanInputTypeKadetConfig, input_path, compile_
target_name=target_name,
indent=indent,
) as fp:
if output == "json":
if output_type == OutputType.JSON:
fp.write_json(item_value)
elif output in ["yml", "yaml"]:
elif output_type in [OutputType.YAML, OutputType.YML]:
fp.write_yaml(item_value)
elif output == "plain":
elif output_type == OutputType.PLAIN:
fp.write(item_value)
elif output == "toml":
elif output_type == OutputType.TOML:
fp.write_toml(item_value)
else:
raise ValueError(
f"Output type defined in inventory for {input_path} is neither 'json', 'yaml', 'toml' nor 'plain'"
f"Output type defined in inventory for {input_path} not supported: {output_type}: {OutputType}"
)


Expand Down
1 change: 1 addition & 0 deletions kapitan/inventory/model/input_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class OutputType(StrEnum):
YML = "yml"
PLAIN = "plain"
TOML = "toml"
AUTO = "auto"


class KapitanInputTypeBaseConfig(BaseModel):
Expand Down
Loading