summaryrefslogtreecommitdiff
path: root/doc/tools/doc_status.py
blob: 00907bb01da7e214ccd2a590f71877c4be4850a3 (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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/usr/bin/env python3

import sys
import re
import math
import platform
import xml.etree.ElementTree as ET

################################################################################
#                                    Config                                    #
################################################################################

flags = {
    'c': platform.platform() != 'Windows', # Disable by default on windows, since we use ANSI escape codes
    'b': False,
    'g': False,
    's': False,
    'u': False,
    'h': False,
    'p': False,
    'o': True,
    'i': False,
}
flag_descriptions = {
    'c': 'Toggle colors when outputting.',
    'b': 'Toggle showing only not fully described classes.',
    'g': 'Toggle showing only completed classes.',
    's': 'Toggle showing comments about the status.',
    'u': 'Toggle URLs to docs.',
    'h': 'Show help and exit.',
    'p': 'Toggle showing percentage as well as counts.',
    'o': 'Toggle overall column.',
    'i': 'Toggle collapse of class items columns.',
}
long_flags = {
    'colors': 'c',
    'use-colors': 'c',

    'bad': 'b',
    'only-bad': 'b',

    'good': 'g',
    'only-good': 'g',

    'comments': 's',
    'status': 's',

    'urls': 'u',
    'gen-url': 'u',

    'help': 'h',

    'percent': 'p',
    'use-percentages': 'p',

    'overall': 'o',
    'use-overall': 'o',

    'items': 'i',
    'collapse': 'i',
}
table_columns = ['name', 'brief_description', 'description', 'methods', 'constants', 'members', 'signals']
table_column_names = ['Name', 'Brief Desc.', 'Desc.', 'Methods', 'Constants', 'Members', 'Signals']
colors = {
    'name': [36], # cyan
    'part_big_problem': [4, 31], # underline, red
    'part_problem': [31], # red
    'part_mostly_good': [33], # yellow
    'part_good': [32], # green
    'url': [4, 34], # underline, blue
    'section': [1, 4], # bold, underline
    'state_off': [36], # cyan
    'state_on': [1, 35], # bold, magenta/plum
}
overall_progress_description_weigth = 10



################################################################################
#                                    Utils                                     #
################################################################################

def validate_tag(elem, tag):
    if elem.tag != tag:
        print('Tag mismatch, expected "' + tag + '", got ' + elem.tag)
        sys.exit(255)

def color(color, string):
    if flags['c']:
        color_format = ''
        for code in colors[color]:
            color_format += '\033[' + str(code) + 'm'
        return color_format + string + '\033[0m'
    else:
        return string

ansi_escape = re.compile(r'\x1b[^m]*m')
def nonescape_len(s):
    return len(ansi_escape.sub('', s))



################################################################################
#                                   Classes                                    #
################################################################################

class ClassStatusProgress:
    def __init__(self, described = 0, total = 0):
        self.described = described
        self.total = total

    def __add__(self, other):
        return ClassStatusProgress(self.described + other.described, self.total + other.total)

    def increment(self, described):
        if described:
            self.described += 1
        self.total += 1

    def is_ok(self):
        return self.described >= self.total

    def to_configured_colored_string(self):
        if flags['p']:
            return self.to_colored_string('{percent}% ({has}/{total})', '{pad_percent}{pad_described}{s}{pad_total}')
        else:
            return self.to_colored_string()

    def to_colored_string(self, format='{has}/{total}', pad_format='{pad_described}{s}{pad_total}'):
        ratio = self.described/self.total if self.total != 0 else 1
        percent = round(100*ratio)
        s = format.format(has = str(self.described), total = str(self.total), percent = str(percent))
        if self.described >= self.total:
            s = color('part_good', s)
        elif self.described >= self.total/4*3:
            s = color('part_mostly_good', s)
        elif self.described > 0:
            s = color('part_problem', s)
        else:
            s = color('part_big_problem', s)
        pad_size = max(len(str(self.described)), len(str(self.total)))
        pad_described = ''.ljust(pad_size - len(str(self.described)))
        pad_percent = ''.ljust(3 - len(str(percent)))
        pad_total = ''.ljust(pad_size - len(str(self.total)))
        return pad_format.format(pad_described = pad_described, pad_total = pad_total, pad_percent = pad_percent, s = s)


class ClassStatus:
    def __init__(self, name=''):
        self.name = name
        self.has_brief_description = True
        self.has_description = True
        self.progresses = {
            'methods': ClassStatusProgress(),
            'constants': ClassStatusProgress(),
            'members': ClassStatusProgress(),
            'signals': ClassStatusProgress()
        }

    def __add__(self, other):
        new_status = ClassStatus()
        new_status.name = self.name
        new_status.has_brief_description = self.has_brief_description and other.has_brief_description
        new_status.has_description = self.has_description and other.has_description
        for k in self.progresses:
            new_status.progresses[k] = self.progresses[k] + other.progresses[k]
        return new_status

    def is_ok(self):
        ok = True
        ok = ok and self.has_brief_description
        ok = ok and self.has_description
        for k in self.progresses:
            ok = ok and self.progresses[k].is_ok()
        return ok

    def make_output(self):
        output = {}
        output['name'] = color('name', self.name)

        ok_string = color('part_good', 'OK')
        missing_string = color('part_big_problem', 'MISSING')

        output['brief_description'] = ok_string if self.has_brief_description else missing_string
        output['description'] = ok_string if self.has_description else missing_string

        description_progress = ClassStatusProgress(
            (self.has_brief_description + self.has_description) * overall_progress_description_weigth,
            2 * overall_progress_description_weigth
        )
        items_progress = ClassStatusProgress()

        for k in ['methods', 'constants', 'members', 'signals']:
            items_progress += self.progresses[k]
            output[k] = self.progresses[k].to_configured_colored_string()

        output['items'] = items_progress.to_configured_colored_string()

        output['overall'] = (description_progress + items_progress).to_colored_string('{percent}%', '{pad_percent}{s}')

        if self.name.startswith('Total'):
            output['url'] = color('url', 'http://docs.godotengine.org/en/latest/classes/_classes.html')
            if flags['s']:
                output['comment'] = color('part_good', 'ALL OK')
        else:
            output['url'] = color('url', 'http://docs.godotengine.org/en/latest/classes/class_{name}.html'.format(name=self.name.lower()))

            if flags['s'] and not flags['g'] and self.is_ok():
                output['comment'] = color('part_good', 'ALL OK')

        return output

    def generate_for_class(c):
        status = ClassStatus()
        status.name = c.attrib['name']
        for tag in list(c):

            if tag.tag == 'brief_description':
                status.has_brief_description = len(tag.text.strip()) > 0

            elif tag.tag == 'description':
                status.has_description = len(tag.text.strip()) > 0

            elif tag.tag in ['methods', 'signals']:
                for sub_tag in list(tag):
                    descr = sub_tag.find('description')
                    status.progresses[tag.tag].increment(len(descr.text.strip()) > 0)

            elif tag.tag in ['constants', 'members']:
                for sub_tag in list(tag):
                    status.progresses[tag.tag].increment(len(sub_tag.text.strip()) > 0)

            elif tag.tag in ['theme_items']:
                pass #Ignore those tags, since they seem to lack description at all

            else:
                print(tag.tag, tag.attrib)

        return status



################################################################################
#                                  Arguments                                   #
################################################################################

input_file_list = []
input_class_list = []

for arg in sys.argv[1:]:
    if arg.startswith('--'):
        flags[long_flags[arg[2:]]] = not flags[long_flags[arg[2:]]]
    elif arg.startswith('-'):
        for f in arg[1:]:
            flags[f] = not flags[f]
    elif arg.endswith('.xml'):
        input_file_list.append(arg)
    else:
        input_class_list.append(arg)

if flags['i']:
    for r in ['methods', 'constants', 'members', 'signals']:
        index = table_columns.index(r)
        del table_column_names[index]
        del table_columns[index]
    table_column_names.append('Items')
    table_columns.append('items')

if flags['o'] == (not flags['i']):
    table_column_names.append('Overall')
    table_columns.append('overall')

if flags['u']:
    table_column_names.append('Docs URL')
    table_columns.append('url')


################################################################################
#                                     Help                                     #
################################################################################

if len(input_file_list) < 1 or flags['h']:
    if not flags['h']:
        print(color('section', 'Invalid usage') + ': At least one classes.xml file is required')
    print(color('section', 'Usage') + ': doc_status.py [flags] <classes.xml> [class names]')
    print('\t< and > signify required parameters, while [ and ] signify optional parameters.')
    print('\tNote that you can give more than one classes file, in which case they will be merged on-the-fly.')
    print(color('section', 'Available flags') + ':')
    possible_synonym_list = list(long_flags)
    possible_synonym_list.sort()
    flag_list = list(flags)
    flag_list.sort()
    for flag in flag_list:
        synonyms = [color('name', '-' + flag)]
        for synonym in possible_synonym_list:
            if long_flags[synonym] == flag:
                synonyms.append(color('name', '--' + synonym))

        print(('{synonyms} (Currently '+color('state_'+('on' if flags[flag] else 'off'), '{value}')+')\n\t{description}').format(
            synonyms = ', '.join(synonyms),
            value = ('on' if flags[flag] else 'off'),
            description = flag_descriptions[flag]
        ))
    sys.exit(0)



################################################################################
#                               Parse class list                               #
################################################################################

class_names = []
classes = {}

for file in input_file_list:
    tree = ET.parse(file)
    doc = tree.getroot()

    if 'version' not in doc.attrib:
        print('Version missing from "doc"')
        sys.exit(255)

    version = doc.attrib['version']

    for c in list(doc):
        if c.attrib['name'] in class_names:
            continue
        class_names.append(c.attrib['name'])
        classes[c.attrib['name']] = c

class_names.sort()

if len(input_class_list) < 1:
    input_class_list = class_names



################################################################################
#                               Make output table                              #
################################################################################

table = [table_column_names]
table_row_chars = '+- '
table_column_chars = '|'

total_status = ClassStatus('Total')

for cn in input_class_list:
    if not cn in classes:
        print('Cannot find class ' + cn + '!')
        sys.exit(255)

    c = classes[cn]
    validate_tag(c, 'class')
    status = ClassStatus.generate_for_class(c)

    if flags['b'] and status.is_ok():
        continue
    if flags['g'] and not status.is_ok():
        continue

    total_status = total_status + status
    out = status.make_output()
    row = []
    for column in table_columns:
        if column in out:
            row.append(out[column])
        else:
            row.append('')

    if 'comment' in out and out['comment'] != '':
        row.append(out['comment'])

    table.append(row)




################################################################################
#                              Print output table                              #
################################################################################

if len(table) == 1:
    print(color('part_big_problem', 'No classes suitable for printing!'))
    sys.exit(0)

if len(table) > 2:
    total_status.name = 'Total = {0}'.format(len(table) - 1)
    out = total_status.make_output()
    row = []
    for column in table_columns:
        if column in out:
            row.append(out[column])
        else:
            row.append('')
    table.append(row)

table_column_sizes = []
for row in table:
    for cell_i, cell in enumerate(row):
        if cell_i >= len(table_column_sizes):
            table_column_sizes.append(0)

        table_column_sizes[cell_i] = max(nonescape_len(cell), table_column_sizes[cell_i])

divider_string = table_row_chars[0]
for cell_i in range(len(table[0])):
    divider_string += table_row_chars[1] * (table_column_sizes[cell_i] + 2) + table_row_chars[0]
print(divider_string)

for row_i, row in enumerate(table):
    row_string = table_column_chars
    for cell_i, cell in enumerate(row):
        padding_needed = table_column_sizes[cell_i] - nonescape_len(cell) + 2
        if cell_i == 0:
            row_string += table_row_chars[2] + cell + table_row_chars[2]*(padding_needed-1)
        else:
            row_string += table_row_chars[2]*math.floor(padding_needed/2) + cell + table_row_chars[2]*math.ceil((padding_needed/2))
        row_string += table_column_chars

    print(row_string)

    if row_i == 0 or row_i == len(table) - 2:
        print(divider_string)

print(divider_string)

if total_status.is_ok() and not flags['g']:
    print('All listed classes are ' + color('part_good', 'OK') + '!')