Skip to content

Commit

Permalink
refactor: render from template in the context class
Browse files Browse the repository at this point in the history
  • Loading branch information
Paulo Henrique Cuchi committed Oct 17, 2020
1 parent fb642e9 commit 7559ced
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
18 changes: 6 additions & 12 deletions entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
#!/usr/bin/env python3

import os
from jinja2 import Template, StrictUndefined

# Montar o objeto do template com as variáveis que eu defini em todos os modos
with open(os.environ['INPUT_TEMPLATE'], 'r') as file:
template_kwargs = {}
if os.environ.get('INPUT_STRICT') == 'true':
template_kwargs.update({'undefined': StrictUndefined})
template = Template(str(file.read()), **template_kwargs)

# Aplicar variáveis dentro do template
with open(os.environ['INPUT_OUTPUT_FILE'], 'w') as file:
file.write(template.render(**variables) + '\n')
from main import Context

if __name__ == '__main__':
main(os.environ)
context = Context(os.environ)
context.load_from_env()
context.load_from_input()
context.load_from_data_file()
context.render_template()
19 changes: 13 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from j2cli.context import read_context_data

from jinja2 import Template, StrictUndefined

class Context:
def __init__(self, environ):
Expand All @@ -24,6 +24,18 @@ def load_from_data_file(self):
with open(data_file, 'r') as file:
self._variables.update(read_context_data(format, file, None))


def render_template(self):
with open(self._environ['INPUT_TEMPLATE'], 'r') as file:
template_kwargs = {}
if self._environ.get('INPUT_STRICT') == 'true':
template_kwargs.update({'undefined': StrictUndefined})
template = Template(str(file.read()), **template_kwargs)

with open(self._environ['INPUT_OUTPUT_FILE'], 'w') as file:
file.write(template.render(**self._variables) + '\n')


def _guess_format(file_name):
_, extension = os.path.splitext(file_name)
formats = {
Expand All @@ -35,8 +47,3 @@ def _guess_format(file_name):
}
return formats.get(extension, 'env')

def main(environ):
context = Context(environ)
context.load_from_env()
context.load_from_input()
context.load_from_data_file()

0 comments on commit 7559ced

Please sign in to comment.