aboutsummaryrefslogtreecommitdiff
path: root/tools/gen_config_doc.py
blob: caedba5777ccc76943384f6a709701f93c024e26 (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
#!/usr/bin/python

import re
import os


_KEYS_FILE = os.path.join(
    os.path.dirname(__file__), "../src/main/java/org/traccar/config/Keys.java"
)


def get_config_keys():
    """Parses Keys.java to extract keys to be used in configuration files

    Args: None

    Returns:
        list: A list of dict containing the following keys -
            'key': A dot separated name of the config key
            'description': A list of str
    """
    desc_re = re.compile(r"(/\*\*\n|\s+\*/|\s+\*)")
    key_match_re = re.compile(r"\(\n(.+)\);", re.DOTALL)
    key_split_re = re.compile(r",\s+", re.DOTALL)
    keys = []

    with open(_KEYS_FILE, "r") as f:
        config = re.findall(
            r"(/\*\*.*?\*/)\n\s+(public static final Config.*?;)", f.read(), re.DOTALL
        )
        for i in config:
            try:
                key_match = key_match_re.search(i[1])
                if key_match:
                    terms = [x.strip() for x in key_split_re.split(key_match.group(1))]
                    key = terms[0].replace('"', "")
                    description = [
                        x.strip().replace("\n", "")
                        for x in desc_re.sub("\n", i[0]).strip().split("\n\n")
                    ]
                    if len(terms) == 3:
                        description.append(f"Default: {terms[2]}")
                    keys.append(
                        {
                            "key": key,
                            "description": description,
                        }
                    )
            except IndexError:
                # will continue if key_match.group(1) or terms[0] does not exist
                # for some reason
                pass

    return keys


def get_html():
    return ("\n").join(
        [
            f"""        <div class="card mt-3">
          <div class="card-body">
              <h5 class="card-title">
                {x["key"]} <span class="badge badge-dark">config</span>
              </h5>
              <p class="card-text">
                {"<br /> ".join(x["description"])}
              </p>
          </div>
        </div>"""
            for x in get_config_keys()
        ]
    )


if __name__ == "__main__":
    html = get_html()
    print(html)