improve pylint score for the book dialog

This commit is contained in:
Paul Franklin
2016-06-20 08:51:05 -07:00
parent 0cf5a81e7a
commit 4b84a04de7

View File

@ -22,6 +22,8 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# #
""" GUI dialog for creating and managing books """
# Written by Alex Roitman, # Written by Alex Roitman,
# largely based on the BaseDoc classes by Don Allingham # largely based on the BaseDoc classes by Don Allingham
@ -30,8 +32,6 @@
# Standard Python modules # Standard Python modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -39,7 +39,7 @@ _ = glocale.translation.gettext
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import logging import logging
log = logging.getLogger(".Book") LOG = logging.getLogger(".Book")
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -55,6 +55,8 @@ from gi.repository import GObject
# gramps modules # gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from ...listmodel import ListModel from ...listmodel import ListModel
from gramps.gen.errors import FilterError, ReportError from gramps.gen.errors import FilterError, ReportError
from ...pluginmanager import GuiPluginManager from ...pluginmanager import GuiPluginManager
@ -181,10 +183,10 @@ def _get_subject(options, dbase):
else: else:
mother_name = _("unknown mother") mother_name = _("unknown mother")
name = _("%(father)s and %(mother)s (%(id)s)") % { name = _("%(father_name)s and %(mother_name)s (%(family_id)s)"
'father' : father_name, ) % {'father_name' : father_name,
'mother' : mother_name, 'mother_name' : mother_name,
'id' : family_id } 'family_id' : family_id}
return name return name
return "" return ""
@ -241,7 +243,7 @@ class BookListDisplay:
self.guilistbooks = self.xml.get_object('list') self.guilistbooks = self.xml.get_object('list')
self.guilistbooks.connect('button-press-event', self.on_button_press) self.guilistbooks.connect('button-press-event', self.on_button_press)
self.guilistbooks.connect('key-press-event', self.on_key_pressed) self.guilistbooks.connect('key-press-event', self.on_key_pressed)
self.blist = ListModel(self.guilistbooks, [('Name',-1,10)],) self.blist = ListModel(self.guilistbooks, [('Name', -1, 10)],)
self.redraw() self.redraw()
self.selection = None self.selection = None
@ -284,14 +286,15 @@ class BookListDisplay:
self.top.run() self.top.run()
def on_booklist_cancel_clicked(self, obj): def on_booklist_cancel_clicked(self, obj):
""" cancel the booklist dialog """
if self.unsaved_changes: if self.unsaved_changes:
from ...dialog import QuestionDialog2 from ...dialog import QuestionDialog2
q = QuestionDialog2( qqq = QuestionDialog2(
_('Discard Unsaved Changes'), _('Discard Unsaved Changes'),
_('You have made changes which have not been saved.'), _('You have made changes which have not been saved.'),
_('Proceed'), _('Proceed'),
_('Cancel')) _('Cancel'))
if q.run(): if qqq.run():
return return
else: else:
self.top.run() self.top.run()
@ -324,7 +327,8 @@ class BookListDisplay:
return True return True
return False return False
def do_nothing(self, object): def do_nothing(self, obj):
""" do nothing """
pass pass
#------------------------------------------------------------------------ #------------------------------------------------------------------------
@ -346,8 +350,8 @@ class BookOptions(ReportOptions):
'bookname' : '', 'bookname' : '',
} }
self.options_help = { self.options_help = {
'bookname' : ("=name",_("Name of the book. MANDATORY"), 'bookname' : ("=name", _("Name of the book. MANDATORY"),
BookList('books.xml',dbase).get_book_names(), BookList('books.xml', dbase).get_book_names(),
False), False),
} }
@ -365,7 +369,7 @@ class BookSelector(ManagedWindow):
""" """
def __init__(self, dbstate, uistate): def __init__(self, dbstate, uistate):
self.db = dbstate.db self._db = dbstate.db
self.dbstate = dbstate self.dbstate = dbstate
self.uistate = uistate self.uistate = uistate
self.title = _('Manage Books') self.title = _('Manage Books')
@ -393,11 +397,11 @@ class BookSelector(ManagedWindow):
"destroy_passed_object" : self.close, "destroy_passed_object" : self.close,
# Insert dummy handlers for second top level in the glade file # Insert dummy handlers for second top level in the glade file
"on_booklist_ok_clicked" : lambda _:None, "on_booklist_ok_clicked" : lambda _: None,
"on_booklist_delete_clicked": lambda _:None, "on_booklist_delete_clicked" : lambda _: None,
"on_booklist_cancel_clicked": lambda _:None, "on_booklist_cancel_clicked" : lambda _: None,
"on_booklist_ok_clicked" : lambda _:None, "on_booklist_ok_clicked" : lambda _: None,
"on_booklist_ok_clicked" : lambda _:None, "on_booklist_ok_clicked" : lambda _: None,
}) })
self.avail_tree = self.xml.get_object("avail_tree") self.avail_tree = self.xml.get_object("avail_tree")
@ -417,14 +421,14 @@ class BookSelector(ManagedWindow):
book_label.set_use_underline(True) book_label.set_use_underline(True)
book_label.set_use_markup(True) book_label.set_use_markup(True)
avail_titles = [ (_('Name'), 0, 230), avail_titles = [(_('Name'), 0, 230),
(_('Type'), 1, 80 ), (_('Type'), 1, 80),
( '' , -1, 0 ) ] ('', -1, 0)]
book_titles = [ (_('Item name'), -1, 230), book_titles = [(_('Item name'), -1, 230),
(_('Type'), -1, 80 ), (_('Type'), -1, 80),
( '', -1, 0 ), ('', -1, 0),
(_('Subject'), -1, 50 ) ] (_('Subject'), -1, 50)]
self.avail_nr_cols = len(avail_titles) self.avail_nr_cols = len(avail_titles)
self.book_nr_cols = len(book_titles) self.book_nr_cols = len(book_titles)
@ -454,7 +458,7 @@ class BookSelector(ManagedWindow):
category = _UNSUPPORTED category = _UNSUPPORTED
if pdata.supported and pdata.category in book_categories: if pdata.supported and pdata.category in book_categories:
category = book_categories[pdata.category] category = book_categories[pdata.category]
available_reports.append([ pdata.name, category, pdata.id ]) available_reports.append([pdata.name, category, pdata.id])
for data in sorted(available_reports): for data in sorted(available_reports):
new_iter = self.avail_model.add(data) new_iter = self.avail_model.add(data)
@ -486,24 +490,22 @@ class BookSelector(ManagedWindow):
self.book.set_format_name(book.get_format_name()) self.book.set_format_name(book.get_format_name())
if book.get_output(): if book.get_output():
self.book.set_output(book.get_output()) self.book.set_output(book.get_output())
if book.get_dbname() == self.db.get_save_path(): if book.get_dbname() != self._db.get_save_path():
same_db = 1 WarningDialog(
else: _('Different database'),
same_db = 0 _('This book was created with the references to database '
WarningDialog(_('Different database'), _(
'This book was created with the references to database '
'%s.\n\n This makes references to the central person ' '%s.\n\n This makes references to the central person '
'saved in the book invalid.\n\n' 'saved in the book invalid.\n\n'
'Therefore, the central person for each item is being set ' 'Therefore, the central person for each item is being set '
'to the active person of the currently opened database.') 'to the active person of the currently opened database.'
% book.get_dbname(), ) % book.get_dbname(),
parent=self.window) parent=self.window)
self.book.clear() self.book.clear()
self.book_model.clear() self.book_model.clear()
for saved_item in book.get_item_list(): for saved_item in book.get_item_list():
name = saved_item.get_name() name = saved_item.get_name()
item = BookItem(self.db, name) item = BookItem(self._db, name)
item.option_class = saved_item.option_class item.option_class = saved_item.option_class
# The option values were loaded magically by the book parser. # The option values were loaded magically by the book parser.
@ -519,10 +521,10 @@ class BookSelector(ManagedWindow):
item.set_style_name(saved_item.get_style_name()) item.set_style_name(saved_item.get_style_name())
self.book.append_item(item) self.book.append_item(item)
data = [ item.get_translated_name(), data = [item.get_translated_name(),
item.get_category(), item.get_name() ] item.get_category(), item.get_name()]
data[2] = _get_subject(item.option_class, self.db) data[2] = _get_subject(item.option_class, self._db)
self.book_model.add(data) self.book_model.add(data)
def on_add_clicked(self, obj): def on_add_clicked(self, obj):
@ -534,10 +536,11 @@ class BookSelector(ManagedWindow):
store, the_iter = self.avail_model.get_selected() store, the_iter = self.avail_model.get_selected()
if not the_iter: if not the_iter:
return return
data = self.avail_model.get_data(the_iter, list(range(self.avail_nr_cols))) data = self.avail_model.get_data(the_iter,
item = BookItem(self.db, data[2]) list(range(self.avail_nr_cols)))
item = BookItem(self._db, data[2])
_initialize_options(item.option_class, self.dbstate, self.uistate) _initialize_options(item.option_class, self.dbstate, self.uistate)
data[2] = _get_subject(item.option_class, self.db) data[2] = _get_subject(item.option_class, self._db)
self.book_model.add(data) self.book_model.add(data)
self.book.append_item(item) self.book.append_item(item)
@ -567,7 +570,8 @@ class BookSelector(ManagedWindow):
if not row or row == -1: if not row or row == -1:
return return
store, the_iter = self.book_model.get_selected() store, the_iter = self.book_model.get_selected()
data = self.book_model.get_data(the_iter, list(range(self.book_nr_cols))) data = self.book_model.get_data(the_iter,
list(range(self.book_nr_cols)))
self.book_model.remove(the_iter) self.book_model.remove(the_iter)
self.book_model.insert(row-1, data, None, 1) self.book_model.insert(row-1, data, None, 1)
item = self.book.pop_item(row) item = self.book.pop_item(row)
@ -581,7 +585,8 @@ class BookSelector(ManagedWindow):
if row + 1 >= self.book_model.count or row == -1: if row + 1 >= self.book_model.count or row == -1:
return return
store, the_iter = self.book_model.get_selected() store, the_iter = self.book_model.get_selected()
data = self.book_model.get_data(the_iter, list(range(self.book_nr_cols))) data = self.book_model.get_data(the_iter,
list(range(self.book_nr_cols)))
self.book_model.remove(the_iter) self.book_model.remove(the_iter)
self.book_model.insert(row+1, data, None, 1) self.book_model.insert(row+1, data, None, 1)
item = self.book.pop_item(row) item = self.book.pop_item(row)
@ -597,7 +602,8 @@ class BookSelector(ManagedWindow):
_('Please select a book item to configure.'), _('Please select a book item to configure.'),
parent=self.window) parent=self.window)
return return
data = self.book_model.get_data(the_iter, list(range(self.book_nr_cols))) ## data = self.book_model.get_data(the_iter,
## list(range(self.book_nr_cols)))
row = self.book_model.get_selected_row() row = self.book_model.get_selected_row()
item = self.book.get_item(row) item = self.book.get_item(row)
option_class = item.option_class option_class = item.option_class
@ -613,7 +619,7 @@ class BookSelector(ManagedWindow):
# rest of dialog is unresponsive, release when finished # rest of dialog is unresponsive, release when finished
style = option_class.handler.get_default_stylesheet_name() style = option_class.handler.get_default_stylesheet_name()
item.set_style_name(style) item.set_style_name(style)
subject = _get_subject(option_class, self.db) subject = _get_subject(option_class, self._db)
self.book_model.model.set_value(the_iter, 2, subject) self.book_model.model.set_value(the_iter, 2, subject)
self.book.set_item(row, item) self.book.set_item(row, item)
item_dialog.close() item_dialog.close()
@ -666,8 +672,8 @@ class BookSelector(ManagedWindow):
(_("Edit"), None, self.on_edit_clicked, 1), (_("Edit"), None, self.on_edit_clicked, 1),
] ]
self.menu = Gtk.Menu() self.menu1 = Gtk.Menu() # TODO could this be just a local "menu ="?
self.menu.set_title(_('Book Menu')) self.menu1.set_title(_('Book Menu'))
for title, icon_name, callback, sensitivity in entries: for title, icon_name, callback, sensitivity in entries:
if icon_name: if icon_name:
item = Gtk.ImageMenuItem.new_with_mnemonic(title) item = Gtk.ImageMenuItem.new_with_mnemonic(title)
@ -679,8 +685,8 @@ class BookSelector(ManagedWindow):
item.connect("activate", callback) item.connect("activate", callback)
item.set_sensitive(sensitivity) item.set_sensitive(sensitivity)
item.show() item.show()
self.menu.append(item) self.menu1.append(item)
self.menu.popup(None, None, None, None, event.button, event.time) self.menu1.popup(None, None, None, None, event.button, event.time)
def build_avail_context_menu(self, event): def build_avail_context_menu(self, event):
"""Builds the menu with the single Add option.""" """Builds the menu with the single Add option."""
@ -694,8 +700,8 @@ class BookSelector(ManagedWindow):
(_('_Add'), 'list-add', self.on_add_clicked, sensitivity), (_('_Add'), 'list-add', self.on_add_clicked, sensitivity),
] ]
self.menu = Gtk.Menu() self.menu2 = Gtk.Menu() # TODO could this be just a local "menu ="?
self.menu.set_title(_('Available Items Menu')) self.menu2.set_title(_('Available Items Menu'))
for title, icon_name, callback, sensitivity in entries: for title, icon_name, callback, sensitivity in entries:
if icon_name: if icon_name:
item = Gtk.ImageMenuItem.new_with_mnemonic(title) item = Gtk.ImageMenuItem.new_with_mnemonic(title)
@ -707,16 +713,15 @@ class BookSelector(ManagedWindow):
item.connect("activate", callback) item.connect("activate", callback)
item.set_sensitive(sensitivity) item.set_sensitive(sensitivity)
item.show() item.show()
self.menu.append(item) self.menu2.append(item)
self.menu.popup(None, None, None, None, event.button, event.time) self.menu2.popup(None, None, None, None, event.button, event.time)
def on_book_ok_clicked(self, obj): def on_book_ok_clicked(self, obj):
""" """
Run final BookDialog with the current book. Run final BookDialog with the current book.
""" """
if self.book.item_list: if self.book.item_list:
BookDialog(self.dbstate, self.uistate, BookDialog(self.dbstate, self.uistate, self.book, BookOptions)
self.book, BookOptions)
else: else:
WarningDialog(_('No items'), _('This book has no items.'), WarningDialog(_('No items'), _('This book has no items.'),
parent=self.window) parent=self.window)
@ -727,31 +732,31 @@ class BookSelector(ManagedWindow):
""" """
Save the current book in the xml booklist file. Save the current book in the xml booklist file.
""" """
self.book_list = BookList(self.file, self.db) self.book_list = BookList(self.file, self._db)
name = str(self.name_entry.get_text()) name = str(self.name_entry.get_text())
if not name: if not name:
WarningDialog(_('No book name'), _( WarningDialog(
'You are about to save away a book with no name.\n\n' _('No book name'),
_('You are about to save away a book with no name.\n\n'
'Please give it a name before saving it away.'), 'Please give it a name before saving it away.'),
parent=self.window) parent=self.window)
return return
if name in self.book_list.get_book_names(): if name in self.book_list.get_book_names():
from ...dialog import QuestionDialog2 from ...dialog import QuestionDialog2
q = QuestionDialog2( qqq = QuestionDialog2(
_('Book name already exists'), _('Book name already exists'),
_('You are about to save away a ' _('You are about to save away a '
'book with a name which already exists.' 'book with a name which already exists.'),
),
_('Proceed'), _('Proceed'),
_('Cancel'), _('Cancel'),
parent=self.window) parent=self.window)
if q.run(): if qqq.run():
self.book.set_name(name) self.book.set_name(name)
else: else:
return return
else: else:
self.book.set_name(name) self.book.set_name(name)
self.book.set_dbname(self.db.get_save_path()) self.book.set_dbname(self._db.get_save_path())
self.book_list.set_book(name, self.book) self.book_list.set_book(name, self.book)
self.book_list.save() self.book_list.save()
@ -759,7 +764,7 @@ class BookSelector(ManagedWindow):
""" """
Run the BookListDisplay dialog to present the choice of books to open. Run the BookListDisplay dialog to present the choice of books to open.
""" """
self.book_list = BookList(self.file, self.db) self.book_list = BookList(self.file, self._db)
booklistdisplay = BookListDisplay(self.book_list, 1, 0) booklistdisplay = BookListDisplay(self.book_list, 1, 0)
booklistdisplay.top.destroy() booklistdisplay.top.destroy()
book = booklistdisplay.selection book = booklistdisplay.selection
@ -772,7 +777,7 @@ class BookSelector(ManagedWindow):
""" """
Run the BookListDisplay dialog to present the choice of books to delete. Run the BookListDisplay dialog to present the choice of books to delete.
""" """
self.book_list = BookList(self.file, self.db) self.book_list = BookList(self.file, self._db)
booklistdisplay = BookListDisplay(self.book_list, 0, 1) booklistdisplay = BookListDisplay(self.book_list, 0, 1)
booklistdisplay.top.destroy() booklistdisplay.top.destroy()
book = booklistdisplay.selection book = booklistdisplay.selection
@ -825,7 +830,7 @@ class BookItemDialog(ReportDialog):
def init_options(self, option_class): def init_options(self, option_class):
try: try:
if issubclass(option_class, object): if issubclass(option_class, object):
self.options = option_class(self.raw_name, self.db) self.options = option_class(self.raw_name, self.database)
except TypeError: except TypeError:
self.options = option_class self.options = option_class
if not self.is_from_saved_book: if not self.is_from_saved_book:
@ -864,6 +869,9 @@ class BookItemDialog(ReportDialog):
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class _BookFormatComboBox(Gtk.ComboBox): class _BookFormatComboBox(Gtk.ComboBox):
"""
Build a menu of report types that are appropriate for a book
"""
def __init__(self, active): def __init__(self, active):
@ -916,22 +924,23 @@ class BookDialog(DocReportDialog):
self.page_html_added = False self.page_html_added = False
self.book = book self.book = book
self.title = _('Generate Book') self.title = _('Generate Book')
self.database = dbstate.db
DocReportDialog.__init__(self, dbstate, uistate, options, DocReportDialog.__init__(self, dbstate, uistate, options,
'book', self.title) 'book', self.title)
self.options.options_dict['bookname'] = self.book.name self.options.options_dict['bookname'] = self.book.name
self.database = dbstate.db
response = self.window.run() response = self.window.run()
if response == Gtk.ResponseType.OK: if response == Gtk.ResponseType.OK:
handler = oh = self.options.handler handler = self.options.handler
if self.book.get_paper_name() != handler.get_paper_name(): if self.book.get_paper_name() != handler.get_paper_name():
self.book.set_paper_name(handler.get_paper_name()) self.book.set_paper_name(handler.get_paper_name())
if self.book.get_orientation() != handler.get_orientation(): if self.book.get_orientation() != handler.get_orientation():
self.book.set_orientation(handler.get_orientation()) self.book.set_orientation(handler.get_orientation())
if self.book.get_paper_metric() != handler.get_paper_metric(): if self.book.get_paper_metric() != handler.get_paper_metric():
self.book.set_paper_metric(handler.get_paper_metric()) self.book.set_paper_metric(handler.get_paper_metric())
if self.book.get_custom_paper_size() != oh.get_custom_paper_size(): if (self.book.get_custom_paper_size() !=
self.book.set_custom_paper_size(oh.get_custom_paper_size()) handler.get_custom_paper_size()):
self.book.set_custom_paper_size(handler.get_custom_paper_size())
if self.book.get_margins() != handler.get_margins(): if self.book.get_margins() != handler.get_margins():
self.book.set_margins(handler.get_margins()) self.book.set_margins(handler.get_margins())
if self.book.get_format_name() != handler.get_format_name(): if self.book.get_format_name() != handler.get_format_name():
@ -944,21 +953,26 @@ class BookDialog(DocReportDialog):
ErrorDialog(str(msg), parent=self.window) ErrorDialog(str(msg), parent=self.window)
self.close() self.close()
def setup_style_frame(self): pass def setup_style_frame(self):
def setup_other_frames(self): pass pass
def parse_style_frame(self): pass def setup_other_frames(self):
pass
def parse_style_frame(self):
pass
def get_title(self): def get_title(self):
""" get the title """
return self.title return self.title
def get_header(self, name): def get_header(self, name):
""" get the header """
return _("Gramps Book") return _("Gramps Book")
def make_doc_menu(self, active=None): def make_doc_menu(self, active=None):
"""Build a menu of document types that are appropriate for """Build a menu of document types that are appropriate for
this text report. This menu will be generated based upon this text report. This menu will be generated based upon
whether the document requires table support, etc.""" whether the document requires table support, etc."""
self.format_menu = _BookFormatComboBox( active ) self.format_menu = _BookFormatComboBox(active)
def make_document(self): def make_document(self):
"""Create a document of the type requested by the user.""" """Create a document of the type requested by the user."""
@ -1000,9 +1014,8 @@ class BookDialog(DocReportDialog):
def init_options(self, option_class): def init_options(self, option_class):
try: try:
if (issubclass(option_class, object) or # New-style class if issubclass(option_class, object):
isinstance(option_class, ClassType)): # Old-style class self.options = option_class(self.raw_name, self.database)
self.options = option_class(self.raw_name, self.db)
except TypeError: except TypeError:
self.options = option_class self.options = option_class
if not self.is_from_saved_book: if not self.is_from_saved_book:
@ -1034,11 +1047,11 @@ def write_book_item(database, report_class, options, user):
try: try:
return report_class(database, options, user) return report_class(database, options, user)
except ReportError as msg: except ReportError as msg:
(m1, m2) = msg.messages() (msg1, msg2) = msg.messages()
ErrorDialog(m1, m2) ErrorDialog(msg1, msg2)
except FilterError as msg: except FilterError as msg:
(m1, m2) = msg.messages() (msg1, msg2) = msg.messages()
ErrorDialog(m1, m2) ErrorDialog(msg1, msg2)
except: except:
log.error("Failed to write book item.", exc_info=True) LOG.error("Failed to write book item.", exc_info=True)
return None return None