[275] | 1 | from zope.interface import classProvides, implements |
---|
| 2 | from ZODB.POSException import ConflictError |
---|
| 3 | |
---|
| 4 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
| 5 | from collective.transmogrifier.utils import defaultMatcher |
---|
| 6 | |
---|
| 7 | from Products.Marshall import registry |
---|
| 8 | from Products.Archetypes.interfaces import IBaseObject |
---|
| 9 | |
---|
| 10 | class MarshallSection(object): |
---|
| 11 | classProvides(ISectionBlueprint) |
---|
| 12 | implements(ISection) |
---|
| 13 | |
---|
| 14 | def __init__(self, transmogrifier, name, options, previous): |
---|
| 15 | self.previous = previous |
---|
| 16 | self.context = transmogrifier.context |
---|
| 17 | |
---|
| 18 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
| 19 | self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_fields') |
---|
| 20 | |
---|
| 21 | self.marshaller = registry.getComponent("atxml") |
---|
| 22 | |
---|
| 23 | def __iter__(self): |
---|
| 24 | for item in self.previous: |
---|
| 25 | pathkey = self.pathkey(*item.keys())[0] |
---|
| 26 | |
---|
| 27 | if not pathkey: |
---|
| 28 | yield item; continue |
---|
| 29 | |
---|
| 30 | path = item[pathkey] |
---|
| 31 | obj = self.context.unrestrictedTraverse(path, None) |
---|
| 32 | if obj is None: # path doesn't exist |
---|
| 33 | yield item; continue |
---|
| 34 | |
---|
| 35 | if IBaseObject.providedBy(obj): |
---|
| 36 | excludekey = self.excludekey(*item.keys())[0] |
---|
| 37 | atns_exclude = () |
---|
| 38 | if excludekey: |
---|
| 39 | atns_exclude = item[excludekey] |
---|
| 40 | |
---|
| 41 | try: |
---|
| 42 | content_type, length, data = self.marshaller.marshall(obj, atns_exclude=atns_exclude) |
---|
| 43 | except ConflictError: |
---|
| 44 | raise |
---|
| 45 | except: |
---|
| 46 | data = None |
---|
| 47 | |
---|
| 48 | if data or data is None: |
---|
| 49 | # None value has special meaning for IExportDataCorrector adapter for topic criterias |
---|
| 50 | files = item.setdefault('_files', {}) |
---|
| 51 | item['_files']['marshall'] = { |
---|
| 52 | 'name': '.marshall.xml', |
---|
| 53 | 'data': data, |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | yield item |
---|