diff --git a/CHANGELOG.md b/CHANGELOG.md index 74be373..66e0cf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +# 1.0.0 + +## Bug fixes + +* Compatibility with `pydantic>=2.0` (https://github.com/microsoft/planetary-computer-sdk-for-python/pull/59) + +## API Breaking Changes + +* `planetary_computer.settings.Settings()` is no longer a pydantic Model. To support both pydantic 1.x and 2.x, + the implementation of `Settings` changed. There aren't any user-facing changes in the primary API exposed by + `Settings`, around creating the settings object and getting / setting values. But it no longer subclasses + `pydantic.BaseModel` (https://github.com/microsoft/planetary-computer-sdk-for-python/pull/59). + # 0.5.1 ## Bug fixes diff --git a/planetary_computer/sas.py b/planetary_computer/sas.py index 71d9fe4..0b6f311 100644 --- a/planetary_computer/sas.py +++ b/planetary_computer/sas.py @@ -9,6 +9,8 @@ from urllib.parse import urlparse, parse_qs import requests import requests.adapters +import packaging.version +import pydantic from pydantic import BaseModel, Field from pystac import Asset, Item, ItemCollection, STACObjectType, Collection from pystac.utils import datetime_to_str @@ -26,6 +28,10 @@ asset_xpr, ) +_PYDANTIC_2_0 = packaging.version.parse( + pydantic.__version__ +) >= packaging.version.parse("2.0.0") + BLOB_STORAGE_DOMAIN = ".blob.core.windows.net" AssetLike = TypeVar("AssetLike", Asset, Dict[str, Any]) @@ -38,8 +44,11 @@ class SASBase(BaseModel): """RFC339 datetime format of the time this token will expire""" class Config: - json_encoders = {datetime: datetime_to_str} - allow_population_by_field_name = True + if _PYDANTIC_2_0: + populate_by_name = True + else: + allow_population_by_field_name = True + json_encoders = {datetime: datetime_to_str} class SignedLink(SASBase): diff --git a/planetary_computer/settings.py b/planetary_computer/settings.py index 32a963a..96b324c 100644 --- a/planetary_computer/settings.py +++ b/planetary_computer/settings.py @@ -1,6 +1,9 @@ +import os from functools import lru_cache from typing import Optional -import pydantic +import dataclasses +import dotenv + SETTINGS_ENV_FILE = "~/.planetarycomputer/settings.env" SETTINGS_ENV_PREFIX = "PC_SDK_" @@ -8,7 +11,37 @@ DEFAULT_SAS_TOKEN_ENDPOINT = "https://planetarycomputer.microsoft.com/api/sas/v1/token" -class Settings(pydantic.BaseSettings): +def set_subscription_key(key: str) -> None: + """Sets the Planetary Computer API subscription key to use + within the process that loaded this module. Ths does not write + to the settings file. + + Args: + key: The Planetary Computer API subscription key to use + for methods inside this library that can utilize the key, + such as SAS token generation. + """ + Settings.get().subscription_key = key + + +def _from_env(key: str) -> Optional[str]: + value = os.environ.get(key) + if value is None: + dotenv.load_dotenv(os.path.expanduser(SETTINGS_ENV_FILE)) + value = os.environ.get(key) + return value + + +def _subscription_key_default() -> Optional[str]: + return _from_env("PC_SDK_SUBSCRIPTION_KEY") + + +def _sas_url_default() -> str: + return _from_env("PC_SDK_SAS_URL") or DEFAULT_SAS_TOKEN_ENDPOINT + + +@dataclasses.dataclass +class Settings: """PC SDK configuration settings Settings defined here are attempted to be read in two ways, in this order: @@ -21,32 +54,12 @@ class Settings(pydantic.BaseSettings): All settings are prefixed with `PC_SDK_` """ - # PC_SDK_SUBSCRIPTION_KEY: subscription key to send along with token - # requests. If present, allows less restricted rate limiting. - subscription_key: Optional[str] = None - - # PC_SDK_SAS_URL: The planetary computer SAS endpoint URL. - # This will default to the main planetary computer endpoint. - sas_url: str = DEFAULT_SAS_TOKEN_ENDPOINT - - class Config: - env_file = SETTINGS_ENV_FILE - env_prefix = SETTINGS_ENV_PREFIX + subscription_key: Optional[str] = dataclasses.field( + default_factory=_subscription_key_default + ) + sas_url: Optional[str] = dataclasses.field(default_factory=_sas_url_default) @staticmethod @lru_cache(maxsize=1) def get() -> "Settings": return Settings() - - -def set_subscription_key(key: str) -> None: - """Sets the Planetary Computer API subscription key to use - within the process that loaded this module. Ths does not write - to the settings file. - - Args: - key: The Planetary Computer API subscription key to use - for methods inside this library that can utilize the key, - such as SAS token generation. - """ - Settings.get().subscription_key = key diff --git a/planetary_computer/version.py b/planetary_computer/version.py index 276d011..4298325 100644 --- a/planetary_computer/version.py +++ b/planetary_computer/version.py @@ -1,3 +1,3 @@ """Library version""" -__version__ = "0.5.1" +__version__ = "1.0.0" diff --git a/pyproject.toml b/pyproject.toml index 672d9d2..a2bbc21 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,18 +4,21 @@ build-backend = "setuptools.build_meta" [project] name = "planetary-computer" -version = "0.5.1" authors = [{name = "microsoft", email = "planetarycomputer@microsoft.com"}] description = "Planetary Computer SDK for Python" requires-python = ">=3.7" dependencies = [ "click>=7.1", - "pydantic[dotenv]>=1.7.3", + "pydantic>=1.7.3", "pystac>=1.0.0", "pystac-client>=0.2.0", "pytz>=2020.5", "requests>=2.25.1", + "packaging", + "python-dotenv", + ] +dynamic = ["version"] readme = "README.md" license = {file = "LICENSE"} @@ -37,3 +40,6 @@ planetarycomputer = "planetary_computer.scripts.cli:app" [tool.setuptools] include-package-data = false + +[tool.setuptools.dynamic] +version = {attr = "planetary_computer.version.__version__"} \ No newline at end of file