ldap3cli.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:ldap3-cli 作者: cannatag 项目源码 文件源码
def build_table(name, heading, rows, styles=None, sort=None, max_width=MAX_COL_WIDTH, level=0):
    if rows:
        if max_width == 0:
            max_width = 99999
        lengths = dict()
        max_cols = max(len(heading), len(rows[0]))
        for col in range(max_cols):
            lengths[col] = 0
            for row in [heading] + rows:
                if row:
                    if isinstance(row[col], SEQUENCE_TYPES):
                        for el in row[col]:
                            lengths[col] = max(len(click.unstyle(str(el))[:max_width]), lengths[col])
                    else:
                        lengths[col] = max(len(click.unstyle(str(row[col]))[:max_width]), lengths[col])
        if heading:
            table = [H_SEPARATOR.join([ljust_style(str(element)[:max_width], col, styles, lengths) for col, element in enumerate(heading)]),
                     H_SEPARATOR.join([''.ljust(min(lengths[col], max_width), '=') for col, element in enumerate(heading)])]
        else:
            table = []
        if sort and not isinstance(sort, SEQUENCE_TYPES):
            sort = [sort]
        subrows = []
        sorted_rows = sorted(rows, key=lambda x: [x[i].lower() if hasattr(x[i], 'lower') else x[i] for i in sort]) if sort is not None else rows
        for row in sorted_rows:
            max_depth = max([len(element) if isinstance(element, SEQUENCE_TYPES) else 1 for element in row])
            for depth in range(max_depth):
                subrow = []
                for col in range(max_cols):
                    if isinstance(row[col], SEQUENCE_TYPES):
                        subrow.append(ljust_style(str(row[col][depth])[:max_width], col, styles, lengths) if row[col] and len(row[col]) > depth else ljust_style('', col, styles, lengths))
                    elif depth == 0:
                        subrow.append(ljust_style(str(row[col])[:max_width], col, styles, lengths))
                    else:
                        subrow.append(ljust_style('', col, styles, lengths))
                table.append(H_SEPARATOR.join(subrow))
    else:
        table = ['']
    if name:
        echo_title(name, level=level)
    echo_detail_multiline('', table, level=level + (1 if name else 0))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号