Skip to content

Commit bd63cb3

Browse files
committed
Merge pull request #43 from totem/develop
0.3.8 Release
2 parents 95078ed + 666925f commit bd63cb3

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

orchestrator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from celery.signals import setup_logging
33
import orchestrator.logger
44

5-
__version__ = '0.3.7'
5+
__version__ = '0.3.8'
66
__author__ = 'sukrit'
77

88
orchestrator.logger.init_logging()

orchestrator/jinja/filters.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from future.builtins import ( # noqa
2+
bytes, dict, int, list, object, range, str,
3+
ascii, chr, hex, input, next, oct, open,
4+
pow, round, filter, map, zip)
5+
6+
import re
7+
8+
__author__ = 'sukrit'
9+
10+
"""
11+
Package that includes custom filters required for totem config processing
12+
"""
13+
14+
USE_FILTERS = ('replace_regex', )
15+
16+
17+
def replace_regex(input_str, find, replace):
18+
"""
19+
Regex replace filter that replaces all occurrences of given regex match
20+
in a given string
21+
22+
:param input_str: Input string on which replacement is to be performed
23+
:type input_str: str
24+
:param find: Regular expression string that needs to be used for
25+
find/replacement
26+
:type find: str
27+
:param replace: Regex replacement string
28+
:type replace: str
29+
:return: Regex replaced string
30+
:rtype: str
31+
"""
32+
return re.sub(find, replace, input_str)
33+
34+
35+
def apply_filters(env):
36+
"""
37+
Applies filters on jinja env.
38+
39+
:param env: Jinja environment
40+
:return:
41+
"""
42+
43+
for name in USE_FILTERS:
44+
env.filters[name] = globals()[name]
45+
return env

orchestrator/services/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from orchestrator.cluster_config.etcd import EtcdConfigProvider
2020
from orchestrator.cluster_config.github import GithubConfigProvider
2121
from orchestrator.cluster_config.s3 import S3ConfigProvider
22-
from orchestrator.jinja import conditions
22+
from orchestrator.jinja import conditions, filters
2323
from orchestrator.services.errors import ConfigProviderNotFound
2424
from orchestrator.services.exceptions import ConfigValueError, \
2525
ConfigValidationError, ConfigParseError
@@ -237,7 +237,8 @@ def _get_jinja_environment():
237237
"""
238238
env = get_spontaneous_environment()
239239
env.line_statement_prefix = '#'
240-
return conditions.apply_conditions(env)
240+
241+
return filters.apply_filters(conditions.apply_conditions(env))
241242

242243

243244
def evaluate_template(template_value, variables={}):
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from jinja2 import environment
2+
from nose.tools import eq_
3+
from orchestrator.jinja import filters
4+
5+
6+
class TestJinjaFilters:
7+
8+
def setup(self):
9+
self.env = environment.get_spontaneous_environment()
10+
filters.apply_filters(self.env)
11+
12+
def test_regex_replace(self):
13+
# Given: Jinja template using starting_with
14+
template = \
15+
'{{ "mock_a$b$c%d^host" | replace_regex("[^A-Za-z0-9-]","-") }}'
16+
17+
# When: I render template
18+
ret_value = self.env.from_string(template).render()
19+
20+
# Then: Template is rendered as expected
21+
eq_(ret_value, 'mock-a-b-c-d-host')

0 commit comments

Comments
 (0)