From ff0eeeaf585a1492ba3233816e1f8e61cc0e8233 Mon Sep 17 00:00:00 2001 From: lmichel Date: Tue, 1 Oct 2024 11:19:34 +0200 Subject: [PATCH] Rename Exceptions - MappingException renamed as MappingError and MivotException as MivotError - NoMatchingDMTypeError typed as child of TypeError. - Both MappingError and MivotError keep children of Exception. --- .../features/static_reference_resolver.py | 6 ++--- pyvo/mivot/seekers/annotation_seeker.py | 10 ++++----- pyvo/mivot/tests/test_mivot_viewer.py | 6 ++--- pyvo/mivot/tests/test_vizier_cs.py | 4 ++-- pyvo/mivot/tests/test_xml_viewer.py | 14 ++++++------ pyvo/mivot/utils/dict_utils.py | 4 ++-- pyvo/mivot/utils/exceptions.py | 10 ++++----- pyvo/mivot/utils/xml_utils.py | 4 ++-- pyvo/mivot/viewer/mivot_viewer.py | 22 +++++++++---------- pyvo/mivot/viewer/xml_viewer.py | 8 +++---- 10 files changed, 44 insertions(+), 44 deletions(-) diff --git a/pyvo/mivot/features/static_reference_resolver.py b/pyvo/mivot/features/static_reference_resolver.py index 8e194c5c8..fd376e924 100644 --- a/pyvo/mivot/features/static_reference_resolver.py +++ b/pyvo/mivot/features/static_reference_resolver.py @@ -2,7 +2,7 @@ Class used to resolve each static REFERENCE found in mivot_block. """ from copy import deepcopy -from pyvo.mivot.utils.exceptions import MivotException +from pyvo.mivot.utils.exceptions import MivotError from pyvo.mivot.utils.xpath_utils import XPath from pyvo.utils.prototype import prototype_feature @@ -33,7 +33,7 @@ def resolve(annotation_seeker, templates_ref, mivot_block): The number of references resolved. Raises ------ - MappingException + MappingError If the reference cannot be resolved. NotImplementedError If the reference is dynamic. @@ -50,7 +50,7 @@ def resolve(annotation_seeker, templates_ref, mivot_block): target = annotation_seeker.get_templates_instance_by_dmid(templates_ref, dmref) found_in_global = False if target is None: - raise MivotException(f"Cannot resolve reference={dmref}") + raise MivotError(f"Cannot resolve reference={dmref}") # Resolve static references recursively if not found_in_global: StaticReferenceResolver.resolve(annotation_seeker, templates_ref, ele) diff --git a/pyvo/mivot/seekers/annotation_seeker.py b/pyvo/mivot/seekers/annotation_seeker.py index 4f74eca5c..5c0d718a6 100644 --- a/pyvo/mivot/seekers/annotation_seeker.py +++ b/pyvo/mivot/seekers/annotation_seeker.py @@ -3,7 +3,7 @@ Utilities for extracting sub-blocks from a MIVOT mapping block. """ import logging -from pyvo.mivot.utils.exceptions import MivotException, MappingException +from pyvo.mivot.utils.exceptions import MivotError, MappingError from pyvo.mivot.utils.vocabulary import Att, Ele from pyvo.mivot.utils.vocabulary import Constant from pyvo.mivot.utils.xpath_utils import XPath @@ -73,7 +73,7 @@ def _find_templates_blocks(self): logging.debug("Found " + Ele.TEMPLATES + " without " + Att.tableref) self._templates_blocks["DEFAULT"] = child else: - raise MivotException(Ele.TEMPLATES + " without " + Att.tableref + " must be unique") + raise MivotError(Ele.TEMPLATES + " without " + Att.tableref + " must be unique") def _rename_ref_and_join(self): """ @@ -391,7 +391,7 @@ def get_collection_item_by_primarykey(self, coll_dmid, key_value): Raises ------ MivotElementNotFound: If no element matches the criteria. - MappingException: If more than one element matches the criteria. + MappingError: If more than one element matches the criteria. """ eset = XPath.x_path(self._globals_block, ".//" + Ele.COLLECTION + "[@" + Att.dmid + "='" + coll_dmid + "']/" + Ele.INSTANCE + "/" + Att.primarykey @@ -400,14 +400,14 @@ def get_collection_item_by_primarykey(self, coll_dmid, key_value): message = (f"{Ele.INSTANCE} with {Att.primarykey} = {key_value} in " f"{Ele.COLLECTION} {Att.dmid} {key_value} not found" ) - raise MivotException(message) + raise MivotError(message) if len(eset) > 1: message = ( f"More than one {Ele.INSTANCE} with {Att.primarykey}" f" = {key_value} found in {Ele.COLLECTION} " f"{Att.dmid} {key_value}" ) - raise MappingException(message) + raise MappingError(message) logging.debug(Ele.INSTANCE + " with " + Att.primarykey + "=%s found in " + Ele.COLLECTION + " " + Att.dmid + "=%s", key_value, coll_dmid) diff --git a/pyvo/mivot/tests/test_mivot_viewer.py b/pyvo/mivot/tests/test_mivot_viewer.py index 866839c8e..20fb63f85 100644 --- a/pyvo/mivot/tests/test_mivot_viewer.py +++ b/pyvo/mivot/tests/test_mivot_viewer.py @@ -8,7 +8,7 @@ from astropy.utils.data import get_pkg_data_filename from pyvo.mivot.utils.vocabulary import Constant from pyvo.mivot.utils.dict_utils import DictUtils -from pyvo.mivot.utils.exceptions import MappingException +from pyvo.mivot.utils.exceptions import MappingError from pyvo.mivot.version_checker import check_astropy_version from pyvo.mivot import MivotViewer from astropy import version as astropy_version @@ -84,9 +84,9 @@ def test_no_mivot(path_no_mivot): assert m_viewer.get_globals_models() is None assert m_viewer.get_templates_models() is None - with pytest.raises(MappingException): + with pytest.raises(MappingError): m_viewer._connect_table('_PKTable') - with pytest.raises(MappingException): + with pytest.raises(MappingError): m_viewer._connect_table() assert m_viewer.next_table_row() is None diff --git a/pyvo/mivot/tests/test_vizier_cs.py b/pyvo/mivot/tests/test_vizier_cs.py index 52c5ed696..573430cec 100644 --- a/pyvo/mivot/tests/test_vizier_cs.py +++ b/pyvo/mivot/tests/test_vizier_cs.py @@ -21,7 +21,7 @@ from urllib.request import urlretrieve from pyvo.mivot.version_checker import check_astropy_version from pyvo.mivot import MivotViewer -from pyvo.mivot.utils.exceptions import MivotException +from pyvo.mivot.utils.exceptions import MivotError @pytest.fixture @@ -125,5 +125,5 @@ def test_bad_ref(path_to_badref, delt_coo): """ Test that the epoch propagation works with all FIELDs referenced by name or by ID """ # Test with all FILELDs referenced by names - with (pytest.raises(MivotException, match="Attribute mango:EpochPosition.epoch can not be set.*")): + with (pytest.raises(MivotError, match="Attribute mango:EpochPosition.epoch can not be set.*")): MivotViewer(votable_path=path_to_badref) diff --git a/pyvo/mivot/tests/test_xml_viewer.py b/pyvo/mivot/tests/test_xml_viewer.py index 8431c500b..e0dde04ea 100644 --- a/pyvo/mivot/tests/test_xml_viewer.py +++ b/pyvo/mivot/tests/test_xml_viewer.py @@ -10,7 +10,7 @@ from astropy.utils.data import get_pkg_data_filename from pyvo.mivot.version_checker import check_astropy_version from pyvo.mivot import MivotViewer -from pyvo.mivot.utils.exceptions import MivotException +from pyvo.mivot.utils.exceptions import MivotError @pytest.mark.skipif(not check_astropy_version(), reason="need astropy 6+") @@ -18,27 +18,27 @@ def test_xml_viewer(m_viewer): m_viewer.next() xml_viewer = m_viewer.xml_viewer - with pytest.raises(MivotException, + with pytest.raises(MivotError, match="Cannot find dmrole wrong_role in any instances of the VOTable"): xml_viewer.get_instance_by_role("wrong_role") - with pytest.raises(MivotException, + with pytest.raises(MivotError, match="Cannot find dmrole wrong_role in any instances of the VOTable"): xml_viewer.get_instance_by_role("wrong_role", all_instances=True) - with pytest.raises(MivotException, + with pytest.raises(MivotError, match="Cannot find dmtype wrong_dmtype in any instances of the VOTable"): xml_viewer.get_instance_by_type("wrong_dmtype") - with pytest.raises(MivotException, + with pytest.raises(MivotError, match="Cannot find dmtype wrong_dmtype in any instances of the VOTable"): xml_viewer.get_instance_by_type("wrong_dmtype", all_instances=True) - with pytest.raises(MivotException, + with pytest.raises(MivotError, match="Cannot find dmrole wrong_role in any collections of the VOTable"): xml_viewer.get_collection_by_role("wrong_role") - with pytest.raises(MivotException, + with pytest.raises(MivotError, match="Cannot find dmrole wrong_role in any collections of the VOTable"): xml_viewer.get_collection_by_role("wrong_role", all_instances=True) diff --git a/pyvo/mivot/utils/dict_utils.py b/pyvo/mivot/utils/dict_utils.py index bd9d76e97..679c55a23 100644 --- a/pyvo/mivot/utils/dict_utils.py +++ b/pyvo/mivot/utils/dict_utils.py @@ -3,7 +3,7 @@ """ import json import logging -from pyvo.mivot.utils.exceptions import MivotException +from pyvo.mivot.utils.exceptions import MivotError from pyvo.mivot.utils.json_encoder import MivotJsonEncoder @@ -31,7 +31,7 @@ def read_dict_from_file(filename, fatal=False): return json.load(file, object_pairs_hook=OrderedDict) except Exception as exception: if fatal: - raise MivotException("reading {}".format(filename)) + raise MivotError("reading {}".format(filename)) else: logging.error("{} reading {}".format(exception, filename)) diff --git a/pyvo/mivot/utils/exceptions.py b/pyvo/mivot/utils/exceptions.py index 7e688b57c..16944d090 100644 --- a/pyvo/mivot/utils/exceptions.py +++ b/pyvo/mivot/utils/exceptions.py @@ -3,19 +3,19 @@ 3 exception classes - AstropyVersionException that prevent to use the package -- MappingException if the annotation cannot be processed (e.g. no MIVOT block) +- MappingError if the annotation cannot be processed (e.g. no MIVOT block) but the VOtable parsing can continue -- MivotException in any other case (block the processing) +- MivotError in any other case (block the processing) """ -class MivotException(Exception): +class MivotError(Exception): """ The annotation block is there but something went wrong with its processing """ -class MappingException(Exception): +class MappingError(Exception): """ Exception raised if a Resource or MIVOT element can't be mapped for one of these reasons: - It doesn't match with any Resource/MIVOT element expected. @@ -25,7 +25,7 @@ class MappingException(Exception): """ -class NoMatchingDMTypeError(Exception): +class NoMatchingDMTypeError(TypeError): """ Exception thrown when some PyVO code misses MIVOT element: - When trying to build a SkyCoord while there is no position in the annotations diff --git a/pyvo/mivot/utils/xml_utils.py b/pyvo/mivot/utils/xml_utils.py index 7c3f40cc9..83f15a108 100644 --- a/pyvo/mivot/utils/xml_utils.py +++ b/pyvo/mivot/utils/xml_utils.py @@ -5,7 +5,7 @@ import xml.etree.ElementTree as ET from pyvo.mivot.utils.vocabulary import Constant from pyvo.mivot.utils.vocabulary import Att -from pyvo.mivot.utils.exceptions import MivotException +from pyvo.mivot.utils.exceptions import MivotError class XmlUtils: @@ -95,7 +95,7 @@ def add_column_indices(mapping_block, index_map): break if not field_desc: if not ele.get(Att.value): - raise MivotException( + raise MivotError( f"Attribute {ele.get(Att.dmrole)} can not be set:" f" references a non existing column: {attr_ref} " f"and has no default value") diff --git a/pyvo/mivot/viewer/mivot_viewer.py b/pyvo/mivot/viewer/mivot_viewer.py index a6f1224f7..2521036d2 100644 --- a/pyvo/mivot/viewer/mivot_viewer.py +++ b/pyvo/mivot/viewer/mivot_viewer.py @@ -29,8 +29,8 @@ from pyvo.dal import DALResults from pyvo.mivot.utils.vocabulary import Ele, Att from pyvo.mivot.utils.vocabulary import Constant, NoMapping -from pyvo.mivot.utils.exceptions import (MappingException, - MivotException, +from pyvo.mivot.utils.exceptions import (MappingError, + MivotError, AstropyVersionException) from pyvo.mivot.utils.xml_utils import XmlUtils from pyvo.mivot.utils.xpath_utils import XPath @@ -100,7 +100,7 @@ def __init__(self, votable_path, tableref=None): self._set_mapped_tables() self._connect_table(tableref) self._init_instance() - except MappingException as mnf: + except MappingError as mnf: logging.error(str(mnf)) def __enter__(self): @@ -325,7 +325,7 @@ def get_first_instance_dmtype(self, tableref=None): elif child[0] in collection: return collection[0].get(Att.dmtype) else: - raise MivotException("Can't find the first " + Ele.INSTANCE + raise MivotError("Can't find the first " + Ele.INSTANCE + "/" + Ele.COLLECTION + " in " + Ele.TEMPLATES) def _connect_table(self, tableref=None): @@ -340,7 +340,7 @@ def _connect_table(self, tableref=None): Identifier of the table. If None, connects to the first table. """ if not self._resource_seeker: - raise MappingException("No mapping block found") + raise MappingError("No mapping block found") stableref = tableref if tableref is None: @@ -350,7 +350,7 @@ def _connect_table(self, tableref=None): "the mapping will be applied to the first table." ) elif tableref not in self._mapped_tables: - raise MappingException(f"The table {self._connected_tableref} doesn't match with any " + raise MappingError(f"The table {self._connected_tableref} doesn't match with any " f"mapped_table ({self._mapped_tables}) encountered in " + Ele.TEMPLATES ) @@ -359,11 +359,11 @@ def _connect_table(self, tableref=None): self._connected_table = self._resource_seeker.get_table(tableref) if self.connected_table is None: - raise MivotException(f"Cannot find table {stableref} in VOTable") + raise MivotError(f"Cannot find table {stableref} in VOTable") logging.debug("table %s found in VOTable", stableref) self._templates = deepcopy(self.annotation_seeker.get_templates_block(tableref)) if self._templates is None: - raise MivotException("Cannot find " + Ele.TEMPLATES + f" {stableref} ") + raise MivotError("Cannot find " + Ele.TEMPLATES + f" {stableref} ") logging.debug(Ele.TEMPLATES + " %s found ", stableref) self._table_iterator = TableIterator(self._connected_tableref, self.connected_table.to_table()) @@ -432,7 +432,7 @@ def _set_resource(self): """ if len(self._parsed_votable.resources) < 1: - raise MivotException("No resource detected in the VOTable") + raise MivotError("No resource detected in the VOTable") rnb = 0 for res in self._parsed_votable.resources: if res.type.lower() == "results": @@ -440,14 +440,14 @@ def _set_resource(self): self._resource = self._parsed_votable.resources[rnb] return rnb += 1 - raise MivotException("No resource @type='results'detected in the VOTable") + raise MivotError("No resource @type='results'detected in the VOTable") def _set_mapping_block(self): """ Set the mapping block found in the resource and set the annotation_seeker """ if NoMapping.search(self._resource.mivot_block.content): - raise MappingException("Mivot block is not found") + raise MappingError("Mivot block is not found") # The namespace should be removed self._mapping_block = ( etree.fromstring(self._resource.mivot_block.content diff --git a/pyvo/mivot/viewer/xml_viewer.py b/pyvo/mivot/viewer/xml_viewer.py index 75103a0ec..fd69e79ce 100644 --- a/pyvo/mivot/viewer/xml_viewer.py +++ b/pyvo/mivot/viewer/xml_viewer.py @@ -3,7 +3,7 @@ XMLViewer provides several getters on XML instances built by `pyvo.mivot.viewer.mivot_viewer`. """ -from pyvo.mivot.utils.exceptions import MivotException +from pyvo.mivot.utils.exceptions import MivotError from pyvo.mivot.utils.xpath_utils import XPath from pyvo.utils.prototype import prototype_feature @@ -60,7 +60,7 @@ def get_instance_by_role(self, dmrole, all_instances=False): dmrole) if len(instances) == 0: - raise MivotException( + raise MivotError( f"Cannot find dmrole {dmrole} in any instances of the VOTable") if all_instances is False: @@ -98,7 +98,7 @@ def get_instance_by_type(self, dmtype, all_instances=False): dmtype) if len(instances) == 0: - raise MivotException( + raise MivotError( f"Cannot find dmtype {dmtype} in any instances of the VOTable") if all_instances is False: @@ -136,7 +136,7 @@ def get_collection_by_role(self, dmrole, all_instances=False): dmrole) if len(collections) == 0: - raise MivotException( + raise MivotError( f"Cannot find dmrole {dmrole} in any collections of the VOTable") if all_instances is False: