Modernize code for easier Python 3 port

- Replace print statement with function call
- Replace except ..., ...: syntax with except ... as ...:
- Simplify obsolete try ... except ... code block
- Replace some unicode calls with u-prefixed strings
- Do not use `len(SEQUENCE)` to determine if a sequence is empty
- Replace dictionary creation
- Drop support for GObject static bindings
This commit is contained in:
JoeLametta
2019-01-16 21:08:43 +00:00
parent 0e17b32740
commit fef7973113
6 changed files with 17 additions and 28 deletions

View File

@@ -64,4 +64,4 @@ if len(line) > 11:
line = line[:-2] + '"'
lines.append(line)
print "\n".join(lines)
print("\n".join(lines))

View File

@@ -59,9 +59,7 @@ retrieves and display accuraterip data from the given URL
assert len(r.checksums) == r.num_tracks
assert len(r.confidences) == r.num_tracks
entry = {}
entry["confidence"] = r.confidences[track]
entry["response"] = i + 1
entry = {"confidence": r.confidences[track], "response": i + 1}
checksum = r.checksums[track]
if checksum in checksums:
checksums[checksum].append(entry)

View File

@@ -53,7 +53,7 @@ class Popen(subprocess.Popen):
(errCode, written) = WriteFile(x, input)
except ValueError:
return self._close('stdin')
except (subprocess.pywintypes.error, Exception), why:
except (subprocess.pywintypes.error, Exception) as why:
if why.args[0] in (109, errno.ESHUTDOWN):
return self._close('stdin')
raise
@@ -74,7 +74,7 @@ class Popen(subprocess.Popen):
(errCode, read) = ReadFile(x, nAvail, None)
except ValueError:
return self._close(which)
except (subprocess.pywintypes.error, Exception), why:
except (subprocess.pywintypes.error, Exception) as why:
if why.args[0] in (109, errno.ESHUTDOWN):
return self._close(which)
raise
@@ -94,7 +94,7 @@ class Popen(subprocess.Popen):
try:
written = os.write(self.stdin.fileno(), input)
except OSError, why:
except OSError as why:
if why.args[0] == errno.EPIPE: # broken pipe
return self._close('stdin')
raise
@@ -153,7 +153,7 @@ def recv_some(p, t=.1, e=1, tr=5, stderr=0):
def send_all(p, data):
while len(data):
while data:
sent = p.send(data)
if sent is None:
raise Exception(message)

View File

@@ -22,10 +22,7 @@ from __future__ import print_function
import logging
import sys
try:
from gi.repository import GLib as gobject
except ImportError:
import gobject
from gi.repository import GLib as GLib
logger = logging.getLogger(__name__)
@@ -463,7 +460,7 @@ class TaskRunner(LogStub):
class SyncRunner(TaskRunner, ITaskListener):
"""
I run the task synchronously in a gobject MainLoop.
I run the task synchronously in a GObject MainLoop.
"""
def __init__(self, verbose=True):
@@ -478,11 +475,11 @@ class SyncRunner(TaskRunner, ITaskListener):
self._verboseRun = verbose
self._skip = skip
self._loop = gobject.MainLoop()
self._loop = GLib.MainLoop()
self._task.addListener(self)
# only start the task after going into the mainloop,
# otherwise the task might complete before we are in it
gobject.timeout_add(0L, self._startWrap, self._task)
GLib.timeout_add(0L, self._startWrap, self._task)
self.debug('run loop')
self._loop.run()
@@ -526,7 +523,7 @@ class SyncRunner(TaskRunner, ITaskListener):
self.debug('schedule: scheduling %r(*args=%r, **kwargs=%r)',
callable, args, kwargs)
gobject.timeout_add(int(delta * 1000L), c)
GLib.timeout_add(int(delta * 1000L), c)
# ITaskListener methods
def progressed(self, task, value):

View File

@@ -339,13 +339,9 @@ class Table(object):
values = self._getMusicBrainzValues()
# MusicBrainz disc id does not take into account data tracks
# P2.3
try:
import hashlib
sha1 = hashlib.sha1
except ImportError:
from sha import sha as sha1
import base64
import hashlib
sha1 = hashlib.sha1
sha = sha1()

View File

@@ -15,9 +15,8 @@ class PathTestCase(unittest.TestCase):
path = prog.getPath(u'/tmp', DEFAULT_DISC_TEMPLATE,
'mbdiscid', None)
self.assertEqual(path,
unicode('/tmp/unknown/Unknown Artist - mbdiscid/'
'Unknown Artist - mbdiscid'))
self.assertEqual(path, (u'/tmp/unknown/Unknown Artist - mbdiscid/'
u'Unknown Artist - mbdiscid'))
def testStandardTemplateFilled(self):
prog = program.Program(config.Config())
@@ -27,9 +26,8 @@ class PathTestCase(unittest.TestCase):
path = prog.getPath(u'/tmp', DEFAULT_DISC_TEMPLATE,
'mbdiscid', md, 0)
self.assertEqual(path,
unicode('/tmp/unknown/Jeff Buckley - Grace/'
'Jeff Buckley - Grace'))
self.assertEqual(path, (u'/tmp/unknown/Jeff Buckley - Grace/'
u'Jeff Buckley - Grace'))
def testIssue66TemplateFilled(self):
prog = program.Program(config.Config())