From 620d43a544ee9c22d09d8e0e1dbf03468bf87c8f Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Thu, 14 Jul 2016 21:43:10 +0100 Subject: [PATCH] Convert gui.views to use abstract base classes --- gramps/gui/views/bookmarks.py | 11 +++---- gramps/gui/views/listview.py | 18 ++++-------- gramps/gui/views/navigationview.py | 38 +++--------------------- gramps/gui/views/pageview.py | 43 ++++------------------------ gramps/plugins/lib/libpersonview.py | 6 ++++ gramps/plugins/view/dashboardview.py | 6 ++++ gramps/plugins/view/familyview.py | 6 ++++ 7 files changed, 39 insertions(+), 89 deletions(-) diff --git a/gramps/gui/views/bookmarks.py b/gramps/gui/views/bookmarks.py index 9521192e8..c93181fea 100644 --- a/gramps/gui/views/bookmarks.py +++ b/gramps/gui/views/bookmarks.py @@ -26,6 +26,7 @@ # Standard python modules # #------------------------------------------------------------------------- +from abc import ABCMeta, abstractmethod from io import StringIO #------------------------------------------------------------------------- @@ -74,7 +75,7 @@ BTM = '''''' DISABLED = -1 -class Bookmarks: +class Bookmarks(metaclass=ABCMeta): "Handle the bookmarks interface for Gramps." def __init__(self, dbstate, uistate, callback=None): @@ -113,17 +114,17 @@ class Bookmarks: self.connect_signals() self.update_bookmarks() + @abstractmethod def connect_signals(self): """ Connect the person-delete signal """ - raise NotImplementedError + @abstractmethod def get_bookmarks(self): """ Retrieve bookmarks from the database. """ - raise NotImplementedError def update_bookmarks(self): """ @@ -184,20 +185,20 @@ class Bookmarks: self.uistate.uimanager.ensure_update() text.close() + @abstractmethod def make_label(self, handle): """ Returns a (label, object) tuple appropriate to the type of the object that the handle refers to. The label is a text for the object, e.g. the object name. """ - raise NotImplementedError + @abstractmethod def callback(self, handle): """ Returns a unique call to a function with the associated handle. The function that will be called is defined in the derived class """ - raise NotImplementedError def add(self, person_handle): """Append the person to the bottom of the bookmarks.""" diff --git a/gramps/gui/views/listview.py b/gramps/gui/views/listview.py index e58dc61d5..e5280aad8 100644 --- a/gramps/gui/views/listview.py +++ b/gramps/gui/views/listview.py @@ -29,6 +29,7 @@ Provide the base classes for GRAMPS' DataView classes # python modules # #---------------------------------------------------------------- +from abc import abstractmethod import pickle import time import logging @@ -1095,42 +1096,35 @@ class ListView(NavigationView): #################################################################### # Template functions #################################################################### - def get_bookmarks(self): - """ - Template function to get bookmarks. - We could implement this in the NavigationView - """ - raise NotImplementedError - + @abstractmethod def edit(self, obj, data=None): """ Template function to allow the editing of the selected object """ - raise NotImplementedError + @abstractmethod def remove(self, handle, data=None): """ Template function to allow the removal of an object by its handle """ - raise NotImplementedError + @abstractmethod def add(self, obj, data=None): """ Template function to allow the adding of a new object """ - raise NotImplementedError + @abstractmethod def merge(self, obj, data=None): """ Template function to allow the merger of two objects. """ - raise NotImplementedError + @abstractmethod def remove_object_from_handle(self, handle): """ Template function to allow the removal of an object by its handle """ - raise NotImplementedError def open_all_nodes(self, obj): """ diff --git a/gramps/gui/views/navigationview.py b/gramps/gui/views/navigationview.py index 1c6ff5080..38b54e637 100644 --- a/gramps/gui/views/navigationview.py +++ b/gramps/gui/views/navigationview.py @@ -28,6 +28,7 @@ Provide the base classes for GRAMPS' DataView classes # python modules # #---------------------------------------------------------------- +from abc import abstractmethod import logging _LOG = logging.getLogger('.navigationview') @@ -216,12 +217,12 @@ class NavigationView(PageView): if handle and not hobj.lock and not (handle == hobj.present()): hobj.push(handle) + @abstractmethod def goto_handle(self, handle): """ Needs to be implemented by classes derived from this. Used to move to the given handle. """ - raise NotImplementedError def selected_handles(self): """ @@ -457,50 +458,19 @@ class NavigationView(PageView): #################################################################### # Template functions #################################################################### - def get_bookmarks(self): - """ - Template function to get bookmarks. - We could implement this here based on navigation_type() - """ - raise NotImplementedError - - def edit(self, obj): - """ - Template function to allow the editing of the selected object - """ - raise NotImplementedError - - def remove(self, handle): - """ - Template function to allow the removal of an object by its handle - """ - raise NotImplementedError - - def add(self, obj): - """ - Template function to allow the adding of a new object - """ - raise NotImplementedError - - def remove_object_from_handle(self, handle): - """ - Template function to allow the removal of an object by its handle - """ - raise NotImplementedError - + @abstractmethod def build_tree(self): """ Rebuilds the current display. This must be overridden by the derived class. """ - raise NotImplementedError + @abstractmethod def build_widget(self): """ Builds the container widget for the interface. Must be overridden by the the base class. Returns a gtk container widget. """ - raise NotImplementedError def key_press_handler(self, widget, event): """ diff --git a/gramps/gui/views/pageview.py b/gramps/gui/views/pageview.py index 84694cdcb..beba84c03 100644 --- a/gramps/gui/views/pageview.py +++ b/gramps/gui/views/pageview.py @@ -27,6 +27,7 @@ Provide the base class for GRAMPS' DataView classes # python modules # #---------------------------------------------------------------- +from abc import ABCMeta, abstractmethod import logging _LOG = logging.getLogger('.pageview') @@ -57,7 +58,7 @@ from ..actiongroup import ActionGroup # PageView # #------------------------------------------------------------------------------ -class PageView(DbGUIElement): +class PageView(DbGUIElement, metaclass=ABCMeta): """ The PageView class is the base class for all Data Views in GRAMPS. All Views should derive from this class. The ViewManager understands the public @@ -312,12 +313,12 @@ class PageView(DbGUIElement): self.bottombar.set_inactive() self.active = False + @abstractmethod def build_tree(self): """ Rebuilds the current display. This must be overridden by the derived class. """ - raise NotImplementedError def ui_definition(self): """ @@ -389,12 +390,12 @@ class PageView(DbGUIElement): self.top = self.build_interface() return self.top + @abstractmethod def build_widget(self): """ Builds the container widget for the main view pane. Must be overridden by the base class. Returns a gtk container widget. """ - raise NotImplementedError def define_actions(self): """ @@ -473,40 +474,6 @@ class PageView(DbGUIElement): """ self.uistate.clear_filter_results() - def edit(self, obj): - """ - Template function to allow the editing of the selected object - """ - raise NotImplementedError - - def remove(self, handle): - """ - Template function to allow the removal of an object by its handle - """ - raise NotImplementedError - - def remove_object_from_handle(self, handle): - """ - Template function to allow the removal of an object by its handle - """ - raise NotImplementedError - - def add(self, obj): - """ - Template function to allow the adding of a new object - """ - raise NotImplementedError - - def _key_press(self, obj, event): - """ - Define the action for a key press event - """ - # TODO: This is never used? (replaced in ListView) - if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter): - self.edit(obj) - return True - return False - def on_delete(self): """ Method called on shutdown. Data views should put code here @@ -569,7 +536,7 @@ class PageView(DbGUIElement): :return: list of functions """ - raise NotImplementedError + return [] def configure(self): """ diff --git a/gramps/plugins/lib/libpersonview.py b/gramps/plugins/lib/libpersonview.py index 751d8b65f..ad6fbc174 100644 --- a/gramps/plugins/lib/libpersonview.py +++ b/gramps/plugins/lib/libpersonview.py @@ -346,6 +346,12 @@ class BasePersonView(ListView): self.uistate.set_busy_cursor(False) + def remove_object_from_handle(self, handle): + """ + The remove_selected_objects method is not called in this view. + """ + pass + def define_actions(self): """ Required define_actions function for PageView. Builds the action diff --git a/gramps/plugins/view/dashboardview.py b/gramps/plugins/view/dashboardview.py index 8f4272d17..2bd9a2382 100644 --- a/gramps/plugins/view/dashboardview.py +++ b/gramps/plugins/view/dashboardview.py @@ -75,6 +75,12 @@ class DashboardView(PageView): self.dbstate, self.uistate) return self.widget + def build_tree(self): + """ + Rebuilds the current display. + """ + pass + def get_stock(self): """ Return image associated with the view, which is used for the diff --git a/gramps/plugins/view/familyview.py b/gramps/plugins/view/familyview.py index b9e3c54fb..ebc5ccf75 100644 --- a/gramps/plugins/view/familyview.py +++ b/gramps/plugins/view/familyview.py @@ -281,6 +281,12 @@ class FamilyView(ListView): trans.set_description(_("Family [%s]") % gramps_id) self.uistate.set_busy_cursor(False) + def remove_object_from_handle(self, handle): + """ + The remove_selected_objects method is not called in this view. + """ + pass + def edit(self, obj): for handle in self.selected_handles(): family = self.dbstate.db.get_family_from_handle(handle)