diff --git a/ChangeLog b/ChangeLog index cb8237f..6a17ca1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2012-11-19 Thomas Vander Stichele + + * morituri/common/common.py: + * morituri/image/cue.py: + * morituri/image/toc.py: + factor out getRealPath + 2012-11-19 Thomas Vander Stichele * morituri/image/toc.py: diff --git a/morituri/common/common.py b/morituri/common/common.py index cf0cac7..bf8dd00 100644 --- a/morituri/common/common.py +++ b/morituri/common/common.py @@ -295,3 +295,42 @@ def shrinkPath(path): parts[-1] = u'%s%s' % (name, ext) path = os.path.join(*parts) return path + +def getRealPath(refPath, filePath): + """ + Translate a .cue or .toc's FILE to an existing path. + + @type refPath: unicode + @type filePath: unicode + """ + assert type(filePath) is unicode, "%r is not unicode" % filePath + + if os.path.exists(filePath): + return filePath + + # .cue FILE statements have Windows-style path separators, so convert + parts = filePath.split('\\') + if parts[0] == '': + parts[0] = os.path.sep + tpath = os.path.join(*parts) + candidatePaths = [] + + if tpath == os.path.abspath(tpath): + candidatePaths.append(tpath) + else: + # if the path is relative: + # - check relatively to the cue file + # - check only the filename part relative to the cue file + candidatePaths.append(os.path.join( + os.path.dirname(refPath), tpath)) + candidatePaths.append(os.path.join( + os.path.dirname(refPath), os.path.basename(tpath))) + + for candidate in candidatePaths: + noext, _ = os.path.splitext(candidate) + for ext in ['wav', 'flac']: + cpath = '%s.%s' % (noext, ext) + if os.path.exists(cpath): + return cpath + + raise KeyError("Cannot find file for %r" % filePath) diff --git a/morituri/image/cue.py b/morituri/image/cue.py index 686c966..7e6580f 100644 --- a/morituri/image/cue.py +++ b/morituri/image/cue.py @@ -184,34 +184,7 @@ class CueFile(object, log.Loggable): @type path: unicode """ - assert type(path) is unicode, "%r is not unicode" % path - - if os.path.exists(path): - return path - - # .cue FILE statements have Windows-style path separators, so convert - tpath = os.path.join(*path.split('\\')) - candidatePaths = [] - - # if the path is relative: - # - check relatively to the cue file - # - check only the filename part relative to the cue file - if tpath == os.path.abspath(tpath): - candidatePaths.append(tpath) - else: - candidatePaths.append(os.path.join( - os.path.dirname(self._path), tpath)) - candidatePaths.append(os.path.join( - os.path.dirname(self._path), os.path.basename(tpath))) - - for candidate in candidatePaths: - noext, _ = os.path.splitext(candidate) - for ext in ['wav', 'flac']: - cpath = '%s.%s' % (noext, ext) - if os.path.exists(cpath): - return cpath - - raise KeyError("Cannot find file for %r" % path) + return common.getRealPath(self._path, path) class File: diff --git a/morituri/image/toc.py b/morituri/image/toc.py index 70d5d0c..f94de57 100644 --- a/morituri/image/toc.py +++ b/morituri/image/toc.py @@ -328,37 +328,7 @@ class TocFile(object, log.Loggable): @type path: unicode """ - assert type(path) is unicode, "%r is not unicode" % path - - if os.path.exists(path): - return path - - # .cue FILE statements have Windows-style path separators, so convert - parts = path.split('\\') - if parts[0] == '': - parts[0] = os.path.sep - tpath = os.path.join(*parts) - candidatePaths = [] - - if tpath == os.path.abspath(tpath): - candidatePaths.append(tpath) - else: - # if the path is relative: - # - check relatively to the cue file - # - check only the filename part relative to the cue file - candidatePaths.append(os.path.join( - os.path.dirname(self._path), tpath)) - candidatePaths.append(os.path.join( - os.path.dirname(self._path), os.path.basename(tpath))) - - for candidate in candidatePaths: - noext, _ = os.path.splitext(candidate) - for ext in ['wav', 'flac']: - cpath = '%s.%s' % (noext, ext) - if os.path.exists(cpath): - return cpath - - raise KeyError("Cannot find file for %r" % path) + return common.getRealPath(self._path, path) class File: