From 306f3abd01d7bad5a7ca5cc7add09ff49c398a05 Mon Sep 17 00:00:00 2001 From: Nick Hall Date: Fri, 3 Jun 2016 23:46:23 +0100 Subject: [PATCH] More pylint improvements --- gramps/plugins/docgen/asciidoc.py | 106 ++++++++++++++++++------------ gramps/plugins/docgen/cairodoc.py | 21 +++--- gramps/plugins/docgen/htmldoc.py | 105 ++++++++++++++--------------- 3 files changed, 126 insertions(+), 106 deletions(-) diff --git a/gramps/plugins/docgen/asciidoc.py b/gramps/plugins/docgen/asciidoc.py index 9876b00ed..926f9e43e 100644 --- a/gramps/plugins/docgen/asciidoc.py +++ b/gramps/plugins/docgen/asciidoc.py @@ -23,25 +23,22 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # -#------------------------------------------------------------------------ -# -# python modules -# -#------------------------------------------------------------------------ -import sys +""" +ACSII document generator. +""" #------------------------------------------------------------------------ # # Gramps modules # #------------------------------------------------------------------------ -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.translation.gettext from gramps.gen.plug.docgen import (BaseDoc, TextDoc, - PARA_ALIGN_RIGHT, PARA_ALIGN_CENTER) + PARA_ALIGN_RIGHT, PARA_ALIGN_CENTER) from gramps.gen.errors import ReportError from gramps.gen.plug.menu import NumberOption from gramps.gen.plug.report import DocOptions +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext #------------------------------------------------------------------------ # @@ -70,7 +67,7 @@ def reformat_para(para='', left=0, right=72, just=LEFT, right_pad=0, first=0): alllines = para.split('\n') for realline in alllines: words = realline.split() - line = '' + line = '' word = 0 end_words = 0 while not end_words: @@ -79,7 +76,7 @@ def reformat_para(para='', left=0, right=72, just=LEFT, right_pad=0, first=0): break if len(words[word]) > right-real_left: # Handle very long words line = words[word] - word +=1 + word += 1 if word >= len(words): end_words = 1 else: # Compose line of words @@ -99,15 +96,15 @@ def reformat_para(para='', left=0, right=72, just=LEFT, right_pad=0, first=0): if right_pad: return '\n'.join( [' '*(left+first) + ln.center(right-left-first) - for ln in lines[0:1] ] + - [ ' '*left + ln.center(right-left) for ln in lines[1:] ] + for ln in lines[0:1]] + + [' '*left + ln.center(right-left) for ln in lines[1:]] ) else: return '\n'.join( [' '*(left+first) + ln.center(right-left-first).rstrip() - for ln in lines[0:1] ] + + for ln in lines[0:1]] + [' '*left + ln.center(right-left).rstrip() - for ln in lines[1:] ] + for ln in lines[1:]] ) elif just == RIGHT: if right_pad: @@ -118,13 +115,13 @@ def reformat_para(para='', left=0, right=72, just=LEFT, right_pad=0, first=0): if right_pad: return '\n'.join( [' '*(left+first) + line.ljust(right-left-first) - for line in lines[0:1] ] + - [' '*left + line.ljust(right-left) for line in lines[1:] ] + for line in lines[0:1]] + + [' '*left + line.ljust(right-left) for line in lines[1:]] ) else: return '\n'.join( - [' '*(left+first) + line for line in lines[0:1] ] + - [' '*left + line for line in lines[1:] ] + [' '*(left+first) + line for line in lines[0:1]] + + [' '*left + line for line in lines[1:]] ) #------------------------------------------------------------------------ @@ -133,9 +130,11 @@ def reformat_para(para='', left=0, right=72, just=LEFT, right_pad=0, first=0): # #------------------------------------------------------------------------ class AsciiDoc(BaseDoc, TextDoc): - - def __init__(self, styles, type, options=None): - BaseDoc.__init__(self, styles, type) + """ + ASCII document generator. + """ + def __init__(self, styles, paper_style, options=None): + BaseDoc.__init__(self, styles, paper_style) self.__note_format = False self._cpl = 72 # characters per line, in case the options are ignored @@ -143,6 +142,22 @@ class AsciiDoc(BaseDoc, TextDoc): menu = options.menu self._cpl = menu.get_option_by_name('linechars').get_value() + self.file = None + self.filename = '' + + self.text = '' + self.para = None + self.leader = None + + self.tbl_style = None + self.in_cell = None + self.ncols = 0 + self.cellpars = [] + self.cell_lines = [] + self.cell_widths = [] + self.cellnum = -1 + self.maxlines = 0 + #-------------------------------------------------------------------- # # Opens the file, resets the text buffer. @@ -155,7 +170,7 @@ class AsciiDoc(BaseDoc, TextDoc): self.filename = filename try: - self.f = open(self.filename, "w", errors = 'backslashreplace') + self.file = open(self.filename, "w", errors='backslashreplace') except Exception as msg: errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg) raise ReportError(errmsg) @@ -169,9 +184,12 @@ class AsciiDoc(BaseDoc, TextDoc): # #-------------------------------------------------------------------- def close(self): - self.f.close() + self.file.close() def get_usable_width(self): + """ + Return the usable width of the document in characters. + """ return self._cpl #-------------------------------------------------------------------- @@ -180,7 +198,7 @@ class AsciiDoc(BaseDoc, TextDoc): # #-------------------------------------------------------------------- def page_break(self): - self.f.write('\012') + self.file.write('\012') def start_bold(self): pass @@ -201,7 +219,7 @@ class AsciiDoc(BaseDoc, TextDoc): #-------------------------------------------------------------------- def start_paragraph(self, style_name, leader=None): styles = self.get_style_sheet() - self.p = styles.get_paragraph_style(style_name) + self.para = styles.get_paragraph_style(style_name) self.leader = leader #-------------------------------------------------------------------- @@ -212,9 +230,9 @@ class AsciiDoc(BaseDoc, TextDoc): # #-------------------------------------------------------------------- def end_paragraph(self): - if self.p.get_alignment() == PARA_ALIGN_RIGHT: + if self.para.get_alignment() == PARA_ALIGN_RIGHT: fmt = RIGHT - elif self.p.get_alignment() == PARA_ALIGN_CENTER: + elif self.para.get_alignment() == PARA_ALIGN_CENTER: fmt = CENTER else: fmt = LEFT @@ -227,10 +245,10 @@ class AsciiDoc(BaseDoc, TextDoc): # Compute indents in characters. Keep first_indent relative! regular_indent = 0 first_indent = 0 - if self.p.get_left_margin(): - regular_indent = int(4*self.p.get_left_margin()) - if self.p.get_first_indent(): - first_indent = int(4*self.p.get_first_indent()) + if self.para.get_left_margin(): + regular_indent = int(4*self.para.get_left_margin()) + if self.para.get_first_indent(): + first_indent = int(4*self.para.get_first_indent()) if self.in_cell and self.cellnum < self.ncols - 1: right_pad = 1 @@ -247,7 +265,8 @@ class AsciiDoc(BaseDoc, TextDoc): # Do not reformat if preformatted notes if not self.__note_format: self.leader += ' ' - start_at = regular_indent + min(len(self.leader)+first_indent, 0) + start_at = regular_indent + min(len(self.leader)+first_indent, + 0) this_text = reformat_para(self.text, regular_indent, right, fmt, right_pad) this_text = (' ' * (regular_indent+first_indent) + @@ -261,7 +280,7 @@ class AsciiDoc(BaseDoc, TextDoc): # line indent, as specified by style. # Do not reformat if preformatted notes if not self.__note_format: - this_text = reformat_para(self.text, regular_indent, right,fmt, + this_text = reformat_para(self.text, regular_indent, right, fmt, right_pad, first_indent) else: this_text = ' ' * (regular_indent + first_indent) + self.text @@ -277,7 +296,7 @@ class AsciiDoc(BaseDoc, TextDoc): if self.in_cell: self.cellpars[self.cellnum] += this_text else: - self.f.write(this_text) + self.file.write(this_text) self.text = "" @@ -312,10 +331,11 @@ class AsciiDoc(BaseDoc, TextDoc): self.cell_widths = [0] * self.ncols self.cellnum = -1 self.maxlines = 0 - table_width = self.get_usable_width() * self.tbl_style.get_width() / 100.0 + table_width = (self.get_usable_width() * + self.tbl_style.get_width() / 100.0) for cell in range(self.ncols): - self.cell_widths[cell] = int(table_width * - self.tbl_style.get_column_width(cell) / 100.0) + self.cell_widths[cell] = int( + table_width * self.tbl_style.get_column_width(cell) / 100.0) #-------------------------------------------------------------------- # @@ -337,8 +357,8 @@ class AsciiDoc(BaseDoc, TextDoc): for line in range(self.maxlines): for cell in range(self.ncols): if self.cell_widths[cell]: - self.f.write(cell_text[cell][line]) - self.f.write('\n') + self.file.write(cell_text[cell][line]) + self.file.write('\n') #-------------------------------------------------------------------- # @@ -370,13 +390,13 @@ class AsciiDoc(BaseDoc, TextDoc): if self.cell_lines[self.cellnum] > self.maxlines: self.maxlines = self.cell_lines[self.cellnum] - def add_media(self, name, align, w_cm, h_cm, alt='', - style_name=None, crop=None): + def add_media(self, name, align, w_cm, h_cm, alt='', style_name=None, + crop=None): this_text = '(photo)' if self.in_cell: self.cellpars[self.cellnum] += this_text else: - self.f.write(this_text) + self.file.write(this_text) def write_styled_note(self, styledtext, format, style_name, contains_html=False, links=False): diff --git a/gramps/plugins/docgen/cairodoc.py b/gramps/plugins/docgen/cairodoc.py index 0df744852..e5eb4a2b9 100644 --- a/gramps/plugins/docgen/cairodoc.py +++ b/gramps/plugins/docgen/cairodoc.py @@ -28,7 +28,15 @@ # Python modules # #------------------------------------------------------------------------ -import sys +import logging + +#------------------------------------------------------------------------- +# +# GTK modules +# +#------------------------------------------------------------------------- +from gi.repository import Pango, PangoCairo +import cairo #------------------------------------------------------------------------ # @@ -40,23 +48,14 @@ from gramps.gen.plug.docgen import INDEX_TYPE_ALP, INDEX_TYPE_TOC from gramps.gen.errors import ReportError from gramps.gen.const import GRAMPS_LOCALE as glocale _ = glocale.translation.gettext -from gramps.gen.constfunc import lin + #------------------------------------------------------------------------ # # Set up logging # #------------------------------------------------------------------------ -import logging LOG = logging.getLogger(".cairodoc") -#------------------------------------------------------------------------- -# -# GTK modules -# -#------------------------------------------------------------------------- -from gi.repository import Pango, PangoCairo -import cairo - #------------------------------------------------------------------------ # # Constants diff --git a/gramps/plugins/docgen/htmldoc.py b/gramps/plugins/docgen/htmldoc.py index c75892f37..635467b9e 100644 --- a/gramps/plugins/docgen/htmldoc.py +++ b/gramps/plugins/docgen/htmldoc.py @@ -31,33 +31,32 @@ Report output generator for html documents, based on Html and HtmlBackend #------------------------------------------------------------------------ # -# python modules +# Python modules # #------------------------------------------------------------------------ import os import shutil -import time +import logging #------------------------------------------------------------------------ # # Gramps modules # #------------------------------------------------------------------------ -from gramps.gen.const import GRAMPS_LOCALE as glocale -_ = glocale.translation.gettext from gramps.gen.utils.image import resize_to_jpeg from gramps.gen.const import DATA_DIR, IMAGE_DIR, PROGRAM_NAME, URL_HOMEPAGE from gramps.version import VERSION -from gramps.gen.plug.docgen import BaseDoc, TextDoc, FONT_SANS_SERIF, URL_PATTERN +from gramps.gen.plug.docgen import BaseDoc, TextDoc, URL_PATTERN from gramps.plugins.lib.libhtmlbackend import HtmlBackend, process_spaces from gramps.plugins.lib.libhtml import Html +from gramps.gen.const import GRAMPS_LOCALE as glocale +_ = glocale.translation.gettext #------------------------------------------------------------------------ # # Set up logging # #------------------------------------------------------------------------ -import logging LOG = logging.getLogger(".htmldoc") _TEXTDOCSCREEN = 'grampstextdoc.css' @@ -107,6 +106,7 @@ class HtmlDoc(BaseDoc, TextDoc): self.__title_written = -1 # -1 = not written, 0 = writing, 1 = written self.__link_attrs = {} # additional link attrs, eg {"style": "...", "class": "..."} self.use_table_headers = False # th, td + self.first_row = True def set_css_filename(self, css_filename): """ @@ -136,9 +136,9 @@ class HtmlDoc(BaseDoc, TextDoc): """ # add additional meta tags and stylesheet links to head section # create additional meta tags - _meta1 = 'name="generator" content="%s %s %s"' % (PROGRAM_NAME, - VERSION, URL_HOMEPAGE) - meta = Html('meta', attr = _meta1) + _meta1 = 'name="generator" content="%s %s %s"' % ( + PROGRAM_NAME, VERSION, URL_HOMEPAGE) + meta = Html('meta', attr=_meta1) #set styles of the report as inline css self.build_style_declaration() @@ -151,13 +151,12 @@ class HtmlDoc(BaseDoc, TextDoc): # links for GRAMPS favicon and stylesheets links = Html('link', rel='shortcut icon', href=fname1, - type='image/x-icon') + ( - Html('link', rel='stylesheet', href=fname2, type='text/css', - media='screen', indent=False),) + type='image/x-icon') + ( + Html('link', rel='stylesheet', href=fname2, + type='text/css', media='screen', indent=False),) if self.css_filename: links += (Html('link', rel='stylesheet', href=fname3, - type='text/css', media='screen', indent=False), - ) + type='text/css', media='screen', indent=False),) self._backend.html_header += (meta, links) def build_style_declaration(self, id="grampstextdoc"): @@ -184,8 +183,8 @@ class HtmlDoc(BaseDoc, TextDoc): '\tpadding: %s %s %s %s;\n' '\tborder-top:%s; border-bottom:%s;\n' '\tborder-left:%s; border-right:%s;\n}' - % (id, sname, pad, pad, pad, pad, top, bottom, - left, right)) + % (id, sname, pad, pad, pad, pad, top, bottom, + left, right)) for style_name in sorted(styles.get_paragraph_style_names()): @@ -243,7 +242,7 @@ class HtmlDoc(BaseDoc, TextDoc): """ Overwrite base method """ - while len(self.htmllist)>1 : + while len(self.htmllist) > 1: self.__reduce_list() #now write the actual file self._backend.close() @@ -251,8 +250,8 @@ class HtmlDoc(BaseDoc, TextDoc): def copy_file(self, from_fname, to_fname, to_dir=''): """ - Copy a file from a source to a (report) destination. - If to_dir is not present, then the destination directory will be created. + Copy a file from a source to a (report) destination. If to_dir is not + present, then the destination directory will be created. Normally 'to_fname' will be just a filename, without directory path. @@ -285,7 +284,8 @@ class HtmlDoc(BaseDoc, TextDoc): Copy support files to the datadir that needs to hold them """ #css of textdoc styles - with open(os.path.join(self._backend.datadirfull(), _TEXTDOCSCREEN), 'w') as tdfile: + with open(os.path.join(self._backend.datadirfull(), + _TEXTDOCSCREEN), 'w') as tdfile: tdfile.write(self.style_declaration) #css file if self.css_filename: @@ -295,7 +295,7 @@ class HtmlDoc(BaseDoc, TextDoc): self.copy_file(fullpath, _HTMLSCREEN) #favicon self.copy_file(os.path.join(IMAGE_DIR, 'webstuff', 'favicon.ico'), - 'favicon.ico') + 'favicon.ico') def __reduce_list(self): """ @@ -315,9 +315,9 @@ class HtmlDoc(BaseDoc, TextDoc): """ if not markup: text = self._backend.ESCAPE_FUNC()(text) - if self.__title_written == 0 : + if self.__title_written == 0: self.title += text - if links == True: + if links is True: import re text = re.sub(URL_PATTERN, _CLICKABLE, text) self.htmllist[-1] += text @@ -352,7 +352,7 @@ class HtmlDoc(BaseDoc, TextDoc): styles = self.get_style_sheet() self._tbl = styles.get_table_style(style) self.htmllist += [Html('table', width=str(self._tbl.get_width())+'%', - cellspacing='0')] + cellspacing='0')] def end_table(self): """ @@ -384,14 +384,13 @@ class HtmlDoc(BaseDoc, TextDoc): tag = "td" self._empty = 1 if span > 1: - self.htmllist += (Html(tag, colspan=str(span), - class_=style_name),) + self.htmllist += (Html(tag, colspan=str(span), class_=style_name),) self._col += span else: self.htmllist += (Html(tag, colspan=str(span), - width=str(self._tbl.get_column_width( - self._col))+ '%', - class_=style_name),) + width=str(self._tbl.get_column_width( + self._col))+ '%', + class_=style_name),) self._col += 1 def end_cell(self): @@ -419,7 +418,7 @@ class HtmlDoc(BaseDoc, TextDoc): inline=True),) else: self.htmllist += (Html('h1', class_=style_name, inline=True),) - elif 2<= level <= 5: + elif 2 <= level <= 5: tag = 'h'+str(level+1) self.htmllist += (Html(tag, class_=style_name, inline=True),) else: @@ -506,7 +505,7 @@ class HtmlDoc(BaseDoc, TextDoc): for line in markuptext.split('\n'): [line, sigcount] = process_spaces(line, format) if sigcount == 0: - if inpara == False: + if inpara is False: # needed for runs of three or more newlines self.start_paragraph(style_name) inpara = True @@ -515,7 +514,7 @@ class HtmlDoc(BaseDoc, TextDoc): inpara = False linenb = 1 else: - if inpara == False: + if inpara is False: self.start_paragraph(style_name) inpara = True self._empty = 1 # para is empty @@ -524,19 +523,20 @@ class HtmlDoc(BaseDoc, TextDoc): self.__write_text(line, markup=True, links=links) self._empty = 0 # para is not empty linenb += 1 - if inpara == True: + if inpara is True: self.end_paragraph() if sigcount == 0: - # if the last line was blank, then as well as outputting the previous para, - # which we have just done, - # we also output a new blank para + # if the last line was blank, then as well as outputting the + # previous para, which we have just done, we also output a new + # blank para self.start_paragraph(style_name) self._empty = 1 # para is empty self.end_paragraph() #end div element self.__reduce_list() - def add_media(self, name, pos, w_cm, h_cm, alt='', style_name=None, crop=None): + def add_media(self, name, pos, w_cm, h_cm, alt='', style_name=None, + crop=None): """ Overwrite base method """ @@ -547,35 +547,36 @@ class HtmlDoc(BaseDoc, TextDoc): imdir = self._backend.datadirfull() try: - resize_to_jpeg(name, imdir + os.sep + refname, size, size, crop=crop) + resize_to_jpeg(name, imdir + os.sep + refname, size, size, + crop=crop) except: - LOG.warn(_("Could not create jpeg version of image %(name)s") % - {'name' : name}) + LOG.warning(_("Could not create jpeg version of image %(name)s"), + name) return if len(alt): alt = '
'.join(alt) - if pos not in ["right", "left"] : + if pos not in ["right", "left"]: if len(alt): self.htmllist[-1] += Html('div') + ( - Html('img', src= imdir + os.sep + refname, - border = '0', alt=alt), + Html('img', src=imdir + os.sep + refname, + border='0', alt=alt), Html('p', class_="DDR-Caption") + alt ) else: - self.htmllist[-1] += Html('img', src= imdir + os.sep + refname, - border = '0', alt=alt) + self.htmllist[-1] += Html('img', src=imdir + os.sep + refname, + border='0', alt=alt) else: if len(alt): - self.htmllist[-1] += Html('div', style_="float: %s; padding: 5px; margin: 0;" % pos) + ( - Html('img', src= imdir + os.sep + refname, - border = '0', alt=alt), - Html('p', class_="DDR-Caption") + alt - ) + self.htmllist[-1] += Html( + 'div', style_="float: %s; padding: 5px; margin: 0;" % pos + ) + (Html('img', src=imdir + os.sep + refname, + border='0', alt=alt), + Html('p', class_="DDR-Caption") + alt) else: - self.htmllist[-1] += Html('img', src= imdir + os.sep + refname, - border = '0', alt=alt, align=pos) + self.htmllist[-1] += Html('img', src=imdir + os.sep + refname, + border='0', alt=alt, align=pos) def page_break(self): """