Skip to content

Instantly share code, notes, and snippets.

@mojodna
Last active December 16, 2015 12:59
Show Gist options
  • Save mojodna/5438489 to your computer and use it in GitHub Desktop.
Save mojodna/5438489 to your computer and use it in GitHub Desktop.
Additional TileStache caches
{
"cache":
{
"class": "SparseCache",
"kwargs": {
"path": "/tmp/stache",
"umask": "0000",
"empty_size": 334
}
},
"layers":
{
}
}
""" Additional TileStache caches.
"""
import os
from os.path import exists
from .Core import TheTileLeftANote
from .Caches import Disk
class SparseCache(Disk):
""" Disk cache which 404s "empty" tiles.
"""
# TODO support multiple sizes
def __init__(self, empty_size=334, **kwargs):
self.empty_size = empty_size
return Disk.__init__(self, **kwargs)
def read(self, layer, coord, format):
""" Read a cached tile.
"""
fullpath = self._fullpath(layer, coord, format)
if not exists(fullpath):
return None
if os.stat(fullpath).st_size == self.empty_size:
raise TheTileLeftANote(status_code=404, emit_content_type=False)
return Disk.read(self, layer, coord, format)
class LanternCache(Disk):
""" Disk cache which appends metadata about the content of a tile.
"""
def __init__(self, land='', sea='', **kwargs):
self.land_md5 = land
self.sea_md5 = sea
return Disk.__init__(self, **kwargs)
def signal_land_or_sea(self, body):
if body:
m = hashlib.md5()
m.update(body)
md5sum = m.hexdigest()
if md5sum == self.land_md5:
raise TheTileLeftANote(content=body, headers=Headers([('X-Land-Or-Sea', 1)]))
elif md5sum == self.sea_md5:
raise TheTileLeftANote(content=body, headers=Headers([('X-Land-Or-Sea', 2)]))
else:
raise TheTileLeftANote(content=body, headers=Headers([('X-Land-Or-Sea', 0)]))
def read(self, layer, coord, format):
body = Disk.read(self, layer, coord, format)
self.signal_land_or_sea(body)
# we should never get here
return body
def save(self, body, layer, coord, format):
Disk.save(self, body, layer, coord, format)
self.signal_land_or_sea(body)
{
"cache":
{
"class": "LanternCache",
"kwargs": {
"path": "/tmp/stache",
"umask": "0000",
"land": "cd37744a985b79ed29bb6269e45da27f",
"sea": "35f515ac80cd2fb679de95699044ee7b"
}
},
"layers":
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment