monkey_patches.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. __package__ = 'archivebox'
  2. import sys
  3. import shutil
  4. import django
  5. import pydantic
  6. import django_stubs_ext
  7. django_stubs_ext.monkeypatch()
  8. # monkey patch django timezone to add back utc (it was removed in Django 5.0)
  9. import datetime
  10. from django.utils import timezone
  11. timezone.utc = datetime.timezone.utc
  12. # monkey patch django-signals-webhooks to change how it shows up in Admin UI
  13. # from signal_webhooks.apps import DjangoSignalWebhooksConfig
  14. # DjangoSignalWebhooksConfig.verbose_name = 'API'
  15. # Install rich for pretty tracebacks in console logs
  16. # https://rich.readthedocs.io/en/stable/traceback.html#traceback-handler
  17. from rich.traceback import install
  18. TERM_WIDTH = (shutil.get_terminal_size((200, 10)).columns - 1) if sys.stdout.isatty() else 200
  19. # os.environ.setdefault('COLUMNS', str(TERM_WIDTH))
  20. install(show_locals=True, word_wrap=False, locals_max_length=10, locals_hide_dunder=True, suppress=[django, pydantic], extra_lines=2, width=TERM_WIDTH)
  21. from daphne import access # noqa
  22. class ModifiedAccessLogGenerator(access.AccessLogGenerator):
  23. """Clutge workaround until daphne uses the Python logging framework. https://github.com/django/daphne/pull/473/files"""
  24. def write_entry(self, host, date, request, status=None, length=None, ident=None, user=None):
  25. # Ignore noisy requests to staticfiles / favicons / etc.
  26. if 'GET /static/' in request:
  27. return
  28. if "GET /health/" in request:
  29. return
  30. if 'GET /admin/jsi18n/' in request:
  31. return
  32. if request.endswith("/favicon.ico") or request.endswith("/robots.txt") or request.endswith("/screenshot.png"):
  33. return
  34. if request.endswith('.css') or request.endswith('.js') or request.endswith('.woff') or request.endswith('.ttf'):
  35. return
  36. if str(status) in ('404', '304'):
  37. return
  38. # clean up the log format to mostly match the same format as django.conf.settings.LOGGING rich formats
  39. self.stream.write(
  40. "[%s] HTTP %s (%s) %s\n"
  41. % (
  42. date.strftime("%Y-%m-%d %H:%M:%S"),
  43. request,
  44. status or "-",
  45. "localhost" if host.startswith("127.") else host.split(":")[0],
  46. )
  47. )
  48. access.AccessLogGenerator.write_entry = ModifiedAccessLogGenerator.write_entry # type: ignore
  49. # fix benedict objects to pretty-print/repr more nicely with rich
  50. # https://stackoverflow.com/a/79048811/2156113
  51. # https://rich.readthedocs.io/en/stable/pretty.html#rich-repr-protocol
  52. import benedict # noqa
  53. benedict.benedict.__rich_repr__ = lambda self: (dict(self),) # type: ignore