Skip to content

Commit

Permalink
fix: error when bigquery dependencies not installed (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoizueta authored Mar 22, 2024
1 parent 547d87a commit dd462aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 64 deletions.
49 changes: 6 additions & 43 deletions raster_loader/errors.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,17 @@
def import_error_bigquery(): # pragma: no cover
msg = (
"Google Cloud BigQuery is not installed.\n"
"Please install Google Cloud BigQuery to use this function.\n"
"See https://googleapis.dev/python/bigquery/latest/index.html\n"
"for installation instructions.\n"
"OR, run `pip install google-cloud-bigquery` to install from pypi."
"Google Cloud BigQuery client is not installed.\n"
"Please install Google Cloud BigQuery dependencies to use this function.\n"
"run `pip install -U raster-loader[bigquery]` to install from pypi."
)
raise ImportError(msg)


def import_error_snowflake(): # pragma: no cover
msg = (
"Google Snowflake is not installed.\n"
"Please install Snowflake to use this function.\n"
"See https://docs.snowflake.com/en/developer-guide/python-connector\n"
"for installation instructions.\n"
"OR, run `pip install snowflake-connector-python` to install from pypi."
)
raise ImportError(msg)


def import_error_rasterio(): # pragma: no cover
msg = (
"Rasterio is not installed.\n"
"Please install rasterio to use this function.\n"
"See https://rasterio.readthedocs.io/en/latest/installation.html\n"
"for installation instructions.\n"
"Alternatively, run `pip install rasterio` to install from pypi."
)
raise ImportError(msg)


def import_error_rio_cogeo(): # pragma: no cover
msg = (
"Rasterio is not installed.\n"
"Please install rio_cogeo to use this function.\n"
"See https://cogeotiff.github.io/rio-cogeo/\n"
"for installation instructions.\n"
"Alternatively, run `pip install rio-cogeo` to install from pypi."
)
raise ImportError(msg)


def import_error_quadbin(): # pragma: no cover
msg = (
"Quadbin is not installed.\n"
"Please install quadbin to use this function.\n"
"See https://github.com/CartoDB/quadbin-py\n"
"for installation instructions.\n"
"Alternatively, run `pip install quadbin` to install from pypi."
"Snowflake client is not installed.\n"
"Please install Snowflake dependencies to use this function.\n"
"run `pip install -U raster-loader[snowflake]` to install from pypi."
)
raise ImportError(msg)

Expand Down
27 changes: 19 additions & 8 deletions raster_loader/io/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,28 @@

from raster_loader.io.datawarehouse import DataWarehouseConnection

if _has_bigquery:

class AccessTokenCredentials(Credentials):
def __init__(self, access_token):
super(AccessTokenCredentials, self).__init__()
self._access_token = access_token
class AccessTokenCredentials(Credentials):
def __init__(self, access_token):
super(AccessTokenCredentials, self).__init__()
self._access_token = access_token

def refresh(self, request):
pass
def refresh(self, request):
pass

def apply(self, headers, token=None):
headers["Authorization"] = f"Bearer {self._access_token}"
def apply(self, headers, token=None):
headers["Authorization"] = f"Bearer {self._access_token}"

else:

class Credentials:
def __init__(self):
import_error_bigquery()

class AccessTokenCredentials:
def __init__(self, access_token):
import_error_bigquery()


class BigQueryConnection(DataWarehouseConnection):
Expand Down
14 changes: 1 addition & 13 deletions raster_loader/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,12 @@
from typing import List
from typing import Tuple
from affine import Affine

try:
import rio_cogeo
except ImportError: # pragma: no cover
_has_rio_cogeo = False
else:
_has_rio_cogeo = True

import rio_cogeo
import rasterio
import quadbin

from raster_loader.geo import raster_bounds
from raster_loader.errors import (
import_error_rio_cogeo,
error_not_google_compatible,
)

Expand Down Expand Up @@ -99,10 +91,6 @@ def rasterio_metadata(
bands_info: List[Tuple[int, str]],
band_rename_function: Callable,
):
"""Requires rio_cogeo."""
if not _has_rio_cogeo: # pragma: no cover
import_error_rio_cogeo()

"""Open a raster file with rasterio."""
raster_info = rio_cogeo.cog_info(file_path).dict()

Expand Down

0 comments on commit dd462aa

Please sign in to comment.