improve pylint score (to over 9.00)

This commit is contained in:
Paul Franklin 2016-08-29 20:21:56 -07:00
parent a7e47bba3a
commit f389df59c4

View File

@ -18,6 +18,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.
# #
""" Container class for managing the generic filters """
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
# #
# Standard Python modules # Standard Python modules
@ -25,7 +27,6 @@
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from xml.sax import make_parser, SAXParseException from xml.sax import make_parser, SAXParseException
import os import os
import sys
import collections import collections
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -35,7 +36,6 @@ import collections
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
from ._filterparser import FilterParser from ._filterparser import FilterParser
from ..plug import BasePluginManager from ..plug import BasePluginManager
from ..const import GRAMPS_LOCALE as glocale
PLUGMAN = BasePluginManager.get_instance() PLUGMAN = BasePluginManager.get_instance()
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -92,13 +92,15 @@ class FilterList:
return filters return filters
def add(self, namespace, filt): def add(self, namespace, filt):
assert(isinstance(namespace, str)) """ add a custom filter """
assert isinstance(namespace, str)
if namespace not in self.filter_namespaces: if namespace not in self.filter_namespaces:
self.filter_namespaces[namespace] = [] self.filter_namespaces[namespace] = []
self.filter_namespaces[namespace].append(filt) self.filter_namespaces[namespace].append(filt)
def load(self): def load(self):
""" load a custom filter """
try: try:
if os.path.isfile(self.file): if os.path.isfile(self.file):
parser = make_parser() parser = make_parser()
@ -111,36 +113,40 @@ class FilterList:
print("Parser error") print("Parser error")
def fix(self, line): def fix(self, line):
l = line.strip() """ sanitize the custom filter name, if needed """
l = l.replace('&', '&') new_line = line.strip()
l = l.replace('>', '>') new_line = new_line.replace('&', '&')
l = l.replace('<', '&lt;') new_line = new_line.replace('>', '&gt;')
return l.replace('"', '&quot;') new_line = new_line.replace('<', '&lt;')
return new_line.replace('"', '&quot;')
def save(self): def save(self):
with open(self.file, 'w', encoding='utf8') as f: """ save the list of custom filters """
f.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n") with open(self.file, 'w', encoding='utf8') as file:
f.write('<filters>\n') file.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
file.write('<filters>\n')
for namespace in self.filter_namespaces: for namespace in self.filter_namespaces:
f.write('<object type="%s">\n' % namespace) file.write(' <object type="%s">\n' % namespace)
filter_list = self.filter_namespaces[namespace] filter_list = self.filter_namespaces[namespace]
sorted_filters = sorted([(filter.get_name(), filter) sorted_filters = sorted([(filter.get_name(), filter)
for filter in filter_list]) for filter in filter_list])
for (name, the_filter) in sorted_filters: # enable a diff for (name, the_filter) in sorted_filters: # enable a diff
f.write(' <filter name="%s"' % self.fix(name)) file.write(' <filter name="%s"' % self.fix(name))
f.write(' function="%s"' % the_filter.get_logical_op()) file.write(' function="%s"' % the_filter.get_logical_op())
if the_filter.invert: if the_filter.invert:
f.write(' invert="1"') file.write(' invert="1"')
comment = the_filter.get_comment() comment = the_filter.get_comment()
if comment: if comment:
f.write(' comment="%s"' % self.fix(comment)) file.write(' comment="%s"' % self.fix(comment))
f.write('>\n') file.write('>\n')
for rule in the_filter.get_rules(): for rule in the_filter.get_rules():
f.write(' <rule class="%s" use_regex="%s">\n' file.write(' <rule class="%s" use_regex="%s">'
% (rule.__class__.__name__, rule.use_regex)) '\n' % (rule.__class__.__name__,
rule.use_regex))
for value in list(rule.values()): for value in list(rule.values()):
f.write(' <arg value="%s"/>\n' % self.fix(value)) file.write(' <arg value="%s"/>'
f.write(' </rule>\n') '\n' % self.fix(value))
f.write(' </filter>\n') file.write(' </rule>\n')
f.write('</object>\n') file.write(' </filter>\n')
f.write('</filters>\n') file.write(' </object>\n')
file.write('</filters>\n')