Skip to content

Commit

Permalink
Fix test_with_attrfile bug and re-enable test
Browse files Browse the repository at this point in the history
  • Loading branch information
e10harvey committed Dec 12, 2024
1 parent a5f6fb7 commit ab211ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
49 changes: 28 additions & 21 deletions opencsp/common/lib/file/AttributesManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ def parsers(self):
def _register_parser_class(cls, parser_class: type[aap.AbstractAttributeParser]):
_registered_parser_classes.add(parser_class)

def get_subclass_parser_classes(self, parser_class: type[aap.AbstractAttributeParser]):
"""
Returns a list of all parsers that are subclasses of the given parser_class.
Parameters
----------
parser_class : type[aap.AbstractAttributeParser]
The parser class to search for.
Returns
-------
subclass_parsers : list[aap.AbstractAttributeParser]
If parsers are found, a list of all parsers that are subclasses of the given parser_class. Otherwise, None.
"""
subclass_parsers: list[aap.AbstractAttributeParser] = []
for parser in self.parsers:
if isinstance(parser, parser_class):
subclass_parsers.append(parser)

if len(subclass_parsers) == 0:
lt.debug(
f"In AttributesManager.get_parser(): found no subclass parsers matching parser class {parser_class}"
)
return None
else:
return subclass_parsers

def get_parser(
self, parser_class: type[aap.AbstractAttributeParser], error_on_not_found=True
) -> aap.AbstractAttributeParser:
Expand All @@ -80,27 +107,7 @@ def get_parser(
if parser_class in self.generic_parsers:
return self.generic_parsers[parser_class]

# find all parsers that are a subclass of the requested parser_class
subclass_parsers: list[aap.AbstractAttributeParser] = []
for parser in self.parsers:
if isinstance(parser, parser_class):
subclass_parsers.append(parser)
if len(subclass_parsers) == 1:
return subclass_parsers[0]
if len(subclass_parsers) == 0:
if error_on_not_found:
lt.error_and_raise(
RuntimeError,
"Programmer error in AttributesManager.get_parser(): "
+ f"there is an unregistered parser class {parser_class}! All AbstractAttributeParsers should "
+ "register themselves with RegisterClass() when their file is imported to prevent this error.",
)
else:
return None

# more than one subclass found, just return the first one
lt.debug(f"In AttributesManager.get_parser(): found more than one parser matching parser class {parser_class}")
return subclass_parsers[0]
return None

def set_parser(self, parser: aap.AbstractAttributeParser):
"""Sets the given parser as the default parser for the given class."""
Expand Down
2 changes: 1 addition & 1 deletion opencsp/common/lib/render/ImageAttributeParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(
attributes_file = os.path.join(opath, f"{oname}.txt")
self._previous_attr = am.AttributesManager()
self._previous_attr.load(attributes_file)
except:
except Exception as e:
pass
if self._previous_attr != None:
prev_image_attr: ImageAttributeParser = self._previous_attr.get_parser(self.__class__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def test_has_contents(self):
parser = iap.ImageAttributeParser(notes="")
self.assertEqual(True, parser.has_contents())

@pytest.mark.skip("See https://github.com/sandialabs/OpenCSP/issues/3")
def test_with_attrfile(self):
"""Load all values from the associated attributes file. Use the new current_image_source value."""
parser = iap.ImageAttributeParser(current_image_source=self.img_file)
Expand Down
2 changes: 2 additions & 0 deletions opencsp/common/lib/tool/file_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ def path_components(input_dir_body_ext: str):
See also: body_ext_given_file_dir_body_ext()
"""

dir = os.path.dirname(input_dir_body_ext)
body_ext = os.path.basename(input_dir_body_ext)
body, ext = os.path.splitext(body_ext)

return dir, body, ext


Expand Down

0 comments on commit ab211ad

Please sign in to comment.