File: gen_stats_doc.py

package info (click to toggle)
libtorrent-rasterbar 2.0.11-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (133 lines) | stat: -rwxr-xr-x 3,194 bytes parent folder | download | duplicates (3)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python3
from __future__ import print_function

counter_types = {}

f = open('../include/libtorrent/performance_counters.hpp')

counter_type = ''

for line in f:

    # ignore anything after //
    if '//' in line:
        line = line.split('//')[0]

    line = line.strip()

    if line.startswith('#'):
        continue
    if line == '':
        continue

    if 'enum stats_counter_t' in line:
        counter_type = 'counter'
        continue

    if 'enum stats_gauge_t' in line:
        counter_type = 'gauge'
        continue

    if '{' in line or '}' in line or 'struct' in line or 'namespace' in line:
        continue
    if counter_type == '':
        continue
    if not line.endswith(','):
        continue

    # strip off trailing comma
    line = line[:-1]
    if '=' in line:
        line = line[:line.index('=')].strip()

    counter_types[line] = counter_type

f.close()

f = open('../src/session_stats.cpp')

out = open('stats_counters.rst', 'w+')


def print_field(str, width):
    return '%s%s' % (str, ' ' * (width - len(str)))


def render_section(names, description, types):
    max_name_len = max(len(max(names, key=len)), len('name'))
    max_type_len = max(len(max(types, key=len)), len('type'))

    if description == '':
        for n in names:
            print('WARNING: no description for "%s"' % n)

    # add link targets for the rest of the manual to reference
    for n in names:
        print('.. _%s:\n' % n, file=out)

    if len(names) > 0:
        print('.. raw:: html\n', file=out)
        for n in names:
            print('\t<a name="%s"></a>' % n, file=out)
        print('', file=out)

    separator = '+-' + ('-' * max_name_len) + '-+-' + ('-' * max_type_len) + '-+'

    # build a table for the settings, their type and default value
    print(separator, file=out)
    print('| %s | %s |' % (print_field('name', max_name_len), print_field('type', max_type_len)), file=out)
    print(separator.replace('-', '='), file=out)
    for i in range(len(names)):
        print('| %s | %s |' % (print_field(names[i], max_name_len), print_field(types[i], max_type_len)), file=out)
        print(separator, file=out)
    print(file=out)
    print(description, file=out)
    print('', file=out)


mode = ''

description = ''
names = []
types = []

for line in f:
    description_line = line.lstrip().startswith('//')

    line = line.strip()

    if mode == 'ignore':
        if '#endif' in line:
            mode = ''
        continue

    if 'TORRENT_ABI_VERSION == 1' in line:
        mode = 'ignore'
        continue

    if description_line:
        if len(names) > 0:
            render_section(names, description, types)
            description = ''
            names = []
            types = []

        description += '\n' + line[3:]

    if '#define' in line:
        continue

    if 'METRIC(' in line:
        args = line.split('(')[1].split(')')[0].split(',')

        # args: category, name, type

        args[1] = args[1].strip()
        names.append(args[0].strip() + '.' + args[1].strip())
        types.append(counter_types[args[1]])

if len(names) > 0:
    render_section(names, description, types)

out.close()
f.close()