File: reader_table.py

package info (click to toggle)
satpy 0.59.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,296 kB
  • sloc: python: 93,630; xml: 3,343; makefile: 146; javascript: 23
file content (86 lines) | stat: -rw-r--r-- 2,914 bytes parent folder | download
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2022 Satpy developers
#
# This file is part of satpy.
#
# satpy is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# satpy is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with satpy.  If not, see <http://www.gnu.org/licenses/>.
"""Module for autogenerating reader table from config files."""

from yaml import BaseLoader

from satpy.readers.core.config import available_readers


def rst_table_row(columns=None):
    """Create one row for a rst table.

    Args:
        columns (list[str]): Content of each column.

    Returns:
        str
    """
    row = "    * - {}\n".format(columns[0])
    columns = ["      - {}\n".format(col) for col in columns[1:]]
    row = row + "".join(columns)

    return row


def rst_table_header(name=None, header=None, header_rows=1, widths="auto", class_name="datatable"):
    """Create header for rst table.

    Args:
        name (str): Name of the table
        header (list[str]): Column names
        header_rows (int): Number of header rows
        widths (optional[list[int]]): Width of each column as a list. If not specified
            defaults to auto and will therefore determined by the backend
            (see <https://docutils.sourceforge.io/docs/ref/rst/directives.html#table>)
        class_name (str): The CSS class name for the table. A corresponding js function should be in main.js in
            in the "statis" directory.

    Returns:
        str
    """
    if isinstance(widths, list):
        widths = " ".join([str(w) for w in widths])

    header = rst_table_row(header)

    table_header = (f".. list-table:: {name}\n"
                    f"    :header-rows: {header_rows}\n"
                    f"    :widths: {widths}\n"
                    f"    :class: {class_name}\n\n"
                    f"{header}")

    return table_header


def generate_reader_table():
    """Create reader table from reader yaml config files.

    Returns:
        str
    """
    table = [rst_table_header("Satpy Readers", header=["Description", "Reader name", "Status", "fsspec support"],
                              widths="auto")]

    reader_configs = available_readers(as_dict=True, yaml_loader=BaseLoader)
    for rc in reader_configs:
        table.append(rst_table_row([rc.get("long_name", "").rstrip("\n"), rc.get("name", ""),
                                    rc.get("status", ""), rc.get("supports_fsspec", "false")]))

    return "".join(table)