• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SwissDataScienceCenter / renku-python / 4687758207

pending completion
4687758207

push

github-actions

GitHub
chore: update combined dependencies (#3368)

22850 of 29809 relevant lines covered (76.65%)

6.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.1
/renku/command/format/tabulate.py
1
# Copyright Swiss Data Science Center (SDSC). A partnership between
2
# École Polytechnique Fédérale de Lausanne (EPFL) and
3
# Eidgenössische Technische Hochschule Zürich (ETHZ).
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
"""Tabular format helper functions."""
16✔
17

18
from collections import OrderedDict
16✔
19
from operator import attrgetter
16✔
20

21
from renku.core import errors
16✔
22

23

24
def tabulate(collection, columns, columns_mapping, columns_alignments=None, sort=True, reverse=False) -> str:
16✔
25
    """Format collection with a tabular output.
26

27
    Args:
28
        collection: Collection to format.
29
        columns: Columns to show.
30
        columns_mapping: Mapping of collection fields to columns.
31
        columns_alignments: Column alignment (Default value = None).
32
        sort: Whether to sort by first column or not (Default value = True).
33
        reverse: Whether to sort in reverse (Default value = False).
34
    """
35
    from renku.core.util.tabulate import tabulate as to_table
8✔
36

37
    if not columns:
8✔
38
        raise errors.ParameterError("Columns cannot be empty.")
×
39

40
    columns = [c.lower().strip() for c in columns.split(",") if c]
8✔
41

42
    headers, alignments = _make_headers(columns, columns_mapping, columns_alignments)
8✔
43

44
    # Sort based on the first requested field
45
    if sort:
8✔
46
        try:
8✔
47
            attr = list(headers.keys())[0]
8✔
48
            getter = attrgetter(attr)
8✔
49
            collection = sorted(collection, key=getter, reverse=reverse)
8✔
50
        except TypeError:
×
51
            pass
×
52

53
    alignments = alignments if collection else None  # To avoid a tabulate bug
8✔
54

55
    return to_table(collection, headers=headers, colalign=alignments, disable_numparse=True)
8✔
56

57

58
def _make_headers(columns, columns_mapping, columns_alignments):
16✔
59
    columns_alignments = columns_alignments or {}
8✔
60
    headers = OrderedDict()
8✔
61
    alignments = []
8✔
62
    for column in columns:
8✔
63
        if column not in columns_mapping:
8✔
64
            raise errors.ParameterError(
×
65
                'Invalid column name: "{}".\nPossible values: {}'.format(column, ", ".join(columns_mapping))
66
            )
67
        name, display_name = columns_mapping.get(column)
8✔
68
        headers[name] = display_name
8✔
69

70
        alignment = columns_alignments.get(column, "left")
8✔
71
        alignments.append(alignment)
8✔
72

73
    return headers, alignments
8✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc