summaryrefslogtreecommitdiff
path: root/extract-data-from-abs-guide/extract.py
blob: edc22de17185149466d17b2852458778b1b4d208 (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
#!/usr/bin/env python3

import os, glob
from pytablewriter import MarkdownTableWriter

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

inputs = glob.glob('files/*.txt')

TEMPLATE_CC = '# TEMPLATE: '

for input in inputs:
    table_name = os.path.basename(input).split('.')[0].capitalize()

    with open(input, 'r') as f:
        lines = f.readlines()

    # Ignore empty files.
    if not len(lines):
        continue

    template = None

    if lines[0].startswith(TEMPLATE_CC):
        template = lines.pop(0).split(TEMPLATE_CC)[1]

    # Strip whitespace and \n from lines and filter out empty lines.
    lines = list(filter(bool, [line.strip().replace('\n', '') for line in lines]))

    # Add missing example field using detected template.
    if template:
        value_matrix = list(chunks(lines, 2))

        for idx in range(len(value_matrix)):
            value_matrix[idx].append(
                    template.strip().format(value_matrix[idx][0])
                    )
    else:
        value_matrix = list(chunks(lines, 3))

    # Surround example string with inline code backticks.
    for idx in range(len(value_matrix)):
        value_matrix[idx][2] = "`{}`".format(value_matrix[idx][2])

    md_writer = MarkdownTableWriter(
            table_name=table_name,
            headers=['op', 'desc', 'example'],
            value_matrix=value_matrix,
            )
    md_writer.write_table()