A corporation manager and dashboard for EVE Online
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 

46 lignes
1.5 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_hash_versioning"]
  9. def catch_errors(app):
  10. def callback(func):
  11. @wraps(func)
  12. def inner(*args, **kwargs):
  13. try:
  14. return func(*args, **kwargs)
  15. except TemplateError as exc:
  16. app.logger.error("Caught exception:\n{0}".format(exc.text))
  17. return render_template("error.mako", traceback=exc.text)
  18. except Exception:
  19. app.logger.exception("Caught exception:")
  20. return render_template("error.mako", traceback=format_exc())
  21. return inner
  22. return callback
  23. def set_up_hash_versioning(app):
  24. def callback(app, error, endpoint, values):
  25. if endpoint == "staticv":
  26. filename = values["filename"]
  27. fpath = path.join(app.static_folder, filename)
  28. mtime = path.getmtime(fpath)
  29. cache = app._hash_cache.get(fpath)
  30. if cache and cache[0] == mtime:
  31. hashstr = cache[1]
  32. else:
  33. with open(fpath, "rb") as f:
  34. hashstr = md5(f.read()).hexdigest()
  35. app._hash_cache[fpath] = (mtime, hashstr)
  36. return url_for("static", filename=filename, v=hashstr)
  37. raise error
  38. app._hash_cache = {}
  39. app.url_build_error_handlers.append(lambda *args: callback(app, *args))