Skip to content

Commit

Permalink
Rename Exceptions
Browse files Browse the repository at this point in the history
- MappingException renamed as MappingError and MivotException as MivotError
- NoMatchingDMTypeError typed as child of TypeError.
- Both MappingError and MivotError keep children of Exception.
  • Loading branch information
lmichel committed Oct 1, 2024
1 parent f60533b commit ff0eeea
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 44 deletions.
6 changes: 3 additions & 3 deletions pyvo/mivot/features/static_reference_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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}")

Check warning on line 53 in pyvo/mivot/features/static_reference_resolver.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/features/static_reference_resolver.py#L53

Added line #L53 was not covered by tests
# Resolve static references recursively
if not found_in_global:
StaticReferenceResolver.resolve(annotation_seeker, templates_ref, ele)
Expand Down
10 changes: 5 additions & 5 deletions pyvo/mivot/seekers/annotation_seeker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions pyvo/mivot/tests/test_mivot_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyvo/mivot/tests/test_vizier_cs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
14 changes: 7 additions & 7 deletions pyvo/mivot/tests/test_xml_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@
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+")
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)

Expand Down
4 changes: 2 additions & 2 deletions pyvo/mivot/utils/dict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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))

Check warning on line 34 in pyvo/mivot/utils/dict_utils.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/utils/dict_utils.py#L34

Added line #L34 was not covered by tests
else:
logging.error("{} reading {}".format(exception, filename))

Expand Down
10 changes: 5 additions & 5 deletions pyvo/mivot/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyvo/mivot/utils/xml_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(

Check warning on line 98 in pyvo/mivot/utils/xml_utils.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/utils/xml_utils.py#L98

Added line #L98 was not covered by tests
f"Attribute {ele.get(Att.dmrole)} can not be set:"
f" references a non existing column: {attr_ref} "
f"and has no default value")
Expand Down
22 changes: 11 additions & 11 deletions pyvo/mivot/viewer/mivot_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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:
Expand All @@ -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
)
Expand All @@ -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")

Check warning on line 362 in pyvo/mivot/viewer/mivot_viewer.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/viewer/mivot_viewer.py#L362

Added line #L362 was not covered by tests
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} ")

Check warning on line 366 in pyvo/mivot/viewer/mivot_viewer.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/viewer/mivot_viewer.py#L366

Added line #L366 was not covered by tests
logging.debug(Ele.TEMPLATES + " %s found ", stableref)
self._table_iterator = TableIterator(self._connected_tableref,
self.connected_table.to_table())
Expand Down Expand Up @@ -432,22 +432,22 @@ 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")

Check warning on line 435 in pyvo/mivot/viewer/mivot_viewer.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/viewer/mivot_viewer.py#L435

Added line #L435 was not covered by tests
rnb = 0
for res in self._parsed_votable.resources:
if res.type.lower() == "results":
logging.info("Resource %s selected", rnb)
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")

Check warning on line 443 in pyvo/mivot/viewer/mivot_viewer.py

View check run for this annotation

Codecov / codecov/patch

pyvo/mivot/viewer/mivot_viewer.py#L443

Added line #L443 was not covered by tests

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
Expand Down
8 changes: 4 additions & 4 deletions pyvo/mivot/viewer/xml_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ff0eeea

Please sign in to comment.