source: collective.imstransport/trunk/collective/imstransport/utilities/imsinterchange.py @ 199

Revision 199, 7.6 KB checked in by brent, 4 years ago (diff)

Refactored ccreader and removed ccresourcereader. Refactored imsccreader to be more modular.

Line 
1from Products.CMFCore.interfaces import ISiteRoot
2from collective.imstransport import IMSTransportMessageFactory as _
3from zope.interface import implements
4from collective.imstransport.utilities.interfaces import IIMSManifestReader, IIMSManifestWriter
5import md5
6from zope.component import getUtility
7import re
8
9class 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        result = 'File'
33        docmimetypes = ['text/html', 'text/htm' 'text/plain' 'text/x-rst', 'text/structured']
34
35        if item.has_key('type'):
36            result = type
37        elif item.has_key('Format') and item['Format'] in docmimetypes:
38            result = 'Document'
39        elif item.has_key('Format') and 'image' in item['Format']:
40            result = 'Image'
41        else:
42            """ Determine the file mimetype """
43            site = getUtility(ISiteRoot)
44            mtr = site.mimetypes_registry
45            mimetype = mtr.lookupExtension(filename)
46           
47            if mimetype in docmimetypes:
48                result = 'Document'
49            elif 'image' in mimetype:
50                result = 'Image'
51        return result                   
52
53class IMSInterchangeReader(object):
54
55    implements(IIMSManifestReader)
56    name = _(u'IMS Interchange Reader')
57
58    def determineType(self, context, hashref, objDict, filename):
59        """ Determine the type of the incoming object """
60
61        if objDict[hashref].has_key('type') and objDict[hashref]['type']:
62            return objDict[hashref]['type']
63        elif objDict[hashref].has_key('Format') and objDict[hashref]['Format'] in ['text/html', 'text/htm' 'text/plain' 'text/x-rst', 'text/structured']:
64            return 'Document'
65        elif objDict[hashref].has_key('Format') and re.match('^image', objDict[hashref]['Format']):
66            return 'Image'
67        else:
68            return self.determineFileMimetype(filename, context)
69           
70    def createIdFromFile(self, file):
71        """ Get Id from file path """
72        return file.split('/')[-1]
73
74    def createPathFromFile(self, file):
75        """ Get folder path from file path """
76        return '/'.join(file.split('/')[:-1])
77
78
79    def determineFileMimetype(self, name, context):
80        """ Determine the file mimetype """
81        portal = context.portal_url.getPortalObject()
82        registry = portal.mimetypes_registry
83        mimetype = registry.lookupExtension(name)
84
85        if mimetype and mimetype.major() == 'text' and mimetype.minor() == 'html':
86            return 'Document'
87        elif mimetype and mimetype.major() == 'image':
88            return 'Image'
89        else:
90            return 'File'
91
92    def parseFile(self, context, file, objDict, hashref, id, path):
93        """ parse a file object and add data to it """
94        objDict[hashref]['file'] = file
95        objDict[hashref]['id'] = id
96        objDict[hashref]['path'] = path
97        objDict[hashref]['type'] = self.determineType(context, hashref, objDict, file)
98
99
100    def parseResourceMetadata(self, resourcereader, resourceid, context):
101        """ Read the resource metadata """
102
103        resourcereader.readGeneral()
104        resourcereader.readLifecycle()
105        resourcereader.readMetaMetadata()
106        resourcereader.readTechnical()
107        resourcereader.readRights()
108        customnode = resourcereader.getCustomData('', '')
109        if customnode:
110            metadict = resourcereader.readCustomMetadata(customnode)
111            resourcereader.appendCustomData(metadict)
112        return resourcereader.processResourceMetadata()
113       
114    def readCustomData(self, context, prefix, ns, location):
115        """
116        Hook for reading custom metadata for additional metadata requirements.
117        Should return a dictionary of values representing attribute name and value.
118        """
119
120        return {}
121
122    def getObjectDictionary(self):
123        """ Return the object dictionary """
124        return self.objdict
125
126
127    def getPackageName(self):
128
129        return self.name
130
131
132class IMSInterchangeWriter(object):
133
134    implements(IIMSManifestWriter)
135    name = _(u'IMS Interchange Writer') 
136
137
138    def _writeObjectData(self, obj, path):
139        """ Write file data to the destination object. """
140        if type(obj) == type(''):
141            data = obj       
142        else:
143            format = ''
144            if hasattr(obj.aq_explicit, 'Format'):
145                format = obj.Format()
146            if obj.Type() in ['File', 'Image'] and hasattr(obj.aq_explicit, 'data'):
147                    data = obj.data
148            elif 'text/html' == format and hasattr(obj.aq_explicit, 'getText'):
149                    data = obj.getText()
150            elif format in ['text/plain', 'text/x-rst', 'text/structured'] and hasattr(obj.aq_explicit, 'getRawText'):
151                    data = obj.getRawText()
152            else:
153                data = ''
154
155        if self.destination:
156            self.destination.writeFile(path, data)
157
158    def _getAllObjects(self, context):
159        """ Get all sub objects. """
160        objects = [obj.getObject() for obj in context.portal_catalog.searchResults(path={'query':('/'.join(context.getPhysicalPath())+'/'),'depth':-1})]
161        return [object for object in objects if not object.isPrincipiaFolderish or getattr(object.aq_explicit, 'getText', None)]
162
163    def _getChildrenObjects(self, parent, objects):
164        """ Get objects in current folder """
165        parentpath = '/'.join(parent.getPhysicalPath())
166        children = []
167        for object in objects:
168            rpath = '/'.join(object.getPhysicalPath()).replace(parentpath, '')
169
170            if len(rpath.split('/')) == 1:
171                children.append(object)
172                       
173        return children
174
175    def _getObjectPath(self, obj, context):
176        """ Get the path of an object. """
177
178        root_path = context.aq_explicit.virtual_url_path()
179        obj_path = obj.aq_explicit.virtual_url_path()
180
181        if obj_path.find(root_path) != 0:
182            return ''
183
184        # Remove the path of the folder object
185        path = obj_path.replace(root_path, '')
186        if path and path[0] == '/':
187            path = path[1:]
188
189        if not path:
190            return ''
191
192        if hasattr(obj.aq_explicit, 'Format'):
193            if 'text/html' == obj.Format() and obj.isPrincipiaFolderish:
194                path += '.html'
195
196        return path
197
198    def _createPathId(self, path, pre='RES'):
199        """ Create a unique id given a path """
200        return pre + str(md5.md5(path).hexdigest())
201
202    def _getCopyrightString(self, copyright, rights_holder, rights_holder_email):
203        cp = ''
204        if copyright:
205            cp += copyright
206        if rights_holder:
207            if cp:
208                cp += ', '
209            cp += rights_holder
210        if rights_holder_email:
211            if cp:
212                cp += ', '
213            cp += rights_holder_email
214        return cp
215 
216    def getObjSize(self, object):
217        """ Retrieves the correct size of the object"""
218        return '%d' %object.get_size()
219
220    def writeCustomMetadata(self, object):
221        """ Write the custom metdata for the package. Overridden at lower levels. """
222
223        return
224   
Note: See TracBrowser for help on using the repository browser.