source: rtems-tools/rtemstoolkit/textbox.py @ 3618a62

5
Last change on this file since 3618a62 was 224fb21, checked in by Chris Johns <chrisj@…>, on 04/24/17 at 16:38:40

rtemstoolkit: Add a textbox module for formatted reporting.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1#
2# RTEMS Tools Project (http://www.rtems.org/)
3# Copyright 2017 Chris Johns (chrisj@rtems.org)
4# All rights reserved.
5#
6# This file is part of the RTEMS Tools package in 'rtems-tools'.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions are met:
10#
11# 1. Redistributions of source code must retain the above copyright notice,
12# this list of conditions and the following disclaimer.
13#
14# 2. Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29#
30
31#
32# Manage paths locally. The internally the path is in Unix or shell format and
33# we convert to the native format when performing operations at the Python
34# level. This allows macro expansion to work.
35#
36
37from __future__ import print_function
38
39import copy
40import os
41
42#
43# Support to handle use in a package and as a unit test.
44# If there is a better way to let us know.
45#
46try:
47    from . import error
48except (ValueError, SystemError):
49    import error
50
51def line(cols, line = '-', marker = '|', indent = 0, linesep = os.linesep):
52    s = ' ' * indent + marker
53    for c in cols:
54        s += line[0] * (c - 1) + marker
55    return s + linesep
56
57def row(cols, data, indent = 0, marker = '|', linesep = os.linesep):
58    if len(cols) != len(data):
59        raise error.internal('data size (%d) does not' \
60                             ' match columns (%d)' % (len(data), len(cols)))
61    s = ' ' * indent + '|'
62    for c in range(0, len(cols)):
63        if c < len(cols) - 1:
64            m = marker
65        else:
66            m = '|'
67        s += '%-*s%s' % (cols[c] - 1, str(data[c]), m)
68    return s + linesep
69
70def even_columns(cols, width = 80):
71    per_col = width / cols
72    columns = [per_col for c in range(0, cols)]
73    for remainder in range(0, width - (per_col * cols)):
74        if remainder % 2 == 0:
75            columns[remainder] += 1
76        else:
77            columns[len(columns) - remainder] += 1
78    return columns
79
80def merge_columns(columns):
81    columns = copy.deepcopy(columns)
82    cols = []
83    while True:
84        empty = True
85        for c in columns:
86            if len(c) != 0:
87                empty = False
88                break
89        if empty:
90            break
91        lowest = 0
92        lowest_size = 99999
93        for c in range(0, len(columns)):
94            if len(columns[c]) > 0 and columns[c][0] < lowest_size:
95                lowest = c
96                lowest_size = columns[c][0]
97        cols += [lowest_size]
98        columns[lowest] = columns[lowest][1:]
99        for c in range(0, len(columns)):
100            if len(columns[c]) > 0 and c != lowest:
101                if columns[c][0] != lowest_size:
102                    columns[c][0] -= lowest_size
103                else:
104                    columns[c] = columns[c][1:]
105    return cols
106
107if __name__ == '__main__':
108    width = 75
109    cols_1 = [width]
110    cols_2 = [10, width - 10]
111    cols_3 = even_columns(3, width = width)
112    cols_4 = even_columns(4, width = width)
113    print(line(cols_1, marker = 'X', linesep = ''))
114    print(line(cols_1, marker = '+', indent = 1, linesep = ''))
115    print(line(cols_1, marker = '+', indent = 2, linesep = ''))
116    print(line(cols_2, marker = '+', indent = 2, linesep = ''))
117    print(line(cols_3, marker = '+', indent = 2, linesep = ''))
118    print(line(cols_4, marker = '+', indent = 2, linesep = ''))
119    print(row(cols_4, [' %d' % (i) for i in [1, 2, 3, 4]], indent = 2, linesep = ''))
120    print(line(cols_4, marker = '+', indent = 2, linesep = ''))
121    print(line(merge_columns([cols_2, cols_3, cols_4]), indent = 2, linesep = ''))
Note: See TracBrowser for help on using the repository browser.