Skip to content

Commit

Permalink
add list_files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Ryabov authored and rvadim committed May 20, 2020
1 parent 0bf1afd commit cbe460a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Expand Up @@ -99,3 +99,8 @@ ENV/

# mypy
.mypy_cache/

# Apps
*.DS_Store
.vscode/
.idea/
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -288,7 +288,8 @@ templates
my_file.txt
...
```
> Note, `include_file` also support unix glob. You can import all files from directory conf.d/*.conf for example.
* `{{ list_files('dir/or/glob*') }}` - returns list of files in specified directory. Useful for including all files in folder to configmap. You specify directory path relative to parent of templates folder.
> Note, both fuctions support unix glob. You can import all files from directory `conf.d/*.conf` for example.
You can put *.j2 templates in 'templates' directory and specify it in config.yaml
```yaml
Expand Down
11 changes: 10 additions & 1 deletion k8s_handle/templating.py
Expand Up @@ -68,11 +68,19 @@ def get_env(templates_dir):
def include_file(path):
path = os.path.join(templates_dir, '../', path)
output = []
for file_path in glob.glob(path):
for file_path in sorted(glob.glob(path)):
with open(file_path, 'r') as f:
output.append(f.read())
return '\n'.join(output)

def list_files(path):
path = os.path.join(templates_dir, '../', path)
if os.path.isdir(path):
files = [os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
else:
files = glob.glob(path)
return sorted(files)

env = Environment(
undefined=StrictUndefined,
loader=FileSystemLoader([templates_dir]))
Expand All @@ -82,6 +90,7 @@ def include_file(path):
env.filters['hash_sha256'] = hash_sha256
env.filters['to_yaml'] = to_yaml
env.globals['include_file'] = include_file
env.globals['list_files'] = list_files

log.debug('Available templates in path {}: {}'.format(templates_dir, env.list_templates()))
return env
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/config.yaml
Expand Up @@ -33,6 +33,7 @@ test_dirs:
- template: template3.yaml.j2
- template: template_include_file.yaml.j2
- template: innerdir/template1.yaml.j2
- template: template_list_files.yaml.j2

no_templates:
templates:
Expand Down
7 changes: 7 additions & 0 deletions tests/templates_tests/template_list_files.yaml.j2
@@ -0,0 +1,7 @@
test: |
{% for f in list_files('templates_tests/innerdir') -%}
{{ f.split('/')[-1] }}:
{% endfor -%}
{% for f in list_files('templates_tests/my_file*.txt') -%}
{{ f.split('/')[-1] }}:
{% endfor -%}
6 changes: 5 additions & 1 deletion tests/test_templating.py
Expand Up @@ -41,6 +41,7 @@ def test_generate_templates(self):
file_path_3 = '{}/template3.yaml'.format(settings.TEMP_DIR)
file_path_4 = '{}/innerdir/template1.yaml'.format(settings.TEMP_DIR)
file_path_5 = '{}/template_include_file.yaml'.format(settings.TEMP_DIR)
file_path_6 = '{}/template_list_files.yaml'.format(settings.TEMP_DIR)
self.assertTrue(os.path.exists(file_path_1))
self.assertTrue(os.path.exists(file_path_2))
self.assertTrue(os.path.exists(file_path_3))
Expand All @@ -58,7 +59,10 @@ def test_generate_templates(self):
self.assertEqual(content, "{'ha_ha': 'included_var'}")
with open(file_path_5, 'r') as f:
content = f.read()
self.assertEqual(content, "test: |\n {{ hello world1 }}\n\n {{ hello world }}\n new\n line")
self.assertEqual(content, "test: |\n {{ hello world }}\n new\n line\n {{ hello world1 }}\n")
with open(file_path_6, 'r') as f:
content = f.read()
self.assertEqual(content, "test: |\n template1.yaml.j2:\n my_file.txt:\n my_file1.txt:\n ")

def test_no_templates_in_kubectl(self):
r = templating.Renderer(os.path.join(os.path.dirname(__file__), 'templates_tests'))
Expand Down

0 comments on commit cbe460a

Please sign in to comment.