aboutsummaryrefslogtreecommitdiffstats
path: root/tools/colorfilters2js.py
blob: 49b8a421da2c44a30b681c01a1f4af88154950fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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()