From bd1215209acefdd1fdaa59875c916d2dd37e2a4f Mon Sep 17 00:00:00 2001 From: David Collins Date: Fri, 5 Mar 2021 12:13:33 -0700 Subject: [PATCH] Improved templates in logger (indent is specified in the template now) --- logger/resources/templates/child_logger.html | 2 +- logger/resources/templates/command.html | 2 +- .../templates/command_detail_list.html | 2 +- logger/resources/templates/diagnostics.html | 2 +- logger/resources/templates/message.html | 2 +- logger/resources/templates/output_block.html | 2 +- logger/resources/templates/output_card.html | 2 +- .../templates/output_card_collapsed.html | 2 +- logger/resources/templates/parent_logger.html | 2 +- logger/util.py | 75 +++++++++---------- 10 files changed, 43 insertions(+), 50 deletions(-) diff --git a/logger/resources/templates/child_logger.html b/logger/resources/templates/child_logger.html index 79d2e39..6168515 100644 --- a/logger/resources/templates/child_logger.html +++ b/logger/resources/templates/child_logger.html @@ -4,6 +4,6 @@
Duration: {duration}
-{child_body} + {child_body}
diff --git a/logger/resources/templates/command.html b/logger/resources/templates/command.html index f48713c..ca13499 100644 --- a/logger/resources/templates/command.html +++ b/logger/resources/templates/command.html @@ -4,6 +4,6 @@
{message}

Duration: {duration}
-{more_info} + {more_info}
diff --git a/logger/resources/templates/command_detail_list.html b/logger/resources/templates/command_detail_list.html index 5387cea..bfcbb83 100644 --- a/logger/resources/templates/command_detail_list.html +++ b/logger/resources/templates/command_detail_list.html @@ -6,7 +6,7 @@ data-target=".{cmd_id}-collapsible" data-toggle="collapse">Details diff --git a/logger/resources/templates/diagnostics.html b/logger/resources/templates/diagnostics.html index 390baa2..1123dcb 100644 --- a/logger/resources/templates/diagnostics.html +++ b/logger/resources/templates/diagnostics.html @@ -6,7 +6,7 @@ data-toggle="collapse">Diagnostics
-{diagnostics} + {diagnostics}
diff --git a/logger/resources/templates/message.html b/logger/resources/templates/message.html index 849ef06..4f1db2f 100644 --- a/logger/resources/templates/message.html +++ b/logger/resources/templates/message.html @@ -1,5 +1,5 @@
-{message} + {message}
diff --git a/logger/resources/templates/output_block.html b/logger/resources/templates/output_block.html index a2a043f..c7a4ab1 100644 --- a/logger/resources/templates/output_block.html +++ b/logger/resources/templates/output_block.html @@ -11,7 +11,7 @@
-{table_contents} + {table_contents}
diff --git a/logger/resources/templates/output_card.html b/logger/resources/templates/output_card.html index a3b5e81..fbb67d7 100644 --- a/logger/resources/templates/output_card.html +++ b/logger/resources/templates/output_card.html @@ -5,7 +5,7 @@ data-target="#{cmd_id}-{name}" data-toggle="collapse">{title}
-{output_block} + {output_block}
diff --git a/logger/resources/templates/output_card_collapsed.html b/logger/resources/templates/output_card_collapsed.html index a6389bd..5d22f47 100644 --- a/logger/resources/templates/output_card_collapsed.html +++ b/logger/resources/templates/output_card_collapsed.html @@ -5,7 +5,7 @@ data-target="#{cmd_id}-{name}" data-toggle="collapse">{title}
-{output_block} + {output_block}
diff --git a/logger/resources/templates/parent_logger.html b/logger/resources/templates/parent_logger.html index b6d9720..b9f3cd6 100644 --- a/logger/resources/templates/parent_logger.html +++ b/logger/resources/templates/parent_logger.html @@ -1,4 +1,4 @@

{name}

-{parent_body} + {parent_body}
diff --git a/logger/util.py b/logger/util.py index 04ad5a4..5da5a84 100644 --- a/logger/util.py +++ b/logger/util.py @@ -4,6 +4,7 @@ import pkgutil import os from pathlib import Path +import re import textwrap from types import SimpleNamespace, GeneratorType @@ -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) @@ -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): @@ -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): @@ -120,8 +118,7 @@ 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 = "
" + text.replace('\n', "
") + "
" yield header @@ -129,10 +126,9 @@ def message_card(text): 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): @@ -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) @@ -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): @@ -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: @@ -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()