Update catalog_loader.py

This commit is contained in:
thekiwismarthome
2026-02-14 06:59:16 +13:00
committed by GitHub
parent 1b05185dfe
commit 9a74fd9027
@@ -1,15 +1,14 @@
"""Product catalog loader for Shopping List Manager.""" """Product catalog loader for Shopping List Manager."""
import json import json
import logging import logging
import os
from typing import List, Dict, Any from typing import List, Dict, Any
import aiofiles
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
def load_product_catalog(component_path: str, country_code: str = "NZ") -> List[Dict[str, Any]]: async def load_product_catalog(component_path: str, country_code: str = "NZ") -> List[Dict[str, Any]]:
"""Load product catalog from JSON file. """Load product catalog from JSON file asynchronously.
Args: Args:
component_path: Path to the component directory component_path: Path to the component directory
@@ -18,26 +17,28 @@ def load_product_catalog(component_path: str, country_code: str = "NZ") -> List[
Returns: Returns:
List of product dictionaries List of product dictionaries
""" """
import os
# Try country-specific catalog first # Try country-specific catalog first
if country_code: if country_code:
country_file = os.path.join( catalog_file = os.path.join(
component_path, "data", f"products_catalog_{country_code.lower()}.json" component_path, "data", f"products_catalog_{country_code.lower()}.json"
) )
if os.path.exists(country_file): if not os.path.exists(catalog_file):
catalog_file = country_file
_LOGGER.debug("Using country-specific product catalog: %s", country_code)
else:
_LOGGER.warning( _LOGGER.warning(
"No country-specific catalog found for %s", "No country-specific catalog found for %s at %s",
country_code country_code,
catalog_file
) )
return [] return []
else: else:
return [] return []
try: try:
with open(catalog_file, "r", encoding="utf-8") as f: # Use aiofiles for async file reading
data = json.load(f) async with aiofiles.open(catalog_file, "r", encoding="utf-8") as f:
content = await f.read()
data = json.loads(content)
_LOGGER.info( _LOGGER.info(
"Loaded product catalog version %s for region %s", "Loaded product catalog version %s for region %s",