[SCM] morituri/master: * morituri/common/common.py: * morituri/image/cue.py: * morituri/image/toc.py: factor out getRealPath

js at users.alioth.debian.org js at users.alioth.debian.org
Sun Oct 19 20:09:44 UTC 2014


The following commit has been merged in the master branch:
commit 794a4c3f5ce1db3b94d0182a76074fe5b559be88
Author: Thomas Vander Stichele <thomas (at) apestaart (dot) org>
Date:   Mon Nov 19 08:56:17 2012 +0000

    	* morituri/common/common.py:
    	* morituri/image/cue.py:
    	* morituri/image/toc.py:
    	  factor out getRealPath

diff --git a/ChangeLog b/ChangeLog
index cb8237f..6a17ca1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
 2012-11-19  Thomas Vander Stichele  <thomas at apestaart dot org>
 
+	* morituri/common/common.py:
+	* morituri/image/cue.py:
+	* morituri/image/toc.py:
+	  factor out getRealPath
+
+2012-11-19  Thomas Vander Stichele  <thomas at apestaart dot org>
+
 	* morituri/image/toc.py:
 	* morituri/test/test_image_toc.py:
 	  Add test for getRealPath.  Fix bug found by it for absolute paths.
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:

-- 
morituri packaging



More information about the pkg-multimedia-commits mailing list