summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cliff/command.py5
-rw-r--r--cliff/commandmanager.py2
-rw-r--r--cliff/display.py17
-rw-r--r--cliff/formatters/base.py8
-rw-r--r--cliff/formatters/shell.py3
-rw-r--r--cliff/interactive.py2
-rw-r--r--cliff/lister.py16
-rw-r--r--cliff/show.py7
-rw-r--r--cliff/tests/test_app.py10
-rw-r--r--cliff/tests/test_show.py36
-rw-r--r--docs/source/history.rst2
11 files changed, 62 insertions, 46 deletions
diff --git a/cliff/command.py b/cliff/command.py
index 116ae5c..7dfe749 100644
--- a/cliff/command.py
+++ b/cliff/command.py
@@ -3,15 +3,16 @@ import abc
import argparse
import inspect
+import six
+
+@six.add_metaclass(abc.ABCMeta)
class Command(object):
"""Base class for command plugins.
:param app: Application instance invoking the command.
:paramtype app: cliff.app.App
"""
- __metaclass__ = abc.ABCMeta
-
def __init__(self, app, app_args):
self.app = app
self.app_args = app_args
diff --git a/cliff/commandmanager.py b/cliff/commandmanager.py
index 2b91f17..1469e75 100644
--- a/cliff/commandmanager.py
+++ b/cliff/commandmanager.py
@@ -28,7 +28,7 @@ class CommandManager(object):
for the plugins to be loaded. For example,
``'cliff.formatter.list'``.
:param convert_underscores: Whether cliff should convert underscores to
- to spaces in entry_point commands.
+ spaces in entry_point commands.
"""
def __init__(self, namespace, convert_underscores=True):
self.commands = {}
diff --git a/cliff/display.py b/cliff/display.py
index e04fd29..6efcf23 100644
--- a/cliff/display.py
+++ b/cliff/display.py
@@ -1,8 +1,19 @@
"""Application base class for displaying data.
"""
import abc
+
+try:
+ from itertools import compress
+except ImportError:
+ # for py26 compat
+ from itertools import izip
+
+ def compress(data, selectors):
+ return (d for d, s in izip(data, selectors) if s)
+
import logging
+import six
import stevedore
from .command import Command
@@ -11,10 +22,10 @@ from .command import Command
LOG = logging.getLogger(__name__)
+@six.add_metaclass(abc.ABCMeta)
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)
@@ -80,3 +91,7 @@ class DisplayCommandBase(Command):
column_names, data = self.take_action(parsed_args)
self.produce_output(parsed_args, column_names, data)
return 0
+
+ @staticmethod
+ def _compress_iterable(iterable, selectors):
+ return compress(iterable, selectors)
diff --git a/cliff/formatters/base.py b/cliff/formatters/base.py
index 43b8f17..c477a54 100644
--- a/cliff/formatters/base.py
+++ b/cliff/formatters/base.py
@@ -3,9 +3,11 @@
import abc
+import six
+
+@six.add_metaclass(abc.ABCMeta)
class Formatter(object):
- __metaclass__ = abc.ABCMeta
@abc.abstractmethod
def add_argument_group(self, parser):
@@ -15,10 +17,10 @@ class Formatter(object):
"""
+@six.add_metaclass(abc.ABCMeta)
class ListFormatter(Formatter):
"""Base class for formatters that know how to deal with multiple objects.
"""
- __metaclass__ = abc.ABCMeta
@abc.abstractmethod
def emit_list(self, column_names, data, stdout, parsed_args):
@@ -32,10 +34,10 @@ class ListFormatter(Formatter):
"""
+@six.add_metaclass(abc.ABCMeta)
class SingleFormatter(Formatter):
"""Base class for formatters that work with single objects.
"""
- __metaclass__ = abc.ABCMeta
@abc.abstractmethod
def emit_one(self, column_names, data, stdout, parsed_args):
diff --git a/cliff/formatters/shell.py b/cliff/formatters/shell.py
index fd4f29e..e613c22 100644
--- a/cliff/formatters/shell.py
+++ b/cliff/formatters/shell.py
@@ -3,6 +3,7 @@
from .base import SingleFormatter
+import argparse
import six
@@ -19,7 +20,7 @@ class ShellFormatter(SingleFormatter):
default=[],
dest='variables',
metavar='VARIABLE',
- help='specify the variable(s) to include, can be repeated',
+ help=argparse.SUPPRESS,
)
group.add_argument(
'--prefix',
diff --git a/cliff/interactive.py b/cliff/interactive.py
index e1bd930..e5cddb0 100644
--- a/cliff/interactive.py
+++ b/cliff/interactive.py
@@ -39,7 +39,7 @@ class InteractiveApp(cmd2.Cmd):
cmd2.Cmd.__init__(self, 'tab', stdin=stdin, stdout=stdout)
def default(self, line):
- # Tie in the the default command processor to
+ # Tie in the default command processor to
# dispatch commands known to the command manager.
# We send the message through our parent app,
# since it already has the logic for executing
diff --git a/cliff/lister.py b/cliff/lister.py
index 1b6c3b6..6cf79a2 100644
--- a/cliff/lister.py
+++ b/cliff/lister.py
@@ -1,28 +1,20 @@
"""Application base class for providing a list of data as output.
"""
import abc
-
-try:
- from itertools import compress
-except ImportError:
- # for py26 compat
- from itertools import izip
-
- def compress(data, selectors):
- return (d for d, s in izip(data, selectors) if s)
-
import logging
+import six
+
from .display import DisplayCommandBase
LOG = logging.getLogger(__name__)
+@six.add_metaclass(abc.ABCMeta)
class Lister(DisplayCommandBase):
"""Command base class for providing a list of data as output.
"""
- __metaclass__ = abc.ABCMeta
@property
def formatter_namespace(self):
@@ -56,7 +48,7 @@ class Lister(DisplayCommandBase):
# of data that the user has expressed interest in
# seeing. We have to convert the compress() output to a
# list so the table formatter can ask for its length.
- data_gen = (list(compress(row, selector))
+ data_gen = (list(self._compress_iterable(row, selector))
for row in data)
self.formatter.emit_list(columns_to_include,
data_gen,
diff --git a/cliff/show.py b/cliff/show.py
index 855b2d2..fb01068 100644
--- a/cliff/show.py
+++ b/cliff/show.py
@@ -1,19 +1,20 @@
"""Application base class for displaying data about a single object.
"""
import abc
-import itertools
import logging
+import six
+
from .display import DisplayCommandBase
LOG = logging.getLogger(__name__)
+@six.add_metaclass(abc.ABCMeta)
class ShowOne(DisplayCommandBase):
"""Command base class for displaying data about a single object.
"""
- __metaclass__ = abc.ABCMeta
@property
def formatter_namespace(self):
@@ -38,7 +39,7 @@ class ShowOne(DisplayCommandBase):
# Set up argument to compress()
selector = [(c in columns_to_include)
for c in column_names]
- data = list(itertools.compress(data, selector))
+ data = list(self._compress_iterable(data, selector))
self.formatter.emit_one(columns_to_include,
data,
self.app.stdout,
diff --git a/cliff/tests/test_app.py b/cliff/tests/test_app.py
index e3bc12e..fc1f4ae 100644
--- a/cliff/tests/test_app.py
+++ b/cliff/tests/test_app.py
@@ -106,7 +106,7 @@ def test_clean_up_error_debug():
else:
assert False, 'Should have had an exception'
- app.clean_up.assert_called_once()
+ assert app.clean_up.called
call_args = app.clean_up.call_args_list[0]
assert call_args == mock.call(mock.ANY, 1, mock.ANY)
args, kwargs = call_args
@@ -123,7 +123,7 @@ def test_error_handling_clean_up_raises_exception():
)
app.run(['error'])
- app.clean_up.assert_called_once()
+ assert app.clean_up.called
call_args = app.clean_up.call_args_list[0]
assert call_args == mock.call(mock.ANY, 1, mock.ANY)
args, kwargs = call_args
@@ -150,7 +150,7 @@ def test_error_handling_clean_up_raises_exception_debug():
else:
assert False, 'Should have had an exception'
- app.clean_up.assert_called_once()
+ assert app.clean_up.called
call_args = app.clean_up.call_args_list[0]
assert call_args == mock.call(mock.ANY, 1, mock.ANY)
args, kwargs = call_args
@@ -167,7 +167,7 @@ def test_normal_clean_up_raises_exception():
)
app.run(['mock'])
- app.clean_up.assert_called_once()
+ assert app.clean_up.called
call_args = app.clean_up.call_args_list[0]
assert call_args == mock.call(mock.ANY, 0, None)
@@ -181,7 +181,7 @@ def test_normal_clean_up_raises_exception_debug():
)
app.run(['--debug', 'mock'])
- app.clean_up.assert_called_once()
+ assert app.clean_up.called
call_args = app.clean_up.call_args_list[0]
assert call_args == mock.call(mock.ANY, 0, None)
diff --git a/cliff/tests/test_show.py b/cliff/tests/test_show.py
index 41df5e1..5db07ec 100644
--- a/cliff/tests/test_show.py
+++ b/cliff/tests/test_show.py
@@ -1,5 +1,7 @@
#!/usr/bin/env python
+import weakref
+
from cliff.show import ShowOne
import mock
@@ -9,15 +11,16 @@ class FauxFormatter(object):
def __init__(self):
self.args = []
+ self.obj = weakref.proxy(self)
- def emit_list(self, columns, data, stdout, args):
+ def emit_one(self, columns, data, stdout, args):
self.args.append((columns, data))
class ExerciseShowOne(ShowOne):
- def load_formatter_plugins(self):
- self.formatters = {
+ def _load_formatter_plugins(self):
+ return {
'test': FauxFormatter(),
}
return
@@ -29,21 +32,22 @@ class ExerciseShowOne(ShowOne):
)
-# def test_formatter_args():
-# app = mock.Mock()
-# test_lister = ExerciseLister(app, [])
+def test_formatter_args():
+ app = mock.Mock()
+ test_show = ExerciseShowOne(app, [])
+
+ parsed_args = mock.Mock()
+ parsed_args.columns = ('Col1', 'Col2')
+ parsed_args.formatter = 'test'
-# parsed_args = mock.Mock()
-# parsed_args.columns = ('Col1', 'Col2')
-# parsed_args.formatter = 'test'
+ test_show.run(parsed_args)
+ f = test_show._formatter_plugins['test']
+ assert len(f.args) == 1
+ args = f.args[0]
+ assert args[0] == list(parsed_args.columns)
+ data = list(args[1])
+ assert data == [('a', 'A'), ('b', 'B')]
-# test_lister.run(parsed_args)
-# f = test_lister.formatters['test']
-# assert len(f.args) == 1
-# args = f.args[0]
-# assert args[0] == list(parsed_args.columns)
-# data = list(args[1])
-# assert data == [['a', 'A'], ['b', 'B']]
def test_dict2columns():
app = mock.Mock()
diff --git a/docs/source/history.rst b/docs/source/history.rst
index 00da965..d1dfcf0 100644
--- a/docs/source/history.rst
+++ b/docs/source/history.rst
@@ -124,7 +124,7 @@ dev
0.7
- - Clean up interactive mode flag settting.
+ - Clean up interactive mode flag setting.
- Add support for Python 2.6, contributed by heavenshell.
- Fix multi-word commands in interactive mode.