Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a renderer for log files that can render ansi colours #673

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion airlock/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import csv
import mimetypes
import re
from dataclasses import dataclass
from email.utils import formatdate
from functools import cached_property
from io import BytesIO, StringIO
from pathlib import Path
from typing import IO, Any, ClassVar, Self, cast

from ansi2html import Ansi2HTMLConverter
from django.http import FileResponse, HttpResponseBase
from django.template import Template, loader
from django.template.response import SimpleTemplateResponse
from django.utils.safestring import mark_safe

from airlock.types import UrlPath
from airlock.utils import is_valid_file_type
Expand Down Expand Up @@ -158,9 +161,35 @@ def context(self):
}


class LogRenderer(TextRenderer):
def context(self):
# Convert the text of the log file to HTML, converting ANSI colour codes to css classes
# so we get the colour formatting from the original log.
# We don't need the full HTML file that's produced, so just extract the <pre></pre>
# tag which contains the log content and the inline styles.
conv = Ansi2HTMLConverter()
text = conv.convert(self.stream.read())
match = re.match(
r".*(?P<style_tag><style.*</style>).*(?P<pre_tag><pre.*</pre>).*",
text,
flags=re.S,
)
if match: # pragma: no branch
# After conversion, we should always find a match. As a precaution, check
# and render the plain text if we don't.
style_tag = match.group("style_tag")
pre_tag = match.group("pre_tag")
text = mark_safe(f"{style_tag}{pre_tag}")

return {
"text": text,
"class": Path(self.filename).suffix.lstrip("."),
}


FILE_RENDERERS = {
".csv": CSVRenderer,
".log": TextRenderer,
".log": LogRenderer,
".txt": TextRenderer,
".json": TextRenderer,
".md": TextRenderer,
Expand Down
1 change: 1 addition & 0 deletions requirements.prod.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ansi2html
Django
django-vite
slippers
Expand Down
4 changes: 4 additions & 0 deletions requirements.prod.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#
# pip-compile --allow-unsafe --generate-hashes --strip-extras requirements.prod.in
#
ansi2html==1.9.2 \
--hash=sha256:3453bf87535d37b827b05245faaa756dbab4ec3d69925e352b6319c3c955c0a5 \
--hash=sha256:dccb75aa95fb018e5d299be2b45f802952377abfdce0504c17a6ee6ef0a420c5
# via -r requirements.prod.in
asgiref==3.8.1 \
--hash=sha256:3e1e3ecc849832fe52ccf2cb6686b7a55f82bb1d6aee72a58826471390335e47 \
--hash=sha256:c343bd80a0bec947a9860adb4c432ffa7db769836c64238fc34bdc3fec84d590
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
(".png", "image/png", False, None),
(".csv", "text/html", False, "airlock/templates/file_browser/csv.html"),
(".txt", "text/html", False, "airlock/templates/file_browser/text.html"),
(".log", "text/html", False, "airlock/templates/file_browser/text.html"),
(".html", "text/html", True, "airlock/templates/file_browser/plaintext.html"),
(".png", "text/html", True, "airlock/templates/file_browser/plaintext.html"),
(".csv", "text/html", True, "airlock/templates/file_browser/plaintext.html"),
(".txt", "text/html", True, "airlock/templates/file_browser/plaintext.html"),
(".log", "text/html", True, "airlock/templates/file_browser/plaintext.html"),
]


Expand Down Expand Up @@ -145,3 +147,27 @@ def test_plaintext_renderer_handles_invalid_utf8(tmp_path):
response.render()
assert response.status_code == 200
assert "invalid � continuation byte" in response.rendered_content


def test_log_renderer_handles_ansi_colors(tmp_path):
log_file = tmp_path / "test.log"
# in ansi codes:
# \x1B[32m = foregrouund green
# \x1b[1m bold
# \x1b[0m resets formatting
log_file.write_bytes(
b"No ansi here \x1b[32m\x1b[1mThis is green and bold.\x1b[0m This is not."
)
relpath = log_file.relative_to(tmp_path)
Renderer = renderers.get_renderer(relpath)
renderer = Renderer.from_file(log_file, relpath)
response = renderer.get_response()
response.render()
assert response.status_code == 200

assert "ansi1 { font-weight: bold; }" in response.rendered_content
assert "ansi32 { color: #00aa00; }" in response.rendered_content
assert (
'<span class="ansi1 ansi32">This is green and bold.</span>'
in response.rendered_content
)