Skip to content

Commit

Permalink
Merge pull request #296 from irontable/develop
Browse files Browse the repository at this point in the history
use string template instead of pystache for script/param substition in Python client
  • Loading branch information
irontablee committed Jun 22, 2016
2 parents 12acfcf + 1cf219a commit 1078f41
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
20 changes: 15 additions & 5 deletions genie-client/src/main/python/pygenie/adapter/genie_x.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ def wrapper(*args, **kwargs):


def substitute(template, context):
import pystache
for delim in [(u'{{', u'}}'), (u'${', u'}')]:
pystache.defaults.DELIMITERS = delim
template = pystache.render(template, context)
return template.encode('utf-8')
"""
Performs string substitution.
Args:
template (str): The string to perform the substitution on.
context (dict): A mapping of keys to values for substitution.
Returns:
str: The substituted string.
"""

from string import Template
template = Template(template)

return template.safe_substitute(context)


class GenieBaseAdapter(object):
Expand Down
3 changes: 1 addition & 2 deletions genie-client/src/main/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

setup(
name='nflx-genie-client',
version='3.0.5',
version='3.0.6',
author='Netflix Inc.',
author_email='genieoss@googlegroups.com',
keywords='genie hadoop cloud netflix client bigdata presto',
Expand All @@ -52,7 +52,6 @@
"multipledispatch",
"nose",
"pyconfigurator",
"pystache",
"python-dateutil >= 2.4",
"requests",
"responses",
Expand Down
49 changes: 49 additions & 0 deletions genie-client/src/main/python/tests/test_adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import unittest

from mock import patch
from nose.tools import assert_equals

from pygenie.adapter.genie_x import substitute


@patch.dict('os.environ', {'GENIE_BYPASS_HOME_CONFIG': '1'})
class TestStringSubstitution(unittest.TestCase):
"""Test script parameter substitution."""

def test_substitute(self):
"""Test script parameter substitution."""

assert_equals(
substitute('hello $name, goodbye $last',
dict(name='test1', last='bye1')),
'hello test1, goodbye bye1'
)

def test_substitute_expansion(self):
"""Test script parameter substitution (expansion)."""

assert_equals(
substitute('hello ${name}, goodbye ${last}',
dict(name='test2', last='bye2')),
'hello test2, goodbye bye2'
)

def test_substitute_missing(self):
"""Test script parameter substitution with missing parameters."""

assert_equals(
substitute('hello $name, goodbye $last',
dict(name='tester3')),
'hello tester3, goodbye $last'
)

def test_substitute_missing_expansion(self):
"""Test script parameter substitution with missing parameters (expansion)."""

assert_equals(
substitute('hello ${name}, goodbye ${last}',
dict(name='tester4')),
'hello tester4, goodbye ${last}'
)

0 comments on commit 1078f41

Please sign in to comment.