Skip to content

Commit 5a42904

Browse files
committed
Handle extra fields in Provider object
1 parent f9573f8 commit 5a42904

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

pystac/collection.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -372,29 +372,44 @@ class Provider:
372372
licensor, producer, processor or host.
373373
url : Optional homepage on which the provider describes the dataset
374374
and publishes contact information.
375-
376-
Attributes:
377-
name : The name of the organization or the individual.
378-
description : Optional multi-line description to add further provider
379-
information such as processing details for processors and producers,
380-
hosting details for hosts or basic contact information.
381-
roles : Optional roles of the provider. Any of
382-
licensor, producer, processor or host.
383-
url : Optional homepage on which the provider describes the dataset
384-
and publishes contact information.
375+
extra_fields : Optional dictionary containing additional top-level fields
376+
defined on the Provider object.
377+
object.
385378
"""
386379

380+
name: str
381+
"""The name of the organization or the individual."""
382+
383+
description: Optional[str]
384+
"""Optional multi-line description to add further provider
385+
information such as processing details for processors and producers,
386+
hosting details for hosts or basic contact information."""
387+
388+
roles: Optional[List[str]]
389+
"""Optional roles of the provider. Any of
390+
licensor, producer, processor or host."""
391+
392+
url: Optional[str]
393+
"""Optional homepage on which the provider describes the dataset
394+
and publishes contact information."""
395+
396+
extra_fields: Dict[str, Any]
397+
"""Dictionary containing additional top-level fields defined on the Provider
398+
object."""
399+
387400
def __init__(
388401
self,
389402
name: str,
390403
description: Optional[str] = None,
391404
roles: Optional[List[str]] = None,
392405
url: Optional[str] = None,
406+
extra_fields: Optional[Dict[str, Any]] = None,
393407
):
394408
self.name = name
395409
self.description = description
396410
self.roles = roles
397411
self.url = url
412+
self.extra_fields = extra_fields or {}
398413

399414
def to_dict(self) -> Dict[str, Any]:
400415
"""Generate a dictionary representing the JSON of this Provider.
@@ -410,20 +425,29 @@ def to_dict(self) -> Dict[str, Any]:
410425
if self.url is not None:
411426
d["url"] = self.url
412427

428+
d.update(self.extra_fields)
429+
413430
return d
414431

415432
@staticmethod
416433
def from_dict(d: Dict[str, Any]) -> "Provider":
417434
"""Constructs an Provider from a dict.
418435
419436
Returns:
420-
TemporalExtent: The Provider deserialized from the JSON dict.
437+
Provider: The Provider deserialized from the JSON dict.
421438
"""
422439
return Provider(
423440
name=d["name"],
424441
description=d.get("description"),
425-
roles=d.get("roles"),
442+
roles=d.get(
443+
"roles",
444+
),
426445
url=d.get("url"),
446+
extra_fields={
447+
k: v
448+
for k, v in d.items()
449+
if k not in {"name", "description", "roles", "url"}
450+
},
427451
)
428452

429453

tests/test_collection.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,43 @@
99
import pystac
1010
from pystac.extensions.eo import EOExtension
1111
from pystac.validation import validate_dict
12-
from pystac import Collection, Item, Extent, SpatialExtent, TemporalExtent, CatalogType
12+
from pystac import (
13+
Collection,
14+
Item,
15+
Extent,
16+
SpatialExtent,
17+
TemporalExtent,
18+
CatalogType,
19+
Provider,
20+
)
1321
from pystac.utils import datetime_to_str, get_required
1422
from tests.utils import TestCases, ARBITRARY_GEOM, ARBITRARY_BBOX
1523

1624
TEST_DATETIME = datetime(2020, 3, 14, 16, 32)
1725

1826

27+
class ProviderTest(unittest.TestCase):
28+
def test_to_from_dict(self) -> None:
29+
provider_dict = {
30+
"name": "Remote Data, Inc",
31+
"description": "Producers of awesome spatiotemporal assets",
32+
"roles": ["producer", "processor"],
33+
"url": "http://remotedata.io",
34+
"extension:field": "some value",
35+
}
36+
expected_extra_fields = {"extension:field": provider_dict["extension:field"]}
37+
38+
provider = Provider.from_dict(provider_dict)
39+
40+
self.assertEqual(provider_dict["name"], provider.name)
41+
self.assertEqual(provider_dict["description"], provider.description)
42+
self.assertEqual(provider_dict["roles"], provider.roles)
43+
self.assertEqual(provider_dict["url"], provider.url)
44+
self.assertDictEqual(expected_extra_fields, provider.extra_fields)
45+
46+
self.assertDictEqual(provider_dict, provider.to_dict())
47+
48+
1949
class CollectionTest(unittest.TestCase):
2050
def test_spatial_extent_from_coordinates(self) -> None:
2151
extent = SpatialExtent.from_coordinates(ARBITRARY_GEOM["coordinates"])

0 commit comments

Comments
 (0)