From 533bb149a00d49b8dfdeb35bd02d114815beb55f Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Tue, 16 Feb 2021 19:26:03 -0700 Subject: [PATCH 1/5] Subclass from Enum --- pystac/catalog.py | 15 +++++++++------ pystac/link.py | 6 +++++- pystac/media_type.py | 8 +++++++- pystac/stac_object.py | 6 +++++- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/pystac/catalog.py b/pystac/catalog.py index 3215b0835..4cf86abd6 100644 --- a/pystac/catalog.py +++ b/pystac/catalog.py @@ -1,5 +1,6 @@ import os from copy import deepcopy +from enum import Enum import pystac from pystac import STACError @@ -9,8 +10,10 @@ from pystac.cache import ResolvedObjectCache from pystac.utils import (is_absolute_href, make_absolute_href) +class CatalogType(str, Enum): + def __str__(self): + return str(self.value) -class CatalogType: SELF_CONTAINED = 'SELF_CONTAINED' """A 'self-contained catalog' is one that is designed for portability. Users may want to download a catalog from online and be able to use it on their @@ -38,8 +41,8 @@ class CatalogType: `The best practices documentation on published catalogs `_ """ # noqa E501 - @staticmethod - def determine_type(stac_json): + @classmethod + def determine_type(cls, stac_json): """Determines the catalog type based on a STAC JSON dict. Only applies to Catalogs or Collections @@ -61,12 +64,12 @@ def determine_type(stac_json): if self_link: if relative: - return CatalogType.RELATIVE_PUBLISHED + return cls.RELATIVE_PUBLISHED else: - return CatalogType.ABSOLUTE_PUBLISHED + return cls.ABSOLUTE_PUBLISHED else: if relative: - return CatalogType.SELF_CONTAINED + return cls.SELF_CONTAINED else: return None diff --git a/pystac/link.py b/pystac/link.py index afe06934a..290420323 100644 --- a/pystac/link.py +++ b/pystac/link.py @@ -1,12 +1,16 @@ from copy import (copy, deepcopy) +from enum import Enum from pystac import STACError from pystac.stac_io import STAC_IO from pystac.utils import (make_absolute_href, make_relative_href, is_absolute_href) -class LinkType: +class LinkType(str, Enum): """Enumerates link types; used to determine if a link is absolute or relative.""" + def __str__(self): + return str(self.value) + ABSOLUTE = 'ABSOLUTE' RELATIVE = 'RELATIVE' diff --git a/pystac/media_type.py b/pystac/media_type.py index 89f0790f3..5bc4c2798 100644 --- a/pystac/media_type.py +++ b/pystac/media_type.py @@ -1,6 +1,12 @@ -class MediaType: +from enum import Enum + + +class MediaType(str, Enum): """A list of common media types that can be used in STAC Asset and Link metadata. """ + def __str__(self): + return str(self.value) + COG = 'image/tiff; application=geotiff; profile=cloud-optimized' GEOJSON = 'application/geo+json' GEOPACKAGE = 'application/geopackage+sqlite3' diff --git a/pystac/stac_object.py b/pystac/stac_object.py index bd24a72de..ed3557d74 100644 --- a/pystac/stac_object.py +++ b/pystac/stac_object.py @@ -1,4 +1,5 @@ from abc import (ABC, abstractmethod) +from enum import Enum import pystac from pystac import STACError @@ -8,7 +9,10 @@ from pystac.extensions import ExtensionError -class STACObjectType: +class STACObjectType(str, Enum): + def __str__(self): + return str(self.value) + CATALOG = 'CATALOG' COLLECTION = 'COLLECTION' ITEM = 'ITEM' From ef83a0b76f0a67c826e232e68ea9e1f45cf5f415 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Tue, 16 Feb 2021 19:30:02 -0700 Subject: [PATCH 2/5] Lint --- pystac/catalog.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pystac/catalog.py b/pystac/catalog.py index 4cf86abd6..9c2f164b9 100644 --- a/pystac/catalog.py +++ b/pystac/catalog.py @@ -10,6 +10,7 @@ from pystac.cache import ResolvedObjectCache from pystac.utils import (is_absolute_href, make_absolute_href) + class CatalogType(str, Enum): def __str__(self): return str(self.value) From fa8e9c2a378644cebb6205102f801624f764f042 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Tue, 16 Feb 2021 21:22:54 -0700 Subject: [PATCH 3/5] Add more enums --- pystac/extensions/__init__.py | 6 +++++- pystac/extensions/label.py | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pystac/extensions/__init__.py b/pystac/extensions/__init__.py index d3464ac30..0b18e899d 100644 --- a/pystac/extensions/__init__.py +++ b/pystac/extensions/__init__.py @@ -1,4 +1,5 @@ # flake8: noqa +from enum import Enum class ExtensionError(Exception): @@ -7,8 +8,11 @@ class ExtensionError(Exception): pass -class Extensions: +class Extensions(str, Enum): """Enumerates the IDs of common extensions.""" + def __str__(self): + return str(self.value) + CHECKSUM = 'checksum' COLLECTION_ASSETS = 'collection-assets' DATACUBE = 'datacube' diff --git a/pystac/extensions/label.py b/pystac/extensions/label.py index 7190567f5..5f34d9d90 100644 --- a/pystac/extensions/label.py +++ b/pystac/extensions/label.py @@ -1,5 +1,7 @@ """STAC Model classes for Label extension. """ +from enum import Enum + from pystac import STACError from pystac.extensions import Extensions from pystac.extensions.base import (ItemExtension, ExtensionDefinition, ExtendedObject) @@ -7,7 +9,7 @@ from pystac.link import Link -class LabelType: +class LabelType(str, Enum): """Enumerates valid label types (RASTER or VECTOR).""" VECTOR = 'vector' RASTER = 'raster' From 04e0d26bb91203bfd90fc9c4b44c42014476a119 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Fri, 19 Feb 2021 11:45:46 -0700 Subject: [PATCH 4/5] Add changelog entry --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 966f4651e..186d1ee2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Fix handling of optional properties when using apply on view extension ([#259](https://github.com/stac-utils/pystac/pull/259)) +### Changed + +- 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)) + ## [v0.5.4] ### Added From 9f776b38c424093997e3cd2ed1afd4b1e9a0e993 Mon Sep 17 00:00:00 2001 From: Kyle Barron Date: Fri, 19 Feb 2021 11:57:52 -0700 Subject: [PATCH 5/5] Add missing str override --- pystac/extensions/label.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pystac/extensions/label.py b/pystac/extensions/label.py index 5f34d9d90..d4e5e69b3 100644 --- a/pystac/extensions/label.py +++ b/pystac/extensions/label.py @@ -11,6 +11,9 @@ class LabelType(str, Enum): """Enumerates valid label types (RASTER or VECTOR).""" + def __str__(self): + return str(self.value) + VECTOR = 'vector' RASTER = 'raster'