Skip to content

Commit

Permalink
fix: small fixes for deprecation/linting warnings in test sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
AngRodrigues committed Dec 5, 2024
1 parent f2576ff commit 4ce6d7d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 42 deletions.
39 changes: 10 additions & 29 deletions map2loop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
) # used instead of importlib.version because adheres to PEP 440 using pkg_version.parse
import pathlib
import re
import pkg_resources # import get_distribution, DistributionNotFound # Use pkg_resources for version checking
from importlib.metadata import version as get_installed_version, PackageNotFoundError


class DependencyChecker:
Expand All @@ -29,13 +29,11 @@ class DependencyChecker:
'''

def __init__(self, package_name, dependency_file="dependencies.txt"):

self.package_name = package_name
self.dependency_file = pathlib.Path(__file__).parent.parent / dependency_file
self.required_version = self.get_required_version()
self.installed_version = self.get_installed_version()

## 1) get required version of each dependency from dependencies.txt
def get_required_version(self):
'''
Get the required package version for each package from dependencies.txt;
Expand All @@ -45,25 +43,21 @@ def get_required_version(self):
'''
try:
with self.dependency_file.open("r") as file:
# search for the dependency line in dependencies.txt
for line in file:
if line.startswith(f"{self.package_name}=="):
match = re.match(rf"^{self.package_name}==([\d\.]+)", line.strip())
if match:
return match.group(1) # get the version if found
return match.group(1)
elif line.strip() == self.package_name:
# dependency listed without a version
return None
# if dependency line not found in dependencies.txt (shouldn't happen, but just in case)
print(f"{self.package_name} version not found in {self.dependency_file}.")
except FileNotFoundError: # in case dependencies.txt is not found (shouldn't happen)
except FileNotFoundError:
warnings.warn(
f"{self.dependency_file} not found. Unable to check {self.package_name} version compatibility.",
UserWarning,
)
return None

## 2) Check the installed version of the dependency and compare with required version
def get_installed_version(self):
'''
Get the installed version of the package.
Expand All @@ -72,30 +66,23 @@ def get_installed_version(self):
str: The installed version of the package.
'''
try:
# Get installed version of the package using pkg_resources
return pkg_resources.get_distribution(self.package_name).version
except pkg_resources.DistributionNotFound:
# Raise ImportError if package is not installed
# Use importlib.metadata to get the installed version of the package
return get_installed_version(self.package_name)
except PackageNotFoundError:
raise ImportError(
f"{self.package_name} is not installed. Please install {self.package_name}."
)

## 3) Compare the installed version of the dependency with the required version, if applicable
def check_version(self):
'''
Checks if the installed version of the package matches the required version uin the dependencies.txt file.
Checks if the installed version of the package matches the required version in the dependencies.txt file.
'''
if self.required_version is None:
# No required version specified, only check if installed
if self.installed_version is None:
raise ImportError(
f"{self.package_name} is not installed. Please install {self.package_name} before using map2loop."
)
# else:
# print(f"{self.package_name} is installed.")
else:
# Compare versions if required version is specified
if self.installed_version is None or pkg_version.parse(
self.installed_version
) != pkg_version.parse(self.required_version):
Expand All @@ -104,33 +91,27 @@ def check_version(self):
f"Please install the correct version of {self.package_name}."
)

# else:
# print(f"{self.package_name} version is compatible.")


# check all dependencies & versions in dependencies.txt
def check_all_dependencies(dependency_file="dependencies.txt"):
dependencies_path = pathlib.Path(__file__).parent.parent / dependency_file
try:
with dependencies_path.open("r") as file:
for line in file:
line = line.strip()
# skip the gdal line
if line.startswith("gdal"):
continue
if line:
# separate package name and version (if available)
if "==" in line:
package_name, _ = line.split("==")
else:
package_name = line

# check version for each package
checker = DependencyChecker(package_name, dependency_file=dependency_file)
checker.check_version()
except FileNotFoundError:
ImportError(
f"{dependency_file} not found. No dependencies checked for map2loop", UserWarning
warnings.warn(
f"{dependency_file} not found. No dependencies checked for map2loop.",
UserWarning
)


Expand Down
16 changes: 5 additions & 11 deletions map2loop/_datasets/geodata_files/load_map2loop_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import geopandas
import pkg_resources
from importlib.resources import files
from osgeo import gdal

gdal.UseExceptions()

def load_hamersley_geology():
"""
Expand All @@ -14,9 +14,7 @@ def load_hamersley_geology():
Returns:
geopandas.GeoDataFrame: The geology data
"""
stream = pkg_resources.resource_filename(
"map2loop", "/_datasets/geodata_files/hamersley/geology.geojson"
)
stream = files("map2loop._datasets.geodata_files.hamersley").joinpath("geology.geojson")
return geopandas.read_file(stream)


Expand All @@ -32,9 +30,7 @@ def load_hamersley_structure():
geopandas.GeoDataFrame: The structure data
"""

path = pkg_resources.resource_filename(
"map2loop", "/_datasets/geodata_files/hamersley/structure.geojson"
)
path = files("map2loop._datasets.geodata_files.hamersley").joinpath("structure.geojson")
return geopandas.read_file(path)


Expand All @@ -45,7 +41,5 @@ def load_hamersley_dtm():
Returns:
gdal.Dataset: The DTM data
"""
path = pkg_resources.resource_filename(
"map2loop", "/_datasets/geodata_files/hamersley/dtm_rp.tif"
)
path = files("map2loop._datasets.geodata_files.hamersley").joinpath("dtm_rp.tif")
return gdal.Open(path)
1 change: 1 addition & 0 deletions map2loop/mapdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pathlib
import shapely
from osgeo import gdal, osr
gdal.UseExceptions()
from owslib.wcs import WebCoverageService
import urllib
from gzip import GzipFile
Expand Down
3 changes: 2 additions & 1 deletion map2loop/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@

# external imports
import LoopProjectFile as LPF
from typing import Union, List
from osgeo import gdal
gdal.UseExceptions()
import geopandas
import beartype
from beartype.typing import Union, List
import pathlib
import numpy
import pandas
Expand Down
2 changes: 1 addition & 1 deletion map2loop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def hex_to_rgb(hex_color: str) -> tuple:

@beartype.beartype
def calculate_minimum_fault_length(
bbox: Dict[str, Union[int, float]], area_percentage: float
bbox: dict[str, int | float], area_percentage: float
) -> float:

"""
Expand Down

0 comments on commit 4ce6d7d

Please sign in to comment.