From 35201d52902dedea60fc2fdd1b69851b5c1326b7 Mon Sep 17 00:00:00 2001 From: JoeLametta Date: Sat, 10 Aug 2019 09:10:00 +0000 Subject: [PATCH] Address errors, improvements, formatting - Removed unused code not portable due to buffer() use - raw_input() does not exist in Python 3 - Fixed octal constant syntax for Python 3 - Fixed TypeError - Replace if not exists: makedirs(path) with single call: using makedirs(path, exist_ok=True) - Class inherits from object, can be safely removed from bases in python3: pylint's useless-object-inheritance (W0235) check Signed-off-by: JoeLametta --- whipper/common/accurip.py | 11 ++++------- whipper/common/common.py | 2 +- whipper/common/directory.py | 11 ++++------- whipper/common/mbngs.py | 4 ++-- whipper/common/path.py | 2 +- whipper/common/program.py | 5 ++--- whipper/common/renamer.py | 4 ++-- whipper/extern/asyncsub.py | 24 ------------------------ whipper/extern/freedb.py | 5 +---- whipper/extern/task/task.py | 4 ++-- whipper/image/cue.py | 6 +++--- whipper/image/image.py | 2 +- whipper/image/table.py | 2 +- whipper/image/toc.py | 2 +- whipper/program/cdparanoia.py | 2 +- whipper/program/cdrdao.py | 3 +-- whipper/result/result.py | 4 ++-- whipper/test/test_result_logger.py | 3 ++- 18 files changed, 31 insertions(+), 65 deletions(-) diff --git a/whipper/common/accurip.py b/whipper/common/accurip.py index 0cd0787..7ddfb41 100644 --- a/whipper/common/accurip.py +++ b/whipper/common/accurip.py @@ -21,7 +21,6 @@ import requests import struct -from errno import EEXIST from os import makedirs from os.path import dirname, exists, join @@ -40,7 +39,7 @@ class EntryNotFound(Exception): pass -class _AccurateRipResponse(object): +class _AccurateRipResponse: """ An AccurateRip response contains a collection of metadata identifying a particular digital audio compact disc. @@ -143,13 +142,11 @@ def _download_entry(path): def _save_entry(raw_entry, path): logger.debug('saving AccurateRip entry to %s', path) - # XXX: os.makedirs(exist_ok=True) in py3 try: - makedirs(dirname(path)) + makedirs(dirname(path), exist_ok=True) except OSError as e: - if e.errno != EEXIST: - logger.error('could not save entry to %s: %s', path, e) - return + logger.error('could not save entry to %s: %s', path, e) + return open(path, 'wb').write(raw_entry) diff --git a/whipper/common/common.py b/whipper/common/common.py index 3a06176..6c2ead6 100644 --- a/whipper/common/common.py +++ b/whipper/common/common.py @@ -292,7 +292,7 @@ def validate_template(template, kind): 'variable(s): {}'.format(', '.join(matches))) -class VersionGetter(object): +class VersionGetter: """ I get the version of a program by looking for it in command output according to a regexp. diff --git a/whipper/common/directory.py b/whipper/common/directory.py index 910a42e..55b3880 100644 --- a/whipper/common/directory.py +++ b/whipper/common/directory.py @@ -19,14 +19,13 @@ # along with whipper. If not, see . from os import getenv, makedirs -from os.path import join, expanduser, exists +from os.path import join, expanduser def config_path(): path = join(getenv('XDG_CONFIG_HOME') or join(expanduser('~'), '.config'), 'whipper') - if not exists(path): - makedirs(path) + makedirs(path, exist_ok=True) return join(path, 'whipper.conf') @@ -35,8 +34,7 @@ def cache_path(name=None): 'whipper') if name: path = join(path, name) - if not exists(path): - makedirs(path) + makedirs(path, exist_ok=True) return path @@ -46,6 +44,5 @@ def data_path(name=None): 'whipper') if name: path = join(path, name) - if not exists(path): - makedirs(path) + makedirs(path, exist_ok=True) return path diff --git a/whipper/common/mbngs.py b/whipper/common/mbngs.py index 5017073..f6b014f 100644 --- a/whipper/common/mbngs.py +++ b/whipper/common/mbngs.py @@ -45,7 +45,7 @@ class NotFoundException(MusicBrainzException): return "Disc not found in MusicBrainz" -class TrackMetadata(object): +class TrackMetadata: artist = None title = None duration = None # in ms @@ -56,7 +56,7 @@ class TrackMetadata(object): mbidWorks = [] -class DiscMetadata(object): +class DiscMetadata: """ :param artist: artist(s) name :param sortName: release artist sort name diff --git a/whipper/common/path.py b/whipper/common/path.py index 7c01d8f..43c2353 100644 --- a/whipper/common/path.py +++ b/whipper/common/path.py @@ -21,7 +21,7 @@ import re -class PathFilter(object): +class PathFilter: """ I filter path components for safe storage on file systems. """ diff --git a/whipper/common/program.py b/whipper/common/program.py index 5774862..1d829ae 100644 --- a/whipper/common/program.py +++ b/whipper/common/program.py @@ -333,7 +333,7 @@ class Program: if prompt: guess = (deltas[lowest])[0].mbid - release = raw_input( + release = input( "\nPlease select a release [%s]: " % guess) if not release: @@ -505,8 +505,7 @@ class Program: stop = self.result.table.getTrackEnd(trackResult.number) dirname = os.path.dirname(trackResult.filename) - if not os.path.exists(dirname): - os.makedirs(dirname) + os.makedirs(dirname, exist_ok=True) if not what: what = 'track %d' % (trackResult.number, ) diff --git a/whipper/common/renamer.py b/whipper/common/renamer.py index 22be22a..d2af230 100644 --- a/whipper/common/renamer.py +++ b/whipper/common/renamer.py @@ -24,7 +24,7 @@ import tempfile """Rename files on file system and inside metafiles in a resumable way.""" -class Operator(object): +class Operator: def __init__(self, statePath, key): self._todo = [] @@ -116,7 +116,7 @@ class FileRenamer(Operator): """ -class Operation(object): +class Operation: def verify(self): """ diff --git a/whipper/extern/asyncsub.py b/whipper/extern/asyncsub.py index a2fe021..767752b 100644 --- a/whipper/extern/asyncsub.py +++ b/whipper/extern/asyncsub.py @@ -150,27 +150,3 @@ def recv_some(p, t=.1, e=1, tr=5, stderr=0): else: time.sleep(max((x - time.time()) / tr, 0)) return ''.join(x.decode() for x in y).encode() - - -def send_all(p, data): - while data: - sent = p.send(data) - if sent is None: - raise Exception(message) - data = buffer(data, sent) - - -if __name__ == '__main__': - if sys.platform == 'win32': - shell, commands, tail = ('cmd', ('dir /w', 'echo HELLO WORLD'), '\r\n') - else: - shell, commands, tail = ('sh', ('ls', 'echo HELLO WORLD'), '\n') - - a = Popen(shell, stdin=PIPE, stdout=PIPE) - print(recv_some(a)) - for cmd in commands: - send_all(a, cmd + tail) - print(recv_some(a)) - send_all(a, 'exit' + tail) - print(recv_some(a, e=0)) - a.wait() diff --git a/whipper/extern/freedb.py b/whipper/extern/freedb.py index bd0ee54..4b86de3 100644 --- a/whipper/extern/freedb.py +++ b/whipper/extern/freedb.py @@ -17,16 +17,13 @@ # USA -import sys - - def digit_sum(i): """returns the sum of all digits for the given integer""" return sum(map(int, str(i))) -class DiscID(object): +class DiscID: def __init__(self, offsets, total_length, track_count, playable_length): """offsets is a list of track offsets, in CD frames total_length is the total length of the disc, in seconds diff --git a/whipper/extern/task/task.py b/whipper/extern/task/task.py index cdd572f..d739b7b 100644 --- a/whipper/extern/task/task.py +++ b/whipper/extern/task/task.py @@ -68,7 +68,7 @@ def _getExceptionMessage(exception, frame=-1, filename=None): % locals() -class LogStub(object): +class LogStub: """ I am a stub for a log interface. """ @@ -243,7 +243,7 @@ class Task(LogStub): # FIXME: should this become a real interface, like in zope ? -class ITaskListener(object): +class ITaskListener: """ I am an interface for objects listening to tasks. """ diff --git a/whipper/image/cue.py b/whipper/image/cue.py index a72e36d..64b09a7 100644 --- a/whipper/image/cue.py +++ b/whipper/image/cue.py @@ -58,7 +58,7 @@ _INDEX_RE = re.compile(r""" """, re.VERBOSE) -class CueFile(object): +class CueFile: """ I represent a .cue file as an object. @@ -138,8 +138,8 @@ class CueFile(object): seconds = int(m.expand('\\3')) frames = int(m.expand('\\4')) frameOffset = int(frames - + seconds * common.FRAMES_PER_SECOND - + minutes * common.FRAMES_PER_SECOND * 60) + + seconds * common.FRAMES_PER_SECOND + + minutes * common.FRAMES_PER_SECOND * 60) logger.debug('found index %d of track %r in %r:%d', indexNumber, currentTrack, currentFile.path, diff --git a/whipper/image/image.py b/whipper/image/image.py index 8d51983..c68a6f0 100644 --- a/whipper/image/image.py +++ b/whipper/image/image.py @@ -34,7 +34,7 @@ import logging logger = logging.getLogger(__name__) -class Image(object): +class Image: """ :ivar table: The Table of Contents for this image. :vartype table: table.Table diff --git a/whipper/image/table.py b/whipper/image/table.py index d196835..cbb2711 100644 --- a/whipper/image/table.py +++ b/whipper/image/table.py @@ -157,7 +157,7 @@ class Index: self.number, self.absolute, self.path, self.relative, self.counter) -class Table(object): +class Table: """ I represent a table of indexes on a CD. diff --git a/whipper/image/toc.py b/whipper/image/toc.py index f9cef34..9cf1f12 100644 --- a/whipper/image/toc.py +++ b/whipper/image/toc.py @@ -134,7 +134,7 @@ class Sources: return self._sources[-1][1] -class TocFile(object): +class TocFile: def __init__(self, path): """ diff --git a/whipper/program/cdparanoia.py b/whipper/program/cdparanoia.py index 5478311..636e6f5 100644 --- a/whipper/program/cdparanoia.py +++ b/whipper/program/cdparanoia.py @@ -452,7 +452,7 @@ class ReadVerifyTrackTask(task.MultiSeparateTask): logger.debug('read and verify with taglist %r', taglist) # FIXME: choose a dir on the same disk/dir as the final path fd, tmppath = tempfile.mkstemp(suffix='.whipper.wav') - os.fchmod(fd, 0644) + os.fchmod(fd, 0o644) os.close(fd) self._tmpwavpath = tmppath diff --git a/whipper/program/cdrdao.py b/whipper/program/cdrdao.py index 365c5ad..9bf3399 100644 --- a/whipper/program/cdrdao.py +++ b/whipper/program/cdrdao.py @@ -151,8 +151,7 @@ class ReadTOCTask(task.Task): t_comp = os.path.abspath(self.toc_path).split(os.sep) t_dirn = os.sep.join(t_comp[:-1]) # If the output path doesn't exist, make it recursively - if not os.path.isdir(t_dirn): - os.makedirs(t_dirn) + os.makedirs(t_dirn, exist_ok=True) t_dst = truncate_filename( os.path.join(t_dirn, t_comp[-1] + '.toc')) shutil.copy(self.tocfile, os.path.join(t_dirn, t_dst)) diff --git a/whipper/result/result.py b/whipper/result/result.py index 7947730..51a3d6a 100644 --- a/whipper/result/result.py +++ b/whipper/result/result.py @@ -119,7 +119,7 @@ class RipResult: return None -class Logger(object): +class Logger: """ I log the result of a rip. """ @@ -140,7 +140,7 @@ class Logger(object): # A setuptools-like entry point -class EntryPoint(object): +class EntryPoint: name = 'whipper' @staticmethod diff --git a/whipper/test/test_result_logger.py b/whipper/test/test_result_logger.py index 11fa39b..6e9d509 100644 --- a/whipper/test/test_result_logger.py +++ b/whipper/test/test_result_logger.py @@ -163,7 +163,8 @@ class LoggerTestCase(unittest.TestCase): Dumper=ruamel.yaml.RoundTripDumper ) ) + log_body = "\n".join(actualLines[:-1]).encode() self.assertEqual( parsedLog['SHA-256 hash'], - hashlib.sha256("\n".join(actualLines[:-1])).hexdigest().upper() + hashlib.sha256(log_body).hexdigest().upper() )