def compile_jsonnet(file_path, compile_path, search_paths, ext_vars, **kwargs): """ Write file_path (jsonnet evaluated) items as files to compile_path. Set output to write as json or yaml search_paths and ext_vars will be passed as parameters to jsonnet_file() kwargs: output: default 'yaml', accepts 'json' prune: default False, accepts True secrets_path: default None, set to access secrets backend secrets_reveal: default False, set to reveal secrets on compile target_name: default None, set to current target being compiled indent: default 2 """ _search_imports = lambda cwd, imp: search_imports(cwd, imp, search_paths) json_output = jsonnet_file( file_path, import_callback=_search_imports, native_callbacks=resource_callbacks(search_paths), ext_vars=ext_vars) json_output = json.loads(json_output) output = kwargs.get('output', 'yaml') prune = kwargs.get('prune', False) secrets_path = kwargs.get('secrets_path', None) secrets_reveal = kwargs.get('secrets_reveal', False) target_name = kwargs.get('target_name', None) indent = kwargs.get('indent', 2) if prune: json_output = prune_empty(json_output) logger.debug("Pruned output for: %s", file_path) for item_key, item_value in json_output.items(): # write each item to disk if output == 'json': file_path = os.path.join(compile_path, '%s.%s' % (item_key, output)) with CompiledFile(file_path, mode="w", secrets_path=secrets_path, secrets_reveal=secrets_reveal, target_name=target_name, indent=indent) as fp: fp.write_json(item_value) logger.debug("Wrote %s", file_path) elif output == 'yaml': file_path = os.path.join(compile_path, '%s.%s' % (item_key, "yml")) with CompiledFile(file_path, mode="w", secrets_path=secrets_path, secrets_reveal=secrets_reveal, target_name=target_name, indent=indent) as fp: fp.write_yaml(item_value) logger.debug("Wrote %s", file_path) else: raise ValueError('output is neither "json" or "yaml"')
def compile_file(self, file_path, compile_path, ext_vars, **kwargs): """ Write file_path (kadet evaluated) items as files to compile_path. ext_vars is not used in Kadet kwargs: output: default 'yaml', accepts 'json' prune: default False reveal: default False, set to reveal refs on compile target_name: default None, set to current target being compiled indent: default 2 """ output = kwargs.get('output', 'yaml') prune = kwargs.get('prune', False) reveal = kwargs.get('reveal', False) target_name = kwargs.get('target_name', None) indent = kwargs.get('indent', 2) # These will be updated per target # XXX At the moment we have no other way of setting externals for modules... global search_paths search_paths = self.search_paths global inventory inventory = lambda: Dict(inventory_func(self.search_paths, target_name)) # noqa E731 global inventory_global inventory_global = lambda: Dict(inventory_func(self.search_paths, None)) # noqa E731 kadet_module, spec = module_from_path(file_path) sys.modules[spec.name] = kadet_module spec.loader.exec_module(kadet_module) logger.debug('Kadet.compile_file: spec.name: %s', spec.name) output_obj = kadet_module.main().to_dict() if prune: output_obj = prune_empty(output_obj) for item_key, item_value in output_obj.items(): # write each item to disk if output == 'json': file_path = os.path.join(compile_path, '%s.%s' % (item_key, output)) with CompiledFile(file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent) as fp: fp.write_json(item_value) elif output == 'yaml': file_path = os.path.join(compile_path, '%s.%s' % (item_key, "yml")) with CompiledFile(file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent) as fp: fp.write_yaml(item_value) elif output == 'plain': file_path = os.path.join(compile_path, '%s' % item_key) with CompiledFile(file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent) as fp: fp.write(item_value) else: raise ValueError('output is neither "json", "yaml" or "plain"') logger.debug("Pruned output for: %s", file_path)
def compile_file(self, file_path, compile_path, ext_vars, **kwargs): """ Write file_path (jsonnet evaluated) items as files to compile_path. ext_vars will be passed as parameters to jsonnet_file() kwargs: output: default 'yaml', accepts 'json' prune: default False, accepts True reveal: default False, set to reveal refs on compile target_name: default None, set to current target being compiled indent: default 2 """ def _search_imports(cwd, imp): return search_imports(cwd, imp, self.search_paths) json_output = jsonnet_file(file_path, import_callback=_search_imports, native_callbacks=resource_callbacks(self.search_paths), ext_vars=ext_vars) output_obj = json.loads(json_output) output = kwargs.get('output', 'yaml') prune = kwargs.get('prune', False) reveal = kwargs.get('reveal', False) target_name = kwargs.get('target_name', None) indent = kwargs.get('indent', 2) if prune: output_obj = prune_empty(output_obj) logger.debug("Pruned output for: %s", file_path) for item_key, item_value in output_obj.items(): # write each item to disk if output == 'json': file_path = os.path.join(compile_path, '%s.%s' % (item_key, output)) with CompiledFile(file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent) as fp: fp.write_json(item_value) elif output == 'yaml': file_path = os.path.join(compile_path, '%s.%s' % (item_key, "yml")) with CompiledFile(file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent) as fp: fp.write_yaml(item_value) else: raise ValueError('output is neither "json" or "yaml"')
def test_prune_empty(self): """Remove empty lists and empty dictionaries from dict""" dictionary = {"hello": "world", "array": [1, 2], "foo": {}, "bar": []} pruned = prune_empty(dictionary) self.assertEqual(pruned, {"hello": "world", "array": [1, 2]})
def compile_file(self, file_path, compile_path, ext_vars, **kwargs): """ Write file_path (kadet evaluated) items as files to compile_path. ext_vars is not used in Kadet kwargs: output: default 'yaml', accepts 'json' prune: default False reveal: default False, set to reveal refs on compile target_name: default None, set to current target being compiled indent: default 2 """ output = kwargs.get("output", "yaml") prune = kwargs.get("prune", False) reveal = kwargs.get("reveal", False) target_name = kwargs.get("target_name", None) inventory_path = kwargs.get("inventory_path", None) indent = kwargs.get("indent", 2) input_params = self.input_params # set compile_path allowing kadet functions to have context on where files # are being compiled on the current kapitan run input_params["compile_path"] = compile_path # reset between each compile if kadet component is used multiple times self.input_params = {} # These will be updated per target # XXX At the moment we have no other way of setting externals for modules... global search_paths search_paths = self.search_paths global inventory inventory = lambda: Dict( inventory_func(self.search_paths, target_name, inventory_path) ) # noqa E731 global inventory_global inventory_global = lambda: Dict( inventory_func(self.search_paths, None, inventory_path) ) # noqa E731 kadet_module, spec = module_from_path(file_path) sys.modules[spec.name] = kadet_module spec.loader.exec_module(kadet_module) logger.debug("Kadet.compile_file: spec.name: %s", spec.name) kadet_arg_spec = inspect.getfullargspec(kadet_module.main) logger.debug("Kadet main args: %s", kadet_arg_spec.args) if len(kadet_arg_spec.args) == 1: output_obj = kadet_module.main(input_params).to_dict() elif len(kadet_arg_spec.args) == 0: output_obj = kadet_module.main().to_dict() else: raise ValueError( f"Kadet {spec.name} main parameters not equal to 1 or 0") if prune: output_obj = prune_empty(output_obj) # Return None if output_obj has no output if output_obj is None: return None for item_key, item_value in output_obj.items(): # write each item to disk if output == "json": file_path = os.path.join(compile_path, "%s.%s" % (item_key, output)) with CompiledFile( file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent, ) as fp: fp.write_json(item_value) elif output in ["yml", "yaml"]: file_path = os.path.join(compile_path, "%s.%s" % (item_key, output)) with CompiledFile( file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent, ) as fp: fp.write_yaml(item_value) elif output == "plain": file_path = os.path.join(compile_path, "%s" % item_key) with CompiledFile( file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent, ) as fp: fp.write(item_value) else: raise ValueError( f"Output type defined in inventory for {file_path} is neither 'json', 'yaml' nor 'plain'" ) logger.debug("Pruned output for: %s", file_path)
def compile_file(self, file_path, compile_path, ext_vars, **kwargs): """ Write file_path (jsonnet evaluated) items as files to compile_path. ext_vars will be passed as parameters to jsonnet_file() kwargs: output: default 'yaml', accepts 'json' prune: default False, accepts True reveal: default False, set to reveal refs on compile target_name: default None, set to current target being compiled indent: default 2 """ def _search_imports(cwd, imp): return search_imports(cwd, imp, self.search_paths) json_output = jsonnet_file( file_path, import_callback=_search_imports, native_callbacks=resource_callbacks(self.search_paths), ext_vars=ext_vars, ) output_obj = json.loads(json_output) output = kwargs.get("output", "yaml") prune = kwargs.get("prune_input", False) reveal = kwargs.get("reveal", False) target_name = kwargs.get("target_name", None) indent = kwargs.get("indent", 2) if prune: output_obj = prune_empty(output_obj) logger.debug("Pruned output for: %s", file_path) if not isinstance(output_obj, dict): tmp_output_obj = output_obj # assume that the output filename is the # same as the input jsonnet filename filename = os.path.splitext(os.path.basename(file_path))[0] output_obj = {} output_obj[filename] = tmp_output_obj for item_key, item_value in output_obj.items(): # write each item to disk if output == "json": file_path = os.path.join(compile_path, "%s.%s" % (item_key, output)) with CompiledFile( file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent, ) as fp: fp.write_json(item_value) elif output in ["yml", "yaml"]: file_path = os.path.join(compile_path, "%s.%s" % (item_key, output)) with CompiledFile( file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent, ) as fp: fp.write_yaml(item_value) elif output == "plain": file_path = os.path.join(compile_path, "%s" % item_key) with CompiledFile( file_path, self.ref_controller, mode="w", reveal=reveal, target_name=target_name, indent=indent, ) as fp: fp.write(item_value) else: raise ValueError( f"Output type defined in inventory for {file_path} is neither 'json', 'yaml' nor 'plain'" )