| 1 | from zope.interface import implements |
|---|
| 2 | from zope.component import getUtility, getUtilitiesFor |
|---|
| 3 | from collective.imstransport.utilities.interfaces import IIMSTransportUtility, IIMSObjectCreator, IIMSManifestReader, IIMSManifestWriter |
|---|
| 4 | from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
|---|
| 5 | #from collective.imstransport.config import WWW_DIR |
|---|
| 6 | from OFS.SimpleItem import SimpleItem |
|---|
| 7 | from zipfile import ZipFile, ZIP_DEFLATED |
|---|
| 8 | from StringIO import StringIO |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | class IMSTransportUtility(SimpleItem): |
|---|
| 12 | |
|---|
| 13 | implements(IIMSTransportUtility) |
|---|
| 14 | # tocpage = PageTemplateFile('tableofcontents',WWW_DIR) |
|---|
| 15 | |
|---|
| 16 | def importPackage(self, context, package, packagetype): |
|---|
| 17 | reader = getUtility(IIMSManifestReader, packagetype) |
|---|
| 18 | manifestDictionary = reader.readManifest(package) |
|---|
| 19 | oc = getUtility(IIMSObjectCreator) |
|---|
| 20 | oc.createObject(context, manifestDictionary, package) |
|---|
| 21 | |
|---|
| 22 | def exportPackage(self, package, filename, packagetype = "eduCommons Common Cartridge", wtype='Common Cartridge'): |
|---|
| 23 | writer = getUtility(IIMSManifestWriter, packagetype) |
|---|
| 24 | # writer.setDestination(ZipfileWriter(filename, package.getId())) |
|---|
| 25 | return writer.createPackage(package, filename) |
|---|
| 26 | |
|---|
| 27 | def getWriters(self): |
|---|
| 28 | writers = getUtilitiesFor(IIMSManifestWriter) |
|---|
| 29 | values = [] |
|---|
| 30 | for writer in writers: |
|---|
| 31 | values.append((writer[1].getPackageName(), writer[0])) |
|---|
| 32 | return values |
|---|
| 33 | |
|---|
| 34 | def getReaders(self): |
|---|
| 35 | utils = getUtilitiesFor(IIMSManifestReader) |
|---|
| 36 | values = [] |
|---|
| 37 | for x in utils: |
|---|
| 38 | values.append((x[1].getPackageName(), x[0])) |
|---|
| 39 | return values |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | class ZipfileWriter: |
|---|
| 43 | """ Write a zip file which contains all the IMS Content packaging stuff. """ |
|---|
| 44 | |
|---|
| 45 | def __init__(self, archive_name, package_name): |
|---|
| 46 | self.fullpath = '%s/' %package_name |
|---|
| 47 | self.archive_name = archive_name |
|---|
| 48 | self.package_name = package_name |
|---|
| 49 | self.archive = StringIO() |
|---|
| 50 | self.zipfile = ZipFile(self.archive, 'w', ZIP_DEFLATED) |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | def writeFile(self, path, data): |
|---|
| 54 | """ Write a file to the zip archive. """ |
|---|
| 55 | fpath = '%s%s' %(self.fullpath, path) |
|---|
| 56 | self.zipfile.writestr(fpath, data) |
|---|
| 57 | |
|---|
| 58 | def getOutput(self): |
|---|
| 59 | """ Close the zip file and get the binary archive. """ |
|---|
| 60 | self.zipfile.close() |
|---|
| 61 | self.archive.seek(0) |
|---|
| 62 | return self.archive.read(), self.archive_name |
|---|