Skip to content

Commit

Permalink
Merge pull request #102 from flomotlik/feature/now-utcnow
Browse files Browse the repository at this point in the history
Feature/now utcnow
  • Loading branch information
flomotlik committed Aug 17, 2018
2 parents 98f6eba + 254522f commit 8298873
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ integration-test:
build-dev:
docker-compose build formica

dev: build-dev
shell: build-dev
docker-compose run formica bash

clean:
Expand All @@ -38,4 +38,4 @@ release: release-pypi release-docker

whalebrew:
docker build -t flomotlik/formica:whalebrew -f Dockerfile.whalebrew .
whalebrew install -f flomotlik/formica:whalebrew
whalebrew install -f flomotlik/formica:whalebrew
6 changes: 5 additions & 1 deletion formica/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import yaml
from jinja2 import Environment, FileSystemLoader
from jinja2.exceptions import TemplateSyntaxError, TemplateNotFound
import arrow

from .exceptions import FormicaArgumentException

Expand Down Expand Up @@ -84,7 +85,10 @@ def include_file(self, filename, **args):

def render(self, filename, **args):
template_path = os.path.normpath("{}/{}".format(self.path, filename))
return self.env.get_template(template_path).render(code=self.include_file, **args)
return self.env.get_template(template_path).render(code=self.include_file,
now=arrow.now,
utcnow=arrow.utcnow,
** args)

def template(self, indent=4, sort_keys=True, separators=(',', ':'), dumper=None):
if dumper is not None:
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
],
keywords='cloudformation, aws, cloud',
packages=['formica'],
install_requires=['boto3>=1.4.4,<2.0.0', 'texttable==1.2.1', 'jinja2==2.10', 'pyyaml==3.12',
'deepdiff==3.3.0'],
install_requires=['boto3>=1.4.4,<2.0.0', 'texttable>=1.2.0,<2.0.0', 'jinja2>=2.10,<3.0', 'pyyaml>=3.12,<4.0',
'deepdiff==3.3.0', 'arrow>=0.12.1,<1.0.0'],
entry_points={
'console_scripts': [
'formica=formica.cli:formica',
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from path import Path

from formica.loader import Loader
from datetime import datetime, timedelta


@pytest.fixture()
Expand Down Expand Up @@ -171,7 +172,7 @@ def test_template_submodule_loads_variables(load, tmpdir):
with open('test.template.json', 'w') as f:
f.write(json.dumps(
{'Resources': {'TestResource':
{'From': 'Moduledir', 'Properties': {'test': 'Variable'}}}}))
{'From': 'Moduledir', 'Properties': {'test': 'Variable'}}}}))
load.load()
actual = json.loads(load.template())
assert actual == {"Description": "Variable"}
Expand Down Expand Up @@ -390,3 +391,25 @@ def test_un_indented_large_templates(load):
load.cftemplate = {'Resources': {i: 'a' * 84 for i in range(512)}}
assert len(load.template()) > 51200
assert len(load.template(indent=None)) < 51200


def test_now(load, tmpdir):
example = '{"Resources": {"Test": "{{ now().shift(days=1).format("YYYY-MM-DD HH:mm") }}"}}'
with Path(tmpdir):
with open('test.template.json', 'w') as f:
f.write(example)
load.load()
print(load.template())
actual = json.loads(load.template())
assert actual == {"Resources": {"Test": (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d %H:%M")}}


def test_utcnow(load, tmpdir):
example = '{"Resources": {"Test": "{{ utcnow().shift(days=1).format("YYYY-MM-DD HH:mm") }}"}}'
with Path(tmpdir):
with open('test.template.json', 'w') as f:
f.write(example)
load.load()
print(load.template())
actual = json.loads(load.template())
assert actual == {"Resources": {"Test": (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d %H:%M")}}

0 comments on commit 8298873

Please sign in to comment.