From 1fdbc6539407aa3f0abd4678f2b8e55e09fdd655 Mon Sep 17 00:00:00 2001 From: Moshe Kaplan Date: Mon, 27 Jun 2022 23:14:19 -0400 Subject: [PATCH] tools: Port colorfilters2js.pl to colorfilters2js.py Port colorfilters2js.pl to Python. Slight differences in output code formatting, but is otherwise consistent. Ping #18152 --- tools/colorfilters2js.pl | 50 ----------------------- tools/colorfilters2js.py | 85 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 50 deletions(-) delete mode 100755 tools/colorfilters2js.pl create mode 100644 tools/colorfilters2js.py diff --git a/tools/colorfilters2js.pl b/tools/colorfilters2js.pl deleted file mode 100755 index 3d8e5efc2c..0000000000 --- a/tools/colorfilters2js.pl +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env perl -# -# Copyright 2011 Dirk Jagdmann -# -# Wireshark - Network traffic analyzer -# By Gerald Combs -# Copyright 1998 Gerald Combs -# -# SPDX-License-Identifier: GPL-2.0-or-later - - -# perl program to convert a Wireshark color scheme to javascript -# code. The javascript function should then be inserted into the -# pdml2html.xsl file. -# -# run this as: perl tools/colorfilters2js.pl colorfilters - -print<<'EOF'; -function set_node_color(node,colorname) -{ - if(dojo.isString(node)) - node = dojo.byId(node); - if(!node) return; - var fg; - var bg; -EOF - -my $elseflow = ""; - -while(<>) -{ - if(/\@(.+?)\@.+\[(\d+),(\d+),(\d+)\]\[(\d+),(\d+),(\d+)\]/) - { - print " " . $elseflow . "if (colorname == '$1') {\n"; - printf(" bg='#%02x%02x%02x';\n", $2/256, $3/256, $4/256); - printf(" fg='#%02x%02x%02x';\n", $5/256, $6/256, $7/256); - print " }\n"; - } - $elseflow = "else "; -} - -print<<'EOF'; - if(fg.length > 0) - node.style.color = fg; - if(bg.length > 0) - node.style.background = bg; -} -EOF - -exit 0; diff --git a/tools/colorfilters2js.py b/tools/colorfilters2js.py new file mode 100644 index 0000000000..49b8a421da --- /dev/null +++ b/tools/colorfilters2js.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python +# +# Copyright 2022 by Moshe Kaplan +# Based on colorfilter2js.pl by Dirk Jagdmann +# +# Wireshark - Network traffic analyzer +# By Gerald Combs +# Copyright 1998 Gerald Combs +# +# SPDX-License-Identifier: GPL-2.0-or-later + + +# Python script to convert a Wireshark color scheme to javascript +# code. The javascript function should then be inserted into the +# pdml2html.xsl file. +# +# run this as: python tools/colorfilters2js.py colorfilters + + +import argparse +import io +import re +import sys + +js_prologue = """\ +function set_node_color(node, colorname) +{ + if (dojo.isString(node)) + node = dojo.byId(node); + if (!node) return; + var fg; + var bg; +""" + +js_color_entry = """\ + {7}if (colorname == '{0}') {{ + bg='#{1:02x}{2:02x}{3:02x}'; + fg='#{4:02x}{5:02x}{6:02x}'; + }}\ +""" + +js_epilogue = """ + if (fg.length > 0) + node.style.color = fg; + if (bg.length > 0) + node.style.background = bg; +} +""" + + +def generate_javascript(colorlines): + output = [js_prologue] + else_text = "" + for colorline in colorlines: + colorvalues = colorline[0], int(colorline[1])//256, int(colorline[2])//256, int(colorline[3])//256, int(colorline[4])//256, int(colorline[5])//256, int(colorline[6])//256, else_text + output += [js_color_entry.format(*colorvalues)] + else_text = "else " + output += [js_epilogue] + return "\n".join(output) + + +def main(): + parser = argparse.ArgumentParser(description="Convert a Wireshark color scheme to javascript code.") + parser.add_argument("files", metavar='files', nargs='+', help="paths to colorfiles") + parsed_args = parser.parse_args() + + COLORLINE_PATTERN = r"\@(.+?)\@.+\[(\d+),(\d+),(\d+)\]\[(\d+),(\d+),(\d+)\]" + colorlines = [] + + # Sample line: + # @Errors@ct.error@[4626,10023,11822][63479,34695,34695] + + # Read the lines from all files: + for filename in parsed_args.files: + with open(filename, encoding='utf-8') as fh: + file_content = fh.read() + colorlines += re.findall(COLORLINE_PATTERN, file_content) + javascript_code = generate_javascript(colorlines) + + stdoutu8 = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') + stdoutu8.write(javascript_code) + + +if __name__ == "__main__": + main()