| 1 | from Products.CMFCore.interfaces import ISiteRoot |
|---|
| 2 | from collective.imstransport import IMSTransportMessageFactory as _ |
|---|
| 3 | from zope.interface import implements |
|---|
| 4 | from collective.imstransport.utilities.interfaces import IIMSManifestReader, IIMSManifestWriter |
|---|
| 5 | import md5 |
|---|
| 6 | from zope.component import getUtility |
|---|
| 7 | import re |
|---|
| 8 | |
|---|
| 9 | class IMSReader(object): |
|---|
| 10 | """ Base class for IMS reader objects. """ |
|---|
| 11 | |
|---|
| 12 | implements(IIMSManifestReader) |
|---|
| 13 | |
|---|
| 14 | def getPackageName(self): |
|---|
| 15 | """ Return the desciptive name of the package type. """ |
|---|
| 16 | return None |
|---|
| 17 | |
|---|
| 18 | def readPackage(self, file, context): |
|---|
| 19 | """ Read IMS manifest. Override this to read specific package info. """ |
|---|
| 20 | |
|---|
| 21 | # Helper functions for readPackage |
|---|
| 22 | |
|---|
| 23 | def createIdFromFile(self, file): |
|---|
| 24 | """ Get Id from file path """ |
|---|
| 25 | return file.split('/')[-1] |
|---|
| 26 | |
|---|
| 27 | def createPathFromFile(self, file): |
|---|
| 28 | """ Get folder path from file path """ |
|---|
| 29 | return '/'.join(file.split('/')[:-1]) |
|---|
| 30 | |
|---|
| 31 | def determineType(self, item, fn): |
|---|
| 32 | """ Determine the type of the item """ |
|---|
| 33 | result = 'File' |
|---|
| 34 | docmimetypes = ['text/html', 'text/htm', 'text/plain', 'text/x-rst', 'text/structured'] |
|---|
| 35 | |
|---|
| 36 | if item.has_key('type') and item['type']: |
|---|
| 37 | result = item['type'] |
|---|
| 38 | elif item.has_key('Format') and item['Format'] in docmimetypes: |
|---|
| 39 | result = 'Document' |
|---|
| 40 | elif item.has_key('Format') and 'image' in item['Format']: |
|---|
| 41 | result = 'Image' |
|---|
| 42 | else: |
|---|
| 43 | site = getUtility(ISiteRoot) |
|---|
| 44 | mtr = site.mimetypes_registry |
|---|
| 45 | mimetype = mtr.lookupExtension(fn) |
|---|
| 46 | |
|---|
| 47 | if mimetype in docmimetypes: |
|---|
| 48 | result = 'Document' |
|---|
| 49 | elif 'image' in mimetype: |
|---|
| 50 | result = 'Image' |
|---|
| 51 | return result |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | class IMSWriter(object): |
|---|
| 55 | """ Base class for IMS writer objects. """ |
|---|
| 56 | |
|---|
| 57 | implements(IIMSManifestWriter) |
|---|
| 58 | |
|---|
| 59 | def _writeObjectData(self, obj, path, destination): |
|---|
| 60 | """ Write file data to the destination object. """ |
|---|
| 61 | if type(obj) == type(''): |
|---|
| 62 | data = obj |
|---|
| 63 | else: |
|---|
| 64 | format = '' |
|---|
| 65 | if hasattr(obj.aq_explicit, 'Format'): |
|---|
| 66 | format = obj.Format() |
|---|
| 67 | if obj.Type() in ['File', 'Image'] and hasattr(obj.aq_explicit, 'data'): |
|---|
| 68 | data = obj.data |
|---|
| 69 | elif 'text/html' == format and hasattr(obj.aq_explicit, 'getText'): |
|---|
| 70 | data = obj.getText() |
|---|
| 71 | elif format in ['text/plain', 'text/x-rst', 'text/structured'] and hasattr(obj.aq_explicit, 'getRawText'): |
|---|
| 72 | data = obj.getRawText() |
|---|
| 73 | else: |
|---|
| 74 | data = '' |
|---|
| 75 | |
|---|
| 76 | if destination: |
|---|
| 77 | destination.writeFile(path, data) |
|---|
| 78 | |
|---|
| 79 | def _getAllObjects(self, context): |
|---|
| 80 | """ Get all sub objects. """ |
|---|
| 81 | objects = [obj.getObject() for obj in context.portal_catalog.searchResults(path={'query':('/'.join(context.getPhysicalPath())+'/'),'depth':-1})] |
|---|
| 82 | return [object for object in objects if not object.isPrincipiaFolderish or getattr(object.aq_explicit, 'getText', None)] |
|---|
| 83 | |
|---|
| 84 | def _getChildrenObjects(self, parent, objects): |
|---|
| 85 | """ Get objects in current folder """ |
|---|
| 86 | parentpath = '/'.join(parent.getPhysicalPath()) |
|---|
| 87 | children = [] |
|---|
| 88 | for object in objects: |
|---|
| 89 | rpath = '/'.join(object.getPhysicalPath()).replace(parentpath, '') |
|---|
| 90 | |
|---|
| 91 | if len(rpath.split('/')) == 1: |
|---|
| 92 | children.append(object) |
|---|
| 93 | |
|---|
| 94 | return children |
|---|
| 95 | |
|---|
| 96 | def _getObjectPath(self, obj, context): |
|---|
| 97 | """ Get the path of an object. """ |
|---|
| 98 | |
|---|
| 99 | root_path = context.aq_explicit.virtual_url_path() |
|---|
| 100 | obj_path = obj.aq_explicit.virtual_url_path() |
|---|
| 101 | |
|---|
| 102 | if obj_path.find(root_path) != 0: |
|---|
| 103 | return '' |
|---|
| 104 | |
|---|
| 105 | # Remove the path of the folder object |
|---|
| 106 | path = obj_path.replace(root_path, '') |
|---|
| 107 | if path and path[0] == '/': |
|---|
| 108 | path = path[1:] |
|---|
| 109 | |
|---|
| 110 | if not path: |
|---|
| 111 | return '' |
|---|
| 112 | |
|---|
| 113 | if hasattr(obj.aq_explicit, 'Format'): |
|---|
| 114 | if 'text/html' == obj.Format() and obj.isPrincipiaFolderish: |
|---|
| 115 | path += '.html' |
|---|
| 116 | |
|---|
| 117 | return path |
|---|
| 118 | |
|---|
| 119 | def _createPathId(self, path, pre='RES'): |
|---|
| 120 | """ Create a unique id given a path """ |
|---|
| 121 | return pre + str(md5.md5(path).hexdigest()) |
|---|
| 122 | |
|---|
| 123 | def _getCopyrightString(self, copyright, rights_holder, rights_holder_email): |
|---|
| 124 | """ Return a copyright string """ |
|---|
| 125 | cp = '' |
|---|
| 126 | if copyright: |
|---|
| 127 | cp += copyright |
|---|
| 128 | if rights_holder: |
|---|
| 129 | if cp: |
|---|
| 130 | cp += ', ' |
|---|
| 131 | cp += rights_holder |
|---|
| 132 | if rights_holder_email: |
|---|
| 133 | if cp: |
|---|
| 134 | cp += ', ' |
|---|
| 135 | cp += rights_holder_email |
|---|
| 136 | return cp |
|---|
| 137 | |
|---|
| 138 | def getObjSize(self, object): |
|---|
| 139 | """ Retrieves the correct size of the object""" |
|---|
| 140 | return '%d' %object.get_size() |
|---|