From d4d61cf452df12c340f83d608446687f0f81b8d8 Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Sun, 16 Mar 2014 21:22:43 +0000 Subject: [PATCH] Simplify callback type checking. Signed-off-by: brian m. carlson --- lib/newfol/exception.py | 3 +++ newfol | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/newfol/exception.py b/lib/newfol/exception.py index d88ab59..31ad559 100644 --- a/lib/newfol/exception.py +++ b/lib/newfol/exception.py @@ -4,6 +4,9 @@ class NewfolError(Exception): class LockError(NewfolError): pass +class ImplementationError(NewfolError): + pass + class FilemanipError(Exception): pass diff --git a/newfol b/newfol index b76ea8b..2abd3d8 100755 --- a/newfol +++ b/newfol @@ -226,6 +226,17 @@ class View: @staticmethod def text(text, *classes): return urwid.AttrMap(urwid.Text(text), *classes) + @staticmethod + def is_callback_type(type_, category): + if category == "select": + return type_.startswith("select") or type_.startswith("next") + elif category == "simple-select": + return type_ in ("select", "next") + elif category in ("change", "cancel"): + return type_ == category + else: + raise newfol.exception.ImplementationError("Unknown type %s" % + type_) class IntroView(View): def render(self, loop): @@ -436,7 +447,7 @@ class DisplayRecordView(RecordView): continue self.cells[i+1].widget_list[1].set_edit_text(str(val)) def callback(self, type_, obj): - if type_ in ("select", "next"): + if self.is_callback_type(type_, "simple-select"): dbd = DatabaseData() newrec = self._build_record() recs = [] @@ -474,7 +485,7 @@ class SearchRecordView(RecordView): return [i.widget_list[1].get_edit_text() for i in self.cells] def callback(self, type_, obj): dbd = DatabaseData() - if type_ in ("select", "next"): + if self.is_callback_type(type_, "select"): fields = self._get_fields() fieldres = [] for f in fields: @@ -521,7 +532,7 @@ class DisplayTemplateRecordView(RecordView): def _get_fields(self): return [i.widget_list[1].get_state() for i in self.cells] def callback(self, type_, obj): - if type_.startswith("select") or type_.startswith("next"): + if self.is_callback_type(type_, "select"): dbd = DatabaseData() selected = self._get_fields() view = SortingTemplateRecordView(self.table, self.records, @@ -584,7 +595,7 @@ class SortingTemplateRecordView(RecordView): l[0] = True return l def callback(self, type_, obj): - if type_.startswith("select") or type_.startswith("next"): + if self.is_callback_type(type_, "select"): dbd = DatabaseData() fieldno = self._get_fields().index(True) def lookup(x): @@ -743,7 +754,7 @@ class RecordListView(ListView): if type_ == "remove": self._toggle_selected_record() self._full_rerender = True - if type_ != "select" and not type_.startswith("next"): + if not self.is_callback_type(type_, "select"): return cur = self._current_item() if cur is None: @@ -764,7 +775,7 @@ class TableContentsListView(RecordListView): class TableListView(ListView): def callback(self, type_, obj): - if type_ != "select" and not type_.startswith("next"): + if not self.is_callback_type(type_, "select"): return if obj is None: text = self._current_item() @@ -825,7 +836,7 @@ class MessageBox(View): else: self.message = message def callback(self, type_, obj): - if type_ == "select": + if self.is_callback_type(type_, "select"): render_previous_view(self.loop) def _render_buttons(self): def callback_glue(button): @@ -858,12 +869,12 @@ class MenuView(ListView): ("About newfol", "about") ]) def callback(self, type_, obj): - if type_ != "select" and not type_.startswith("next"): + if not self.is_callback_type(type_, "select"): return text = self.listbox.get_focus()[0].original_widget.get_text()[0] command = self.OPTIONS[text] if command == "browse-all": - if type_ == "select" or type_.startswith("next"): + if self.is_callback_type(type_, "select"): ccview = CompleteContentsListView() ccview.set_render_type(type_) ccview.render(self.loop)