A tool that evaluates high-risk Wikipedia templates https://tools.wmflabs.org/earwig-dev/tif
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

util.py 1.5 KiB

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