Skip to content

Commit

Permalink
refactor(capabilities): add top level feature_enabled dict for evalua…
Browse files Browse the repository at this point in the history
…ting enabled features
  • Loading branch information
sqr00t committed Feb 5, 2024
1 parent 238ba38 commit dd5ed10
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
6 changes: 6 additions & 0 deletions nesta_ds_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from nesta_ds_utils.loading_saving import _gis_enabled, _excel_backend_available

feature_enabled = {
"gis": _gis_enabled,
"excel": _excel_backend_available,
}
13 changes: 6 additions & 7 deletions nesta_ds_utils/loading_saving/S3.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
import warnings
from nesta_ds_utils.loading_saving import file_ops

from nesta_ds_utils.loading_saving.gis_interface import _gis_enabled
from nesta_ds_utils.loading_saving import _excel_backend_available
from nesta_ds_utils import feature_enabled

if _gis_enabled:
if feature_enabled["gis"]:
from nesta_ds_utils.loading_saving.gis_interface import (
_gdf_to_fileobj,
_fileobj_to_gdf,
Expand Down Expand Up @@ -55,7 +54,7 @@ def _df_to_fileobj(df_data: pd.DataFrame, path_to: str, **kwargs) -> io.BytesIO:
elif fnmatch(path_to, "*.parquet"):
df_data.to_parquet(buffer, **kwargs)
elif fnmatch(path_to, "*.xlsx") or fnmatch(path_to, "*.xlsm"):
if _excel_backend_available:
if feature_enabled["excel"]:
df_data.to_excel(buffer, **kwargs)
else:
raise ModuleNotFoundError(
Expand Down Expand Up @@ -221,7 +220,7 @@ def upload_obj(
"""
if isinstance(obj, pd.DataFrame):
if type(obj).__name__ == "GeoDataFrame":
if _gis_enabled:
if feature_enabled["gis"]:
obj = _gdf_to_fileobj(obj, path_to, **kwargs_writing)
else:
raise ModuleNotFoundError(
Expand Down Expand Up @@ -263,7 +262,7 @@ def _fileobj_to_df(fileobj: io.BytesIO, path_from: str, **kwargs) -> pd.DataFram
elif fnmatch(path_from, "*.parquet"):
return pd.read_parquet(fileobj, **kwargs)
elif fnmatch(path_from, "*.xlsx") or fnmatch(path_from, "*.xlsm"):
if _excel_backend_available:
if feature_enabled["excel"]:
return pd.read_excel(fileobj, **kwargs)
else:
raise ModuleNotFoundError(
Expand Down Expand Up @@ -382,7 +381,7 @@ def download_obj(
)
elif download_as == "geodf":
if path_from.endswith(tuple([".geojson"])):
if _gis_enabled:
if feature_enabled["gis"]:
return _fileobj_to_gdf(fileobj, path_from, **kwargs_reading)
else:
raise ModuleNotFoundError(
Expand Down
2 changes: 2 additions & 0 deletions nesta_ds_utils/loading_saving/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from nesta_ds_utils.loading_saving.gis_interface import _gis_enabled

try:
import openpyxl

Expand Down
72 changes: 37 additions & 35 deletions nesta_ds_utils/loading_saving/gis_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,42 @@

_gis_enabled = True

def _gdf_to_fileobj(df_data: GeoDataFrame, path_to: str, **kwargs) -> BytesIO:
"""Convert GeoDataFrame into bytes file object.
Args:
df_data (gpd.DataFrame): Dataframe to convert.
path_to (str): Saving file name.
Returns:
io.BytesIO: Bytes file object.
"""
buffer = BytesIO()
if fnmatch(path_to, "*.geojson"):
df_data.to_file(buffer, driver="GeoJSON", **kwargs)
else:
raise NotImplementedError(
"Uploading geodataframe currently supported only for 'geojson'."
)
buffer.seek(0)
return buffer

def _fileobj_to_gdf(fileobj: BytesIO, path_from: str, **kwargs) -> GeoDataFrame:
"""Convert bytes file object into geodataframe.
Args:
fileobj (io.BytesIO): Bytes file object.
path_from (str): Path of loaded data.
Returns:
gpd.DataFrame: Data as geodataframe.
"""
if fnmatch(path_from, "*.geojson"):
return GeoDataFrame.from_features(
load_json(fileobj.getvalue().decode())["features"]
)

except ImportError:
_gis_enabled = False


def _gdf_to_fileobj(df_data: GeoDataFrame, path_to: str, **kwargs) -> BytesIO:
"""Convert GeoDataFrame into bytes file object.
Args:
df_data (gpd.DataFrame): Dataframe to convert.
path_to (str): Saving file name.
Returns:
io.BytesIO: Bytes file object.
"""
buffer = BytesIO()
if fnmatch(path_to, "*.geojson"):
df_data.to_file(buffer, driver="GeoJSON", **kwargs)
else:
raise NotImplementedError(
"Uploading geodataframe currently supported only for 'geojson'."
)
buffer.seek(0)
return buffer


def _fileobj_to_gdf(fileobj: BytesIO, path_from: str, **kwargs) -> GeoDataFrame:
"""Convert bytes file object into geodataframe.
Args:
fileobj (io.BytesIO): Bytes file object.
path_from (str): Path of loaded data.
Returns:
gpd.DataFrame: Data as geodataframe.
"""
if fnmatch(path_from, "*.geojson"):
return GeoDataFrame.from_features(
load_json(fileobj.getvalue().decode())["features"]
)

0 comments on commit dd5ed10

Please sign in to comment.