Skip to content

Commit

Permalink
Improved templates in logger (indent is specified in the template now)
Browse files Browse the repository at this point in the history
  • Loading branch information
dc-snl committed Mar 5, 2021
1 parent c75fe33 commit bd12152
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 50 deletions.
2 changes: 1 addition & 1 deletion logger/resources/templates/child_logger.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
<br>Duration: {duration}
</summary>
<div class="child-logger-body">
{child_body}
{child_body}
</div>
</details>
2 changes: 1 addition & 1 deletion logger/resources/templates/command.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ <h5 class="message-header">{message}</h5><br>
Duration: {duration}
</summary>
<div class="command-more-info">
{more_info}
{more_info}
</div>
</details>
2 changes: 1 addition & 1 deletion logger/resources/templates/command_detail_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
data-target=".{cmd_id}-collapsible"
data-toggle="collapse">Details</h5>
<ul class="list-group">
{details}
{details}
</ul>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion logger/resources/templates/diagnostics.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
data-toggle="collapse">Diagnostics</h5>
<div class="collapse" id="{cmd_id}-diagnostics">
<div class="diagnostics-body card-body">
{diagnostics}
{diagnostics}
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion logger/resources/templates/message.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="card">
<div class="card-body">
{message}
{message}
</div>
</div>
2 changes: 1 addition & 1 deletion logger/resources/templates/output_block.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<label for="{cmd_id}-{name}-show-duplicates"> Show duplicates</label><br>
<table class="output display table" id="{cmd_id}-{name}-table">
<tbody>
{table_contents}
{table_contents}
</tbody>
</table>
</div>
2 changes: 1 addition & 1 deletion logger/resources/templates/output_card.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
data-target="#{cmd_id}-{name}"
data-toggle="collapse">{title}</h5>
<div class="show collapse" id={cmd_id}-{name}>
{output_block}
{output_block}
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion logger/resources/templates/output_card_collapsed.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
data-target="#{cmd_id}-{name}"
data-toggle="collapse">{title}</h5>
<div class="collapse" id={cmd_id}-{name}>
{output_block}
{output_block}
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion logger/resources/templates/parent_logger.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1>{name}</h1>
<div class="parent-logger-body">
{parent_body}
{parent_body}
</div>
75 changes: 34 additions & 41 deletions logger/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pkgutil
import os
from pathlib import Path
import re
import textwrap
from types import SimpleNamespace, GeneratorType

Expand Down Expand Up @@ -74,10 +75,9 @@ def flatten(element):
yield element

def parent_logger_card_html(name, *args):
indent = ' '*4
header, footer = split_template(parent_logger_template,
"parent_body",
name=name)
header, indent, footer = split_template(parent_logger_template,
"parent_body",
name=name)
yield header
for arg in flatten(args):
yield textwrap.indent(arg, indent)
Expand All @@ -89,12 +89,11 @@ def child_logger_card(log):
return child_logger_card_html(log.name, heading, log.duration, *child_html)

def child_logger_card_html(name, heading, duration, *args):
indent = ' '*8
header, footer = split_template(child_logger_template,
"child_body",
name=name,
heading=heading,
duration=duration)
header, indent, footer = split_template(child_logger_template,
"child_body",
name=name,
heading=heading,
duration=duration)
yield header
for arg in args:
if isinstance(arg, str):
Expand All @@ -105,11 +104,10 @@ def child_logger_card_html(name, heading, duration, *args):
yield footer

def command_card_html(message, duration, *args):
indent = ' '*8
header, footer = split_template(command_template,
"more_info",
message=message,
duration=duration)
header, indent, footer = split_template(command_template,
"more_info",
message=message,
duration=duration)
yield header
for arg in args:
if isinstance(arg, str):
Expand All @@ -120,19 +118,17 @@ def command_card_html(message, duration, *args):
yield footer

def message_card(text):
indent = ' '*8
header, footer = split_template(message_template, "message")
header, indent, footer = split_template(message_template, "message")
text = html_encode(text)
text = "<pre>" + text.replace('\n', "<br>") + "</pre>"
yield header
yield textwrap.indent(text, indent) + '\n'
yield footer

def command_detail_list(cmd_id, *args):
indent = ' '*16
header, footer = split_template(command_detail_list_template,
"details",
cmd_id=cmd_id)
header, indent, footer = split_template(command_detail_list_template,
"details",
cmd_id=cmd_id)
yield header
for arg in args:
if isinstance(arg, str):
Expand Down Expand Up @@ -217,16 +213,15 @@ def stat_chart_card(labels, data, title, id):

def output_block_card(title, string, cmd_id, collapsed=True):
name = title.replace(' ', '_').lower()
indent = ' '*12
if collapsed:
template = output_card_collapsed_template
else:
template = output_card_template
header, footer = split_template(template,
"output_block",
name=name,
title=title,
cmd_id=cmd_id)
header, indent, footer = split_template(template,
"output_block",
name=name,
title=title,
cmd_id=cmd_id)
yield header
for line in output_block(string, name, cmd_id):
yield textwrap.indent(line, indent)
Expand All @@ -242,11 +237,9 @@ def output_block(input, name, cmd_id):
yield string

def diagnostics_card(cmd_id, *args):
indent = ' '*16

header, footer = split_template(diagnostics_template,
"diagnostics",
cmd_id=cmd_id)
header, indent, footer = split_template(diagnostics_template,
"diagnostics",
cmd_id=cmd_id)
yield header
for arg in args:
if isinstance(arg, str):
Expand All @@ -257,13 +250,12 @@ def diagnostics_card(cmd_id, *args):
yield footer

def output_block_html(lines, name, cmd_id):
indent = ' '*12
if isinstance(lines, str):
lines = lines.split('\n')
header, footer = split_template(output_block_template,
"table_contents",
name=name,
cmd_id=cmd_id)
header, indent, footer = split_template(output_block_template,
"table_contents",
name=name,
cmd_id=cmd_id)
yield header
lineno = 0
for line in lines:
Expand All @@ -272,10 +264,11 @@ def output_block_html(lines, name, cmd_id):
yield footer

def split_template(template, split_at, **kwargs):
split_marker = bytes([4]).decode()
placeholder = template.format(**{**kwargs, split_at: split_marker})
before, after = placeholder.split(split_marker + '\n')
return before, after
format = { k:v for k, v in kwargs.items() if k != split_at }
pattern = re.compile(f"(.*\\n)(\\s*)\\{{{split_at}\\}}\\n(.*)",
flags=re.DOTALL)
before, indent, after = pattern.search(template).groups()
return before.format(**format), indent, after.format(**format)

def output_line_html(line, lineno):
encoded_line = html_encode(line).rstrip()
Expand Down

0 comments on commit bd12152

Please sign in to comment.