forked from osmocom/wireshark
tools: Port colorfilters2js.pl to colorfilters2js.py
Port colorfilters2js.pl to Python. Slight differences in output code formatting, but is otherwise consistent. Ping #18152
This commit is contained in:
parent
487165dd50
commit
1fdbc65394
|
@ -1,50 +0,0 @@
|
|||
#!/usr/bin/env perl
|
||||
#
|
||||
# Copyright 2011 Dirk Jagdmann <doj@cubic.org>
|
||||
#
|
||||
# Wireshark - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@wireshark.org>
|
||||
# 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;
|
|
@ -0,0 +1,85 @@
|
|||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright 2022 by Moshe Kaplan
|
||||
# Based on colorfilter2js.pl by Dirk Jagdmann <doj@cubic.org>
|
||||
#
|
||||
# Wireshark - Network traffic analyzer
|
||||
# By Gerald Combs <gerald@wireshark.org>
|
||||
# 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()
|
Loading…
Reference in New Issue