summaryrefslogtreecommitdiff
path: root/cliff/show.py
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-27 19:56:45 -0400
committerDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-27 19:56:45 -0400
commit556495e530c9cb2dc67300d1f199780e247921dc (patch)
treee7c63773f3001742dce82082efb13a52939326b5 /cliff/show.py
parentb8f3ad548d02eff5fe1b3c8d8515fab9db888204 (diff)
downloadcliff-556495e530c9cb2dc67300d1f199780e247921dc.tar.gz
add ShowOne base class for commands that need to show properties of an individual object
make the table formatter work as a single object formatter update the docs for the new features
Diffstat (limited to 'cliff/show.py')
-rw-r--r--cliff/show.py65
1 files changed, 65 insertions, 0 deletions
diff --git a/cliff/show.py b/cliff/show.py
new file mode 100644
index 0000000..9f77703
--- /dev/null
+++ b/cliff/show.py
@@ -0,0 +1,65 @@
+"""Application base class for displaying data about a single object.
+"""
+import abc
+import logging
+
+import pkg_resources
+
+from .command import Command
+
+
+LOG = logging.getLogger(__name__)
+
+
+class ShowOne(Command):
+ """Command base class for displaying data about a single object.
+ """
+ __metaclass__ = abc.ABCMeta
+
+ def __init__(self, app, app_args):
+ super(ShowOne, self).__init__(app, app_args)
+ self.load_formatter_plugins()
+
+ def load_formatter_plugins(self):
+ self.formatters = {}
+ for ep in pkg_resources.iter_entry_points('cliff.formatter.show'):
+ try:
+ self.formatters[ep.name] = ep.load()()
+ except Exception as err:
+ LOG.error(err)
+ if self.app_args.debug:
+ raise
+
+ def get_parser(self, prog_name):
+ parser = super(ShowOne, self).get_parser(prog_name)
+ formatter_group = parser.add_argument_group(
+ title='Output Formatters',
+ description='List output formatter options',
+ )
+ formatter_choices = sorted(self.formatters.keys())
+ formatter_default = 'table'
+ if formatter_default not in formatter_choices:
+ formatter_default = formatter_choices[0]
+ formatter_group.add_argument(
+ '-f', '--format',
+ dest='formatter',
+ action='store',
+ choices=formatter_choices,
+ default=formatter_default,
+ help='the output format to use, defaults to %s' % formatter_default,
+ )
+ for name, formatter in sorted(self.formatters.items()):
+ formatter.add_argument_group(parser)
+ return parser
+
+ @abc.abstractmethod
+ def get_data(self, parsed_args):
+ """Return a two-part tuple with a tuple of column names
+ and a tuple of values.
+ """
+
+ def run(self, parsed_args):
+ column_names, data = self.get_data(parsed_args)
+ formatter = self.formatters[parsed_args.formatter]
+ formatter.emit_one(column_names, data, self.app.stdout, parsed_args)
+ return 0