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, NodeAdapterBase |
---|
10 | |
---|
11 | class Helper(PropertyManagerHelpers, NodeAdapterBase): |
---|
12 | """ We need this class because PropertyManagerHelpers in _initProperties |
---|
13 | method uses _convertToBoolean and _getNodeText methods from |
---|
14 | NodeAdapterBase class. |
---|
15 | """ |
---|
16 | def __init__(self): |
---|
17 | pass |
---|
18 | |
---|
19 | def _getNodeText(self, node): |
---|
20 | # We override method in NodeAdapterBase, because it return bad property value. |
---|
21 | # When properties are extracted newline charcters and indentation were added to |
---|
22 | # them, but these aren't stripped on import. Maybe this method doesn't handle |
---|
23 | # properly multiline string values, but it is needed for importing. |
---|
24 | text = '' |
---|
25 | for child in node.childNodes: |
---|
26 | if child.nodeName != '#text': |
---|
27 | continue |
---|
28 | text += child.nodeValue.strip() |
---|
29 | return text |
---|
30 | |
---|
31 | class PropertiesExporterSection(object): |
---|
32 | classProvides(ISectionBlueprint) |
---|
33 | implements(ISection) |
---|
34 | |
---|
35 | def __init__(self, transmogrifier, name, options, previous): |
---|
36 | self.previous = previous |
---|
37 | self.context = transmogrifier.context |
---|
38 | |
---|
39 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
40 | self.fileskey = options.get('files-key', '_files').strip() |
---|
41 | |
---|
42 | self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_properties') |
---|
43 | self.exclude = filter(None, [i.strip() for i in |
---|
44 | options.get('exclude', '').splitlines()]) |
---|
45 | |
---|
46 | self.helper = Helper() |
---|
47 | self.doc = minidom.Document() |
---|
48 | self.helper._doc = self.doc |
---|
49 | |
---|
50 | def __iter__(self): |
---|
51 | helper = self.helper |
---|
52 | doc = self.doc |
---|
53 | |
---|
54 | for item in self.previous: |
---|
55 | pathkey = self.pathkey(*item.keys())[0] |
---|
56 | |
---|
57 | if not pathkey: |
---|
58 | yield item; continue |
---|
59 | |
---|
60 | path = item[pathkey] |
---|
61 | obj = self.context.unrestrictedTraverse(path, None) |
---|
62 | if obj is None: # path doesn't exist |
---|
63 | yield item; continue |
---|
64 | |
---|
65 | if IPropertyManager.providedBy(obj): |
---|
66 | data = None |
---|
67 | excludekey = self.excludekey(*item.keys())[0] |
---|
68 | excluded_props = tuple(self.exclude) |
---|
69 | if excludekey: |
---|
70 | excluded_props = tuple(set(item[excludekey]) | set(excluded_props)) |
---|
71 | |
---|
72 | helper.context = obj |
---|
73 | node = doc.createElement('properties') |
---|
74 | for elem in helper._extractProperties().childNodes: |
---|
75 | if elem.nodeName != 'property': |
---|
76 | continue |
---|
77 | if elem.getAttribute('name') not in excluded_props: |
---|
78 | node.appendChild(elem) |
---|
79 | if node.hasChildNodes(): |
---|
80 | doc.appendChild(node) |
---|
81 | data = doc.toprettyxml(indent=' ', encoding='utf-8') |
---|
82 | doc.unlink() |
---|
83 | |
---|
84 | if data: |
---|
85 | files = item.setdefault(self.fileskey, {}) |
---|
86 | item[self.fileskey]['propertymanager'] = { |
---|
87 | 'name': '.properties.xml', |
---|
88 | 'data': data, |
---|
89 | } |
---|
90 | |
---|
91 | yield item |
---|
92 | |
---|
93 | class PropertiesImporterSection(object): |
---|
94 | classProvides(ISectionBlueprint) |
---|
95 | implements(ISection) |
---|
96 | |
---|
97 | def __init__(self, transmogrifier, name, options, previous): |
---|
98 | self.previous = previous |
---|
99 | self.context = transmogrifier.context |
---|
100 | |
---|
101 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
102 | self.fileskey = defaultMatcher(options, 'files-key', name, 'files') |
---|
103 | |
---|
104 | self.excludekey = defaultMatcher(options, 'exclude-key', name, 'excluded_properties') |
---|
105 | self.exclude = filter(None, [i.strip() for i in |
---|
106 | options.get('exclude', '').splitlines()]) |
---|
107 | |
---|
108 | self.helper = Helper() |
---|
109 | self.helper._encoding = 'utf-8' |
---|
110 | |
---|
111 | def __iter__(self): |
---|
112 | helper = self.helper |
---|
113 | |
---|
114 | for item in self.previous: |
---|
115 | pathkey = self.pathkey(*item.keys())[0] |
---|
116 | fileskey = self.fileskey(*item.keys())[0] |
---|
117 | |
---|
118 | if not (pathkey and fileskey): |
---|
119 | yield item; continue |
---|
120 | if 'propertymanager' not in item[fileskey]: |
---|
121 | yield item; continue |
---|
122 | |
---|
123 | path = item[pathkey] |
---|
124 | obj = self.context.unrestrictedTraverse(path, None) |
---|
125 | if obj is None: # path doesn't exist |
---|
126 | yield item; continue |
---|
127 | |
---|
128 | if IPropertyManager.providedBy(obj): |
---|
129 | data = None |
---|
130 | excludekey = self.excludekey(*item.keys())[0] |
---|
131 | excluded_props = self.exclude |
---|
132 | if excludekey: |
---|
133 | excluded_props = tuple(set(item[excludekey]) | set(excluded_props)) |
---|
134 | |
---|
135 | data = item[fileskey]['propertymanager']['data'] |
---|
136 | doc = minidom.parseString(data) |
---|
137 | root = doc.documentElement |
---|
138 | for child in root.childNodes: |
---|
139 | if child.nodeName != 'property': |
---|
140 | continue |
---|
141 | if child.getAttribute('name') in excluded_props: |
---|
142 | root.removeChild(child) |
---|
143 | |
---|
144 | helper.context = obj |
---|
145 | helper._initProperties(root) |
---|
146 | |
---|
147 | yield item |
---|