[pitivi] Formatter: Add minimalistic support for moved file mapping.



commit eb98fa65a1e52ba60a336bed8d17256f5dbf816c
Author: Edward Hervey <bilboed bilboed com>
Date:   Mon May 18 21:15:38 2009 +0200

    Formatter: Add minimalistic support for moved file mapping.
    
    Requires implementation in the UI.
    
    The idea is that when a file has moved, Formatter will emit the
    'missing-uri' signal.
    Within that callback, the application should:
    * ask the user for the new location
    * call Formatter.addMapping()
    
    Hopefully... if files have moved for a project, they most likely
    have all moved in the same fashion, so this signal should only be
    emitted once.
---
 pitivi/formatters/base.py |   28 ++++++++++++++++++++++++++--
 tests/test_formatter.py   |    8 ++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/pitivi/formatters/base.py b/pitivi/formatters/base.py
index e035882..711ca97 100644
--- a/pitivi/formatters/base.py
+++ b/pitivi/formatters/base.py
@@ -23,6 +23,7 @@
 Base Formatter classes
 """
 
+import os
 from pitivi.project import Project
 from pitivi.utils import uri_is_reachable, uri_is_valid
 from pitivi.signalinterface import Signallable
@@ -270,7 +271,29 @@ class Formatter(Signallable, Loggable):
         @param newpath: The new location corresponding to oldpath.
         @type newpath: C{URI}
         """
-        raise NotImplementedError
+        self.debug("oldpath:%r, newpath:%r", oldpath, newpath)
+        # FIXME dumbest of dumbest implementation, whoever comes up
+        # with a less ugly code is welcome to change this :)
+        a = oldpath.split(os.sep)
+        b = newpath.split(os.sep)
+
+        # search backwards for when the mapping starts
+        ia = len(a)
+        ib = len(b)
+        while ia > 0 and ib > 0:
+            self.debug("ia:%d, ib:%d", ia, ib)
+            if a[ia - 1] != b[ib - 1]:
+                break
+            ia -= 1
+            ib -= 1
+
+        oldprefix = os.sep.join(a[:ia])
+        newprefix = os.sep.join(b[:ib])
+        self.debug("oldprefix:%r, newprefix:%r", oldprefix, newprefix)
+        if oldprefix in self.directorymapping.keys():
+            raise FormatterException
+
+        self.directorymapping[oldprefix] = newprefix
 
     def validateSourceURI(self, uri):
         """
@@ -315,7 +338,8 @@ class Formatter(Signallable, Loggable):
 
         # and check again
         for k, v in self.directorymapping.iteritems():
-            if localpath.startswith(k):
+            self.debug("localpath:%r, k:%r, v:%r", localpath, k, v)
+            if uri.startswith(k):
                 return uri.replace(k, v, 1)
 
         # Houston, we have lost contact with mission://fail
diff --git a/tests/test_formatter.py b/tests/test_formatter.py
index 2d4bf23..13f42c8 100644
--- a/tests/test_formatter.py
+++ b/tests/test_formatter.py
@@ -528,3 +528,11 @@ class TestFormatterLoad(TestCase):
         f = file("/tmp/untitled.pptv", "w")
         f.write(tostring(element))
         f.close()
+
+    def testDirectoryMapping(self):
+        pa = "file:///if/you/have/this/file/you/are/on/crack.avi"
+        pb = "file:///I/really/mean/it/you/crack.avi"
+
+        self.formatter.addMapping(pa,pb)
+        self.assertEquals(self.formatter.validateSourceURI(pa),
+                          pb)



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