Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to display tags #303

Merged
merged 3 commits into from
Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
- Added a diff tool to ``asdftool`` to allow for visual comparison of pairs of
ASDF files. [#286]

- Added command to ``asdftool`` to display available tags. [#303]


1.2.1(2016-11-07)
-----------------
Expand Down
1 change: 1 addition & 0 deletions asdf/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
from .to_yaml import *
from .defragment import *
from .diff import *
from .tags import *
52 changes: 52 additions & 0 deletions asdf/commands/tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

"""
Implementation of command for displaying available tags in asdf
"""

from __future__ import absolute_import, division, unicode_literals, print_function

import sys

from .main import Command
from .. import AsdfFile


__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(
str("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 _qualified_name(_class):
return "{}.{}".format(_class.__module__, _class.__name__)

def list_tags(display_classes=False, iostream=sys.stdout):
"""Function to list tags"""
af = AsdfFile()
type_by_tag = af._extensions._type_index._type_by_tag
tags = sorted(type_by_tag.keys())

for tag in tags:
string = str(tag)
if display_classes:
string += ": " + _qualified_name(type_by_tag[tag])
iostream.write(string + '\n')
41 changes: 41 additions & 0 deletions asdf/commands/tests/test_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
# -*- coding: utf-8 -*-

from __future__ import absolute_import, division, unicode_literals, print_function

import io

from ... import AsdfFile
from .. import list_tags

def _get_tags(display_classes):
iostream = io.StringIO()
list_tags(display_classes=display_classes, iostream=iostream)
iostream.seek(0)
return [line.strip() for line in iostream.readlines()]

def _class_to_string(_class):
return "{}.{}".format(_class.__module__, _class.__name__)

def test_list_schemas():
obs_tags = _get_tags(False)

af = AsdfFile()
exp_tags = sorted(af._extensions._type_index._type_by_tag.keys())

for exp, obs in zip(exp_tags, obs_tags):
assert exp == obs

def test_list_schemas_and_tags():
tag_lines = _get_tags(True)

af = AsdfFile()
type_by_tag = af._extensions._type_index._type_by_tag
exp_tags = sorted(type_by_tag.keys())

for exp_tag, line in zip(exp_tags, tag_lines):
tag_name, tag_class = line.split(": ")
assert tag_name == exp_tag

exp_class = _class_to_string(type_by_tag[exp_tag])
assert tag_class == exp_class