A corporation manager and dashboard for EVE Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

51 lines
1.8 KiB

  1. # -*- coding: utf-8 -*-
  2. from functools import wraps
  3. from hashlib import md5
  4. from os import path
  5. from traceback import format_exc
  6. from flask import url_for
  7. from flask_mako import render_template, TemplateError
  8. __all__ = ["catch_errors", "set_up_asset_versioning"]
  9. def catch_errors(app):
  10. """Wrap a route to display and log any uncaught exceptions."""
  11. def callback(func):
  12. @wraps(func)
  13. def inner(*args, **kwargs):
  14. try:
  15. return func(*args, **kwargs)
  16. except TemplateError as exc:
  17. app.logger.error("Caught exception:\n{0}".format(exc.text))
  18. return render_template("error.mako", traceback=exc.text)
  19. except Exception:
  20. app.logger.exception("Caught exception:")
  21. return render_template("error.mako", traceback=format_exc())
  22. return inner
  23. return callback
  24. def set_up_asset_versioning(app):
  25. """Add a staticv endpoint that adds hash versioning to static assets."""
  26. def callback(app, error, endpoint, values):
  27. if endpoint == "staticv":
  28. filename = values["filename"]
  29. fpath = path.join(app.static_folder, filename)
  30. try:
  31. mtime = path.getmtime(fpath)
  32. except OSError:
  33. return url_for("static", filename=filename)
  34. cache = app._hash_cache.get(fpath)
  35. if cache and cache[0] == mtime:
  36. hashstr = cache[1]
  37. else:
  38. with open(fpath, "rb") as f:
  39. hashstr = md5(f.read()).hexdigest()
  40. app._hash_cache[fpath] = (mtime, hashstr)
  41. return url_for("static", filename=filename, v=hashstr)
  42. raise error
  43. app._hash_cache = {}
  44. app.url_build_error_handlers.append(lambda a, b, c: callback(app, a, b, c))