Skip to content
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 11 additions & 2 deletions planetary_computer/sas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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):
Expand Down
65 changes: 39 additions & 26 deletions planetary_computer/settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,47 @@
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_"

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:
Expand All @@ -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
2 changes: 1 addition & 1 deletion planetary_computer/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Library version"""

__version__ = "0.5.1"
__version__ = "1.0.0"
10 changes: 8 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

Expand All @@ -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__"}