Skip to content

Commit d6e3863

Browse files
authored
Merge pull request #411 from apdavison/issue367
Fix for #367 - handle YAML files with both .yml and .yaml extensions
2 parents a137384 + 37a9230 commit d6e3863

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

sumatra/core.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ def register(self, component):
129129
if not hasattr(component, attr):
130130
raise TypeError("%s is missing required attribute %s" % (component, attr))
131131
if hasattr(component, "name"):
132-
name = component.name
132+
names = [component.name]
133+
elif hasattr(component, "names"):
134+
names = component.names
133135
else:
134-
name = component.__name__
135-
self._components[base_class][name] = component
136+
names = [component.__name__]
137+
for name in names:
138+
self._components[base_class][name] = component
136139
return
137140
raise TypeError("%s is not a Sumatra component." % component)
138141

sumatra/parameters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class YAMLParameterSet(ParameterSet):
132132
Handles parameter files in YAML format, as parsed by the
133133
PyYAML module
134134
"""
135-
name = ".yaml"
135+
names = (".yaml", ".yml")
136136

137137
def __init__(self, initialiser):
138138
"""

test/unittests/test_parameters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ def setUp(self):
513513
if yaml_loaded:
514514
with open("test_file.yaml", "w") as f:
515515
f.write(init2)
516+
with open("test_file.yml", "w") as f:
517+
f.write(init2)
516518

517519
def tearDown(self):
518520
os.remove("test_file.simple")
@@ -548,6 +550,12 @@ def test__build_parameters_yaml(self):
548550
self.assertEqual(P.as_dict(), {'x': 2, 'y': {'a': 3, 'b': 4}})
549551
self.assertIsInstance(P, YAMLParameterSet)
550552

553+
@unittest.skipUnless(yaml_loaded, "PyYAML not available")
554+
def test__build_parameters_yml(self):
555+
P = build_parameters("test_file.yml")
556+
self.assertEqual(P.as_dict(), {'x': 2, 'y': {'a': 3, 'b': 4}})
557+
self.assertIsInstance(P, YAMLParameterSet)
558+
551559

552560
# these tests should now be applied to commands.parse_arguments and/or commands.parse_command_line_parameter()
553561
#def test__build_parameters__should_add_new_command_line_parameters_to_the_file_parameters(self):

0 commit comments

Comments
 (0)