1 | from zope.interface import classProvides, implements |
---|
2 | from zope.component import queryAdapter |
---|
3 | |
---|
4 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
5 | from collective.transmogrifier.utils import defaultMatcher |
---|
6 | |
---|
7 | from quintagroup.transmogrifier.simpleblog2quills.interfaces import \ |
---|
8 | IItemManipulator, IExportItemManipulator, IImportItemManipulator |
---|
9 | |
---|
10 | class ItemManipulatorSection(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 | |
---|
20 | # 'type' options specifies adapter interface |
---|
21 | type_ = options.get('type', 'general') |
---|
22 | if type_ == 'general': |
---|
23 | self.interface = IItemManipulator |
---|
24 | elif type_ == 'export': |
---|
25 | self.interface = IExportItemManipulator |
---|
26 | elif type_ == 'import': |
---|
27 | self.interface = IImportItemManipulator |
---|
28 | else: |
---|
29 | self.interface = None |
---|
30 | |
---|
31 | def __iter__(self): |
---|
32 | for item in self.previous: |
---|
33 | pathkey = self.pathkey(*item.keys())[0] |
---|
34 | |
---|
35 | if not (pathkey and self.interface is not None): |
---|
36 | yield item; continue |
---|
37 | |
---|
38 | path = item[pathkey] |
---|
39 | obj = self.context.unrestrictedTraverse(path, None) |
---|
40 | if obj is None: # path doesn't exist |
---|
41 | yield item; continue |
---|
42 | |
---|
43 | adapter = queryAdapter(obj, self.interface, name='') |
---|
44 | if adapter: |
---|
45 | item = adapter(item, path=pathkey) |
---|
46 | |
---|
47 | yield item |
---|