From d579efa0414e15f7c7c5bd6daf20750429ac0605 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Mon, 17 Oct 2016 22:32:43 -0700 Subject: [PATCH] [Lint] Fix various pylint warnings and fixup code * Use print function * Fix except as statements * Remove old twisted 8 code * Remove empty docstring * Refactor try statement to only contain the relevant import and disable pylint import msgs. * Use flake8 noqa and pylint comment and drop pyflakes workarounds. --- deluge/common.py | 5 ++-- .../WebUi/deluge/plugins/webui/core.py | 3 +-- deluge/tests/common.py | 8 +++--- deluge/tests/daemon_base.py | 4 ++- deluge/tests/test_files_tab.py | 8 +++--- deluge/tests/test_httpdownloader.py | 8 +----- deluge/tests/test_torrentview.py | 13 +++++----- deluge/tests/test_webserver.py | 3 --- deluge/ui/gtkui/preferences.py | 3 +-- deluge/ui/tracker_icons.py | 10 ++------ msgfmt.py | 25 ++++++++++--------- setup.py | 4 +-- 12 files changed, 43 insertions(+), 51 deletions(-) diff --git a/deluge/common.py b/deluge/common.py index 70d06abbd..59289a9af 100644 --- a/deluge/common.py +++ b/deluge/common.py @@ -8,6 +8,7 @@ # """Common functions for various parts of Deluge to use.""" +from __future__ import print_function import base64 import functools @@ -1047,14 +1048,14 @@ def run_profiled(func, *args, **kwargs): if output_file: profiler.dump_stats(output_file) log.info("Profile stats saved to %s", output_file) - print "Profile stats saved to %s" % output_file + print("Profile stats saved to %s" % output_file) else: import pstats import StringIO strio = StringIO.StringIO() ps = pstats.Stats(profiler, stream=strio).sort_stats('cumulative') ps.print_stats() - print strio.getvalue() + print(strio.getvalue()) try: return profiler.runcall(func, *args) diff --git a/deluge/plugins/WebUi/deluge/plugins/webui/core.py b/deluge/plugins/WebUi/deluge/plugins/webui/core.py index d617d105e..3c803847d 100644 --- a/deluge/plugins/WebUi/deluge/plugins/webui/core.py +++ b/deluge/plugins/WebUi/deluge/plugins/webui/core.py @@ -50,8 +50,7 @@ class Core(CorePluginBase): @export def got_deluge_web(self): try: - from deluge.ui.web import server - assert server # silence pyflakes + from deluge.ui.web import server # noqa pylint: disable=unused-import return True except ImportError: return False diff --git a/deluge/tests/common.py b/deluge/tests/common.py index d1de80792..9f3fd1790 100644 --- a/deluge/tests/common.py +++ b/deluge/tests/common.py @@ -7,6 +7,8 @@ # See LICENSE for more details. # +from __future__ import print_function + import os import sys import tempfile @@ -64,7 +66,7 @@ def add_watchdog(deferred, timeout=0.05, message=None): watchdog.cancel() if not deferred.called: if message: - print message + print(message) deferred.cancel() return value @@ -190,7 +192,7 @@ class ProcessOutputHandler(protocol.ProcessProtocol): if self.check_callbacks(data): pass elif '[ERROR' in data: - print data, + print(data, end=' ') def errReceived(self, data): # NOQA """Process output from stderr""" @@ -201,7 +203,7 @@ class ProcessOutputHandler(protocol.ProcessProtocol): return data = "\n%s" % data.strip() prefixed = data.replace("\n", "\nSTDERR: ") - print "\n%s" % prefixed + print("\n%s" % prefixed) def start_core(listen_port=58846, logfile=None, timeout=10, timeout_msg=None, diff --git a/deluge/tests/daemon_base.py b/deluge/tests/daemon_base.py index ccfaf790d..10ff951f8 100644 --- a/deluge/tests/daemon_base.py +++ b/deluge/tests/daemon_base.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import os.path import pytest @@ -20,7 +22,7 @@ class DaemonBase(object): def terminate_core(self, *args): if args[0] is not None: if hasattr(args[0], "getTraceback"): - print "terminate_core: Errback Exception: %s" % args[0].getTraceback() + print("terminate_core: Errback Exception: %s" % args[0].getTraceback()) if not self.core.killed: d = self.core.kill() diff --git a/deluge/tests/test_files_tab.py b/deluge/tests/test_files_tab.py index e27e376ef..4b749e5fc 100644 --- a/deluge/tests/test_files_tab.py +++ b/deluge/tests/test_files_tab.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import pytest from twisted.trial import unittest @@ -46,7 +48,7 @@ class FilesTabTestCase(BaseTestCase): level = 1 def p_level(s, l): - print "%s%s" % (" " * l, s) + print("%s%s" % (" " * l, s)) def _print_treestore_children(i, lvl): while i: @@ -55,9 +57,9 @@ class FilesTabTestCase(BaseTestCase): _print_treestore_children(treestore.iter_children(i), lvl + 2) i = treestore.iter_next(i) - print "\n%s" % title + print("\n%s" % title) _print_treestore_children(root, level) - print "" + print("") def verify_treestore(self, treestore, tree): diff --git a/deluge/tests/test_httpdownloader.py b/deluge/tests/test_httpdownloader.py index 3243943ce..d68f3ffdb 100644 --- a/deluge/tests/test_httpdownloader.py +++ b/deluge/tests/test_httpdownloader.py @@ -7,6 +7,7 @@ from twisted.python.failure import Failure from twisted.trial import unittest from twisted.web.error import PageRedirect from twisted.web.http import NOT_MODIFIED +from twisted.web.resource import Resource from twisted.web.server import Site from twisted.web.util import redirectTo @@ -14,13 +15,6 @@ from deluge.httpdownloader import download_file from deluge.log import setup_logger from deluge.ui.web.common import compress -try: - from twisted.web.resource import Resource -except ImportError: - # twisted 8 - from twisted.web.error import Resource - - temp_dir = tempfile.mkdtemp() diff --git a/deluge/tests/test_torrentview.py b/deluge/tests/test_torrentview.py index 8db0c12e0..5579a66ad 100644 --- a/deluge/tests/test_torrentview.py +++ b/deluge/tests/test_torrentview.py @@ -8,20 +8,21 @@ from deluge.ui.util import lang from . import common from .basetest import BaseTestCase -libs_available = True # Allow running other tests without GTKUI dependencies available try: from gobject import TYPE_UINT64 - from deluge.ui.gtkui.mainwindow import MainWindow - from deluge.ui.gtkui.menubar import MenuBar - from deluge.ui.gtkui.torrentdetails import TorrentDetails - from deluge.ui.gtkui.torrentview import TorrentView - from deluge.ui.gtkui.gtkui import DEFAULT_PREFS except ImportError as err: libs_available = False TYPE_UINT64 = "Whatever" import traceback traceback.print_exc() +else: + libs_available = True + from deluge.ui.gtkui.mainwindow import MainWindow # pylint: disable=ungrouped-imports + from deluge.ui.gtkui.menubar import MenuBar + from deluge.ui.gtkui.torrentdetails import TorrentDetails + from deluge.ui.gtkui.torrentview import TorrentView + from deluge.ui.gtkui.gtkui import DEFAULT_PREFS lang.setup_translations() diff --git a/deluge/tests/test_webserver.py b/deluge/tests/test_webserver.py index f4be92700..1350dc9c3 100644 --- a/deluge/tests/test_webserver.py +++ b/deluge/tests/test_webserver.py @@ -26,9 +26,6 @@ class WebServerTestCase(WebServerTestBase, WebServerMockBase): @defer.inlineCallbacks def test_get_torrent_info(self): - """ - - """ agent = Agent(reactor) self.mock_authentication_ignore(self.deluge_web.auth) diff --git a/deluge/ui/gtkui/preferences.py b/deluge/ui/gtkui/preferences.py index a51c98e96..cb22becf5 100644 --- a/deluge/ui/gtkui/preferences.py +++ b/deluge/ui/gtkui/preferences.py @@ -150,8 +150,7 @@ class Preferences(component.Component): if not deluge.common.osx_check() and not deluge.common.windows_check(): try: - import appindicator - assert appindicator # silence pyflakes + import appindicator # noqa pylint: disable=unused-import except ImportError: pass else: diff --git a/deluge/ui/tracker_icons.py b/deluge/ui/tracker_icons.py index e59a257bf..a6ff56bf8 100644 --- a/deluge/ui/tracker_icons.py +++ b/deluge/ui/tracker_icons.py @@ -15,25 +15,19 @@ from urlparse import urljoin, urlparse from twisted.internet import defer, threads from twisted.web.error import PageRedirect +from twisted.web.resource import ForbiddenResource, NoResource from deluge.component import Component from deluge.configmanager import get_config_dir from deluge.decorators import proxy from deluge.httpdownloader import download_file -try: - from twisted.web.resource import NoResource, ForbiddenResource -except ImportError: - # twisted 8 - from twisted.web.error import NoResource, ForbiddenResource - try: import PIL.Image as Image - import deluge.ui.Win32IconImagePlugin - assert deluge.ui.Win32IconImagePlugin # silence pyflakes except ImportError: PIL_INSTALLED = False else: + import deluge.ui.Win32IconImagePlugin # noqa pylint: disable=unused-import, ungrouped-imports PIL_INSTALLED = True log = logging.getLogger(__name__) diff --git a/msgfmt.py b/msgfmt.py index 81510e19c..fb78532fb 100755 --- a/msgfmt.py +++ b/msgfmt.py @@ -25,6 +25,7 @@ Options: --version Display version information and exit. """ +from __future__ import print_function import array import ast @@ -42,9 +43,9 @@ def usage(ecode, msg=''): """ Print usage and msg and exit with given code. """ - print >> sys.stderr, __doc__ + print(__doc__, file=sys.stderr) if msg: - print >> sys.stderr, msg + print(msg, file=sys.stderr) sys.exit(ecode) @@ -115,8 +116,8 @@ def make(filename, outfile): try: with open(infile) as _file: lines = _file.readlines() - except IOError, msg: - print >> sys.stderr, msg + except IOError as msg: + print(msg, file=sys.stderr) sys.exit(1) section = None @@ -170,8 +171,8 @@ def make(filename, outfile): elif section == section_str: msgstr += l else: - print >> sys.stderr, 'Syntax error on %s:%d' % (infile, lno), 'before:' - print >> sys.stderr, l + print('Syntax error on %s:%d' % (infile, lno), 'before:', file=sys.stderr) + print(l, file=sys.stderr) sys.exit(1) # Add last entry if section == section_str: @@ -183,15 +184,15 @@ def make(filename, outfile): try: with open(outfile, "wb") as _file: _file.write(output) - except IOError, msg: - print >> sys.stderr, msg + except IOError as msg: + print(msg, file=sys.stderr) def main(): try: opts, args = getopt.getopt(sys.argv[1:], 'hVo:', ['help', 'version', 'output-file=']) - except getopt.error, msg: + except getopt.error as msg: usage(1, msg) outfile = None @@ -200,14 +201,14 @@ def main(): if opt in ('-h', '--help'): usage(0) elif opt in ('-V', '--version'): - print >> sys.stderr, "msgfmt.py", __version__ + print("msgfmt.py", __version__, file=sys.stderr) sys.exit(0) elif opt in ('-o', '--output-file'): outfile = arg # do it if not args: - print >> sys.stderr, 'No input file given' - print >> sys.stderr, "Try `msgfmt --help' for more information." + print('No input file given', file=sys.stderr) + print("Try `msgfmt --help' for more information.", file=sys.stderr) return for filename in args: diff --git a/setup.py b/setup.py index bc1781054..9bc06e39a 100755 --- a/setup.py +++ b/setup.py @@ -232,8 +232,8 @@ class Build(_build): try: from deluge._libtorrent import lt print('Found libtorrent version: %s' % lt.__version__) - except ImportError, e: - print('Warning libtorrent not found: %s' % e) + except ImportError as ex: + print('Warning libtorrent not found: %s' % ex) class InstallData(_install_data):