2021-05-25 01:25:00 +02:00
|
|
|
from datetime import datetime
|
2022-04-27 02:23:56 +02:00
|
|
|
from typing import Protocol
|
2020-10-12 19:48:47 +02:00
|
|
|
|
|
|
|
from django.core.management.commands.runserver import Command as DjangoCommand
|
|
|
|
|
|
|
|
|
2021-05-25 01:25:00 +02:00
|
|
|
class Writable(Protocol):
|
|
|
|
def write(self, s: str) -> None:
|
|
|
|
...
|
2020-10-12 19:48:47 +02:00
|
|
|
|
|
|
|
|
2021-05-25 01:25:00 +02:00
|
|
|
class FakeStdout:
|
|
|
|
"""
|
|
|
|
Filter stdout from Django's runserver, to declutter run-dev.py
|
|
|
|
startup output.
|
|
|
|
"""
|
2020-10-12 19:48:47 +02:00
|
|
|
|
2021-05-25 01:25:00 +02:00
|
|
|
def __init__(self, stdout: Writable) -> None:
|
|
|
|
self.stdout = stdout
|
|
|
|
# Might fail to suppress the date line around midnight, but, whatever.
|
|
|
|
self.date_prefix = datetime.now().strftime("%B %d, %Y - ")
|
|
|
|
|
|
|
|
def write(self, s: str) -> None:
|
|
|
|
if (
|
|
|
|
s == "Performing system checks...\n\n"
|
|
|
|
or s.startswith("System check identified no issues")
|
|
|
|
or s.startswith(self.date_prefix)
|
|
|
|
):
|
|
|
|
pass
|
|
|
|
elif "Quit the server with " in s:
|
|
|
|
self.stdout.write(
|
|
|
|
"Django process (re)started. " + s[s.index("Quit the server with ") :]
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-05-25 01:25:00 +02:00
|
|
|
else:
|
|
|
|
self.stdout.write(s)
|
|
|
|
|
|
|
|
|
|
|
|
class Command(DjangoCommand):
|
|
|
|
stdout: Writable
|
|
|
|
|
|
|
|
def inner_run(self, *args: object, **options: object) -> None:
|
|
|
|
self.stdout = FakeStdout(self.stdout)
|
|
|
|
try:
|
|
|
|
super().inner_run(*args, **options)
|
|
|
|
finally:
|
|
|
|
self.stdout = self.stdout.stdout
|