[pitivi: 41/65] Extracted duplicated code into Formatter._searchMissingFile



commit 92e306ae79f17edfd533fc89fc95fc4049e680d0
Author: Alex BÄluÈ <alexandru balut gmail com>
Date:   Sat Jun 11 00:52:32 2011 +0200

    Extracted duplicated code into Formatter._searchMissingFile

 pitivi/formatters/base.py     |   39 +++++++++++++++++-------------
 tests/Makefile.am             |    1 +
 tests/test_formatters_base.py |   52 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 75 insertions(+), 17 deletions(-)
---
diff --git a/pitivi/formatters/base.py b/pitivi/formatters/base.py
index a0abae4..b7510c8 100644
--- a/pitivi/formatters/base.py
+++ b/pitivi/formatters/base.py
@@ -362,34 +362,39 @@ class Formatter(Signallable, Loggable):
             self.warning("invalid URI")
             raise FormatterError("invalid URI", uri)
 
-        # first check the good old way
         if uri_is_reachable(uri):
             self.debug("URI is reachable")
             return uri
 
-        self.debug("URI might have moved...")
+        # The file might have been moved.
+        probable = self._searchMissingFile(uri)
+        if probable:
+            # We already have a mapping which allowed us to find
+            # the new position of the file.
+            return probable
 
-        # else let's figure out if we have a compatible mapping
-        for k, v in self.directorymapping.iteritems():
-            if uri.startswith(k):
-                probable = uri.replace(k, v, 1)
-                if uri_is_valid(probable) and uri_is_reachable(probable):
-                    return probable
-
-        # else, let's fire the signal...
+        # Inform the user that the file cannot be found.
         self.emit('missing-uri', uri, factory)
 
-        # and check again
-        for k, v in self.directorymapping.iteritems():
-            self.debug("uri:%r, k:%r, v:%r", uri, k, v)
-            if uri.startswith(k):
-                probable = uri.replace(k, v, 1)
-                if uri_is_valid(probable) and uri_is_reachable(probable):
-                    return probable
+        # Check again, as emitting the missing-uri signal could result in
+        # a new mapping in self.directorymapping.
+        probable = self._searchMissingFile(uri)
+        if probable:
+            return probable
 
         # Houston, we have lost contact with mission://fail
         raise FormatterError("Couldn't find %s" % uri)
 
+    def _searchMissingFile(self, uri):
+        """Search for a replacement for the specified file:// URI."""
+        for old_prefix, new_prefix in self.directorymapping.iteritems():
+            self.debug("uri:%r, k:%r, v:%r", uri, old_prefix, new_prefix)
+            if uri.startswith(old_prefix):
+                probable = uri.replace(old_prefix, new_prefix, 1)
+                if uri_is_valid(probable) and uri_is_reachable(probable):
+                    return probable
+        return None
+
     #}
 
     def _sourcesReadyCb(self, sources):
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 46174dd..b1d088c 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -14,6 +14,7 @@ tests = \
 	test_factories_base.py \
 	test_factories_file.py \
 	test_factories_operation.py \
+	test_formatters_base.py \
 	test_gap.py \
 	test_pipeline_action.py \
 	test_pipeline.py \
diff --git a/tests/test_formatters_base.py b/tests/test_formatters_base.py
new file mode 100644
index 0000000..ab2ac6e
--- /dev/null
+++ b/tests/test_formatters_base.py
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+# PiTiVi , Non-linear video editor
+#
+#       test_formatters_base.py
+#
+# Copyright (c) 2011, Alex Balut <alexandru balut gmail com>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the
+# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+# Boston, MA 02110-1301, USA.
+
+import shutil
+import tempfile
+
+from pitivi.formatters.base import Formatter
+
+from common import TestCase
+
+
+class TestFormatter(TestCase):
+
+    def setUp(self):
+        TestCase.setUp(self)
+        self.formatter = Formatter(avalaible_effects=None)
+
+    def testSearchMissingFile(self):
+        # The scenario is that a file has been moved from dir1 to dir2.
+        dir0 = tempfile.mkdtemp()
+        try:
+            dir1 = tempfile.mkdtemp(dir=dir0)
+            dir2 = tempfile.mkdtemp(dir=dir0)
+            unused_file2, file2_path = tempfile.mkstemp(dir=dir2)
+            uri2 = 'file://%s' % file2_path
+            uri1 = uri2.replace(dir2, dir1)
+
+            self.assertIsNone(self.formatter._searchMissingFile(uri1))
+
+            self.formatter.addMapping('file://%s' % dir1, 'file://%s' % dir2)
+            self.assertEqual(uri2, self.formatter._searchMissingFile(uri1))
+        finally:
+            shutil.rmtree(dir0)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]