[275] | 1 | from zope.interface import classProvides, implements |
---|
[415] | 2 | from zope.annotation.interfaces import IAnnotations |
---|
[275] | 3 | |
---|
| 4 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
[1446] | 5 | from collective.transmogrifier.utils import Condition |
---|
[275] | 6 | |
---|
| 7 | from Products.CMFCore.interfaces import IFolderish |
---|
| 8 | from Products.Archetypes.interfaces import IBaseFolder |
---|
| 9 | |
---|
[415] | 10 | from quintagroup.transmogrifier.logger import VALIDATIONKEY |
---|
| 11 | |
---|
[275] | 12 | class SiteWalkerSection(object): |
---|
| 13 | classProvides(ISectionBlueprint) |
---|
| 14 | implements(ISection) |
---|
| 15 | |
---|
| 16 | def __init__(self, transmogrifier, name, options, previous): |
---|
| 17 | self.previous = previous |
---|
| 18 | self.context = transmogrifier.context |
---|
[415] | 19 | self.pathkey = options.get('path-key', '_path').strip() |
---|
| 20 | self.typekey = options.get('type-key', '_type').strip() |
---|
| 21 | self.entrieskey = options.get('entries-key', '_entries').strip() |
---|
| 22 | # this is used for communication with 'logger' section |
---|
| 23 | self.anno = IAnnotations(transmogrifier) |
---|
| 24 | self.storage = self.anno.setdefault(VALIDATIONKEY, []) |
---|
[275] | 25 | |
---|
[1446] | 26 | self.condition = Condition(options.get('condition', 'python:True'), |
---|
| 27 | transmogrifier, name, options) |
---|
| 28 | |
---|
| 29 | def getContained(self, obj): |
---|
| 30 | contained = [(k, v) for k, v in obj.contentItems() |
---|
| 31 | if self.condition(None, context=v)] |
---|
| 32 | return tuple(contained) |
---|
| 33 | |
---|
[275] | 34 | def walk(self, obj): |
---|
| 35 | if IFolderish.providedBy(obj) or IBaseFolder.providedBy(obj): |
---|
[1446] | 36 | contained = self.getContained(obj) |
---|
| 37 | yield obj, tuple([(k, v.getPortalTypeName()) for k, v in contained]) |
---|
| 38 | for k, v in contained: |
---|
[275] | 39 | for x in self.walk(v): |
---|
| 40 | yield x |
---|
| 41 | else: |
---|
| 42 | yield obj, () |
---|
| 43 | |
---|
| 44 | def __iter__(self): |
---|
| 45 | for item in self.previous: |
---|
| 46 | yield item |
---|
| 47 | |
---|
| 48 | for obj, contained in self.walk(self.context): |
---|
| 49 | item = { |
---|
[415] | 50 | self.pathkey: '/'.join(obj.getPhysicalPath()[2:]), |
---|
| 51 | self.typekey: obj.getPortalTypeName(), |
---|
[275] | 52 | } |
---|
| 53 | if contained: |
---|
[415] | 54 | item[self.entrieskey] = contained |
---|
| 55 | # add item path to stack |
---|
| 56 | self.storage.append(item[self.pathkey]) |
---|
| 57 | |
---|
[275] | 58 | yield item |
---|
| 59 | |
---|
[415] | 60 | # cleanup |
---|
| 61 | if VALIDATIONKEY in self.anno: |
---|
| 62 | del self.anno[VALIDATIONKEY] |
---|