Added new filters for templates (#18125)
* added additional filters Added base64_encode, base64_decode and ordinal filters. * added test cases added test cases for base64_encode, base64_decode and ordinal filters. * forgot to add filters :)
This commit is contained in:
parent
1ae58ce48b
commit
c23792d1fb
2 changed files with 52 additions and 0 deletions
|
@ -4,6 +4,7 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
|
import base64
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
|
@ -602,6 +603,23 @@ def bitwise_or(first_value, second_value):
|
||||||
return first_value | second_value
|
return first_value | second_value
|
||||||
|
|
||||||
|
|
||||||
|
def base64_encode(value):
|
||||||
|
"""Perform base64 encode."""
|
||||||
|
return base64.b64encode(value.encode('utf-8')).decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
def base64_decode(value):
|
||||||
|
"""Perform base64 denode."""
|
||||||
|
return base64.b64decode(value).decode('utf-8')
|
||||||
|
|
||||||
|
|
||||||
|
def ordinal(value):
|
||||||
|
"""Perform ordinal conversion."""
|
||||||
|
return str(value) + (list(['th', 'st', 'nd', 'rd'] + ['th'] * 6)
|
||||||
|
[(int(str(value)[-1])) % 10] if not
|
||||||
|
int(str(value)[-2:]) % 100 in range(11, 14) else 'th')
|
||||||
|
|
||||||
|
|
||||||
@contextfilter
|
@contextfilter
|
||||||
def random_every_time(context, values):
|
def random_every_time(context, values):
|
||||||
"""Choose a random value.
|
"""Choose a random value.
|
||||||
|
@ -640,6 +658,9 @@ ENV.filters['is_defined'] = fail_when_undefined
|
||||||
ENV.filters['max'] = max
|
ENV.filters['max'] = max
|
||||||
ENV.filters['min'] = min
|
ENV.filters['min'] = min
|
||||||
ENV.filters['random'] = random_every_time
|
ENV.filters['random'] = random_every_time
|
||||||
|
ENV.filters['base64_encode'] = base64_encode
|
||||||
|
ENV.filters['base64_decode'] = base64_decode
|
||||||
|
ENV.filters['ordinal'] = ordinal
|
||||||
ENV.filters['regex_match'] = regex_match
|
ENV.filters['regex_match'] = regex_match
|
||||||
ENV.filters['regex_replace'] = regex_replace
|
ENV.filters['regex_replace'] = regex_replace
|
||||||
ENV.filters['regex_search'] = regex_search
|
ENV.filters['regex_search'] = regex_search
|
||||||
|
|
|
@ -274,6 +274,37 @@ class TestHelpersTemplate(unittest.TestCase):
|
||||||
template.Template('{{ [1, 2, 3] | max }}',
|
template.Template('{{ [1, 2, 3] | max }}',
|
||||||
self.hass).render()
|
self.hass).render()
|
||||||
|
|
||||||
|
def test_base64_encode(self):
|
||||||
|
"""Test the base64_encode filter."""
|
||||||
|
self.assertEqual(
|
||||||
|
'aG9tZWFzc2lzdGFudA==',
|
||||||
|
template.Template('{{ "homeassistant" | base64_encode }}',
|
||||||
|
self.hass).render())
|
||||||
|
|
||||||
|
def test_base64_decode(self):
|
||||||
|
"""Test the base64_decode filter."""
|
||||||
|
self.assertEqual(
|
||||||
|
'homeassistant',
|
||||||
|
template.Template('{{ "aG9tZWFzc2lzdGFudA==" | base64_decode }}',
|
||||||
|
self.hass).render())
|
||||||
|
|
||||||
|
def test_ordinal(self):
|
||||||
|
"""Test the ordinal filter."""
|
||||||
|
tests = [
|
||||||
|
(1, '1st'),
|
||||||
|
(2, '2nd'),
|
||||||
|
(3, '3rd'),
|
||||||
|
(4, '4th'),
|
||||||
|
(5, '5th'),
|
||||||
|
]
|
||||||
|
|
||||||
|
for value, expected in tests:
|
||||||
|
self.assertEqual(
|
||||||
|
expected,
|
||||||
|
template.Template(
|
||||||
|
'{{ %s | ordinal }}' % value,
|
||||||
|
self.hass).render())
|
||||||
|
|
||||||
def test_timestamp_utc(self):
|
def test_timestamp_utc(self):
|
||||||
"""Test the timestamps to local filter."""
|
"""Test the timestamps to local filter."""
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
|
|
Loading…
Add table
Reference in a new issue