[Zope3-checkins] SVN: Zope3/branches/zipimport-support/src/zope/resource/reference.py make zope.resource work when pkg_resources is not available

Fred L. Drake, Jr. fdrake at gmail.com
Tue Nov 8 22:51:52 EST 2005


Log message for revision 39998:
  make zope.resource work when pkg_resources is not available

Changed:
  U   Zope3/branches/zipimport-support/src/zope/resource/reference.py

-=-
Modified: Zope3/branches/zipimport-support/src/zope/resource/reference.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/resource/reference.py	2005-11-09 03:40:52 UTC (rev 39997)
+++ Zope3/branches/zipimport-support/src/zope/resource/reference.py	2005-11-09 03:51:52 UTC (rev 39998)
@@ -93,19 +93,11 @@
         return self.__class__(value, self._package, relpath)
 
     def open_pkg_resources(self, mode="rb"):
-        try:
-            data = pkg_resources.resource_string(
-                self._package.__name__, self._relpath)
-        except IOError, e:
-            if len(e.args) == 1:
-                # zipimport raises IOError w/ insufficient arguments
-                raise IOError(errno.ENOENT, "file not found", self)
-            else:
-                raise
-        f = StringIO.StringIO(data)
-        f.name = self
-        f.mode = mode
-        return f
+        return self._open_packaged_resource(
+            mode,
+            pkg_resources.resource_string,
+            self._package.__name__,
+            self._relpath)
 
     def open_path_or_loader(self, mode="rb"):
         try:
@@ -119,9 +111,23 @@
         else:
             dir = os.path.dirname(self._package.__file__)
             filename = os.path.join(dir, self._relpath)
-            return loader.get_data(self._package.__name__)
+            return self._open_packaged_resource(
+                mode, loader.get_data, filename)
 
     if pkg_resources:
         open = open_pkg_resources
     else:
         open = open_path_or_loader
+
+    def _open_packaged_resource(self, mode, opener, *args):
+        try:
+            data = opener(*args)
+        except IOError, e:
+            if len(e.args) == 1:
+                raise IOError(errno.ENOENT, "file not found", self)
+            else:
+                raise
+        f = StringIO.StringIO(data)
+        f.name = self
+        f.mode = mode
+        return f



More information about the Zope3-Checkins mailing list