From 2e3a4fd55fe5cce54a5ba622e93235672e370d06 Mon Sep 17 00:00:00 2001 From: Paul Franklin Date: Sat, 6 Aug 2016 16:13:30 -0700 Subject: [PATCH] increase pylint score --- gramps/gui/viewmanager.py | 201 +++++++++++++++++++++----------------- 1 file changed, 109 insertions(+), 92 deletions(-) diff --git a/gramps/gui/viewmanager.py b/gramps/gui/viewmanager.py index 6a9316dbc..1318a72cc 100644 --- a/gramps/gui/viewmanager.py +++ b/gramps/gui/viewmanager.py @@ -65,7 +65,7 @@ _ = glocale.translation.sgettext from gramps.cli.grampscli import CLIManager from .user import User from .plug import tool -from gramps.gen.plug import (START, END) +from gramps.gen.plug import START from gramps.gen.plug import REPORT from gramps.gen.plug.report._constants import standalone_categories from .plug import (PluginWindows, ReportPluginDialog, ToolPluginDialog) @@ -87,7 +87,6 @@ from gramps.gen.utils.file import media_path_full from .dbloader import DbLoader from .display import display_help, display_url from .configure import GrampsPreferences -from gramps.gen.db.exceptions import DbException from .aboutdialog import GrampsAboutDialog from .navigator import Navigator from .views.tags import Tags @@ -103,7 +102,7 @@ if is_quartz(): from gi.repository import GtkosxApplication as QuartzApp _GTKOSXAPPLICATION = True except: - print ("Failed to import gtk_osxapplication") + print("Failed to import gtk_osxapplication") _GTKOSXAPPLICATION = False else: _GTKOSXAPPLICATION = False @@ -264,7 +263,7 @@ class ViewManager(CLIManager): """ - def __init__(self, dbstate, view_category_order, user = None): + def __init__(self, dbstate, view_category_order, user=None): """ The viewmanager is initialised with a dbstate on which GRAMPS is working, and a fixed view_category_order, which is the order in which @@ -320,9 +319,9 @@ class ViewManager(CLIManager): howoften = config.get("behavior.check-for-updates") update = False if howoften != 0: # update never if zero - y,m,d = list(map(int, - config.get("behavior.last-check-for-updates").split("/"))) - days = (datetime.date.today() - datetime.date(y, m, d)).days + year, mon, day = list(map( + int, config.get("behavior.last-check-for-updates").split("/"))) + days = (datetime.date.today() - datetime.date(year, mon, day)).days if howoften == 1 and days >= 30: # once a month update = True elif howoften == 2 and days >= 7: # once a week @@ -524,16 +523,26 @@ class ViewManager(CLIManager): ('F8', None, 'F9', "F8", None, self.__keypress), ('F9', None, 'F9', "F9", None, self.__keypress), ('F11', None, 'F11', "F11", None, self.__keypress), - ('1', None, '1', "1", None, self.__gocat), - ('2', None, '2', "2", None, self.__gocat), - ('3', None, '3', "3", None, self.__gocat), - ('4', None, '4', "4", None, self.__gocat), - ('5', None, '5', "5", None, self.__gocat), - ('6', None, '6', "6", None, self.__gocat), - ('7', None, '7', "7", None, self.__gocat), - ('8', None, '8', "8", None, self.__gocat), - ('9', None, '9', "9", None, self.__gocat), - ('0', None, '0', "0", None, self.__gocat), + ('1', None, '1', "1", None, + self.__gocat), + ('2', None, '2', "2", None, + self.__gocat), + ('3', None, '3', "3", None, + self.__gocat), + ('4', None, '4', "4", None, + self.__gocat), + ('5', None, '5', "5", None, + self.__gocat), + ('6', None, '6', "6", None, + self.__gocat), + ('7', None, '7', "7", None, + self.__gocat), + ('8', None, '8', "8", None, + self.__gocat), + ('9', None, '9', "9", None, + self.__gocat), + ('0', None, '0', "0", None, + self.__gocat), # NOTE: CTRL+ALT+NUMBER is set in src/plugins/sidebar/cat...py ('BackSpace', None, 'BackSpace', "BackSpace", None, self.__keypress), @@ -566,9 +575,9 @@ class ViewManager(CLIManager): self._file_toggle_action_list = [ ('Navigator', None, _('_Navigator'), "m", None, - self.navigator_toggle, self.show_navigator ), + self.navigator_toggle, self.show_navigator), ('Toolbar', None, _('_Toolbar'), None, None, self.toolbar_toggle, - self.show_toolbar ), + self.show_toolbar), ('Fullscreen', None, _('F_ull Screen'), "F11", None, self.fullscreen_toggle, self.fullscreen), ] @@ -626,8 +635,8 @@ class ViewManager(CLIManager): def __next_view(self, action): """ - Callback that is called when the next category action is selected. - It selects the next category as the active category. If we reach the end, + Callback that is called when the next category action is selected. It + selects the next category as the active category. If we reach the end, we wrap around to the first. """ curpage = self.notebook.get_current_page() @@ -645,8 +654,8 @@ class ViewManager(CLIManager): def __prev_view(self, action): """ Callback that is called when the previous category action is selected. - It selects the previous category as the active category. If we reach the - beginning of the list, we wrap around to the last. + It selects the previous category as the active category. If we reach + the beginning of the list, we wrap around to the last. """ curpage = self.notebook.get_current_page() #find cat and view of the current page @@ -677,7 +686,7 @@ class ViewManager(CLIManager): self.__build_tools_menu(self._pmgr.get_reg_tools()) self.__build_report_menu(self._pmgr.get_reg_reports()) self._pmgr.connect('plugins-reloaded', - self.__rebuild_report_and_tool_menus) + self.__rebuild_report_and_tool_menus) self.fileactions.set_sensitive(True) self.uistate.widget.set_sensitive(True) if not self.file_loaded: @@ -832,12 +841,16 @@ class ViewManager(CLIManager): self.uimanager.ensure_update() def __attach_menubar(self, vbox): + """ + Attach the menubar + """ vbox.pack_start(self.menubar, False, True, 0) if _GTKOSXAPPLICATION: self.menubar.hide() quit_item = self.uimanager.get_widget("/MenuBar/FileMenu/Quit") about_item = self.uimanager.get_widget("/MenuBar/HelpMenu/About") - prefs_item = self.uimanager.get_widget("/MenuBar/EditMenu/Preferences") + prefs_item = self.uimanager.get_widget( + "/MenuBar/EditMenu/Preferences") self.macapp.set_menu_bar(self.menubar) self.macapp.insert_app_menu_item(about_item, 0) self.macapp.insert_app_menu_item(prefs_item, 1) @@ -943,9 +956,10 @@ class ViewManager(CLIManager): return None def __create_dummy_page(self, pdata, error): + """ Create a dummy page """ from .views.pageview import DummyPage return DummyPage(pdata.name, pdata, self.dbstate, self.uistate, - _("View failed to load. Check error output."), error) + _("View failed to load. Check error output."), error) def __create_page(self, pdata, page_def): """ @@ -955,7 +969,7 @@ class ViewManager(CLIManager): page = page_def(pdata, self.dbstate, self.uistate) except: import traceback - LOG.warning("View '%s' failed to load." % pdata.id) + LOG.warning("View '%s' failed to load.", pdata.id) traceback.print_exc() page = self.__create_dummy_page(pdata, traceback.format_exc()) @@ -1122,12 +1136,13 @@ class ViewManager(CLIManager): delim = old_title.find(' - ') tit1 = old_title[:delim] tit2 = old_title[delim:] + new_title = dialog.after_change if '<=' in tit2: - delim2 = tit2.find('<=') + 3 - tit3 = tit2[delim2:-1] - new_title = dialog.after_change + tit2.replace(']', '') + ' => ' + tit1 + ']' + ## delim2 = tit2.find('<=') + 3 + ## tit3 = tit2[delim2:-1] + new_title += tit2.replace(']', '') + ' => ' + tit1 + ']' else: - new_title = dialog.after_change + tit2 + ' <= [' + tit1 + ']' + new_title += tit2 + ' <= [' + tit1 + ']' self.uistate.window.set_title(new_title) def __post_load(self): @@ -1163,7 +1178,7 @@ class ViewManager(CLIManager): name = title if self.dbstate.db.readonly: - msg = "%s (%s) - Gramps" % (name, _('Read Only')) + msg = "%s (%s) - Gramps" % (name, _('Read Only')) self.uistate.window.set_title(msg) self.actiongroup.set_sensitive(False) else: @@ -1292,7 +1307,8 @@ class ViewManager(CLIManager): file_entry = Gtk.Entry() button = Gtk.Button() button.connect("clicked", - lambda widget: self.select_backup_path(widget, path_entry)) + lambda widget: + self.select_backup_path(widget, path_entry)) image = Gtk.Image() image.set_from_icon_name('document-open', Gtk.IconSize.BUTTON) image.show() @@ -1306,30 +1322,30 @@ class ViewManager(CLIManager): label.set_halign(Gtk.Align.START) hbox.pack_start(label, False, True, 0) struct_time = time.localtime() - file_entry.set_text(config.get('paths.quick-backup-filename') % - {"filename": self.dbstate.db.get_dbname(), - "year": struct_time.tm_year, - "month": struct_time.tm_mon, - "day": struct_time.tm_mday, - "hour": struct_time.tm_hour, - "minutes": struct_time.tm_min, - "seconds": struct_time.tm_sec, - "extension": "gpkg", - }) + file_entry.set_text( + config.get('paths.quick-backup-filename' + ) % {"filename": self.dbstate.db.get_dbname(), + "year": struct_time.tm_year, + "month": struct_time.tm_mon, + "day": struct_time.tm_mday, + "hour": struct_time.tm_hour, + "minutes": struct_time.tm_min, + "seconds": struct_time.tm_sec, + "extension": "gpkg"}) hbox.pack_end(file_entry, True, True, 0) vbox.pack_start(hbox, False, True, 0) hbox = Gtk.Box() - bytes = 0 + fbytes = 0 mbytes = "0" for media in self.dbstate.db.iter_media(): fullname = media_path_full(self.dbstate.db, media.get_path()) try: - bytes += posixpath.getsize(fullname) - length = len(str(bytes)) - if bytes <= 999999: + fbytes += posixpath.getsize(fullname) + length = len(str(fbytes)) + if fbytes <= 999999: mbytes = "< 1" else: - mbytes = str(bytes)[:(length-6)] + mbytes = str(fbytes)[:(length-6)] except OSError: pass label = Gtk.Label(label=_("Media:")) @@ -1340,26 +1356,28 @@ class ViewManager(CLIManager): include = Gtk.RadioButton.new_with_mnemonic_from_widget( None, "%s (%s %s)" % (_("Include"), mbytes, _("Megabyte|MB"))) - exclude = Gtk.RadioButton.new_with_mnemonic_from_widget(include, _("Exclude")) - include.connect("toggled", lambda widget: self.media_toggle(widget, file_entry)) + exclude = Gtk.RadioButton.new_with_mnemonic_from_widget(include, + _("Exclude")) + include.connect("toggled", lambda widget: self.media_toggle(widget, + file_entry)) hbox.pack_start(include, False, True, 0) hbox.pack_end(exclude, False, True, 0) vbox.pack_start(hbox, False, True, 0) window.show_all() - d = window.run() + dbackup = window.run() window.hide() - if d == Gtk.ResponseType.APPLY: + if dbackup == Gtk.ResponseType.APPLY: # if file exists, ask if overwrite; else abort basefile = file_entry.get_text() basefile = basefile.replace("/", r"-") filename = os.path.join(path_entry.get_text(), basefile) if os.path.exists(filename): question = QuestionDialog2( # parent-OK - _("Backup file already exists! Overwrite?"), - _("The file '%s' exists.") % filename, - _("Proceed and overwrite"), - _("Cancel the backup"), - parent=self.window) + _("Backup file already exists! Overwrite?"), + _("The file '%s' exists.") % filename, + _("Proceed and overwrite"), + _("Cancel the backup"), + parent=self.window) yes_no = question.run() if not yes_no: return @@ -1378,7 +1396,8 @@ class ViewManager(CLIManager): writer.write(filename) self.uistate.set_busy_cursor(False) self.uistate.progress.hide() - self.uistate.push_message(self.dbstate, _("Backup saved to '%s'") % filename) + self.uistate.push_message(self.dbstate, + _("Backup saved to '%s'") % filename) config.set('paths.quick-backup-directory', path_entry.get_text()) else: self.uistate.push_message(self.dbstate, _("Backup aborted")) @@ -1389,7 +1408,7 @@ class ViewManager(CLIManager): Choose a backup folder. Make sure there is one highlighted in right pane, otherwise FileChooserDialog will hang. """ - f = Gtk.FileChooserDialog( + fdialog = Gtk.FileChooserDialog( title=_("Select backup directory"), parent=self.window, action=Gtk.FileChooserAction.SELECT_FOLDER, @@ -1400,14 +1419,14 @@ class ViewManager(CLIManager): mpath = path_entry.get_text() if not mpath: mpath = HOME_DIR - f.set_current_folder(os.path.dirname(mpath)) - f.set_filename(os.path.join(mpath, ".")) - status = f.run() + fdialog.set_current_folder(os.path.dirname(mpath)) + fdialog.set_filename(os.path.join(mpath, ".")) + status = fdialog.run() if status == Gtk.ResponseType.OK: - filename = f.get_filename() + filename = fdialog.get_filename() if filename: path_entry.set_text(filename) - f.destroy() + fdialog.destroy() return True def media_toggle(self, widget, file_entry): @@ -1525,11 +1544,11 @@ class ViewManager(CLIManager): self.uistate.uimanager.remove_action_group(self.reportactions) self.uistate.uimanager.remove_ui(self.report_menu_ui_id) self.reportactions = Gtk.ActionGroup(name='ReportWindow') - (uidef, actions) = self.build_plugin_menu( + (udef, actions) = self.build_plugin_menu( 'ReportsMenu', report_menu_list, standalone_categories, make_plugin_callback) self.reportactions.add_actions(actions) - self.report_menu_ui_id = self.uistate.uimanager.add_ui_from_string(uidef) + self.report_menu_ui_id = self.uistate.uimanager.add_ui_from_string(udef) self.uimanager.insert_action_group(self.reportactions, 1) self.uistate.uimanager.ensure_update() @@ -1554,8 +1573,7 @@ class ViewManager(CLIManager): hash_data[category].append(pdata) # Sort categories, skipping the unsupported - catlist = sorted(item for item in hash_data - if item != _UNSUPPORTED) + catlist = sorted(item for item in hash_data if item != _UNSUPPORTED) for key in catlist: new_key = key[0].replace(' ', '-') @@ -1625,9 +1643,9 @@ class ViewManager(CLIManager): 'again, you can hide it by using the Plugin Manager ' 'on the Help menu.' ) % {'name': pdata.name, - 'gramps_bugtracker_url' : URL_BUGHOME, - 'firstauthoremail' : pdata.authors_email[0] - if pdata.authors_email else '...', + 'gramps_bugtracker_url': URL_BUGHOME, + 'firstauthoremail': pdata.authors_email[0] + if pdata.authors_email else '...', 'error_msg': lasterror}, parent=self.uistate.window) continue @@ -1635,19 +1653,19 @@ class ViewManager(CLIManager): # pdata.category is (string, trans-string): if pdata.order == START: - viewstoshow[pdata.category[0]].insert(0, (pdata, viewclass) ) + viewstoshow[pdata.category[0]].insert(0, (pdata, viewclass)) else: - viewstoshow[pdata.category[0]].append( (pdata, viewclass) ) + viewstoshow[pdata.category[0]].append((pdata, viewclass)) # First, get those in order defined, if exists: resultorder = [viewstoshow[cat] - for cat in config.get("interface.view-categories") - if cat in viewstoshow] + for cat in config.get("interface.view-categories") + if cat in viewstoshow] # Next, get the rest in some order: resultorder.extend(viewstoshow[cat] - for cat in sorted(viewstoshow.keys()) - if viewstoshow[cat] not in resultorder) + for cat in sorted(viewstoshow.keys()) + if viewstoshow[cat] not in resultorder) return resultorder def key_bindings(obj): @@ -1716,12 +1734,12 @@ def run_plugin(pdata, dbstate, uistate): 'the plugin author (%(firstauthoremail)s).\n\n' 'If you do not want Gramps to try and load this plugin again, ' 'you can hide it by using the Plugin Manager on the ' - 'Help menu.') % { - 'name': pdata.name, - 'gramps_bugtracker_url' : URL_BUGHOME, - 'firstauthoremail': pdata.authors_email[0] if - pdata.authors_email else '...', - 'error_msg': error_msg}, + 'Help menu.') % {'name' : pdata.name, + 'gramps_bugtracker_url' : URL_BUGHOME, + 'firstauthoremail' : pdata.authors_email[0] + if pdata.authors_email + else '...', + 'error_msg' : error_msg}, parent=uistate.window) return @@ -1730,16 +1748,15 @@ def run_plugin(pdata, dbstate, uistate): getattr(mod, pdata.reportclass), getattr(mod, pdata.optionclass), pdata.name, pdata.id, - pdata.category, pdata.require_active, - ) + pdata.category, pdata.require_active) else: - tool.gui_tool(dbstate = dbstate, user = User(uistate = uistate), - tool_class = getattr(mod, pdata.toolclass), - options_class = getattr(mod, pdata.optionclass), - translated_name = pdata.name, - name = pdata.id, - category = pdata.category, - callback = dbstate.db.request_rebuild) + tool.gui_tool(dbstate=dbstate, user=User(uistate=uistate), + tool_class=getattr(mod, pdata.toolclass), + options_class=getattr(mod, pdata.optionclass), + translated_name=pdata.name, + name=pdata.id, + category=pdata.category, + callback=dbstate.db.request_rebuild) def make_plugin_callback(pdata, dbstate, uistate): """