File: extension.py

package info (click to toggle)
python-asdf 2.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,248 kB
  • sloc: python: 13,104; makefile: 125
file content (74 lines) | stat: -rw-r--r-- 2,473 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
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

"""
Implementation of command for reporting information about installed extensions.
"""

import sys
from pkg_resources import iter_entry_points

from .main import Command

__all__ = ['find_extensions']

class QueryExtension(Command): # pragma: no cover
    """This class is the plugin implementation for the asdftool runner."""
    @classmethod
    def setup_arguments(cls, subparsers):
        parser = subparsers.add_parser(
            "extensions", help="Show information about installed extensions",
            description="""Reports information about installed ASDF extensions""")

        parser.add_argument(
            "-s", "--summary", action="store_true",
            help="Display only the installed extensions themselves")
        parser.add_argument(
            "-t", "--tags-only", action="store_true",
            help="Display tags from installed extensions, but no other information")

        parser.set_defaults(func=cls.run)
        return parser

    @classmethod
    def run(cls, args):
        if args.summary and args.tags_only:
            sys.stderr.write(
                "ERROR: Options -s/--summary and -t/--tags-only are not compatible\n")
            return 1

        return find_extensions(args.summary, args.tags_only)


def _format_entry_point(ep):
    extension_class = "{}.{}".format(ep.module_name, ep.attrs[0])
    return "Extension Name: '{}' (from {}) Class: {}".format(
        ep.name, ep.dist, extension_class)


def _format_type_name(typ):
    return "{}.{}".format(typ.__module__, typ.__name__)


def _tag_comparator(a, b):
    return _format_type_name(a) < _format_type_name(b)


def _print_extension_details(ext, tags_only):
    for typ in sorted(ext.types, key=lambda x: _format_type_name(x)):
        if typ.name is not None:
            print("-  " + _format_type_name(typ))
            if not tags_only:
                print("      implements: {}".format(typ.make_yaml_tag(typ.name)))
                if typ.types:
                    print("      serializes:")
                    for name in typ.types:
                        print("      - {}".format(_format_type_name(name)))


def find_extensions(summary, tags_only):
    for ep in iter_entry_points(group='asdf_extensions'):
        print(_format_entry_point(ep))
        if not summary:
            _print_extension_details(ep.load()(), tags_only)
            print()