[Zope3-checkins] SVN: Zope3/branches/zipimport-support/src/zope/filereference/ - rename IResourceReference --> IFileReference

Fred L. Drake, Jr. fdrake at gmail.com
Wed Nov 9 19:13:46 EST 2005


Log message for revision 40018:
  - rename IResourceReference --> IFileReference
  - add interface for the package module
  - support getmtime operations wherever possible; needed for
    pagetemplatefiles
  

Changed:
  U   Zope3/branches/zipimport-support/src/zope/filereference/README.txt
  U   Zope3/branches/zipimport-support/src/zope/filereference/__init__.py
  U   Zope3/branches/zipimport-support/src/zope/filereference/interfaces.py
  U   Zope3/branches/zipimport-support/src/zope/filereference/reference.py

-=-
Modified: Zope3/branches/zipimport-support/src/zope/filereference/README.txt
===================================================================
--- Zope3/branches/zipimport-support/src/zope/filereference/README.txt	2005-11-09 23:58:20 UTC (rev 40017)
+++ Zope3/branches/zipimport-support/src/zope/filereference/README.txt	2005-11-10 00:13:45 UTC (rev 40018)
@@ -38,9 +38,16 @@
 into the package::
 
   >>> directory = os.path.dirname(zope.filereference.__file__)
-  >>> ref == os.path.join(directory, "README.txt")
+  >>> filename = os.path.join(directory, "README.txt")
+  >>> ref == filename
   True
 
+The `getmtime()` function can be used to get the modification time for
+a referenced file if it's available::
+
+  >>> zope.filereference.getmtime(ref) == os.path.getmtime(filename)
+  True
+
 The reference can be opened using the `open()` function (which
 also accepts simple strings)::
 

Modified: Zope3/branches/zipimport-support/src/zope/filereference/__init__.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/filereference/__init__.py	2005-11-09 23:58:20 UTC (rev 40017)
+++ Zope3/branches/zipimport-support/src/zope/filereference/__init__.py	2005-11-10 00:13:45 UTC (rev 40018)
@@ -18,5 +18,11 @@
 """
 __docformat__ = "reStructuredText"
 
+import zope.interface
 
-from reference import open, new, exists, isdir, isfile
+
+from reference import open, new, exists, isdir, isfile, getmtime
+
+
+from interfaces import IFileReferenceAPI
+zope.interface.moduleProvides(IFileReferenceAPI)

Modified: Zope3/branches/zipimport-support/src/zope/filereference/interfaces.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/filereference/interfaces.py	2005-11-09 23:58:20 UTC (rev 40017)
+++ Zope3/branches/zipimport-support/src/zope/filereference/interfaces.py	2005-11-10 00:13:45 UTC (rev 40018)
@@ -11,7 +11,7 @@
 # FOR A PARTICULAR PURPOSE.
 #
 ##############################################################################
-"""Interfaces for zope.resource.
+"""Interfaces for zope.filereference.
 
 """
 __docformat__ = "reStructuredText"
@@ -19,8 +19,13 @@
 import zope.interface
 
 
-class IResourceReference(zope.interface.Interface):
+class IFileReference(zope.interface.Interface):
+    """Interface of a file reference object.
 
+    File refereneces must also be strings.
+
+    """
+
     def open(mode="r"):
         """Open the referenced resource, returning a file-like object.
 
@@ -28,11 +33,45 @@
 
         """
 
+    def exists():
+        """Returns True iff the resource exists."""
+
+    def isdir():
+        """Returns True iff the resource exists and is a directory."""
+
     def isfile():
         """Returns True iff the resource exists and is a file."""
 
-    def isdir():
+    def getmtime():
+        """Return the last-modified time for the file, or 0 if unavailable."""
+
+
+class IFileReferenceAPI(zope.interface.Interface):
+    """Interface for the general API for working with file references.
+
+    The `ref` arguments for these functions may be paths as strings or
+    `IFileReference` implementations.
+
+    """
+
+    def new(path, package=None, basepath=None):
+        """Return a new `IFileReference` object."""
+
+    def open(ref, mode="r"):
+        """Open the referenced resource, returning a file-like object.
+
+        Only 'read' modes are supported.
+
+        """
+
+    def exists(ref):
+        """Returns True iff the resource exists."""
+
+    def isdir(ref):
         """Returns True iff the resource exists and is a directory."""
 
-    def exists():
-        """Returns True iff the resource exists."""
+    def isfile(ref):
+        """Returns True iff the resource exists and is a file."""
+
+    def getmtime(ref):
+        """Return the last-modified time for the file, or 0 if unavailable."""

Modified: Zope3/branches/zipimport-support/src/zope/filereference/reference.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/filereference/reference.py	2005-11-09 23:58:20 UTC (rev 40017)
+++ Zope3/branches/zipimport-support/src/zope/filereference/reference.py	2005-11-10 00:13:45 UTC (rev 40018)
@@ -24,7 +24,7 @@
 
 import zope.interface
 
-from zope.filereference.interfaces import IResourceReference
+from zope.filereference.interfaces import IFileReference
 
 try:
     import pkg_resources
@@ -33,30 +33,37 @@
 
 
 def exists(path):
-    if IResourceReference.providedBy(path):
+    if IFileReference.providedBy(path):
         return path.exists()
     else:
         return os.path.exists(path)
 
 
 def isdir(path):
-    if IResourceReference.providedBy(path):
+    if IFileReference.providedBy(path):
         return path.isdir()
     else:
         return os.path.isdir(path)
 
 
 def isfile(path):
-    if IResourceReference.providedBy(path):
+    if IFileReference.providedBy(path):
         return path.isfile()
     else:
         return os.path.isfile(path)
 
 
+def getmtime(path):
+    if IFileReference.providedBy(path):
+        return path.getmtime()
+    else:
+        return os.path.getmtime(path)
+
+
 def open(path, mode="r"):
     if not mode.startswith("r"):
         raise ValueError("`mode` must be a read-only mode")
-    if IResourceReference.providedBy(path):
+    if IFileReference.providedBy(path):
         return path.open(mode)
     else:
         return __builtin__.open(path, mode)
@@ -93,7 +100,7 @@
 
 class PathReference(str):
 
-    zope.interface.implements(IResourceReference)
+    zope.interface.implements(IFileReference)
 
     def __add__(self, other):
         path = str(self) + other
@@ -102,19 +109,22 @@
     def open(self, mode="r"):
         return __builtin__.open(self, mode)
 
-    def isfile(self):
-        return os.path.isfile(self)
+    def exists(self):
+        return os.path.exists(self)
 
     def isdir(self):
         return os.path.isdir(self)
 
-    def exists(self):
-        return os.path.exists(self)
+    def isfile(self):
+        return os.path.isfile(self)
 
+    def getmtime(self):
+        return os.path.getmtime(self)
 
+
 class PackageStr(str):
 
-    zope.interface.implements(IResourceReference)
+    zope.interface.implements(IFileReference)
 
     def __add__(self, other):
         value = str(self) + other
@@ -166,17 +176,9 @@
         else:
             return self.open_path_or_loader(mode)
 
-    def isfile(self):
-        try:
-            f = self.open()
-        except IOError:
-            return False
-        f.close()
-        return True
-
-    def isdir(self):
+    def exists(self):
         if pkg_resources:
-            return pkg_resources.resource_isdir(
+            return pkg_resources.resource_exists(
                 self._package.__name__, self._relpath)
         else:
             try:
@@ -184,7 +186,7 @@
             except AttributeError:
                 for dir in self._package.__path__:
                     filename = os.path.join(dir, self._relpath)
-                    if os.path.isdir(filename):
+                    if os.path.exists(filename):
                         return True
                 return False
             else:
@@ -200,11 +202,11 @@
                 except IOError:
                     pass
                 else:
-                    return False
+                    return True
 
-    def exists(self):
+    def isdir(self):
         if pkg_resources:
-            return pkg_resources.resource_exists(
+            return pkg_resources.resource_isdir(
                 self._package.__name__, self._relpath)
         else:
             try:
@@ -212,7 +214,7 @@
             except AttributeError:
                 for dir in self._package.__path__:
                     filename = os.path.join(dir, self._relpath)
-                    if os.path.exists(filename):
+                    if os.path.isdir(filename):
                         return True
                 return False
             else:
@@ -228,9 +230,29 @@
                 except IOError:
                     pass
                 else:
-                    return True
+                    return False
 
+    def isfile(self):
+        try:
+            f = self.open()
+        except IOError:
+            return False
+        f.close()
+        return True
 
+    def getmtime(self):
+        try:
+            self._package.__loader__
+        except AttributeError:
+            for dir in self._package.__path__:
+                filename = os.path.join(dir, self._relpath)
+                if os.path.exists(filename):
+                    return os.path.getmtime(filename)
+            raise OSError(errno.ENOENT, "No such file or directory", self)
+        else:
+            return 0
+
+
 class ZipImporterPackagePathReference(PackageStr):
 
     def __new__(cls, value, package, relpath):
@@ -263,6 +285,21 @@
         relpath = self._getpath()
         return relpath in self._loader._files
 
+    def getmtime(self):
+        relpath = self._getpath()
+        if relpath not in self._loader._files:
+            relpath += os.sep
+        try:
+            info = self._loader._files[relpath]
+        except KeyError:
+            raise OSError(errno.ENOENT, "No such file or directory", self)
+        d = info[5]
+        t = info[4]
+        # This next is taken from the zipfile module:
+        dt = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
+               t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
+        return dt
+
     def _getpath(self):
         relpath = self._relpath.replace("/", os.sep)
         if os.altsep:



More information about the Zope3-Checkins mailing list