1 | from xml.dom import minidom |
---|
2 | |
---|
3 | from zope.interface import classProvides, implements |
---|
4 | |
---|
5 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
6 | from collective.transmogrifier.utils import defaultMatcher |
---|
7 | |
---|
8 | from OFS.interfaces import IPropertyManager |
---|
9 | from Products.GenericSetup.utils import PropertyManagerHelpers |
---|
10 | |
---|
11 | class PropertyManagerSection(object): |
---|
12 | classProvides(ISectionBlueprint) |
---|
13 | implements(ISection) |
---|
14 | |
---|
15 | def __init__(self, transmogrifier, name, options, previous): |
---|
16 | self.previous = previous |
---|
17 | self.context = transmogrifier.context |
---|
18 | |
---|
19 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
20 | self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_properties') |
---|
21 | if 'exclude' in options: |
---|
22 | self.exclude = [i.strip() for i in options['exclude'].splitlines() if i.strip()] |
---|
23 | else: |
---|
24 | self.exclude = [] |
---|
25 | |
---|
26 | self.helper = PropertyManagerHelpers() |
---|
27 | self.doc = minidom.Document() |
---|
28 | self.helper._doc = self.doc |
---|
29 | |
---|
30 | |
---|
31 | def __iter__(self): |
---|
32 | helper = self.helper |
---|
33 | doc = self.doc |
---|
34 | |
---|
35 | for item in self.previous: |
---|
36 | pathkey = self.pathkey(*item.keys())[0] |
---|
37 | |
---|
38 | if not pathkey: |
---|
39 | yield item; continue |
---|
40 | |
---|
41 | path = item[pathkey] |
---|
42 | obj = self.context.unrestrictedTraverse(path, None) |
---|
43 | if obj is None: # path doesn't exist |
---|
44 | yield item; continue |
---|
45 | |
---|
46 | if IPropertyManager.providedBy(obj): |
---|
47 | data = None |
---|
48 | excludekey = self.excludekey(*item.keys())[0] |
---|
49 | excluded_props = self.exclude |
---|
50 | if excludekey: |
---|
51 | excluded_props = tuple(set(item[excludekey]) | set(excluded_props)) |
---|
52 | |
---|
53 | helper.context = obj |
---|
54 | node = doc.createElement('properties') |
---|
55 | for elem in helper._extractProperties().childNodes: |
---|
56 | if elem.getAttribute('name') not in excluded_props: |
---|
57 | node.appendChild(elem) |
---|
58 | if node.hasChildNodes(): |
---|
59 | doc.appendChild(node) |
---|
60 | data = doc.toprettyxml(indent=' ', encoding='utf-8') |
---|
61 | doc.unlink() |
---|
62 | |
---|
63 | if data: |
---|
64 | files = item.setdefault('_files', {}) |
---|
65 | item['_files']['propertymanager'] = { |
---|
66 | 'name': '.properties.xml', |
---|
67 | 'data': data, |
---|
68 | } |
---|
69 | |
---|
70 | yield item |
---|