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

POSTPONED: Dependency handling to support QGIS plugin #315

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions sentinelhub/decoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@
from xml.etree import ElementTree

import numpy as np
import tifffile as tiff
from PIL import Image
from requests import Response

from .constants import MimeType
from .exceptions import ImageDecodingError
from .exceptions import ImageDecodingError, show_import_warning

# The following is only for the purpose of using the package within SentinelHub QGIS plugin
try: # pylint: disable=duplicate-code
import tifffile as tiff
except ImportError:
show_import_warning("tifffile")
try:
from PIL import Image
except ImportError:
show_import_warning("Pillow")


def decode_data(response_content: bytes, data_type: MimeType) -> Any:
Expand Down
10 changes: 8 additions & 2 deletions sentinelhub/download/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,26 @@
import requests
from oauthlib.oauth2 import BackendApplicationClient
from requests import Response
from requests.exceptions import JSONDecodeError
from requests_oauthlib import OAuth2Session

from ..config import SHConfig
from ..download.handlers import fail_user_errors, retry_temporary_errors
from ..download.models import DownloadRequest
from ..exceptions import SHUserWarning
from ..exceptions import SHUserWarning, show_import_warning
from ..type_utils import JsonDict

if sys.version_info < (3, 8):
from shared_memory import SharedMemory
else:
from multiprocessing.shared_memory import SharedMemory

# The following is only for the purpose of using the package within SentinelHub QGIS plugin
try:
from requests.exceptions import JSONDecodeError # pylint: disable=ungrouped-imports
except ImportError:
show_import_warning("requests>=2.27.0")
from json import JSONDecodeError # type: ignore[misc]


LOGGER = logging.getLogger(__name__)

Expand Down
12 changes: 12 additions & 0 deletions sentinelhub/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ class SHUserWarning(UserWarning):
"""A custom user warning for sentinelhub-py package"""


class SHImportWarning(SHUserWarning):
"""A custom warning shown if a dependency package failed to be imported. This can happen in case the package was
installed without some dependencies."""


class SHRuntimeWarning(RuntimeWarning):
"""A custom runtime warning for sentinelhub-py package"""

Expand All @@ -55,7 +60,14 @@ class SHRateLimitWarning(SHRuntimeWarning):
"""A custom runtime warning in case user hit the rate limit for downloads"""


def show_import_warning(package_name: str) -> None:
"""A general way of showing import warnings in the package."""
message = f"Failed to import {package_name} package. Some sentinelhub-py functionalities might not work correctly!"
warnings.warn(message, category=SHImportWarning)


warnings.simplefilter("default", SHDeprecationWarning)
warnings.simplefilter("default", SHUserWarning)
warnings.simplefilter("always", SHImportWarning)
warnings.simplefilter("always", SHRuntimeWarning)
warnings.simplefilter("always", SHRateLimitWarning)
15 changes: 12 additions & 3 deletions sentinelhub/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@
from xml.etree import ElementTree

import numpy as np
import tifffile as tiff
from PIL import Image

from .constants import MimeType
from .decoding import decode_image_with_pillow, decode_jp2_image, decode_tar, get_data_format
from .exceptions import SHUserWarning
from .exceptions import SHUserWarning, show_import_warning
from .os_utils import create_parent_folder
from .type_utils import Json

# The following is only for the purpose of using the package within SentinelHub QGIS plugin
try:
import tifffile as tiff
except ImportError:
show_import_warning("tifffile")
try:
from PIL import Image
except ImportError:
show_import_warning("Pillow")


LOGGER = logging.getLogger(__name__)

CSV_DELIMITER = ";"
Expand Down