From bf73283325a5479e8319dcf00d9f5e40915b9b90 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Wed, 24 Jun 2020 20:54:38 +0200 Subject: [PATCH 1/3] Clean RecCard --- gourmet/reccard.py | 104 ++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/gourmet/reccard.py b/gourmet/reccard.py index e269e4011..209b66172 100644 --- a/gourmet/reccard.py +++ b/gourmet/reccard.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -from typing import Callable, Optional +from typing import Callable, List, Optional import gc import webbrowser @@ -52,54 +52,53 @@ def __init__ (self, refid, title): self.item = title self.amount = 1 -# OVERARCHING RECIPE CARD CLASS - PROVIDES GLUE BETWEEN EDITING AND DISPLAY -class RecCard (object): - - def __init__ (self, rg=None, recipe=None, manual_show=False): - if rg is None: - from .GourmetRecipeManager import get_application - rg = get_application() - self.rg = rg +class RecCard: + """Overarching recipe card class. + Provides glue between editing and display. + """ + def __init__(self, + rec_gui: 'RecGui', + recipe: Optional['RowProxy'] = None, + manual_show: bool = False): + self.rec_gui = rec_gui self.rec_editor: Optional[RecEditor] = None - self.conf = [] - self.new = False - if not recipe: - recipe = self.rg.rd.new_rec() - self.new = True - self.current_rec = recipe + self.rec_display: Optional[RecCardDisplay] = None + self.conf: List = [] + self.new: bool = True if recipe is None else False + self.current_rec: 'RowProxy' = recipe if recipe else self.rec_gui.rd.new_rec() + if not manual_show: self.show() - def set_current_rec (self, rec): + @property + def current_rec(self) -> 'RowProxy': + return self.__current_rec + + @current_rec.setter + def current_rec(self, rec: 'RowProxy') -> None: self.__current_rec = rec if self.rec_editor is not None: self.rec_editor.current_rec = rec - if hasattr(self,'recipe_display'): - self.recipe_display.current_rec = rec - - def get_current_rec (self): - return self.__current_rec + if self.rec_display is not None: + self.rec_display.current_rec = rec - current_rec = property(get_current_rec, - set_current_rec, - None, - "Recipe in the recipe card") - - def get_edited(self) -> bool: + @property + def edited(self) -> bool: if self.rec_editor is not None and self.rec_editor.edited: return True return False - def set_edited(self, val) -> None: + @edited.setter + def edited(self, val: bool) -> None: if self.rec_editor is not None and self.rec_editor.edited: - self.rec_editor.edited = bool(val) - edited = property(get_edited,set_edited) + self.rec_editor.edited = val - def show_display (self): - if not hasattr(self,'recipe_display'): - self.recipe_display = RecCardDisplay(self, self.rg,self.current_rec) - self.recipe_display.window.present() + def show_display(self) -> None: + if self.rec_display is None: + self.rec_display = RecCardDisplay(self, self.rec_gui, + self.current_rec) + self.rec_display.window.present() def show_edit(self, module: Optional[str] = None) -> None: """Draw the recipe editor window. @@ -107,36 +106,37 @@ def show_edit(self, module: Optional[str] = None) -> None: `module` is the string definition of one of the RecEditor's tabs, as defined in RecEditor.module_tab_by_name.""" if self.rec_editor is None: - self.rec_editor = RecEditor(self, self.rg, + self.rec_editor = RecEditor(self, self.rec_gui, self.current_rec, new=self.new) if module: self.rec_editor.show_module(module) self.rec_editor.present() + def show(self) -> None: + if self.new: + self.show_edit() + else: + self.show_display() - def delete (self, *args): - self.rg.rec_tree_delete_recs([self.current_rec]) + def delete(self, *args) -> None: + self.rec_gui.rec_tree_delete_recs([self.current_rec]) - def update_recipe (self, recipe): + def update_recipe(self, recipe: 'RowProxy') -> None: self.current_rec = recipe - if hasattr(self,'recipe_display'): - self.recipe_display.update_from_database() + if self.rec_display is not None: + self.rec_display.update_from_database() + if (self.rec_editor is not None and not self.rec_editor.window.is_visible()): self.rec_editor = None - def show (self): - if self.new: - self.show_edit() - else: - self.show_display() - - def hide (self): - if ((not (hasattr(self,'recipe_display') and self.recipe_display.window.get_property('visible'))) - and - (self.rec_editor is not None and - not self.rec_editor.window.is_visible())): - self.rg.del_rc(self.current_rec.id) + def hide(self) -> None: + rec_displayed = (self.rec_display is not None and + self.rec_display.window.is_visible()) + rec_editor_displayed = (self.rec_editor is not None and + not self.rec_editor.window.is_visible()) + if rec_displayed and rec_editor_displayed: + self.rec_gui.del_rc(self.current_rec.id) # end RecCard From 9e8653624cf5aed06679dc99a472e99096bd92b8 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Wed, 24 Jun 2020 23:47:57 +0200 Subject: [PATCH 2/3] RecCard: add dunder to properties --- gourmet/reccard.py | 71 +++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/gourmet/reccard.py b/gourmet/reccard.py index 209b66172..a4b457031 100644 --- a/gourmet/reccard.py +++ b/gourmet/reccard.py @@ -61,12 +61,11 @@ def __init__(self, rec_gui: 'RecGui', recipe: Optional['RowProxy'] = None, manual_show: bool = False): - self.rec_gui = rec_gui - self.rec_editor: Optional[RecEditor] = None - self.rec_display: Optional[RecCardDisplay] = None - self.conf: List = [] - self.new: bool = True if recipe is None else False - self.current_rec: 'RowProxy' = recipe if recipe else self.rec_gui.rd.new_rec() + self.__rec_gui = rec_gui + self.__rec_editor: Optional[RecEditor] = None + self.__rec_display: Optional[RecCardDisplay] = None + self.__new: bool = True if recipe is None else False + self.__current_rec: 'RowProxy' = recipe if recipe else rec_gui.rd.new_rec() if not manual_show: self.show() @@ -78,65 +77,65 @@ def current_rec(self) -> 'RowProxy': @current_rec.setter def current_rec(self, rec: 'RowProxy') -> None: self.__current_rec = rec - if self.rec_editor is not None: - self.rec_editor.current_rec = rec - if self.rec_display is not None: - self.rec_display.current_rec = rec + if self.__rec_editor is not None: + self.__rec_editor.current_rec = rec + if self.__rec_display is not None: + self.__rec_display.current_rec = rec @property def edited(self) -> bool: - if self.rec_editor is not None and self.rec_editor.edited: + if self.__rec_editor is not None and self.__rec_editor.edited: return True return False @edited.setter def edited(self, val: bool) -> None: - if self.rec_editor is not None and self.rec_editor.edited: - self.rec_editor.edited = val + if self.__rec_editor is not None and self.__rec_editor.edited: + self.__rec_editor.edited = val def show_display(self) -> None: - if self.rec_display is None: - self.rec_display = RecCardDisplay(self, self.rec_gui, - self.current_rec) - self.rec_display.window.present() + if self.__rec_display is None: + self.__rec_display = RecCardDisplay(self, self.__rec_gui, + self.current_rec) + self.__rec_display.window.present() def show_edit(self, module: Optional[str] = None) -> None: """Draw the recipe editor window. `module` is the string definition of one of the RecEditor's tabs, as defined in RecEditor.module_tab_by_name.""" - if self.rec_editor is None: - self.rec_editor = RecEditor(self, self.rec_gui, - self.current_rec, new=self.new) + if self.__rec_editor is None: + self.__rec_editor = RecEditor(self, self.__rec_gui, + self.current_rec, new=self.__new) if module: - self.rec_editor.show_module(module) - self.rec_editor.present() + self.__rec_editor.show_module(module) + self.__rec_editor.present() def show(self) -> None: - if self.new: + if self.__new: self.show_edit() else: self.show_display() def delete(self, *args) -> None: - self.rec_gui.rec_tree_delete_recs([self.current_rec]) + self.__rec_gui.rec_tree_delete_recs([self.current_rec]) def update_recipe(self, recipe: 'RowProxy') -> None: self.current_rec = recipe - if self.rec_display is not None: - self.rec_display.update_from_database() + if self.__rec_display is not None: + self.__rec_display.update_from_database() - if (self.rec_editor is not None and - not self.rec_editor.window.is_visible()): - self.rec_editor = None + if (self.__rec_editor is not None and + not self.__rec_editor.window.is_visible()): + self.__rec_editor = None def hide(self) -> None: - rec_displayed = (self.rec_display is not None and - self.rec_display.window.is_visible()) - rec_editor_displayed = (self.rec_editor is not None and - not self.rec_editor.window.is_visible()) + rec_displayed = (self.__rec_display is not None and + self.__rec_display.window.is_visible()) + rec_editor_displayed = (self.__rec_editor is not None and + not self.__rec_editor.window.is_visible()) if rec_displayed and rec_editor_displayed: - self.rec_gui.del_rc(self.current_rec.id) + self.__rec_gui.del_rc(self.current_rec.id) # end RecCard @@ -181,7 +180,7 @@ class RecCardDisplay (plugin_loader.Pluggable): def __init__ (self, reccard, recGui, recipe=None): self.reccard = reccard; self.rg = recGui; self.current_rec = recipe self.mult = 1 # parameter - self.conf = reccard.conf + self.conf: List[Gtk.Widget] = [] self.prefs = prefs.get_prefs() self.setup_ui() self.setup_uimanager() @@ -830,7 +829,7 @@ def __init__ (self, reccard, rg, recipe=None, recipe_display=None, new=False): recipe = self.recipe_display.current_rec self.current_rec = recipe self.setup_defaults() - self.conf = reccard.conf + self.conf: List[Gtk.Widget] = [] self.setup_ui_manager() #self.setup_undo() self.setup_main_interface() From a88995ba81503cfe2e07bfd31e1ecbe89e054435 Mon Sep 17 00:00:00 2001 From: Cyril Danilevski Date: Thu, 25 Jun 2020 00:47:02 +0200 Subject: [PATCH 3/3] Add RecCard.conf back A more in-depth refactoring is needed to remove it --- gourmet/reccard.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gourmet/reccard.py b/gourmet/reccard.py index a4b457031..88bb26d61 100644 --- a/gourmet/reccard.py +++ b/gourmet/reccard.py @@ -67,6 +67,8 @@ def __init__(self, self.__new: bool = True if recipe is None else False self.__current_rec: 'RowProxy' = recipe if recipe else rec_gui.rd.new_rec() + self.conf = [] # This list is unused, and should be refactored out + if not manual_show: self.show()