Skip to content

Commit

Permalink
refactor: Avoid implicit reexports in validation
Browse files Browse the repository at this point in the history
  • Loading branch information
l0b0 committed Jul 8, 2021
1 parent 5b08f69 commit 5c38b3a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 45 deletions.
15 changes: 9 additions & 6 deletions pystac/validation/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(),
)
Expand Down Expand Up @@ -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.
Expand All @@ -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)

Expand All @@ -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):
Expand All @@ -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
)
Expand Down
64 changes: 29 additions & 35 deletions pystac/validation/schema_uri_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -151,67 +151,63 @@ 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"
),
},
None,
),
OldExtensionShortIDs.COLLECTION_ASSETS.value: (
{
pystac.STACObjectType.COLLECTION: (
STACObjectType.COLLECTION: (
"extensions/collection-assets/json-schema/schema.json"
)
},
None,
),
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"
),
},
[
(
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"},
)
],
),
Expand All @@ -222,74 +218,72 @@ 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"
),
},
None,
),
OldExtensionShortIDs.SINGLE_FILE_STAC.value: (
{
pystac.STACObjectType.CATALOG: (
STACObjectType.CATALOG: (
"extensions/single-file-stac/json-schema/schema.json"
)
},
None,
),
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"
),
},
None,
),
OldExtensionShortIDs.TIMESTAMPS.value: (
{
pystac.STACObjectType.ITEM: (
STACObjectType.ITEM: (
"extensions/timestamps/json-schema/schema.json"
)
},
None,
),
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.
Expand All @@ -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"
)
},
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions pystac/validation/stac_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5c38b3a

Please sign in to comment.