From 1a02b9fb7e12a19eccce573d495dec0ec192d7e6 Mon Sep 17 00:00:00 2001 From: Victor Engmark Date: Thu, 8 Jul 2021 17:18:37 +1200 Subject: [PATCH] refactor: Avoid implicit re-exports in main module --- pystac/collection.py | 21 ++++++++++----------- pystac/item_collection.py | 34 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/pystac/collection.py b/pystac/collection.py index 99ca8ce48..379dc7711 100644 --- a/pystac/collection.py +++ b/pystac/collection.py @@ -18,10 +18,11 @@ import dateutil.parser from dateutil import tz -import pystac -from pystac import STACObjectType, CatalogType +from pystac.rel_type import RelType +from pystac.stac_io import StacIO +from pystac.stac_object import STACObjectType from pystac.asset import Asset -from pystac.catalog import Catalog +from pystac.catalog import Catalog, CatalogType from pystac.layout import HrefLayoutStrategy from pystac.link import Link from pystac.utils import datetime_to_str @@ -653,7 +654,7 @@ def clone(self) -> "Collection": clone._resolved_objects.cache(clone) for link in self.links: - if link.rel == pystac.RelType.ROOT: + if link.rel == RelType.ROOT: # Collection __init__ sets correct root to clone; don't reset # if the root link points to self root_is_self = link.is_resolved() and link.target is self @@ -721,11 +722,11 @@ def from_dict( ) for link in links: - if link["rel"] == pystac.RelType.ROOT: + if link["rel"] == RelType.ROOT: # Remove the link that's generated in Catalog's constructor. - collection.remove_links(pystac.RelType.ROOT) + collection.remove_links(RelType.ROOT) - if link["rel"] != pystac.RelType.SELF or href is None: + if link["rel"] != RelType.SELF or href is None: collection.add_link(Link.from_dict(link)) if assets is not None: @@ -764,12 +765,10 @@ def full_copy( return cast(Collection, super().full_copy(root, parent)) @classmethod - def from_file( - cls, href: str, stac_io: Optional[pystac.StacIO] = None - ) -> "Collection": + def from_file(cls, href: str, stac_io: Optional[StacIO] = None) -> "Collection": result = super().from_file(href, stac_io) if not isinstance(result, Collection): - raise pystac.STACTypeError(f"{result} is not a {Collection}.") + raise STACTypeError(f"{result} is not a {Collection}.") return result @classmethod diff --git a/pystac/item_collection.py b/pystac/item_collection.py index 6744f4606..b5494971a 100644 --- a/pystac/item_collection.py +++ b/pystac/item_collection.py @@ -2,15 +2,17 @@ from pystac.errors import STACTypeError from typing import Any, Dict, Iterator, List, Optional, Collection, Iterable, Union -import pystac +from pystac.item import Item +from pystac.stac_io import StacIO +from pystac.stac_object import STACObjectType from pystac.utils import make_absolute_href, is_absolute_href from pystac.serialization.identify import identify_stac_object_type -ItemLike = Union[pystac.Item, Dict[str, Any]] +ItemLike = Union[Item, Dict[str, Any]] -class ItemCollection(Collection[pystac.Item]): +class ItemCollection(Collection[Item]): """Implementation of a GeoJSON FeatureCollection whose features are all STAC Items. @@ -70,7 +72,7 @@ class ItemCollection(Collection[pystac.Item]): # If an item is present in both ItemCollections it will only be added once """ - items: List[pystac.Item] + items: List[Item] """List of :class:`pystac.Item` instances contained in this ``ItemCollection``.""" extra_fields: Dict[str, Any] @@ -83,20 +85,20 @@ def __init__( extra_fields: Optional[Dict[str, Any]] = None, clone_items: bool = False, ): - def map_item(item_or_dict: ItemLike) -> pystac.Item: + def map_item(item_or_dict: ItemLike) -> Item: # Converts dicts to pystac.Items and clones if necessary - if isinstance(item_or_dict, pystac.Item): + if isinstance(item_or_dict, Item): return item_or_dict.clone() if clone_items else item_or_dict else: - return pystac.Item.from_dict(item_or_dict) + return Item.from_dict(item_or_dict) self.items = list(map(map_item, items)) self.extra_fields = extra_fields or {} - def __getitem__(self, idx: int) -> pystac.Item: + def __getitem__(self, idx: int) -> Item: return self.items[idx] - def __iter__(self) -> Iterator[pystac.Item]: + def __iter__(self) -> Iterator[Item]: return iter(self.items) def __len__(self) -> int: @@ -151,7 +153,7 @@ def from_dict( raise STACTypeError("Dict is not a valid ItemCollection") items = [ - pystac.Item.from_dict(item, preserve_dict=preserve_dict) + Item.from_dict(item, preserve_dict=preserve_dict) for item in d.get("features", []) ] extra_fields = {k: v for k, v in d.items() if k not in ("features", "type")} @@ -159,9 +161,7 @@ def from_dict( return cls(items=items, extra_fields=extra_fields) @classmethod - def from_file( - cls, href: str, stac_io: Optional[pystac.StacIO] = None - ) -> "ItemCollection": + def from_file(cls, href: str, stac_io: Optional[StacIO] = None) -> "ItemCollection": """Reads a :class:`ItemCollection` from a JSON file. Arguments: @@ -169,7 +169,7 @@ def from_file( stac_io : A :class:`~pystac.StacIO` instance to use for file I/O """ if stac_io is None: - stac_io = pystac.StacIO.default() + stac_io = StacIO.default() if not is_absolute_href(href): href = make_absolute_href(href) @@ -181,7 +181,7 @@ def from_file( def save_object( self, dest_href: str, - stac_io: Optional[pystac.StacIO] = None, + stac_io: Optional[StacIO] = None, ) -> None: """Saves this instance to the ``dest_href`` location. @@ -191,7 +191,7 @@ def save_object( will use the default instance. """ if stac_io is None: - stac_io = pystac.StacIO.default() + stac_io = StacIO.default() stac_io.save_json(dest_href, self.to_dict()) @@ -217,6 +217,6 @@ def is_item_collection(d: Dict[str, Any]) -> bool: # Prior to STAC 0.9 ItemCollections did not have a stac_version field and could # only be identified by the fact that all of their 'features' are STAC Items. return all( - identify_stac_object_type(feature) == pystac.STACObjectType.ITEM + identify_stac_object_type(feature) == STACObjectType.ITEM for feature in d.get("features", []) )