Back to index

plone3  3.1.7
archive.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from Products.ATContentTypes.interface.archive import IArchiveAccumulator
00003 from Products.ATContentTypes.interface.folder import IFilterFolder
00004 from Products.ATContentTypes.interface.archive import IArchiver
00005 from Products.ATContentTypes.interface.dataExtractor import IDataExtractor
00006 from Products.ATContentTypes.interface.archive import IArchivable
00007 from cStringIO import StringIO
00008 from zExceptions import BadRequest
00009 from zipfile import ZipFile
00010 
00011 class FolderishArchiver(object):
00012     """
00013     store your files in a zip file
00014     """
00015     implements(IArchiver)
00016 
00017     #XXXX Refactor common methods inside Folderish and non Folderish object
00018 
00019     def __init__(self, context):
00020         """
00021         """
00022         self.context = context
00023 
00024     def getRawArchive(self, accumulator=None, **kwargs):
00025         """
00026         """
00027         if(not accumulator):
00028             accumulator = ZipAccumulator(self.context)
00029         self.createArchive(None,accumulator, **kwargs)
00030         return accumulator.getRaw()
00031 
00032     def createArchive(self, path, accumulator, **kwargs):
00033         """
00034         get the archive file object
00035         """
00036         recursive = kwargs.get('recursive',1)
00037         adapter = IFilterFolder(self.context)
00038         for item in adapter.listObjects():
00039             if IArchivable.providedBy(item):
00040                 archiver = IArchiver(item)
00041                 folderish = isinstance(archiver,FolderishArchiver)
00042                 if path:
00043                     cpath = '%s/%s' % (path, self.context.getId())
00044                 else:
00045                     cpath = self.context.getId()
00046                 if (recursive and folderish) or not folderish:
00047                     archiver.createArchive(cpath, accumulator, **kwargs)
00048 
00049 
00050 class NonFolderishArchiver(object):
00051     """
00052     store your files in a zip file
00053     """
00054     implements(IArchiver)
00055     def __init__(self, context):
00056         self.context = context
00057 
00058     def getRawArchive(self, accumulator=None, **kwargs):
00059         if(not accumulator):
00060             accumulator = ZipAccumulator(self.context)
00061         self.createArchive(None, accumulator, **kwargs)
00062         return accumulator.getRaw()
00063 
00064     def createArchive(self, path, accumulator, **kwargs):
00065         """
00066         get the zip file object
00067         """
00068         dataExtractor = IDataExtractor(self.context)
00069         if dataExtractor and IArchivable.providedBy(self.context):
00070             data = dataExtractor.getData(**kwargs)
00071             if path:
00072               filepath = '%s/%s' % (path, self.context.getId())
00073             else:
00074               filepath = self.context.getId()
00075             accumulator.setFile(filepath,data)
00076 
00077 
00078 class ZipAccumulator(object):
00079     """ Implements an accumulator for zip files.
00080     Note: This should be derived from a superclass allowing for other
00081     compression types, and also for specific handling of object 
00082     specific content - hence the object parameter.
00083     """
00084     implements(IArchiveAccumulator)
00085 
00086     _zip = None
00087     sIO = None
00088 
00089     def __init__(self, context):
00090         self.context = context
00091         self.sIO = StringIO()
00092         self._zip = ZipFile(self.sIO,"w",8)
00093 
00094     def setFile(self,filename,data):
00095         """
00096         store the file inside the zip file
00097         """
00098         if(self._zip):
00099             self._zip.writestr(filename,data)
00100         else:
00101             raise BadRequest, "Unitialized Zip file"
00102 
00103     def setZip(self):
00104         """
00105         initialize the zipe file
00106         """
00107         self._zip = ZipFile(self.sIO,"w",8)
00108 
00109     def close(self):
00110         """
00111         close the zip file
00112         """
00113         self._zip.close()
00114 
00115     def getRaw(self):
00116         """
00117         return the raw zip file
00118         """
00119         if(self._zip):
00120             self.close()
00121             value = self.sIO.getvalue()
00122             self.setZip()
00123             return value
00124         else:
00125             raise BadRequest, "Unitialized Zip file"