[z3-checkins] r25255 - z3/jsonserver/branch/merge/concatresource

reebalazs at codespeak.net reebalazs at codespeak.net
Mon Apr 3 15:59:37 CEST 2006


Author: reebalazs
Date: Mon Apr  3 15:59:35 2006
New Revision: 25255

Modified:
   z3/jsonserver/branch/merge/concatresource/__init__.py
   z3/jsonserver/branch/merge/concatresource/concatfileresource.py
   z3/jsonserver/branch/merge/concatresource/configure.zcml
   z3/jsonserver/branch/merge/concatresource/interfaces.py
Log:
Allow utility mechanism to extend list of files

- Check if files exist on configuration, to report unexistant resource files
  on startup.

- add the utility hook to allow extend filelist by other components

- hack the module to be aliased to under "Products", this allows the product
  to be imported multiple times.



Modified: z3/jsonserver/branch/merge/concatresource/__init__.py
==============================================================================
--- z3/jsonserver/branch/merge/concatresource/__init__.py	(original)
+++ z3/jsonserver/branch/merge/concatresource/__init__.py	Mon Apr  3 15:59:35 2006
@@ -2,3 +2,8 @@
 Product init
 '''
 
+# alias myself to Products, directly
+import sys, Products
+if not hasattr(Products, 'concatresource'):
+    # only 1st import is aliased.
+    Products.concatresource = sys.modules['Products.concatresource'] = sys.modules[globals()['__name__']]

Modified: z3/jsonserver/branch/merge/concatresource/concatfileresource.py
==============================================================================
--- z3/jsonserver/branch/merge/concatresource/concatfileresource.py	(original)
+++ z3/jsonserver/branch/merge/concatresource/concatfileresource.py	Mon Apr  3 15:59:35 2006
@@ -8,30 +8,81 @@
 
 from zope.interface import implements
 from interfaces import IContextFile
+# we are aliased to Products, hence the following absolute import
+from Products.concatresource.interfaces import IConcatResourceAddon
 from fileresource import File
 from compression import compress
+import time
+import zope.component
+from zope.component.exceptions import ComponentLookupError
 
 class ConcatFiles(object):
-    'A resource that concatenates files and compresses the result'
+    '''A resource that concatenates files and compresses the result
+    
+    It is also possible to extend the statically given list via
+    a utility.
+    '''
     implements(IContextFile)
 
     def __init__(self, pathlist, name):
         # Path is now a list.
         assert isinstance(pathlist, (list, tuple))
-        self.pathlist = pathlist
+        # check all files, just to raise error if don't exist
+        for path in pathlist:
+            file(path, 'rb').close()
+        #
+        self.pathlist_base = pathlist
         self.__name__ = name
-        # Init a list of files.
-        self.content = [File(path, name) for path in pathlist]
+        # markers for pathlist modification
+        self.pathlist = []
+        self.fileslist_changed = None
+        self.fileslist = []
 
+    def getPathList(self):
+        'Gets the extended pathlist'
+        # we allow the list to be extended via an utility
+        try:
+            registry = zope.component.getUtility(IConcatResourceAddon, self.__name__)
+        except ComponentLookupError:
+            extend = []
+        else:
+            extend = registry.getAddonFiles()
+        pathlist = self.pathlist_base + extend
+        return pathlist
+        
+    def getFilesList(self):
+        'Gets the list of files'
+        ## # XXX We have two choices:
+        ## # 1. We only calculate the list once, on startup
+        ## # that is, we suppose that the file resource is
+        ## # called up after the extension reg has been finished
+        ## # and that it never changes later.
+        ## # 2. but it also could be like this to allow changes later:
+        pathlist = self.getPathList()
+        if pathlist != self.pathlist:
+        ##if not self.pathlist:
+            ##pathlist = self.getPathList()
+            # mark pathlist modification
+            self.pathlist = pathlist
+            self.fileslist_changed = time.time()
+            fileslist = self.fileslist = [File(path, self.__name__) for path in pathlist]
+        else:
+            fileslist = self.fileslist
+        return fileslist
+        
     def getLastMod(self):
-        return max([f.getLastMod() for f in self.content])
+        # We take in consideration that the pathlist
+        # itself could have changed too.
+        return max([f.getLastMod() for f in self.getFilesList()] + 
+            [self.fileslist_changed])
             
     def getContents(self):
-        assert self.content, 'Must contain at least one resource.'
-        result = self.content[0].getContents()
+        fileslist = self.getFilesList()
+        assert fileslist, 'Must contain at least one resource.'
+        result = fileslist[0].getContents()
         content_type = result['content_type']
         data = [result['data']]
-        for subres in self.content[1:]:
+        for subres in fileslist[1:]:
             d = subres.getContents()
             # all elements must have the same content type.
             assert d['content_type'] == content_type

Modified: z3/jsonserver/branch/merge/concatresource/configure.zcml
==============================================================================
--- z3/jsonserver/branch/merge/concatresource/configure.zcml	(original)
+++ z3/jsonserver/branch/merge/concatresource/configure.zcml	Mon Apr  3 15:59:35 2006
@@ -7,6 +7,6 @@
   />
 
   <!-- Include the next line for testing only. -->
-  <include package=".test" />
+  <!--include package=".test" /-->
   
  </configure>

Modified: z3/jsonserver/branch/merge/concatresource/interfaces.py
==============================================================================
--- z3/jsonserver/branch/merge/concatresource/interfaces.py	(original)
+++ z3/jsonserver/branch/merge/concatresource/interfaces.py	Mon Apr  3 15:59:35 2006
@@ -22,3 +22,20 @@
 
      def purgeData(self):
          'Purges the cached data'
+
+class IConcatResourceAddon(Interface):
+    '''Utility to register addons
+
+    This can be used to dynamically extend components for a given resource.
+    We don't provide implementation for this here, but other
+    components can implement this to provide dynamic add-ons.
+
+    The name of the utility should be the name of the resource.
+    '''
+
+    def getAddonFiles(request):
+        '''Returns a list of addon files.
+        This will be concatenated to the end of the static list.
+        '''
+    
+ 


More information about the z3-checkins mailing list