Skip to content

Commit

Permalink
Merge pull request #4677 from Flamefire/20241010180706_new_pr_iTvtXUcswP
Browse files Browse the repository at this point in the history
Add `resolve_template` method to `EasyConfig` class
  • Loading branch information
boegel authored Dec 11, 2024
2 parents dcbd5c9 + 6381d2f commit ae40c24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
14 changes: 8 additions & 6 deletions easybuild/framework/easyconfig/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,12 @@ def _generate_template_values(self, ignore=None):
if self.template_values[key] is None:
del self.template_values[key]

def resolve_template(self, value):
"""Resolve all templates in the given value using this easyconfig"""
if not self.template_values:
self.generate_template_values()
return resolve_template(value, self.template_values)

@handle_deprecated_or_replaced_easyconfig_parameters
def __contains__(self, key):
"""Check whether easyconfig parameter is defined"""
Expand All @@ -1770,9 +1776,7 @@ def __getitem__(self, key):
raise EasyBuildError("Use of unknown easyconfig parameter '%s' when getting parameter value", key)

if self.enable_templating:
if self.template_values is None or len(self.template_values) == 0:
self.generate_template_values()
value = resolve_template(value, self.template_values)
value = self.resolve_template(value)

return value

Expand Down Expand Up @@ -1848,9 +1852,7 @@ def asdict(self):
for key, tup in self._config.items():
value = tup[0]
if self.enable_templating:
if not self.template_values:
self.generate_template_values()
value = resolve_template(value, self.template_values)
value = self.resolve_template(value)
res[key] = value
return res

Expand Down
23 changes: 23 additions & 0 deletions test/framework/easyconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,29 @@ def test_templating_constants(self):
ec = EasyConfig(test_ec)
self.assertEqual(ec['sanity_check_commands'], ['mpiexec -np 1 -- toy'])

def test_ec_method_resolve_template(self):
"""Test the `resolve_template` method of easyconfig instances."""
# don't use any escaping insanity here, since it is templated itself
self.contents = textwrap.dedent("""
easyblock = "ConfigureMake"
name = "PI"
version = "3.14"
homepage = "http://example.com"
description = "test easyconfig %(name)s version %(version_major)s"
toolchain = SYSTEM
""")
self.prep()
ec = EasyConfig(self.eb_file, validate=False)

# We can resolve anything with values from the EC
self.assertEqual(ec.resolve_template('%(namelower)s %(version_major)s begins with %(nameletterlower)s'),
'pi 3 begins with p')

# `resolve_template` does basically the same resolving any value on acccess
description = ec.get('description', resolve=False)
self.assertIn('%', description, 'Description needs a template for the next test')
self.assertEqual(ec.resolve_template(description), ec['description'])

def test_templating_cuda_toolchain(self):
"""Test templates via toolchain component, like setting %(cudaver)s with fosscuda toolchain."""

Expand Down

0 comments on commit ae40c24

Please sign in to comment.