improve another pylint score (_book.py)

This commit is contained in:
Paul Franklin 2016-06-23 00:53:17 -07:00
parent d297e190e6
commit bf8d6c6c03

View File

@ -24,14 +24,15 @@
# 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
""" the non-UI-specific (i.e. common, shared) classes for books """
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Standard Python modules # Standard Python modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
import copy import copy
import os
#------------------------------------------------------------------------ #------------------------------------------------------------------------
# #
@ -39,8 +40,7 @@ import copy
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
import logging import logging
log = logging.getLogger(".Book") LOG = logging.getLogger(".Book")
import os
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -55,6 +55,8 @@ from xml.sax.saxutils import escape
# gramps modules # gramps modules
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ...const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from ...const import HOME_DIR from ...const import HOME_DIR
from ...utils.cast import get_type_converter_by_name, type_name from ...utils.cast import get_type_converter_by_name, type_name
from ..docgen import StyleSheet, StyleSheetList from ..docgen import StyleSheet, StyleSheetList
@ -148,7 +150,7 @@ class BookItem:
#------------------------------------------------------------------------ #------------------------------------------------------------------------
class Book: class Book:
""" """
Interface into the user-defined book -- a collection of book items. Interface into the user-defined Book -- a collection of book items.
""" """
def __init__(self, obj=None): def __init__(self, obj=None):
@ -454,79 +456,90 @@ class BookList:
""" """
Saves the current BookList to the associated file. Saves the current BookList to the associated file.
""" """
with open(self.file, "w") as f: with open(self.file, "w") as b_f:
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") b_f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
f.write('<booklist>\n') b_f.write('<booklist>\n')
for name in sorted(self.bookmap): # enable a diff of archived copies for name in sorted(self.bookmap): # enable a diff of archived copies
book = self.get_book(name) book = self.get_book(name)
dbname = book.get_dbname() dbname = book.get_dbname()
f.write(' <book name="%s" database="%s">\n' % (name, dbname) ) b_f.write(' <book name="%s" database="%s"'
'>\n' % (name, dbname))
for item in book.get_item_list(): for item in book.get_item_list():
f.write(' <item name="%s" trans_name="%s">\n' % b_f.write(' <item name="%s" '
(item.get_name(), item.get_translated_name() ) ) 'trans_name="%s">\n' % (
item.get_name(),
item.get_translated_name()))
options = item.option_class.handler.options_dict options = item.option_class.handler.options_dict
for option_name in sorted(options.keys()): # enable a diff for option_name in sorted(options.keys()): # enable a diff
option_value = options[option_name] option_value = options[option_name]
if isinstance(option_value, (list, tuple)): if isinstance(option_value, (list, tuple)):
f.write(' <option name="%s" value="" ' b_f.write(' <option name="%s" value="" '
'length="%d">\n' % ( 'length="%d">\n' % (
escape(option_name), escape(option_name),
len(options[option_name]) ) ) len(options[option_name])))
for list_index in range(len(option_value)): for list_index in range(len(option_value)):
option_type = type_name(option_value[list_index]) option_type = type_name(
option_value[list_index])
value = escape(str(option_value[list_index])) value = escape(str(option_value[list_index]))
value = value.replace('"', '&quot;') value = value.replace('"', '&quot;')
f.write(' <listitem number="%d" type="%s" ' b_f.write(' <listitem number="%d" '
'value="%s"/>\n' % ( 'type="%s" value="%s"/>\n' % (
list_index, list_index,
option_type, option_type,
value ) ) value))
f.write(' </option>\n') b_f.write(' </option>\n')
else: else:
option_type = type_name(option_value) option_type = type_name(option_value)
value = escape(str(option_value)) value = escape(str(option_value))
value = value.replace('"', '&quot;') value = value.replace('"', '&quot;')
f.write(' <option name="%s" type="%s" ' b_f.write(' <option name="%s" type="%s" '
'value="%s"/>\n' % ( 'value="%s"/>\n' % (
escape(option_name), escape(option_name),
option_type, option_type,
value) ) value))
f.write(' <style name="%s"/>\n' % item.get_style_name() ) b_f.write(' <style name="%s"'
f.write(' </item>\n') '/>\n' % item.get_style_name())
b_f.write(' </item>\n')
if book.get_paper_name(): if book.get_paper_name():
f.write(' <paper name="%s"/>\n' % book.get_paper_name() ) b_f.write(' <paper name="%s"'
'/>\n' % book.get_paper_name())
if book.get_orientation() is not None: # 0 is legal if book.get_orientation() is not None: # 0 is legal
f.write(' <orientation value="%s"/>\n' % b_f.write(' <orientation value="%s"'
book.get_orientation() ) '/>\n' % book.get_orientation())
if book.get_paper_metric() is not None: # 0 is legal if book.get_paper_metric() is not None: # 0 is legal
f.write(' <metric value="%s"/>\n' % book.get_paper_metric() ) b_f.write(' <metric value="%s"'
'/>\n' % book.get_paper_metric())
if book.get_custom_paper_size(): if book.get_custom_paper_size():
size = book.get_custom_paper_size() size = book.get_custom_paper_size()
f.write(' <size value="%f %f"/>\n' % (size[0], size[1]) ) b_f.write(' <size value="%f %f"'
'/>\n' % (size[0], size[1]))
if book.get_margins(): if book.get_margins():
for pos in range(len(book.get_margins())): for pos in range(len(book.get_margins())):
f.write(' <margin number="%s" value="%f"/>\n' % b_f.write(' <margin number="%s" '
(pos, book.get_margin(pos)) ) 'value="%f"/>\n' % (
pos, book.get_margin(pos)))
if book.get_format_name(): if book.get_format_name():
f.write(' <format name="%s"/>\n' % book.get_format_name() ) b_f.write(' <format name="%s"'
'/>\n' % book.get_format_name())
if book.get_output(): if book.get_output():
f.write(' <output name="%s"/>\n' % book.get_output() ) b_f.write(' <output name="%s"'
f.write(' </book>\n') '/>\n' % book.get_output())
b_f.write(' </book>\n')
f.write('</booklist>\n') b_f.write('</booklist>\n')
def parse(self): def parse(self):
""" """
Loads the BookList from the associated file, if it exists. Loads the BookList from the associated file, if it exists.
""" """
try: try:
p = make_parser() parser = make_parser()
p.setContentHandler(BookParser(self, self.dbase)) parser.setContentHandler(BookParser(self, self.dbase))
with open(self.file) as the_file: with open(self.file) as the_file:
p.parse(the_file) parser.parse(the_file)
except (IOError, OSError, ValueError, SAXParseException, KeyError, except (IOError, OSError, ValueError, SAXParseException, KeyError,
AttributeError): AttributeError):
pass pass
@ -549,25 +562,33 @@ class BookParser(handler.ContentHandler):
handler.ContentHandler.__init__(self) handler.ContentHandler.__init__(self)
self.dbase = dbase self.dbase = dbase
self.booklist = booklist self.booklist = booklist
self.b = None self.book = None
self.i = None self.item = None
self.o = None self.option = None
self.an_o_name = None self.an_opt_name = None
self.an_o_value = None self.an_opt_value = None
self.s = None self.style = None
self.bname = None self.bname = None
self.iname = None self.iname = None
self.dbname = None
self.b_p_name = None
self.b_p_orient = None
self.b_p_metric = None
self.b_p_size = None
self.b_p_margins = None
self.b_p_format = None
self.b_p_output = None
def startElement(self, tag, attrs): def startElement(self, tag, attrs):
""" """
Overridden class that handles the start of a XML element Overridden class that handles the start of a XML element
""" """
if tag == "book": if tag == "book":
self.b = Book() self.book = Book()
self.bname = attrs['name'] self.bname = attrs['name']
self.b.set_name(self.bname) self.book.set_name(self.bname)
self.dbname = attrs['database'] self.dbname = attrs['database']
self.b.set_dbname(self.dbname) self.book.set_dbname(self.dbname)
self.b_p_name = None self.b_p_name = None
self.b_p_orient = None self.b_p_orient = None
self.b_p_metric = None self.b_p_metric = None
@ -576,20 +597,20 @@ class BookParser(handler.ContentHandler):
self.b_p_format = None self.b_p_format = None
self.b_p_output = None self.b_p_output = None
elif tag == "item": elif tag == "item":
self.i = BookItem(self.dbase, attrs['name']) self.item = BookItem(self.dbase, attrs['name'])
self.o = {} self.option = {}
elif tag == "option": elif tag == "option":
self.an_o_name = attrs['name'] self.an_opt_name = attrs['name']
if 'length' in attrs: if 'length' in attrs:
self.an_o_value = [] self.an_opt_value = []
else: else:
converter = get_type_converter_by_name(attrs['type']) converter = get_type_converter_by_name(attrs['type'])
self.an_o_value = converter(attrs['value']) self.an_opt_value = converter(attrs['value'])
elif tag == "listitem": elif tag == "listitem":
converter = get_type_converter_by_name(attrs['type']) converter = get_type_converter_by_name(attrs['type'])
self.an_o_value.append(converter(attrs['value'])) self.an_opt_value.append(converter(attrs['value']))
elif tag == "style": elif tag == "style":
self.s = attrs['name'] self.style = attrs['name']
elif tag == 'paper': elif tag == 'paper':
self.b_p_name = attrs['name'] self.b_p_name = attrs['name']
elif tag == 'orientation': elif tag == 'orientation':
@ -611,29 +632,31 @@ class BookParser(handler.ContentHandler):
pass pass
def endElement(self, tag): def endElement(self, tag):
"Overridden class that handles the end of a XML element" """
Overridden class that handles the end of a XML element
"""
if tag == "option": if tag == "option":
self.o[self.an_o_name] = self.an_o_value self.option[self.an_opt_name] = self.an_opt_value
elif tag == "item": elif tag == "item":
self.i.option_class.handler.options_dict.update(self.o) self.item.option_class.handler.options_dict.update(self.option)
self.i.set_style_name(self.s) self.item.set_style_name(self.style)
self.b.append_item(self.i) self.book.append_item(self.item)
elif tag == "book": elif tag == "book":
if self.b_p_name: if self.b_p_name:
self.b.set_paper_name(self.b_p_name) self.book.set_paper_name(self.b_p_name)
if self.b_p_orient is not None: # 0 is legal if self.b_p_orient is not None: # 0 is legal
self.b.set_orientation(self.b_p_orient) self.book.set_orientation(self.b_p_orient)
if self.b_p_metric is not None: # 0 is legal if self.b_p_metric is not None: # 0 is legal
self.b.set_paper_metric(self.b_p_metric) self.book.set_paper_metric(self.b_p_metric)
if self.b_p_size: if self.b_p_size:
self.b.set_custom_paper_size(self.b_p_size) self.book.set_custom_paper_size(self.b_p_size)
if self.b_p_margins: if self.b_p_margins:
self.b.set_margins(self.b_p_margins) self.book.set_margins(self.b_p_margins)
if self.b_p_format: if self.b_p_format:
self.b.set_format_name(self.b_p_format) self.book.set_format_name(self.b_p_format)
if self.b_p_output: if self.b_p_output:
self.b.set_output(self.b_p_output) self.book.set_output(self.b_p_output)
self.booklist.set_book(self.bname, self.b) self.booklist.set_book(self.bname, self.book)
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
@ -644,38 +667,38 @@ def append_styles(selected_style, item):
""" """
Append the styles for a book item to the stylesheet. Append the styles for a book item to the stylesheet.
""" """
handler = item.option_class.handler ihandler = item.option_class.handler
# Set up default style # Set up default style
handler.set_default_stylesheet_name(item.get_style_name()) ihandler.set_default_stylesheet_name(item.get_style_name())
default_style = StyleSheet() default_style = StyleSheet()
make_default_style = item.option_class.make_default_style make_default_style = item.option_class.make_default_style
make_default_style(default_style) make_default_style(default_style)
# Read all style sheets available for this item # Read all style sheets available for this item
style_file = handler.get_stylesheet_savefile() style_file = ihandler.get_stylesheet_savefile()
style_list = StyleSheetList(style_file, default_style) style_list = StyleSheetList(style_file, default_style)
# Get the selected stylesheet # Get the selected stylesheet
style_name = handler.get_default_stylesheet_name() style_name = ihandler.get_default_stylesheet_name()
style_sheet = style_list.get_style_sheet(style_name) style_sheet = style_list.get_style_sheet(style_name)
for this_style_name in style_sheet.get_paragraph_style_names(): for this_style_name in style_sheet.get_paragraph_style_names():
selected_style.add_paragraph_style( selected_style.add_paragraph_style(
this_style_name, this_style_name,
style_sheet.get_paragraph_style(this_style_name)) style_sheet.get_paragraph_style(this_style_name))
for this_style_name in style_sheet.get_draw_style_names(): for this_style_name in style_sheet.get_draw_style_names():
selected_style.add_draw_style( selected_style.add_draw_style(
this_style_name, this_style_name,
style_sheet.get_draw_style(this_style_name)) style_sheet.get_draw_style(this_style_name))
for this_style_name in style_sheet.get_table_style_names(): for this_style_name in style_sheet.get_table_style_names():
selected_style.add_table_style( selected_style.add_table_style(
this_style_name, this_style_name,
style_sheet.get_table_style(this_style_name)) style_sheet.get_table_style(this_style_name))
for this_style_name in style_sheet.get_cell_style_names(): for this_style_name in style_sheet.get_cell_style_names():
selected_style.add_cell_style( selected_style.add_cell_style(
this_style_name, this_style_name,
style_sheet.get_cell_style(this_style_name)) style_sheet.get_cell_style(this_style_name))