From f394e70575a3239de3451bffc4a306ac1316bd11 Mon Sep 17 00:00:00 2001 From: Ryan Abbott Date: Wed, 4 Feb 2026 16:19:33 -0500 Subject: [PATCH] Adds support for setting cache_dir via an environment variable Fixes #15 --- .../geocode_index/hierachical_place_cache.py | 13 ++++++++++--- .../geocode_index/index.py | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/natural_language_geocoding/geocode_index/hierachical_place_cache.py b/src/natural_language_geocoding/geocode_index/hierachical_place_cache.py index 7919e41..ee96a32 100644 --- a/src/natural_language_geocoding/geocode_index/hierachical_place_cache.py +++ b/src/natural_language_geocoding/geocode_index/hierachical_place_cache.py @@ -7,7 +7,11 @@ from e84_geoai_common.util import timed_function from natural_language_geocoding.geocode_index.geoplace import GeoPlaceType, Hierarchy -from natural_language_geocoding.geocode_index.index import GEOPLACE_INDEX_NAME, GeoPlaceIndexField +from natural_language_geocoding.geocode_index.index import ( + GEOCODE_INDEX_CACHE_DIR, + GEOPLACE_INDEX_NAME, + GeoPlaceIndexField, +) from natural_language_geocoding.geocode_index.opensearch_utils import ( QueryDSL, create_opensearch_client, @@ -236,9 +240,12 @@ class PlaceCache: _cache_file: Path - def __init__(self, *, cache_dir: str | Path = "./temp", force_reload: bool = False) -> None: + def __init__( + self, *, cache_dir: str | Path | None = None, force_reload: bool = False + ) -> None: # Increment the name of the file when something changes about the format of the storage - self._cache_file = Path(cache_dir) / "hierarchical_place_cache_v2.json" + resolved_cache_dir = cache_dir or GEOCODE_INDEX_CACHE_DIR + self._cache_file = Path(resolved_cache_dir) / "hierarchical_place_cache_v2.json" if force_reload or not self._cache_file.exists(): self._dicts = _populate() self._cache_file.parent.mkdir(exist_ok=True) diff --git a/src/natural_language_geocoding/geocode_index/index.py b/src/natural_language_geocoding/geocode_index/index.py index 21a4061..f664a16 100644 --- a/src/natural_language_geocoding/geocode_index/index.py +++ b/src/natural_language_geocoding/geocode_index/index.py @@ -2,6 +2,7 @@ import json import logging +import os import subprocess from abc import ABC, abstractmethod from collections.abc import Iterable @@ -187,6 +188,7 @@ class GeoPlaceDoc(TypedDict): GEOPLACE_INDEX_NAME = "geoplaces" +GEOCODE_INDEX_CACHE_DIR = os.getenv("GEOCODE_INDEX_CACHE_DIR", "./temp") # The set of geo place types for which we'll index geometry spatially. # We don't do this for all types due to some issues getting everything to index. In the future, we