From f1b72e04d8075d6a3516d4eeb0a0ee63dcd23f7d Mon Sep 17 00:00:00 2001 From: arfy slowy Date: Sun, 28 Jul 2024 08:11:02 +0700 Subject: [PATCH] chore(improvement): error handling and typehinting (#856) * chore(improvement): error handling and typehinting Signed-off-by: slowy07 * fix(typos): ocurred -> occurred Signed-off-by: slowy07 --------- Signed-off-by: slowy07 Signed-off-by: slowy07 --- leafmap/bokehmap.py | 31 ++++++++++++++++++------------- leafmap/common.py | 10 +++++++++- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/leafmap/bokehmap.py b/leafmap/bokehmap.py index 14c990daa7..9b87409e9e 100644 --- a/leafmap/bokehmap.py +++ b/leafmap/bokehmap.py @@ -61,7 +61,7 @@ def __init__( self.output_notebook = output_notebook self.output_notebook_done = False - def _repr_mimebundle_(self, **kwargs): + def _repr_mimebundle_(self, **kwargs) -> None: """Display the bokeh map. Reference: https://ipython.readthedocs.io/en/stable/config/integrating.html#MyObject._repr_mimebundle_""" if self.output_notebook and (os.environ["OUTPUT_NOTEBOOK"] == "False"): output_notebook() @@ -73,7 +73,7 @@ def add_basemap( basemap: Optional[str] = "OpenStreetMap", retina: Optional[bool] = True, **kwargs, - ): + ) -> None: """Adds a basemap to the map. Args: @@ -113,7 +113,7 @@ def add_basemap( elif basemap is None: raise ValueError("Please specify a valid basemap") - def add_tile(self, tile: str, **kwargs): + def add_tile(self, tile: str, **kwargs) -> None: """Adds a tile to the map. Args: @@ -136,7 +136,7 @@ def add_cog_layer( cog_args: Dict = {}, fit_bounds: bool = True, **kwargs, - ): + ) -> None: """Adds a COG TileLayer to the map. Args: @@ -174,7 +174,7 @@ def add_raster( layer_name="Local COG", open_args={}, **kwargs, - ): + ) -> None: """Add a local raster dataset to the map. If you are using this function in JupyterHub on a remote server (e.g., Binder, Microsoft Planetary Computer) and if the raster does not render properly, try running the following code before calling this function: @@ -236,7 +236,7 @@ def add_stac_layer( fit_bounds: Optional[bool] = True, open_args={}, **kwargs, - ): + ) -> None: """Adds a STAC TileLayer to the map. Args: @@ -266,8 +266,13 @@ def add_stac_layer( self.fit_bounds(stac_bounds(url, collection, item, titiler_endpoint)) def add_gdf( - self, gdf, to_crs="epsg:3857", tooltips=None, fit_bounds=True, **kwargs - ): + self, + gdf, + to_crs: Optional[str] = "epsg:3857", + tooltips: Optional[list] = None, + fit_bounds: bool = True, + **kwargs, + ) -> None: """Adds a GeoDataFrame to the map. Args: @@ -319,7 +324,7 @@ def add_geojson( tooltips: Optional[List] = None, fit_bounds: bool = True, **kwargs, - ): + ) -> None: """Adds a GeoJSON file to the map. Args: @@ -351,7 +356,7 @@ def add_shp( tooltips: Optional[List] = None, fit_bounds: bool = True, **kwargs, - ): + ) -> None: """Adds a shapefile to the map. Args: @@ -404,7 +409,7 @@ def add_vector( tooltips: Optional[List] = None, fit_bounds: bool = True, **kwargs, - ): + ) -> None: """Adds a vector dataset to the map. Args: @@ -433,7 +438,7 @@ def add_vector( def to_html( self, filename: Optional[str] = None, title: Optional[str] = None, **kwargs - ): + ) -> None: """Converts the map to HTML. Args: @@ -463,7 +468,7 @@ def to_streamlit( height: Optional[int] = 600, use_container_width: bool = True, **kwargs, - ): + ) -> None: """Displays the map in a Streamlit app. Args: diff --git a/leafmap/common.py b/leafmap/common.py index ac8e1a46e3..aaca541acf 100644 --- a/leafmap/common.py +++ b/leafmap/common.py @@ -181,7 +181,15 @@ def _clone_repo(out_dir: Optional[str] = ".", unzip: Optional[bool] = True) -> N """ url = "https://github.com/opengeos/leafmap/archive/master.zip" filename = "leafmap-master.zip" - download_from_url(url, out_file_name=filename, out_dir=out_dir, unzip=unzip) + + try: + if not isinstance(out_dir, str): + raise ValueError( + f"The out_dir parametermust be string. Provided: {type(out_dir).__name__}" + ) + download_from_url(url, out_file_name=filename, out_dir=out_dir, unzip=unzip) + except Exception as error: + print(f"An error occurred while clone the repository: {str(error)}") def __install_from_github(url: str) -> None: