conduit r1505 - in trunk: . conduit/datatypes test/python-tests test/python-tests/data



Author: jstowers
Date: Sat Jun  7 00:45:39 2008
New Revision: 1505
URL: http://svn.gnome.org/viewvc/conduit?rev=1505&view=rev

Log:
2008-06-07  John Stowers  <john stowers gmail com>

	* conduit/datatypes/File.py:
	* test/python-tests/TestCoreFile2.py:
	* test/python-tests/data/file.list: Add a ProxyFile class. This takes
	a remote uri, a mtime, and a size as its args. It can then be compared
	against a local file, and will not becore a *true* local file, until
	it is transferred to a writable filesystem.

2008-06-07  John Stowers  <john stowers gmail com>


Modified:
   trunk/ChangeLog
   trunk/conduit/datatypes/File.py
   trunk/test/python-tests/TestCoreFile2.py
   trunk/test/python-tests/data/file.list

Modified: trunk/conduit/datatypes/File.py
==============================================================================
--- trunk/conduit/datatypes/File.py	(original)
+++ trunk/conduit/datatypes/File.py	Sat Jun  7 00:45:39 2008
@@ -45,6 +45,9 @@
         self.triedOpen = False
         self._newFilename = None
         self._newMtime = None
+
+        self._isProxyFile = False
+        self._proxyFileSize = None
         
     def _open_file(self):
         if self.triedOpen == False:
@@ -129,6 +132,9 @@
             return True
         else:
             return False
+            
+    def _is_proxyfile(self):
+        return self._isProxyFile
 
     def _set_file_mtime(self, mtime):
         timestamp = conduit.utils.datetime_get_timestamp(mtime)
@@ -172,6 +178,8 @@
         self.triedOpen = f.triedOpen
         self._newFilename = f._newFilename
         self._newMtime = f._newMtime
+        self._isProxyFile = f._isProxyFile
+        self._proxyFileSize = f._proxyFileSize
 
     def to_tempfile(self):
         """
@@ -215,7 +223,7 @@
         """
         Renames the file
         """
-        if self._is_tempfile():
+        if self._is_tempfile() or self._is_proxyfile():
             self._defer_rename(filename)
         else:
             try:
@@ -246,7 +254,7 @@
         """
         Changes the mtime of the file
         """
-        if self._is_tempfile():
+        if self._is_tempfile() or self._is_proxyfile():
             self._defer_new_mtime(mtime)
         else:
             try:
@@ -315,6 +323,10 @@
         #close the file and the handle so that the file info is refreshed
         self.URI = newURI
         self._close_file()
+        
+        #if we have been transferred anywhere (i.e. the destination, our
+        #location, is writable) then we are no longer a proxy file
+        self._isProxyFile = False
 
         #apply any pending renames
         if self._is_deferred_rename():
@@ -367,11 +379,14 @@
         """
         Gets the file size
         """
-        self._get_file_info()
-        try:
-            return self.fileInfo.size
-        except:
-            return None
+        if self._is_proxyfile():
+            return self._proxyFileSize
+        else:
+            self._get_file_info()
+            try:
+                return self.fileInfo.size
+            except:
+                return None
 
     def get_hash(self):
         #FIXME: self.get_size() does not seem reliable
@@ -516,9 +531,7 @@
 
 class TempFile(File):
     """
-    A Small extension to a File. This makes new filenames (force_new_filename)
-    to be processed in the transfer method, and not immediately, which may
-    cause name conflicts in the temp directory. 
+    Creates a file in the system temp directory with the given contents.
     """
     def __init__(self, contents=""):
         #create the file containing contents
@@ -527,4 +540,23 @@
         os.close(fd)
         File.__init__(self, name)
         log.debug("New tempfile created at %s" % name)
+        
+class ProxyFile(File):
+    """
+    Pretends to be a file for the sake of comparison and transer. Typically
+    located on a remote, read only resource, such as http://. Once transferred
+    to the local filesystem, it behaves just like a file.
+    """
+    def __init__(self, URI, name, modified, size, **kwargs):
+        File.__init__(self, URI, **kwargs)
+
+        self._isProxyFile = True
+        self._proxyFileSize = size
+        
+        if modified:
+            self.force_new_mtime(modified)
+        if name:
+            self.force_new_filename(name)
+
+            
 

Modified: trunk/test/python-tests/TestCoreFile2.py
==============================================================================
--- trunk/test/python-tests/TestCoreFile2.py	(original)
+++ trunk/test/python-tests/TestCoreFile2.py	Sat Jun  7 00:45:39 2008
@@ -58,4 +58,43 @@
 ok("Transferred R/O file correctly (%s)" % fNewName, f.get_filename() == fNewName)
 ok("Transferred correctly (%s)" % localNewName, local.get_filename() == localNewName)
 
+#play with proxy files, i.e. files that are like remote files, but stop being such
+#when transferred to the local system
+day0 = datetime.datetime(1983,8,16)
+day1 = datetime.datetime(1983,8,17)
+
+#compare two proxy files based on mtime only
+f = File.ProxyFile(
+            URI=get_external_resources("file")["remote"],
+            name=None,
+            modified=day0,
+            size=None)
+f2 = File.ProxyFile(
+            URI=get_external_resources("file")["remote"],
+            name=None,
+            modified=day1,
+            size=None)
+comp = f.compare(f2)
+ok("Proxy file comparison (mtime): %s" % comp,comp == conduit.datatypes.COMPARISON_OLDER)
+
+#compare two proxy files based on size only
+proxyFileName = Utils.random_string()
+f = File.ProxyFile(
+            URI=get_external_resources("file")["remote"],
+            name=None,
+            modified=day0,
+            size=10)
+f2 = File.ProxyFile(
+            URI=get_external_resources("file")["remote"],
+            name=proxyFileName,
+            modified=day0,
+            size=10)
+comp = f.compare(f2)
+ok("Proxy file comparison (size): %s" % comp,comp == conduit.datatypes.COMPARISON_EQUAL)
+
+f2.transfer(tmpdir)
+ok("Transferred ProxyFile correctly (%s)" % proxyFileName, f2.get_filename() == proxyFileName)
+
+ok("ProxyFile graduated to real file", f2._is_proxyfile() == False)
+            
 finished()

Modified: trunk/test/python-tests/data/file.list
==============================================================================
--- trunk/test/python-tests/data/file.list	(original)
+++ trunk/test/python-tests/data/file.list	Sat Jun  7 00:45:39 2008
@@ -8,10 +8,14 @@
 [DEFAULT]
 file1=http://tests.conduit-project.org/index.html
 file2=http://files.conduit-project.org/screenshot.jpg
+remote=http://www.google.com/intl/en_com/images/logo_plain.png
 
 [john nzjrs-desktop]
 local=/home/john/eBooks/Wiley Global Positioning Systems Inertial Navigation and Integration (2nd Edition).pdf
 ssh=ssh://root greenbirdsystems com/var/www/conduit-project.org/files/screenshot.jpg
 ftp=ftp://anonymous 192 168 1 1/Status/index.htm
+doc=/home/john/testing/test-data/test.doc
+xls=/home/john/testing/test-data/test.xls
+ppt=/home/john/testing/test-data/test.ppt
 
 



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