Skip to content

Commit

Permalink
Add Arrow timestamps and utc and utcnow
Browse files Browse the repository at this point in the history
  • Loading branch information
flomotlik committed Aug 17, 2018
1 parent c6b6a1f commit 254522f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
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 254522f

Please sign in to comment.