Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item Extension getattr Overload #159

Merged
merged 3 commits into from
Aug 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pystac/stac_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ def __getattr__(self, extension_id):
item.ext["label"].label_properties
item.ext.label.label_properties
"""
if extension_id.startswith('__') and hasattr(ExtensionIndex, extension_id):
return self.__getattribute__(extension_id)
return self[extension_id]

def enable(self, extension_id):
Expand Down
12 changes: 11 additions & 1 deletion tests/extensions/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import pystac
from pystac import (Catalog, Collection, Item)
from pystac.extensions.base import (CatalogExtension, CollectionExtension, ItemExtension,
ExtensionDefinition, ExtendedObject)
ExtensionDefinition, ExtendedObject, ExtensionError)
from pystac.stac_object import ExtensionIndex

from tests.utils import TestCases

Expand Down Expand Up @@ -88,3 +89,12 @@ def test_can_add_custom_extension(self):

self.assertFalse(pystac.STAC_EXTENSIONS.is_registered_extension("test"))
self.assertEqual(pystac.STAC_EXTENSIONS.get_registered_extensions(), prev_extensions)

def test_getattribute_overload(self):
catalog = Catalog(id='test', description='test')
self.assertEqual(ExtensionIndex.__name__, 'ExtensionIndex')
self.assertRaises(ExtensionError, catalog.ext.__getattr__, 'foo')
self.assertRaises(ExtensionError, catalog.ext.__getattr__, 'eo')
catalog.ext.enable('single-file-stac')
self.assertTrue(catalog.ext.__getattr__('single-file-stac'),
pystac.extensions.single_file_stac.SingleFileSTACCatalogExt)