From ab211ad67bba0998703b263c39070576ae6042fd Mon Sep 17 00:00:00 2001 From: Evan Harvey Date: Thu, 12 Dec 2024 09:52:17 -0700 Subject: [PATCH 1/2] Fix test_with_attrfile bug and re-enable test --- opencsp/common/lib/file/AttributesManager.py | 49 +++++++++++-------- .../common/lib/render/ImageAttributeParser.py | 2 +- .../render/test/test_ImageAttributeParser.py | 1 - opencsp/common/lib/tool/file_tools.py | 2 + 4 files changed, 31 insertions(+), 23 deletions(-) diff --git a/opencsp/common/lib/file/AttributesManager.py b/opencsp/common/lib/file/AttributesManager.py index 2a500bca0..3a387eb55 100644 --- a/opencsp/common/lib/file/AttributesManager.py +++ b/opencsp/common/lib/file/AttributesManager.py @@ -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: @@ -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.""" diff --git a/opencsp/common/lib/render/ImageAttributeParser.py b/opencsp/common/lib/render/ImageAttributeParser.py index dab712209..2b37f18f9 100644 --- a/opencsp/common/lib/render/ImageAttributeParser.py +++ b/opencsp/common/lib/render/ImageAttributeParser.py @@ -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__) diff --git a/opencsp/common/lib/render/test/test_ImageAttributeParser.py b/opencsp/common/lib/render/test/test_ImageAttributeParser.py index 91bc498ea..00a9ff4ae 100644 --- a/opencsp/common/lib/render/test/test_ImageAttributeParser.py +++ b/opencsp/common/lib/render/test/test_ImageAttributeParser.py @@ -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) diff --git a/opencsp/common/lib/tool/file_tools.py b/opencsp/common/lib/tool/file_tools.py index 639d4fb34..1f5a3dffb 100755 --- a/opencsp/common/lib/tool/file_tools.py +++ b/opencsp/common/lib/tool/file_tools.py @@ -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 From 315e02b415a69a26e6181ec9277578a7cfd9f68d Mon Sep 17 00:00:00 2001 From: Evan Harvey Date: Tue, 7 Jan 2025 17:12:17 -0700 Subject: [PATCH 2/2] Remove get_subclass_parser_classes --- opencsp/common/lib/file/AttributesManager.py | 27 -------------------- 1 file changed, 27 deletions(-) diff --git a/opencsp/common/lib/file/AttributesManager.py b/opencsp/common/lib/file/AttributesManager.py index 3a387eb55..f7cd4f0cd 100644 --- a/opencsp/common/lib/file/AttributesManager.py +++ b/opencsp/common/lib/file/AttributesManager.py @@ -62,33 +62,6 @@ 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: