Skip to content

Commit ad06c61

Browse files
authored
Merge pull request #261 from kylebarron/kyle/use-enum
Subclass relevant classes from Enum
2 parents cbf22cf + 9f776b3 commit ad06c61

File tree

7 files changed

+42
-11
lines changed

7 files changed

+42
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Fix handling of optional properties when using apply on view extension ([#259](https://github.com/stac-utils/pystac/pull/259))
88

9+
### Changed
10+
11+
- Subclass relevant classes from `enum.Enum`. This allows iterating over the class' contents. The `__str__` method is overwritten so this should not break backwards compatibility. ([#261](https://github.com/stac-utils/pystac/pull/261))
12+
913
## [v0.5.4]
1014

1115
### Added

pystac/catalog.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from copy import deepcopy
3+
from enum import Enum
34

45
import pystac
56
from pystac import STACError
@@ -10,7 +11,10 @@
1011
from pystac.utils import (is_absolute_href, make_absolute_href)
1112

1213

13-
class CatalogType:
14+
class CatalogType(str, Enum):
15+
def __str__(self):
16+
return str(self.value)
17+
1418
SELF_CONTAINED = 'SELF_CONTAINED'
1519
"""A 'self-contained catalog' is one that is designed for portability.
1620
Users may want to download a catalog from online and be able to use it on their
@@ -38,8 +42,8 @@ class CatalogType:
3842
`The best practices documentation on published catalogs <https://github.com/radiantearth/stac-spec/blob/v0.8.1/best-practices.md#published-catalogs>`_
3943
""" # noqa E501
4044

41-
@staticmethod
42-
def determine_type(stac_json):
45+
@classmethod
46+
def determine_type(cls, stac_json):
4347
"""Determines the catalog type based on a STAC JSON dict.
4448
4549
Only applies to Catalogs or Collections
@@ -61,12 +65,12 @@ def determine_type(stac_json):
6165

6266
if self_link:
6367
if relative:
64-
return CatalogType.RELATIVE_PUBLISHED
68+
return cls.RELATIVE_PUBLISHED
6569
else:
66-
return CatalogType.ABSOLUTE_PUBLISHED
70+
return cls.ABSOLUTE_PUBLISHED
6771
else:
6872
if relative:
69-
return CatalogType.SELF_CONTAINED
73+
return cls.SELF_CONTAINED
7074
else:
7175
return None
7276

pystac/extensions/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# flake8: noqa
2+
from enum import Enum
23

34

45
class ExtensionError(Exception):
@@ -7,8 +8,11 @@ class ExtensionError(Exception):
78
pass
89

910

10-
class Extensions:
11+
class Extensions(str, Enum):
1112
"""Enumerates the IDs of common extensions."""
13+
def __str__(self):
14+
return str(self.value)
15+
1216
CHECKSUM = 'checksum'
1317
COLLECTION_ASSETS = 'collection-assets'
1418
DATACUBE = 'datacube'

pystac/extensions/label.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
"""STAC Model classes for Label extension.
22
"""
3+
from enum import Enum
4+
35
from pystac import STACError
46
from pystac.extensions import Extensions
57
from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject)
68
from pystac.item import (Item, Asset)
79
from pystac.link import Link
810

911

10-
class LabelType:
12+
class LabelType(str, Enum):
1113
"""Enumerates valid label types (RASTER or VECTOR)."""
14+
def __str__(self):
15+
return str(self.value)
16+
1217
VECTOR = 'vector'
1318
RASTER = 'raster'
1419

pystac/link.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from copy import (copy, deepcopy)
2+
from enum import Enum
23

34
from pystac import STACError
45
from pystac.stac_io import STAC_IO
56
from pystac.utils import (make_absolute_href, make_relative_href, is_absolute_href)
67

78

8-
class LinkType:
9+
class LinkType(str, Enum):
910
"""Enumerates link types; used to determine if a link is absolute or relative."""
11+
def __str__(self):
12+
return str(self.value)
13+
1014
ABSOLUTE = 'ABSOLUTE'
1115
RELATIVE = 'RELATIVE'
1216

pystac/media_type.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
class MediaType:
1+
from enum import Enum
2+
3+
4+
class MediaType(str, Enum):
25
"""A list of common media types that can be used in STAC Asset and Link metadata.
36
"""
7+
def __str__(self):
8+
return str(self.value)
9+
410
COG = 'image/tiff; application=geotiff; profile=cloud-optimized'
511
GEOJSON = 'application/geo+json'
612
GEOPACKAGE = 'application/geopackage+sqlite3'

pystac/stac_object.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from abc import (ABC, abstractmethod)
2+
from enum import Enum
23

34
import pystac
45
from pystac import STACError
@@ -8,7 +9,10 @@
89
from pystac.extensions import ExtensionError
910

1011

11-
class STACObjectType:
12+
class STACObjectType(str, Enum):
13+
def __str__(self):
14+
return str(self.value)
15+
1216
CATALOG = 'CATALOG'
1317
COLLECTION = 'COLLECTION'
1418
ITEM = 'ITEM'

0 commit comments

Comments
 (0)