Skip to content

Commit

Permalink
add filter for passing lists of strings (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtbarnes authored Aug 23, 2022
1 parent ad99f41 commit f1806c3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 3 additions & 1 deletion hissw/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions hissw/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
9 changes: 9 additions & 0 deletions hissw/tests/test_hissw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}'
Expand Down

0 comments on commit f1806c3

Please sign in to comment.