diff --git a/hissw/environment.py b/hissw/environment.py index 55bb6e7..d7d5e02 100644 --- a/hissw/environment.py +++ b/hissw/environment.py @@ -9,6 +9,7 @@ import jinja2 from scipy.io import readsav +from .filters import string_list_filter from .read_config import defaults from .util import SSWIDLError, IDLLicenseError @@ -34,7 +35,7 @@ class Environment(object): idl_home : `str`, optional Path to IDL executable filters : `dict`, optional - Filters to use scripts. This should be a dictionary where the key + Filters to use in scripts. This should be a dictionary where the key is the name of the filter and the value is the corresponding function. idl_only : `bool`, optional @@ -57,6 +58,7 @@ def __init__(self, ssw_packages=None, ssw_paths=None, extra_paths=None, self.env = jinja2.Environment(loader=jinja2.PackageLoader('hissw', 'templates')) self.env.filters['to_unit'] = units_filter self.env.filters['log10'] = log10_filter + self.env.filters['string_list'] = string_list_filter if filters is not None: for k, v in filters.items(): self.env.filters[k] = v diff --git a/hissw/filters.py b/hissw/filters.py index 47618d9..5e0ecc3 100644 --- a/hissw/filters.py +++ b/hissw/filters.py @@ -21,3 +21,13 @@ def log10_filter(value): Take the base 10 log of a given value. """ return np.log10(value) + + +def string_list_filter(string_list): + """ + Double quote a list of strings. + + This is needed when passing in a list of strings to IDL as each string + in the list will not be quoted when passed into the template. + """ + return [f"'{s}'" for s in string_list] diff --git a/hissw/tests/test_hissw.py b/hissw/tests/test_hissw.py index 514be41..ab1d83c 100644 --- a/hissw/tests/test_hissw.py +++ b/hissw/tests/test_hissw.py @@ -115,6 +115,15 @@ def test_log10_filter(idl_env): assert res['foo'] == np.log10(foo) +def test_string_list_filter(idl_env): + script = """ + foo = {{ foo | string_list }} + """ + foo = ['my', 'list', 'of', 'strings'] + res = idl_env.run(script, args={'foo': foo}) + assert [v.decode('utf-8') for v in res['foo']] == [f"'{f}'" for f in foo] + + def test_default_ssw_var(ssw_env): script = """ foo = '{{ ssw_home }}'