File: tags.py

package info (click to toggle)
python-asdf 2.14.3-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,280 kB
  • sloc: python: 16,612; makefile: 124
file content (60 lines) | stat: -rw-r--r-- 1,623 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
"""
Implementation of command for displaying available tags in asdf
"""


import sys

from .. import AsdfFile
from .main import Command

__all__ = ["list_tags"]


class TagLister(Command):  # pragma: no cover
    """This class is the plugin implementation for the asdftool runner."""

    @classmethod
    def setup_arguments(cls, subparsers):
        parser = subparsers.add_parser(
            "tags", help="List currently available tags", description="""Lists currently available tags."""
        )

        parser.add_argument(
            "-d",
            "--display-classes",
            action="store_true",
            help="""Display associated class names in addition to tags""",
        )

        parser.set_defaults(func=cls.run)

        return parser

    @classmethod
    def run(cls, args):
        return list_tags(display_classes=args.display_classes)


def _format_type(typ):
    if isinstance(typ, str):
        return typ
    else:
        return f"{typ.__module__}.{typ.__name__}"


def list_tags(display_classes=False, iostream=sys.stdout):
    """Function to list tags"""
    af = AsdfFile()

    tag_pairs = []
    for tag in af.extension_manager._converters_by_tag:
        tag_pairs.append((tag, af.extension_manager.get_converter_for_tag(tag).types))
    for tag in af.type_index._type_by_tag:
        tag_pairs.append((tag, [af.type_index._type_by_tag[tag]]))

    for tag, types in sorted(tag_pairs, key=lambda pair: pair[0]):
        string = str(tag)
        if display_classes:
            string += ":  " + ", ".join(_format_type(t) for t in types)
        iostream.write(string + "\n")