Skip to content

Commit

Permalink
Merge pull request #519 from Cyanopus/master
Browse files Browse the repository at this point in the history
adding dir_files_list and dir_files_read in jsonnet
  • Loading branch information
uberspot committed Jun 8, 2020
2 parents 8900945 + c05d80c commit 8c646fd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/compile.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ yaml_load_stream - returns a list of json strings of the specified yaml file
yaml_dump - returns a string yaml from a json string
yaml_dump_stream - returns a string yaml stream from a json string
file_read - reads the file specified
file_exists - returns informative object if a file exists
dir_files_list - returns a list of file in a dir
dir_files_read - returns an object with keys - file_name and values - file contents
jinja2_template - renders the jinja2 file with context specified
sha256_string - returns sha256 of string
gzip_b64 - returns base64 encoded gzip of obj
Expand Down
2 changes: 2 additions & 0 deletions kapitan/lib/kapitan.libjsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
yaml_load_stream(name):: std.native("yaml_load_stream")(name),
file_read(name):: std.native("file_read")(name),
file_exists(name):: std.native("file_exists")(name),
dir_files_list(name):: std.native("dir_files_list")(name),
dir_files_read(name):: std.native("dir_files_read")(name),
inventory(target=std.extVar("target"), inv_path="inventory/"):: std.native("inventory")(target, inv_path),
inventory_global(target=null, inv_path="inventory/"):: std.native("inventory")(target, inv_path),
yaml_dump(obj):: std.native("yaml_dump")(std.toString(obj)),
Expand Down
22 changes: 22 additions & 0 deletions kapitan/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def resource_callbacks(search_paths):
"inventory": (("target", "inv_path"), partial(inventory, search_paths)),
"file_read": (("name",), partial(read_file, search_paths)),
"file_exists": (("name",), partial(file_exists, search_paths)),
"dir_files_list": (("name",), partial(dir_files_list, search_paths)),
"dir_files_read": (("name",), partial(dir_files_read, search_paths)),
"sha256_string": (("obj",), sha256_string),
"gzip_b64": (("obj",), gzip_b64),
"yaml_dump": (("obj",), yaml_dump),
Expand Down Expand Up @@ -180,6 +182,26 @@ def file_exists(search_paths, name):
return {"exists": False, "path": ""}


def dir_files_list(search_paths, name):
"""returns list of files in a dir"""
for path in search_paths:
full_path = os.path.join(path, name)
logger.debug("dir_files_list trying directory %s", full_path)
if os.path.exists(full_path):
return [f for f in os.listdir(full_path) if os.path.isfile(os.path.join(full_path, f))]
raise IOError("Could not find folder {}".format(name))


def dir_files_read(search_paths, name):
"""returns an object with key:
- file_name (contents of the file)"""
for path in search_paths:
full_path = os.path.join(path, name)
logger.debug("dir_files_list trying directory %s", full_path)
if os.path.exists(full_path):
return {f: read_file([full_path], f) for f in dir_files_list([full_path], "")}


def search_imports(cwd, import_str, search_paths):
"""
This is only to be used as a callback for the jsonnet API!
Expand Down
22 changes: 22 additions & 0 deletions tests/test_jsonnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

from kapitan.resources import (
file_exists,
dir_files_list,
dir_files_read,
gzip_b64,
jsonschema_validate,
yaml_dump,
Expand Down Expand Up @@ -41,6 +43,26 @@ def test_file_exists(self):
expected = {"exists": True, "path": "./tests/test_jsonnet.py"}
self.assertEqual(result, expected)

def test_dir_files_list(self):
"""test if list of files in a dir"""
search_paths = [os.getcwd(), "./tests/"]
result = dir_files_list(search_paths, "test_jsonnet")
expected = ["file1.txt", "file2.txt"]
self.assertEqual(result, expected)
with self.assertRaises(IOError):
dir_files_list(search_paths, "non_existing_dir")

def test_dir_files_read(self):
"""must result in dict with key:
- file_name (contents of the file)"""
search_paths = [os.getcwd(), "./tests/"]
result = dir_files_read(search_paths, "test_jsonnet")
expected = {
"file1.txt": "To be, or not to be: that is the question",
"file2.txt": "Nothing will come of nothing.",
}
self.assertEqual(result, expected)

def test_yaml_load(self):
"""
This tests the yaml_load function.
Expand Down
1 change: 1 addition & 0 deletions tests/test_jsonnet/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
To be, or not to be: that is the question
1 change: 1 addition & 0 deletions tests/test_jsonnet/file2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Nothing will come of nothing.
Empty file.
1 change: 1 addition & 0 deletions tests/test_jsonnet/subfolder2/file1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
should't be loaded

0 comments on commit 8c646fd

Please sign in to comment.