From b1aa386bd358cc975ad6552bfbd230dde50037fc Mon Sep 17 00:00:00 2001 From: Stefan-Gabriel Chitic <stefan-gabriel.chitic@cern.ch> Date: Fri, 20 Jan 2017 10:33:21 +0100 Subject: [PATCH 1/2] Plugin management system for piped transformation functions for log files --- python/LbNightlyTools/HTMLUtils.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/python/LbNightlyTools/HTMLUtils.py b/python/LbNightlyTools/HTMLUtils.py index 6ab7ecd9..dea97110 100644 --- a/python/LbNightlyTools/HTMLUtils.py +++ b/python/LbNightlyTools/HTMLUtils.py @@ -97,7 +97,8 @@ class XTerm2HTML(object): def __init__(self, first_line=1, show_line_no=False, for_bootstrap=False, line_prefix='', - is_escaped=False): + is_escaped=False, + plugins_function=[]): ''' Initialize the conversion instance. An optional first_line can be provided if the output of the processing @@ -111,6 +112,7 @@ class XTerm2HTML(object): self.for_bootstrap = for_bootstrap self.line_prefix = line_prefix self.is_escaped = is_escaped + self.plugins_function = plugins_function def parse_code(self, code): ''' @@ -214,6 +216,10 @@ class XTerm2HTML(object): ''' Choose the correct function to process the data ''' + # Preprocess escape all the text + chunk = cgi.escape(chunk, quote=True) + for plugin_function in self.plugins_function: + chunk = plugin_function(chunk) if self.for_bootstrap: return self._bootstrap_process(chunk) return self._normal_process(chunk) @@ -259,7 +265,7 @@ class XTerm2HTML(object): data.append('<span class="{}">'.format(new_class)) old_class = new_class # flush text - data.append(cgi.escape(line[pos:start], quote=True)) + data.append(line[pos:start]) if m: # update current style @@ -321,7 +327,7 @@ class XTerm2HTML(object): data.append('<samp>%s' '</samp>' % line[pos:start]) else: - data.append(cgi.escape(line[pos:start], quote=True)) + data.append(line[pos:start]) if m: # update current style @@ -338,14 +344,19 @@ class XTerm2HTML(object): return ''.join(data) +def pluginTestFunction(chunk): + return chunk.upper() + + def convertFile(src, dst, show_line_no=False, for_bootstrap=False, - line_prefix=''): + line_prefix='', plugins_function=[]): ''' Small helper to convert a text (ANSI) file to HTML. ''' from os.path import basename conv = XTerm2HTML(show_line_no=show_line_no, for_bootstrap=for_bootstrap, - line_prefix=line_prefix) + line_prefix=line_prefix, + plugins_function=plugins_function) with open(dst, 'w') as dst_file, open(src) as src_file: dst_file.write(conv.head(title=basename(src))) dst_file.write(conv.process(src_file.read())) @@ -373,10 +384,13 @@ if __name__ == '__main__': import sys if '--convertFile' in sys.argv: src = sys.argv[sys.argv.index('--convertFile') + 1] + plugin = [] + if '--plugin' in sys.argv: + plugin = [pluginTestFunction] dest = src + '.html' convertFile(src, dest, show_line_no='--show-line-no' in sys.argv, for_bootstrap='--boostrap' in sys.argv, - line_prefix='test') + line_prefix='test', plugins_function=plugin) else: conv = XTerm2HTML(show_line_no='--show-line-no' in sys.argv, for_bootstrap='--boostrap' in sys.argv) -- GitLab From 62fc68eb6fc7f46900c0ab7b3dc373d0c915b6f4 Mon Sep 17 00:00:00 2001 From: Stefan-Gabriel Chitic <stefan-gabriel.chitic@cern.ch> Date: Fri, 20 Jan 2017 14:09:27 +0100 Subject: [PATCH 2/2] Removed testfunciton --- python/LbNightlyTools/HTMLUtils.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/python/LbNightlyTools/HTMLUtils.py b/python/LbNightlyTools/HTMLUtils.py index dea97110..01ca2a1e 100644 --- a/python/LbNightlyTools/HTMLUtils.py +++ b/python/LbNightlyTools/HTMLUtils.py @@ -344,10 +344,6 @@ class XTerm2HTML(object): return ''.join(data) -def pluginTestFunction(chunk): - return chunk.upper() - - def convertFile(src, dst, show_line_no=False, for_bootstrap=False, line_prefix='', plugins_function=[]): ''' @@ -385,12 +381,9 @@ if __name__ == '__main__': if '--convertFile' in sys.argv: src = sys.argv[sys.argv.index('--convertFile') + 1] plugin = [] - if '--plugin' in sys.argv: - plugin = [pluginTestFunction] dest = src + '.html' convertFile(src, dest, show_line_no='--show-line-no' in sys.argv, - for_bootstrap='--boostrap' in sys.argv, - line_prefix='test', plugins_function=plugin) + for_bootstrap='--boostrap' in sys.argv) else: conv = XTerm2HTML(show_line_no='--show-line-no' in sys.argv, for_bootstrap='--boostrap' in sys.argv) -- GitLab