1 | from zope.interface import classProvides, implements |
---|
2 | from zope.component import queryMultiAdapter |
---|
3 | |
---|
4 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
5 | from collective.transmogrifier.utils import defaultMatcher |
---|
6 | |
---|
7 | from quintagroup.transmogrifier.interfaces import IExportDataCorrector |
---|
8 | from quintagroup.transmogrifier.interfaces import IImportDataCorrector |
---|
9 | |
---|
10 | class DataCorrectorSection(object): |
---|
11 | classProvides(ISectionBlueprint) |
---|
12 | implements(ISection) |
---|
13 | |
---|
14 | def __init__(self, transmogrifier, name, options, previous): |
---|
15 | self.previous = previous |
---|
16 | self.transmogrifier = transmogrifier |
---|
17 | self.context = transmogrifier.context |
---|
18 | |
---|
19 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
20 | self.fileskey = defaultMatcher(options, 'files-key', name, 'files') |
---|
21 | |
---|
22 | # 'type' options specifies adapter interface |
---|
23 | self.type_ = options.get('type', 'export') |
---|
24 | if self.type_ == 'export': |
---|
25 | self.interface = IExportDataCorrector |
---|
26 | elif self.type_ == 'import': |
---|
27 | self.interface = IImportDataCorrector |
---|
28 | else: |
---|
29 | self.interface = None |
---|
30 | |
---|
31 | # 'sources' specifies names of adapters |
---|
32 | self.sources = options.get('sources', '') |
---|
33 | self.sources = filter(None, [i.strip() for i in self.sources.splitlines()]) |
---|
34 | |
---|
35 | def __iter__(self): |
---|
36 | for item in self.previous: |
---|
37 | pathkey = self.pathkey(*item.keys())[0] |
---|
38 | fileskey = self.fileskey(*item.keys())[0] |
---|
39 | |
---|
40 | if not (pathkey and fileskey and self.sources): |
---|
41 | yield item; continue |
---|
42 | |
---|
43 | path = item[pathkey] |
---|
44 | obj = self.context.unrestrictedTraverse(path, None) |
---|
45 | if obj is None: # path doesn't exist |
---|
46 | yield item; continue |
---|
47 | |
---|
48 | file_store = item[fileskey] |
---|
49 | if not file_store: |
---|
50 | yield item; continue |
---|
51 | |
---|
52 | for name in self.sources: |
---|
53 | if not name in file_store: |
---|
54 | continue |
---|
55 | adapter = queryMultiAdapter((obj, self.transmogrifier), |
---|
56 | self.interface, name) |
---|
57 | if adapter: |
---|
58 | file_store[name] = adapter(file_store[name]) |
---|
59 | |
---|
60 | yield item |
---|