Skip to content

Instantly share code, notes, and snippets.

@pgporada
Created January 23, 2017 13:44
Show Gist options
  • Save pgporada/cd8e1eec5fc5cb85ef3e17f15ff651f8 to your computer and use it in GitHub Desktop.
Save pgporada/cd8e1eec5fc5cb85ef3e17f15ff651f8 to your computer and use it in GitHub Desktop.
'''
AUTHOR: [email protected]
WHAT:
This will prevent us from doing ugly shit like this
{% if not((docker_host_tcp is undefined) or (docker_host_tcp is none) or (docker_host_tcp|trim == '')) %}
and start being able to have a nice filter like
{% if not(docker_host_tcp|empty) %}
DOCS:
http://stackoverflow.com/questions/14017996/python-optional-parameter
jinja2 folder https://github.com/pallets/jinja
http://docs.ansible.com/ansible/dev_guide/developing_plugins.html
'''
from jinja2.runtime import Undefined
class _NO_ARG:
def __repr__(self):
return "<no arg>"
def custom_empty_filter(a=_NO_ARG):
if a is _NO_ARG:
# print ("'%s' is empty") % a
return True
if a is None or isinstance(a, Undefined) or a=='':
# print ("'%s' is empty") % a
return True
else:
# print ("'%s' is not empty") % a
return False
''' Uncomment to run my tests
custom_empty_filter()
custom_empty_filter(None)
custom_empty_filter("")
custom_empty_filter('')
custom_empty_filter("None")
custom_empty_filter("Null")
custom_empty_filter("Phil")
custom_empty_filter(1)
'''
class FilterModule(object):
def filters(self):
return {'isempty': custom_empty_filter}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment