Skip to content

Commit 7e27514

Browse files
Merge pull request #1308 from linsword13/expander
Add expander support for string methods
2 parents ac8bb35 + 59e8c6d commit 7e27514

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

lib/ramble/docs/workspace_config.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ Supported functions are:
184184
* ``randint`` (from `random.randint`)
185185
* ``join_str(iterable, sep=",")`` (concatenate iterable into ``sep``-separated string)
186186
* ``re_search(regex, str)`` (determine if ``str`` contains pattern ``regex``, based on ``re.search``)
187-
* ``upper_str(str)`` (convert ``str`` to uppercase)
188-
* ``lower_str(str)`` (convert ``str`` to lowercase)
189187
* ``maybe(var_name, default="")`` (returns the expanded ``var_name`` if it is defined, otherwise returns ``default``)
190188

191189
Besides the above listed, any functions from the ``math`` module can be used in Ramble by referencing ``math_<function_name>``.
@@ -195,6 +193,9 @@ String slicing is supported:
195193

196194
* ``str[start:end:step]`` (string slicing)
197195

196+
In addition to the listed, any string methods can be used by referencing ``str_<method_name>``.
197+
For example, ``str_upper(<str>)`` invokes the ``<str>.upper()`` method.
198+
198199
Dictionary references are supported:
199200

200201
* ``dict_name["key"]`` (dictionary subscript)

lib/ramble/ramble/expander.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ def _join_str(seq, sep=","):
3838
return sep.join(str(i) for i in seq)
3939

4040

41-
def _upper_str(in_str):
42-
return in_str.upper()
43-
44-
45-
def _lower_str(in_str):
46-
return in_str.lower()
47-
48-
4941
def _re_search(regex, s):
5042
return re.search(regex, s) is not None
5143

@@ -128,8 +120,6 @@ def _maybe(expander, var_name, default=""):
128120
"simplify_str": spack.util.naming.simplify_name,
129121
"join_str": _join_str,
130122
"re_search": _re_search,
131-
"upper_str": _upper_str,
132-
"lower_str": _lower_str,
133123
}
134124

135125
# Format Spec Regex:
@@ -976,7 +966,13 @@ def _eval_function_call(self, node):
976966
parts = node.func.id.split("_", 1)
977967
if len(parts) == 2:
978968
module_name, func_name = parts
979-
if module_name in supported_modules:
969+
# Special handling for function calls prefixed with `str_`
970+
if module_name == "str" and len(args) > 0:
971+
s = str(args[0])
972+
if hasattr(s, func_name) and callable(getattr(s, func_name)):
973+
s_method = getattr(s, func_name)
974+
return s_method(*args[1:], **kwargs)
975+
elif module_name in supported_modules:
980976
module = supported_modules[module_name]
981977
if hasattr(module, func_name):
982978
func = getattr(module, func_name)

lib/ramble/ramble/test/expander.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ def build_variant_set():
146146
("math_sqrt(64)", "8.0", set(), 1),
147147
("math_log(9, 3)", "2.0", set(), 1),
148148
("math_not_exist(1)", "math_not_exist(1)", set(), 1),
149-
("upper_str('foo')", "FOO", set(), 1),
150-
("lower_str('FOO')", "foo", set(), 1),
149+
("str_upper('foo')", "FOO", set(), 1),
150+
("str_lower('FOO')", "foo", set(), 1),
151+
("str_capitalize('foo')", "Foo", set(), 1),
152+
("str_lstrip('AAAbbb', 'A')", "bbb", set(), 1),
153+
("str_join('.', str_split('a b c 1'))", "a.b.c.1", set(), 1),
154+
("str_no_such_method('a')", "str_no_such_method('a')", set(), 1),
151155
],
152156
)
153157
def test_expansions(input, output, no_expand_vars, passes):

0 commit comments

Comments
 (0)