File: generate_service_template.py

package info (click to toggle)
bugwarrior 1.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,008 kB
  • sloc: python: 7,762; makefile: 153
file content (102 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download | duplicates (5)
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
from __future__ import print_function
import inspect
import os
import sys

from jinja2 import Template

from bugwarrior.services import Issue
from functools import reduce


def make_table(grid):
    """ Make a RST-compatible table

    From http://stackoverflow.com/a/12539081

    """
    cell_width = 2 + max(
        reduce(
            lambda x, y: x+y, [[len(item) for item in row] for row in grid], []
        )
    )
    num_cols = len(grid[0])
    rst = table_div(num_cols, cell_width, 0)
    header_flag = 1
    for row in grid:
        rst = rst + '| ' + '| '.join(
            [normalize_cell(x, cell_width-1) for x in row]
        ) + '|\n'
        rst = rst + table_div(num_cols, cell_width, header_flag)
        header_flag = 0
    return rst


def table_div(num_cols, col_width, header_flag):
    if header_flag == 1:
        return num_cols*('+' + (col_width)*'=') + '+\n'
    else:
        return num_cols*('+' + (col_width)*'-') + '+\n'


def normalize_cell(string, length):
    return string + ((length - len(string)) * ' ')


def import_by_path(name):
    m = __import__(name)
    for n in name.split(".")[1:]:
        m = getattr(m, n)
    return m


def row_comparator(left_row, right_row):
    left = left_row[0]
    right = right_row[0]
    if left > right:
        return 1
    elif right > left or left == 'Field Name':
        return -1
    return 0


TYPE_NAME_MAP = {
    'date': 'Date & Time',
    'numeric': 'Numeric',
    'string': 'Text (string)',
    'duration': 'Duration'
}


if __name__ == '__main__':
    service = sys.argv[1]
    module = import_by_path(
        'bugwarrior.services.{service}'.format(service=service)
    )
    rows = []
    for name, obj in inspect.getmembers(module):
        if inspect.isclass(obj) and issubclass(obj, Issue):
            for field_name, details in obj.UDAS.items():
                rows.append(
                    [
                        '``%s``' % field_name,
                        ' '.join(details['label'].split(' ')[1:]),
                        TYPE_NAME_MAP.get(
                            details['type'],
                            '``%s``' % details['type'],
                        ),
                    ]
                )

    rows = sorted(rows, cmp=row_comparator)
    rows.insert(0, ['Field Name', 'Description', 'Type'])

    filename = os.path.join(os.path.dirname(__file__), 'service_template.html')
    with open(filename) as template:
        rendered = Template(template.read()).render({
            'service_name_humane': service.title(),
            'service_name': service,
            'uda_table': make_table(rows)
        })

    print(rendered)