Skip to content

Instantly share code, notes, and snippets.

@samuelcolvin
Last active May 4, 2016 14:28
Show Gist options
  • Save samuelcolvin/83a677268e1df28213bc6380c8b9f355 to your computer and use it in GitHub Desktop.
Save samuelcolvin/83a677268e1df28213bc6380c8b9f355 to your computer and use it in GitHub Desktop.
see samuelcolvin/harrier this should one day be a package of its own
import json
from time import time
import urllib.request
class UseHarrierError(Exception):
pass
class UseHarrierKeyError(KeyError):
pass
class HarrierStaticFilter:
_update_t = 0
_file_map = {}
def __init__(self, file_map_ref, max_age=10):
self._file_map_ref = file_map_ref
self._max_age = max_age
self._check_file_map()
def __call__(self, file_key):
self._check_file_map()
f = self._file_map.get(file_key.strip(' /'))
if f:
return f
raise UseHarrierKeyError('file "{}" not found, consult the file map "{}"'.format(file_key, self._file_map_ref))
def _check_file_map(self):
if (time() - self._update_t) < self._max_age:
return
with urllib.request.urlopen(self._file_map_ref) as r:
if r.status != 200:
raise UseHarrierError('bad response {} from "{}"'.format(r.status, self._file_map_ref))
data = r.read()
try:
obj = json.loads(data.decode('utf8'))
except json.JSONDecodeError as e:
raise UseHarrierError('Error loading json: {}'.format(e))
self._file_map = obj['files']
self._update_t = time()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment