corrected overpass return and switched to json
Some checks failed
Build and deploy the backend to staging / Build and push image (pull_request) Successful in 1m32s
Run linting on the backend code / Build (pull_request) Successful in 27s
Run testing on the backend code / Build (pull_request) Failing after 7m11s
Build and deploy the backend to staging / Deploy to staging (pull_request) Successful in 24s

This commit is contained in:
2025-01-28 08:04:54 +01:00
parent bab6cfe74e
commit 978cae290b
7 changed files with 196 additions and 193 deletions

View File

@@ -1,5 +1,5 @@
import os
import xml.etree.ElementTree as ET
import json
import hashlib
from ..constants import OSM_CACHE_DIR, OSM_TYPES
@@ -37,9 +37,9 @@ class CachingStrategyBase:
"""Clean up or close any resources used by the caching strategy."""
class XMLCache(CachingStrategyBase):
class JSONCache(CachingStrategyBase):
"""
A caching strategy that stores and retrieves data in XML format.
A caching strategy that stores and retrieves data in JSON format.
"""
def __init__(self, cache_dir=OSM_CACHE_DIR):
# Add the class name as a suffix to the directory
@@ -48,27 +48,28 @@ class XMLCache(CachingStrategyBase):
os.makedirs(self._cache_dir)
def _filename(self, key):
return os.path.join(self._cache_dir, f'{key}.xml')
return os.path.join(self._cache_dir, f'{key}.json')
def get(self, key):
"""Retrieve XML data from the cache and parse it as an ElementTree."""
"""Retrieve JSON data from the cache and parse it as an ElementTree."""
filename = self._filename(key)
if os.path.exists(filename):
try:
# Parse and return the cached XML data
tree = ET.parse(filename)
return tree.getroot() # Return the root element of the parsed XML
except ET.ParseError:
return None
# Open and parse the cached JSON data
with open(filename, 'r', encoding='utf-8') as file:
data = json.load(file)
return data # Return the parsed JSON data
except json.JSONDecodeError as err:
return None # Return None if parsing fails
return None
def set(self, key, value):
"""Save the XML data as an ElementTree to the cache."""
"""Save the JSON data as an ElementTree to the cache."""
filename = self._filename(key)
tree = ET.ElementTree(value) # value is expected to be an ElementTree root element
try:
with open(filename, 'wb') as file:
tree.write(file, encoding='utf-8', xml_declaration=True)
# Write the JSON data to the cache file
with open(filename, 'w', encoding='utf-8') as file:
json.dump(value, file, ensure_ascii=False, indent=4)
except IOError as e:
raise IOError(f"Error writing to cache file: {filename} - {e}") from e
@@ -77,23 +78,22 @@ class XMLCache(CachingStrategyBase):
"""Create an empty placeholder cache entry for a future fill."""
hollow_key = f'hollow_{key}'
filename = self._filename(hollow_key)
# Create the root element <cache>
root = ET.Element("params")
# Add sub-elements with provided values
ET.SubElement(root, "key").text = key
ET.SubElement(root, "cell").text = f"({cell[0]}, {cell[1]})"
ET.SubElement(root, "osm_types").text = ','.join(osm_types)
ET.SubElement(root, "selector").text = selector
ET.SubElement(root, "conditions").text = ','.join(conditions) if conditions else "none"
ET.SubElement(root, "out").text = out
# Create an ElementTree object from the root
tree = ET.ElementTree(root)
# Write the XML to the file
with open(filename, 'wb') as file:
tree.write(file, encoding='utf-8', xml_declaration=True)
# Create the hollow JSON structure
hollow_data = {
"key": key,
"cell": list(cell),
"osm_types": list(osm_types),
"selector": selector,
"conditions": conditions if conditions else "none",
"out": out
}
# Write the hollow data to the cache file
try:
with open(filename, 'w', encoding='utf-8') as file:
json.dump(hollow_data, file, ensure_ascii=False, indent=4)
except IOError as e:
raise IOError(f"Error writing hollow cache to file: {filename} - {e}") from e
def close(self):
"""Cleanup method, if needed."""
@@ -103,13 +103,13 @@ class CachingStrategy:
"""
A class to manage different caching strategies.
"""
__strategy = XMLCache() # Default caching strategy
__strategy = JSONCache() # Default caching strategy
__strategies = {
'XML': XMLCache,
'JSON': JSONCache,
}
@classmethod
def use(cls, strategy_name='XML', **kwargs):
def use(cls, strategy_name='JSON', **kwargs):
if cls.__strategy:
cls.__strategy.close()