Skip to content

Commit

Permalink
grass.temporal.abstract_space_time_dataset: Use Path.read_text() to l…
Browse files Browse the repository at this point in the history
…oad SQL template files (OSGeo#4335)

grass.temporal.abstract_space_time_dataset: Use Path.read_text() to load SQL template files

Fixes ResourceWarnings about unclosed files, fixing ruff SIM115 at the same time.

These 4 places were opening a file and reading it completely in the same line, but never closed explicitly the file, nor used a context manager that would kick in as much as possible on errors inside the calls.
  • Loading branch information
echoix authored Sep 17, 2024
1 parent 5e47e41 commit 3bc6e95
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ ignore = [
"python/grass/script/db.py" = ["SIM115"]
"python/grass/script/raster.py" = ["SIM115"]
"python/grass/script/utils.py" = ["SIM115"]
"python/grass/temporal/*.py" = ["SIM115"]
"python/grass/temporal/aggregation.py" = ["SIM115"]
"python/grass/temporal/register.py" = ["SIM115"]
"python/grass/temporal/stds_export.py" = ["SIM115"]
"python/grass/temporal/stds_import.py" = ["SIM115"]
"python/grass/temporal/univar_statistics.py" = ["SIM115"]
"python/grass/utils/download.py" = ["SIM115"]
"raster/r.*/testsuite/*.py" = ["SIM115"]
"raster/r.topidx/*.py" = ["SIM115"]
Expand Down
34 changes: 13 additions & 21 deletions python/grass/temporal/abstract_space_time_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class that is the base class for all space time datasets.
import uuid
from abc import ABCMeta, abstractmethod
from datetime import datetime
from pathlib import Path

from .abstract_dataset import AbstractDataset, AbstractDatasetComparisonKeyStartTime
from .core import (
Expand Down Expand Up @@ -395,9 +396,7 @@ def insert(self, dbif=None, execute=True):
# %s;"%(stds_register_table + "_index", stds_register_table))

# Read the SQL template
sql = open(
os.path.join(sql_path, "stds_map_register_table_template.sql"), "r"
).read()
sql = Path(sql_path, "stds_map_register_table_template.sql").read_text()

# Create a raster, raster3d or vector tables
sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
Expand Down Expand Up @@ -2818,13 +2817,10 @@ def update_from_registered_maps(self, dbif=None):
)
if old_sqlite_version:
template_suffix = "_old"
sql = open(
os.path.join(
sql_path,
f"update_stds_spatial_temporal_extent_template{template_suffix}.sql",
),
"r",
).read()
sql = Path(
sql_path,
f"update_stds_spatial_temporal_extent_template{template_suffix}.sql",
).read_text()
sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
sql = sql.replace("SPACETIME_ID", self.base.get_id())
Expand All @@ -2834,13 +2830,10 @@ def update_from_registered_maps(self, dbif=None):
sql_script += "\n"

# Update type specific metadata
sql = open(
os.path.join(
sql_path,
f"update_{self.get_type()}_metadata_template{template_suffix}.sql",
),
"r",
).read()
sql = Path(
sql_path,
f"update_{self.get_type()}_metadata_template{template_suffix}.sql",
).read_text()

# Comment out update of semantic labels for DB version < 3
if get_tgis_db_version_from_metadata() < 3:
Expand All @@ -2853,10 +2846,9 @@ def update_from_registered_maps(self, dbif=None):
"-- count(distinct semantic_label)",
)
elif old_sqlite_version and self.get_type() == "strds":
semantic_label_sql = open(
os.path.join(sql_path, "update_strds_metadata_template_v3.sql"),
"r",
).read()
semantic_label_sql = Path(
sql_path, "update_strds_metadata_template_v3.sql"
).read_text()
sql = sql + "\n" + semantic_label_sql

sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
Expand Down

0 comments on commit 3bc6e95

Please sign in to comment.