Navigation Menu

Skip to content

Commit

Permalink
Remove NotImplementedErrors from "virtual" methods
Browse files Browse the repository at this point in the history
Raising a NotImplementedError in a method of a base class
makes it impossible to use super() with it and do multiple
inheritance properly. Those methods should instead simply
return some default "empty" value, so that the methods in
the subclasses can still call them, if that's where they
fall in the MRO.

Change-Id: I581391e87dc58f0f76adea3dbf90cef173d3daef
Closes-bug: 1278825
  • Loading branch information
deshipu committed Sep 2, 2014
1 parent 6be6755 commit 0f2bf80
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 31 deletions.
4 changes: 1 addition & 3 deletions horizon/base.py
Expand Up @@ -594,9 +594,7 @@ def can_access(self, context):


class Workflow(object):
def __init__(*args, **kwargs):
raise NotImplementedError()

pass

try:
from django.utils.functional import empty # noqa
Expand Down
15 changes: 3 additions & 12 deletions horizon/tables/actions.py
Expand Up @@ -503,8 +503,7 @@ def filter(self, table, data, filter_string):
This method must be overridden by subclasses and return
the filtered data.
"""
raise NotImplementedError("The filter method has not been "
"implemented by %s." % self.__class__)
return data

def is_api_filter(self, filter_field):
"""Determine if the given filter field should be used as an
Expand Down Expand Up @@ -550,18 +549,15 @@ def get_fixed_buttons(self):
* ``value``: Value returned when the button is clicked. This value is
passed to ``filter()`` as ``filter_string``.
"""
raise NotImplementedError("The get_fixed_buttons method has "
"not been implemented by %s." %
self.__class__)
return []

def categorize(self, table, images):
"""Override to separate images into categories.
Return a dict with a key for the value of each fixed button,
and a value that is a list of images in that category.
"""
raise NotImplementedError("The categorize method has not been "
"implemented by %s." % self.__class__)
return {}


class BatchAction(Action):
Expand Down Expand Up @@ -670,8 +666,6 @@ def action(self, request, datum_id):
Return values are discarded, errors raised are caught and logged.
"""
raise NotImplementedError('action() must be defined for %s'
% self.__class__.__name__)

def update(self, request, datum):
"""Switches the action verbose name, if needed."""
Expand Down Expand Up @@ -792,7 +786,6 @@ def delete(self, request, obj_id):
Override to provide delete functionality specific to your data.
"""
raise NotImplementedError("DeleteAction must define a delete method.")

def get_default_classes(self):
"""Appends ``btn-danger`` to the action's default css classes.
Expand Down Expand Up @@ -821,8 +814,6 @@ def update_cell(self, request, datum, obj_id, cell_name, new_cell_value):
This method must implements saving logic of the inline edited table
cell.
"""
raise NotImplementedError(
"UpdateAction must define a update_cell method.")

def allowed(self, request, datum, cell):
"""Determine whether updating is allowed for the current request.
Expand Down
3 changes: 1 addition & 2 deletions horizon/tables/base.py
Expand Up @@ -592,8 +592,7 @@ def get_data(self, request, obj_id):
"""Fetches the updated data for the row based on the object id
passed in. Must be implemented by a subclass to allow AJAX updating.
"""
raise NotImplementedError("You must define a get_data method on %s"
% self.__class__.__name__)
return {}


class Cell(html.HTMLElement):
Expand Down
3 changes: 1 addition & 2 deletions horizon/tables/views.py
Expand Up @@ -186,8 +186,7 @@ def _get_data_dict(self):
return self._data

def get_data(self):
raise NotImplementedError('You must define a "get_data" method on %s.'
% self.__class__.__name__)
return []

def get_tables(self):
if not self._tables:
Expand Down
9 changes: 4 additions & 5 deletions horizon/tabs/base.py
Expand Up @@ -355,12 +355,11 @@ def get_template_name(self, request):
% self.__class__.__name__)
return self.template_name

def get_context_data(self, request):
def get_context_data(self, request, **kwargs):
"""This method should return a dictionary of context data used to
render the tab. Required.
"""
raise NotImplementedError("%s needs to define a get_context_data "
"method." % self.__class__.__name__)
return kwargs

def enabled(self, request):
"""Determines whether or not the tab should be accessible
Expand Down Expand Up @@ -447,14 +446,14 @@ def load_table_data(self):
# Mark our data as loaded so we don't run the loaders again.
self._table_data_loaded = True

def get_context_data(self, request):
def get_context_data(self, request, **kwargs):
"""Adds a ``{{ table_name }}_table`` item to the context for each table
in the :attr:`~horizon.tabs.TableTab.table_classes` attribute.
If only one table class is provided, a shortcut ``table`` context
variable is also added containing the single table.
"""
context = {}
context = super(TableTab, self).get_context_data(request, **kwargs)
# If the data hasn't been manually loaded before now,
# make certain it's loaded before setting the context.
self.load_table_data()
Expand Down
7 changes: 3 additions & 4 deletions horizon/utils/csvbase.py
Expand Up @@ -94,8 +94,8 @@ def __init__(self, request, template, context, content_type, **kwargs):
self.out.close()

def get_row_data(self):
raise NotImplementedError("You must define a get_row_data method on %s"
% self.__class__.__name__)
return []


if VERSION >= (1, 5, 0):

Expand Down Expand Up @@ -140,5 +140,4 @@ def get_content(self):
yield self.buffer()

def get_row_data(self):
raise NotImplementedError("You must define a get_row_data method "
"on %s" % self.__class__.__name__)
return []
3 changes: 1 addition & 2 deletions horizon/views.py
Expand Up @@ -38,8 +38,7 @@ def get_data(self, request, context, *args, **kwargs):
"""This method should handle any necessary API calls, update the
context object, and return the context object at the end.
"""
raise NotImplementedError("You must define a get_data method "
"on %s" % self.__class__.__name__)
return context

def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
Expand Down
2 changes: 1 addition & 1 deletion openstack_dashboard/usage/base.py
Expand Up @@ -191,7 +191,7 @@ def get_limits(self):
self.get_cinder_limits()

def get_usage_list(self, start, end):
raise NotImplementedError("You must define a get_usage_list method.")
return []

def summarize(self, start, end):
if not api.nova.extension_supported('SimpleTenantUsage', self.request):
Expand Down

0 comments on commit 0f2bf80

Please sign in to comment.