Skip to content

Commit

Permalink
Merge pull request #187 from LabSid-USP/task/186-improve-handling-of-…
Browse files Browse the repository at this point in the history
…file-paths-for-absolute-paths-internally

Improve handling of file paths for absolute paths internally
  • Loading branch information
soaressgabriel authored Mar 21, 2024
2 parents fddc3af + 82b7ec7 commit aa8109a
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
10 changes: 7 additions & 3 deletions rubem/configuration/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class AppSettings:

__instance = None
__default_appsettings_dir = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)
__default_appsettings_file = os.path.join(__default_appsettings_dir, "appsettings.json")
__default_appsettings_file = os.path.abspath(
os.path.join(__default_appsettings_dir, "appsettings.json")
)

if "PYTHON_ENVIRONMENT" in os.environ and os.environ["PYTHON_ENVIRONMENT"]:
custom_env_settings = f"appsettings.{os.environ['PYTHON_ENVIRONMENT']}.json"
custom_env_settings_path = os.path.join(__default_appsettings_dir, custom_env_settings)
custom_env_settings_path = os.path.abspath(
os.path.join(__default_appsettings_dir, custom_env_settings)
)
if (
os.path.isfile(custom_env_settings_path)
and os.path.getsize(custom_env_settings_path) > 0
Expand Down Expand Up @@ -58,7 +62,7 @@ def load(self, app_settings_file_path: Optional[Union[str, bytes, os.PathLike]]
"""

if app_settings_file_path:
app_settings_file_path_str = str(app_settings_file_path)
app_settings_file_path_str = os.path.abspath(app_settings_file_path)
else:
app_settings_file_path_str = self.__default_appsettings_file

Expand Down
18 changes: 12 additions & 6 deletions rubem/configuration/input_raster_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@ def __init__(
else:
self.logger.warning("Input data directories validation is disabled.")

self.etp = os.path.join(str(self.__etp_dir_path), self.__etp_filename_prefix)
self.precipitation = os.path.join(
str(self.__precipitation_dir_path), self.__precipitation_filename_prefix
self.etp = os.path.abspath(
os.path.join(str(self.__etp_dir_path), self.__etp_filename_prefix)
)
self.precipitation = os.path.abspath(
os.path.join(str(self.__precipitation_dir_path), self.__precipitation_filename_prefix)
)
self.ndvi = os.path.abspath(
os.path.join(str(self.__ndvi_dir_path), self.__ndvi_filename_prefix)
)
self.kp = os.path.abspath(os.path.join(str(self.__kp_dir_path), self.__kp_filename_prefix))
self.landuse = os.path.abspath(
os.path.join(str(self.__landuse_dir_path), self.__landuse_filename_prefix)
)
self.ndvi = os.path.join(str(self.__ndvi_dir_path), self.__ndvi_filename_prefix)
self.kp = os.path.join(str(self.__kp_dir_path), self.__kp_filename_prefix)
self.landuse = os.path.join(str(self.__landuse_dir_path), self.__landuse_filename_prefix)

def __validate_directories(self) -> None:
directories = [
Expand Down
6 changes: 3 additions & 3 deletions rubem/file/_file_convertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def tss2csv(tss_dir_path: str, cols_names: list[str], should_delete_src_tss: boo
header = ["0"]
header.extend(cols_names)

for tss_file in glob.glob(os.path.join(tss_dir_path, "*.tss")):
dst_file_path = os.path.join(
os.path.dirname(tss_file), f"{os.path.splitext(tss_file)[0]}.csv"
for tss_file in glob.glob(os.path.abspath(os.path.join(tss_dir_path, "*.tss"))):
dst_file_path = os.path.abspath(
os.path.join(os.path.dirname(tss_file), f"{os.path.splitext(tss_file)[0]}.csv")
)
with open(file=tss_file, mode="r", encoding="utf8") as f:
lines = f.readlines()
Expand Down
10 changes: 6 additions & 4 deletions rubem/file/_file_generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ def __report(
no_data_value: float = -9999,
):
if timestep:
out_tif = os.path.join(
str(outpath),
f"{name}{str(timestep).zfill(10 - len(name))}.{extension}",
out_tif = os.path.abspath(
os.path.join(
str(outpath),
f"{name}{str(timestep).zfill(10 - len(name))}.{extension}",
)
)
else:
out_tif = os.path.join(str(outpath), f"{name}.{extension}")
out_tif = os.path.abspath(os.path.join(str(outpath), f"{name}.{extension}"))

gdal.UseExceptions()
gdal.AllRegister()
Expand Down

0 comments on commit aa8109a

Please sign in to comment.