diff --git a/pystac/validation/__init__.py b/pystac/validation/__init__.py index 5a2357e2c..c0b93638d 100644 --- a/pystac/validation/__init__.py +++ b/pystac/validation/__init__.py @@ -1,6 +1,9 @@ from typing import Dict, List, Any, Optional, cast, TYPE_CHECKING -import pystac +from pystac.version import get_stac_version +from pystac.stac_io import StacIO +from pystac.rel_type import RelType +from pystac.stac_object import STACObjectType from pystac.serialization.identify import STACVersionID, identify_stac_object from pystac.validation.schema_uri_map import OldExtensionSchemaUriMap from pystac.utils import make_absolute_href @@ -31,7 +34,7 @@ def validate(stac_object: "STACObject_Type") -> List[Any]: return validate_dict( stac_dict=stac_object.to_dict(), stac_object_type=stac_object.STAC_OBJECT_TYPE, - stac_version=pystac.get_stac_version(), + stac_version=get_stac_version(), extensions=stac_object.stac_extensions, href=stac_object.get_self_href(), ) @@ -105,7 +108,7 @@ def _get_uri(ext: str) -> Optional[str]: def validate_all( - stac_dict: Dict[str, Any], href: str, stac_io: Optional[pystac.StacIO] = None + stac_dict: Dict[str, Any], href: str, stac_io: Optional[StacIO] = None ) -> None: """Validate STAC JSON and all contained catalogs, collections and items. @@ -125,7 +128,7 @@ def validate_all( contained catalog, collection or item has a validation error. """ if stac_io is None: - stac_io = pystac.StacIO.default() + stac_io = StacIO.default() info = identify_stac_object(stac_dict) @@ -138,7 +141,7 @@ def validate_all( href=href, ) - if info.object_type != pystac.STACObjectType.ITEM: + if info.object_type != STACObjectType.ITEM: if "links" in stac_dict: # Account for 0.6 links if isinstance(stac_dict["links"], dict): @@ -147,7 +150,7 @@ def validate_all( links = cast(List[Dict[str, Any]], stac_dict.get("links")) for link in links: rel = link.get("rel") - if rel in [pystac.RelType.ITEM, pystac.RelType.CHILD]: + if rel in [RelType.ITEM, RelType.CHILD]: link_href = make_absolute_href( cast(str, link.get("href")), start_href=href ) diff --git a/pystac/validation/schema_uri_map.py b/pystac/validation/schema_uri_map.py index 0bcd273b9..3bcb64694 100644 --- a/pystac/validation/schema_uri_map.py +++ b/pystac/validation/schema_uri_map.py @@ -3,7 +3,7 @@ from pystac.serialization.identify import OldExtensionShortIDs, STACVersionID from typing import Any, Callable, Dict, List, Optional, Tuple -import pystac +from pystac.version import get_stac_version from pystac.serialization import STACVersionRange from pystac.stac_object import STACObjectType @@ -92,7 +92,7 @@ def get_object_schema_uri( self, object_type: STACObjectType, stac_version: str ) -> Optional[str]: uri = None - is_latest = stac_version == pystac.get_stac_version() + is_latest = stac_version == get_stac_version() if object_type not in self.DEFAULT_SCHEMA_MAP: raise KeyError("Unknown STAC object type {}".format(object_type)) @@ -151,13 +151,13 @@ def get_schema_map(cls) -> Dict[str, Any]: return { OldExtensionShortIDs.CHECKSUM.value: ( { - pystac.STACObjectType.CATALOG: ( + STACObjectType.CATALOG: ( "extensions/checksum/json-schema/schema.json" ), - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/checksum/json-schema/schema.json" ), - pystac.STACObjectType.ITEM: ( + STACObjectType.ITEM: ( "extensions/checksum/json-schema/schema.json" ), }, @@ -165,7 +165,7 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.COLLECTION_ASSETS.value: ( { - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/collection-assets/json-schema/schema.json" ) }, @@ -173,10 +173,10 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.DATACUBE.value: ( { - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/datacube/json-schema/schema.json" ), - pystac.STACObjectType.ITEM: ( + STACObjectType.ITEM: ( "extensions/datacube/json-schema/schema.json" ), }, @@ -184,34 +184,30 @@ def get_schema_map(cls) -> Dict[str, Any]: ( STACVersionRange(min_version="0.5.0", max_version="0.9.0"), { - pystac.STACObjectType.COLLECTION: None, - pystac.STACObjectType.ITEM: None, + STACObjectType.COLLECTION: None, + STACObjectType.ITEM: None, }, ) ], ), OldExtensionShortIDs.EO.value: ( - {pystac.STACObjectType.ITEM: "extensions/eo/json-schema/schema.json"}, + {STACObjectType.ITEM: "extensions/eo/json-schema/schema.json"}, None, ), OldExtensionShortIDs.ITEM_ASSETS.value: ( { - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/item-assets/json-schema/schema.json" ) }, None, ), OldExtensionShortIDs.LABEL.value: ( - { - pystac.STACObjectType.ITEM: ( - "extensions/label/json-schema/schema.json" - ) - }, + {STACObjectType.ITEM: ("extensions/label/json-schema/schema.json")}, [ ( STACVersionRange(min_version="0.8.0-rc1", max_version="0.8.1"), - {pystac.STACObjectType.ITEM: "extensions/label/schema.json"}, + {STACObjectType.ITEM: "extensions/label/schema.json"}, ) ], ), @@ -222,26 +218,26 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.PROJECTION.value: ( { - pystac.STACObjectType.ITEM: ( + STACObjectType.ITEM: ( "extensions/projection/json-schema/schema.json" ) }, None, ), OldExtensionShortIDs.SAR.value: ( - {pystac.STACObjectType.ITEM: "extensions/sar/json-schema/schema.json"}, + {STACObjectType.ITEM: "extensions/sar/json-schema/schema.json"}, None, ), OldExtensionShortIDs.SAT.value: ( - {pystac.STACObjectType.ITEM: "extensions/sat/json-schema/schema.json"}, + {STACObjectType.ITEM: "extensions/sat/json-schema/schema.json"}, None, ), OldExtensionShortIDs.SCIENTIFIC.value: ( { - pystac.STACObjectType.ITEM: ( + STACObjectType.ITEM: ( "extensions/scientific/json-schema/schema.json" ), - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/scientific/json-schema/schema.json" ), }, @@ -249,7 +245,7 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.SINGLE_FILE_STAC.value: ( { - pystac.STACObjectType.CATALOG: ( + STACObjectType.CATALOG: ( "extensions/single-file-stac/json-schema/schema.json" ) }, @@ -257,13 +253,13 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.TILED_ASSETS.value: ( { - pystac.STACObjectType.CATALOG: ( + STACObjectType.CATALOG: ( "extensions/tiled-assets/json-schema/schema.json" ), - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/tiled-assets/json-schema/schema.json" ), - pystac.STACObjectType.ITEM: ( + STACObjectType.ITEM: ( "extensions/tiled-assets/json-schema/schema.json" ), }, @@ -271,7 +267,7 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.TIMESTAMPS.value: ( { - pystac.STACObjectType.ITEM: ( + STACObjectType.ITEM: ( "extensions/timestamps/json-schema/schema.json" ) }, @@ -279,17 +275,15 @@ def get_schema_map(cls) -> Dict[str, Any]: ), OldExtensionShortIDs.VERSION.value: ( { - pystac.STACObjectType.ITEM: ( - "extensions/version/json-schema/schema.json" - ), - pystac.STACObjectType.COLLECTION: ( + STACObjectType.ITEM: ("extensions/version/json-schema/schema.json"), + STACObjectType.COLLECTION: ( "extensions/version/json-schema/schema.json" ), }, None, ), OldExtensionShortIDs.VIEW.value: ( - {pystac.STACObjectType.ITEM: "extensions/view/json-schema/schema.json"}, + {STACObjectType.ITEM: "extensions/view/json-schema/schema.json"}, None, ), # Removed or renamed extensions. @@ -300,7 +294,7 @@ def get_schema_map(cls) -> Dict[str, Any]: ( STACVersionRange(min_version="0.8.0-rc1", max_version="0.9.0"), { - pystac.STACObjectType.COLLECTION: ( + STACObjectType.COLLECTION: ( "extensions/asset/json-schema/schema.json" ) }, @@ -332,7 +326,7 @@ def get_extension_schema_uri( ) -> Optional[str]: uri = None - is_latest = stac_version == pystac.get_stac_version() + is_latest = stac_version == get_stac_version() ext_map = cls.get_schema_map() if extension_id in ext_map: diff --git a/pystac/validation/stac_validator.py b/pystac/validation/stac_validator.py index aff974997..f8dd40d89 100644 --- a/pystac/validation/stac_validator.py +++ b/pystac/validation/stac_validator.py @@ -4,7 +4,8 @@ from pystac.stac_object import STACObjectType from typing import Any, Dict, List, Optional, Tuple -import pystac +from pystac.stac_io import StacIO +from pystac.errors import STACValidationError from pystac.validation.schema_uri_map import DefaultSchemaUriMap, SchemaUriMap try: @@ -144,7 +145,7 @@ def __init__(self, schema_uri_map: Optional[SchemaUriMap] = None) -> None: def get_schema_from_uri(self, schema_uri: str) -> Tuple[Dict[str, Any], Any]: if schema_uri not in self.schema_cache: - s = json.loads(pystac.StacIO.default().read_text(schema_uri)) + s = json.loads(StacIO.default().read_text(schema_uri)) self.schema_cache[schema_uri] = s schema = self.schema_cache[schema_uri] @@ -217,7 +218,7 @@ def validate_core( msg = self._get_error_message( schema_uri, stac_object_type, None, href, stac_dict.get("id") ) - raise pystac.STACValidationError(msg, source=e) from e + raise STACValidationError(msg, source=e) from e def validate_extension( self, @@ -255,7 +256,7 @@ def validate_extension( msg = self._get_error_message( schema_uri, stac_object_type, extension_id, href, stac_dict.get("id") ) - raise pystac.STACValidationError(msg, source=e) from e + raise STACValidationError(msg, source=e) from e except Exception as e: logger.error(f"Exception while validating {stac_object_type} href: {href}") logger.exception(e)