summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-29 19:20:09 -0400
committerDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-29 19:20:09 -0400
commita9cef99c801509c5d3fdb222e8d5ff9f298d6572 (patch)
tree610b6ffb260962db6c509850b7191d4443339d6d
parent076a27835d298a33c98fe49c919ba41a5b8374a5 (diff)
parenta25a7912180e4f41bb1daf4398d23033036a4386 (diff)
downloadcliff-tablib-a9cef99c801509c5d3fdb222e8d5ff9f298d6572.tar.gz
Merge branch 'refactor-display'
-rw-r--r--cliff/display.py75
-rw-r--r--cliff/formatters/base.py6
-rw-r--r--cliff/lister.py46
-rw-r--r--cliff/show.py46
-rw-r--r--tox.ini2
5 files changed, 94 insertions, 81 deletions
diff --git a/cliff/display.py b/cliff/display.py
new file mode 100644
index 0000000..14497fe
--- /dev/null
+++ b/cliff/display.py
@@ -0,0 +1,75 @@
+"""Application base class for displaying data.
+"""
+import abc
+import logging
+
+import pkg_resources
+
+from .command import Command
+
+
+LOG = logging.getLogger(__name__)
+
+
+class DisplayCommandBase(Command):
+ """Command base class for displaying data about a single object.
+ """
+ __metaclass__ = abc.ABCMeta
+
+ def __init__(self, app, app_args):
+ super(DisplayCommandBase, self).__init__(app, app_args)
+ self.load_formatter_plugins()
+
+ @abc.abstractproperty
+ def formatter_namespace(self):
+ "String specifying the namespace to use for loading formatter plugins."
+
+ @abc.abstractproperty
+ def formatter_default(self):
+ "String specifying the name of the default formatter."
+
+ def load_formatter_plugins(self):
+ self.formatters = {}
+ for ep in pkg_resources.iter_entry_points(self.formatter_namespace):
+ try:
+ self.formatters[ep.name] = ep.load()()
+ except Exception as err:
+ LOG.error(err)
+ if self.app_args.debug:
+ raise
+ return
+
+ def get_parser(self, prog_name):
+ parser = super(DisplayCommandBase, self).get_parser(prog_name)
+ formatter_group = parser.add_argument_group(
+ title='output formatters',
+ description='output formatter options',
+ )
+ formatter_choices = sorted(self.formatters.keys())
+ formatter_default = self.formatter_default
+ 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.
+ """
+
+ @abc.abstractmethod
+ 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
diff --git a/cliff/formatters/base.py b/cliff/formatters/base.py
index e8ec524..43b8f17 100644
--- a/cliff/formatters/base.py
+++ b/cliff/formatters/base.py
@@ -5,16 +5,14 @@ import abc
class Formatter(object):
+ __metaclass__ = abc.ABCMeta
- def __init__(self):
- return
-
+ @abc.abstractmethod
def add_argument_group(self, parser):
"""Add any options to the argument parser.
Should use our own argument group.
"""
- return
class ListFormatter(Formatter):
diff --git a/cliff/lister.py b/cliff/lister.py
index d2c1aad..a4ec7c3 100644
--- a/cliff/lister.py
+++ b/cliff/lister.py
@@ -3,54 +3,24 @@
import abc
import logging
-import pkg_resources
-
-from .command import Command
+from .display import DisplayCommandBase
LOG = logging.getLogger(__name__)
-class Lister(Command):
+class Lister(DisplayCommandBase):
"""Command base class for providing a list of data as output.
"""
__metaclass__ = abc.ABCMeta
- def __init__(self, app, app_args):
- super(Lister, 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.list'):
- try:
- self.formatters[ep.name] = ep.load()()
- except Exception as err:
- LOG.error(err)
- if self.app_args.debug:
- raise
+ @property
+ def formatter_namespace(self):
+ return 'cliff.formatter.list'
- def get_parser(self, prog_name):
- parser = super(Lister, 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
+ @property
+ def formatter_default(self):
+ return 'table'
@abc.abstractmethod
def get_data(self, parsed_args):
diff --git a/cliff/show.py b/cliff/show.py
index 93c48ce..b2c7945 100644
--- a/cliff/show.py
+++ b/cliff/show.py
@@ -3,54 +3,24 @@
import abc
import logging
-import pkg_resources
-
-from .command import Command
+from .display import DisplayCommandBase
LOG = logging.getLogger(__name__)
-class ShowOne(Command):
+class ShowOne(DisplayCommandBase):
"""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
+ @property
+ def formatter_namespace(self):
+ return 'cliff.formatter.show'
- 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
+ @property
+ def formatter_default(self):
+ return 'table'
@abc.abstractmethod
def get_data(self, parsed_args):
diff --git a/tox.ini b/tox.ini
index 1052309..e88d318 100644
--- a/tox.ini
+++ b/tox.ini
@@ -2,7 +2,7 @@
envlist = py27,py32
[testenv]
-commands = nosetests -d --with-coverage --cover-package=cliff []
+commands = nosetests -d --with-coverage --cover-inclusive --cover-package cliff []
deps =
nose
mock