1 | from zope.interface import classProvides, implements |
---|
2 | from zope.annotation.interfaces import IAnnotations |
---|
3 | |
---|
4 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
5 | from collective.transmogrifier.utils import Condition |
---|
6 | |
---|
7 | from Products.CMFCore.interfaces import IFolderish |
---|
8 | from Products.Archetypes.interfaces import IBaseFolder |
---|
9 | |
---|
10 | from quintagroup.transmogrifier.logger import VALIDATIONKEY |
---|
11 | |
---|
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 |
---|
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, []) |
---|
25 | |
---|
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 | |
---|
34 | def walk(self, obj): |
---|
35 | if IFolderish.providedBy(obj) or IBaseFolder.providedBy(obj): |
---|
36 | contained = self.getContained(obj) |
---|
37 | yield obj, tuple([(k, v.getPortalTypeName()) for k, v in contained]) |
---|
38 | for k, v in contained: |
---|
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 = { |
---|
50 | self.pathkey: '/'.join(obj.getPhysicalPath()[2:]), |
---|
51 | self.typekey: obj.getPortalTypeName(), |
---|
52 | } |
---|
53 | if contained: |
---|
54 | item[self.entrieskey] = contained |
---|
55 | # add item path to stack |
---|
56 | self.storage.append(item[self.pathkey]) |
---|
57 | |
---|
58 | yield item |
---|
59 | |
---|
60 | # cleanup |
---|
61 | if VALIDATIONKEY in self.anno: |
---|
62 | del self.anno[VALIDATIONKEY] |
---|