Skip to content

Commit

Permalink
Add Asset.common_metadata property and fix some circular imports
Browse files Browse the repository at this point in the history
  • Loading branch information
duckontheweb committed Jul 17, 2021
1 parent f32390f commit d4b18a1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 54 deletions.
13 changes: 10 additions & 3 deletions pystac/asset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from copy import copy
from typing import Any, Dict, List, Optional, TYPE_CHECKING, Union

from pystac.utils import is_absolute_href, make_absolute_href
from pystac import common_metadata
from pystac import utils

if TYPE_CHECKING:
from pystac.collection import Collection as Collection_Type
Expand Down Expand Up @@ -94,11 +95,11 @@ def get_absolute_href(self) -> Optional[str]:
str: The absolute HREF of this asset, or None if an absolute HREF could not
be determined.
"""
if is_absolute_href(self.href):
if utils.is_absolute_href(self.href):
return self.href
else:
if self.owner is not None:
return make_absolute_href(self.href, self.owner.get_self_href())
return utils.make_absolute_href(self.href, self.owner.get_self_href())
else:
return None

Expand Down Expand Up @@ -145,6 +146,12 @@ def clone(self) -> "Asset":
extra_fields=self.extra_fields,
)

@property
def common_metadata(self) -> common_metadata.CommonMetadata:
"""Access the asset's common metadata fields as a
:class:`~pystac.CommonMetadata` object."""
return common_metadata.CommonMetadata(self)

def __repr__(self) -> str:
return "<Asset href={}>".format(self.href)

Expand Down
7 changes: 2 additions & 5 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,8 @@ def from_dict(

@property
def common_metadata(self) -> pystac.CommonMetadata:
"""Access the item's common metadat fields as a pystac.CommonMetadata object
Returns:
CommonMetada: contains all common metadata fields in the items properties
"""
"""Access the item's common metadata fields as a
:class:`~pystac.CommonMetadata` object."""
return pystac.CommonMetadata(self)

def full_copy(
Expand Down
90 changes: 44 additions & 46 deletions tests/test_common_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from typing import Any, Dict, List

import pystac
from pystac import CommonMetadata, Provider, ProviderRole, Item
from pystac import utils

from tests.utils import TestCases
Expand All @@ -13,12 +13,12 @@ def setUp(self) -> None:
self.URI_1 = TestCases.get_path(
"data-files/examples/1.0.0-beta.2/item-spec/examples/datetimerange.json"
)
self.ITEM_1 = pystac.Item.from_file(self.URI_1)
self.ITEM_1 = Item.from_file(self.URI_1)

self.URI_2 = TestCases.get_path(
"data-files/examples/1.0.0-beta.2/item-spec/examples/sample-full.json"
)
self.ITEM_2 = pystac.Item.from_file(self.URI_2)
self.ITEM_2 = Item.from_file(self.URI_2)

self.EXAMPLE_CM_DICT: Dict[str, Any] = {
"start_datetime": "2020-05-21T16:42:24.896Z",
Expand All @@ -39,11 +39,11 @@ def test_datetimes(self) -> None:
start_datetime_str = self.ITEM_1.properties["start_datetime"]
self.assertIsInstance(start_datetime_str, str)

common_metadata = self.ITEM_1.common_metadata
self.assertIsInstance(common_metadata, pystac.CommonMetadata)
self.assertIsInstance(common_metadata.start_datetime, datetime)
cm = self.ITEM_1.common_metadata
self.assertIsInstance(cm, CommonMetadata)
self.assertIsInstance(cm.start_datetime, datetime)
self.assertDictEqual(before, self.ITEM_1.to_dict())
self.assertIsNone(common_metadata.providers)
self.assertIsNone(cm.providers)

def test_common_metadata_start_datetime(self) -> None:
x = self.ITEM_1.clone()
Expand Down Expand Up @@ -115,9 +115,7 @@ def test_common_metadata_providers(self) -> None:
"url": "https://cool-sat.com/",
}
]
providers_object_list = [
pystac.Provider.from_dict(d) for d in providers_dict_list
]
providers_object_list = [Provider.from_dict(d) for d in providers_dict_list]

example_providers_dict_list: List[Dict[str, Any]] = [
{
Expand All @@ -132,14 +130,14 @@ def test_common_metadata_providers(self) -> None:
},
]
example_providers_object_list = [
pystac.Provider.from_dict(d) for d in example_providers_dict_list
Provider.from_dict(d) for d in example_providers_dict_list
]

for i in range(len(utils.get_opt(x.common_metadata.providers))):
p1 = utils.get_opt(x.common_metadata.providers)[i]
p2 = providers_object_list[i]
self.assertIsInstance(p1, pystac.Provider)
self.assertIsInstance(p2, pystac.Provider)
self.assertIsInstance(p1, Provider)
self.assertIsInstance(p2, Provider)
self.assertDictEqual(p1.to_dict(), p2.to_dict())

pd1 = x.properties["providers"][i]
Expand All @@ -153,8 +151,8 @@ def test_common_metadata_providers(self) -> None:
for i in range(len(x.common_metadata.providers)):
p1 = x.common_metadata.providers[i]
p2 = example_providers_object_list[i]
self.assertIsInstance(p1, pystac.Provider)
self.assertIsInstance(p2, pystac.Provider)
self.assertIsInstance(p1, Provider)
self.assertIsInstance(p2, Provider)
self.assertDictEqual(p1.to_dict(), p2.to_dict())

pd1 = x.properties["providers"][i]
Expand Down Expand Up @@ -231,17 +229,17 @@ def test_common_metadata_basics(self) -> None:
class AssetCommonMetadataTest(unittest.TestCase):
def setUp(self) -> None:
self.maxDiff = None
self.item = pystac.Item.from_file(
self.item = Item.from_file(
TestCases.get_path("data-files/item/sample-item-asset-properties.json")
)

def test_title(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.title
a2_known_value = "Thumbnail"
Expand All @@ -261,9 +259,9 @@ def test_description(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.description
a2_known_value = "Thumbnail of the item"
Expand All @@ -283,9 +281,9 @@ def test_start_datetime(self) -> None:
item = self.item.clone()
item_cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = item_cm.start_datetime
a2_known_value = utils.str_to_datetime("2017-05-01T13:22:30.040Z")
Expand All @@ -307,9 +305,9 @@ def test_end_datetime(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.end_datetime
a2_known_value = utils.str_to_datetime("2017-05-02T13:22:30.040Z")
Expand All @@ -331,9 +329,9 @@ def test_license(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.license
a2_known_value = "CC-BY-4.0"
Expand All @@ -353,16 +351,16 @@ def test_providers(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.providers
a2_known_value = [
pystac.Provider(
Provider(
name="USGS",
url="https://landsat.usgs.gov/",
roles=[pystac.ProviderRole.PRODUCER, pystac.ProviderRole.LICENSOR],
roles=[ProviderRole.PRODUCER, ProviderRole.LICENSOR],
)
]

Expand All @@ -372,10 +370,10 @@ def test_providers(self) -> None:

# Set
set_value = [
pystac.Provider(
Provider(
name="John Snow",
url="https://cholera.com/",
roles=[pystac.ProviderRole.PRODUCER],
roles=[ProviderRole.PRODUCER],
)
]
analytic_cm.providers = set_value
Expand All @@ -389,9 +387,9 @@ def test_platform(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.platform
a2_known_value = "shoes"
Expand All @@ -411,9 +409,9 @@ def test_instruments(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.instruments
a2_known_value = ["caliper"]
Expand All @@ -433,9 +431,9 @@ def test_constellation(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.constellation
a2_known_value = "little dipper"
Expand All @@ -455,9 +453,9 @@ def test_mission(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.mission
a2_known_value = "possible"
Expand All @@ -477,9 +475,9 @@ def test_gsd(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.gsd
a2_known_value = 40
Expand All @@ -499,9 +497,9 @@ def test_created(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.created
a2_known_value = utils.str_to_datetime("2017-05-17T13:22:30.040Z")
Expand All @@ -523,9 +521,9 @@ def test_updated(self) -> None:
item = self.item.clone()
cm = item.common_metadata
analytic = item.assets["analytic"]
analytic_cm = pystac.CommonMetadata(analytic)
analytic_cm = CommonMetadata(analytic)
thumbnail = item.assets["thumbnail"]
thumbnail_cm = pystac.CommonMetadata(thumbnail)
thumbnail_cm = CommonMetadata(thumbnail)

item_value = cm.updated
a2_known_value = utils.str_to_datetime("2017-05-18T13:22:30.040Z")
Expand Down

0 comments on commit d4b18a1

Please sign in to comment.