1 | import traceback |
---|
2 | from xml.dom import minidom |
---|
3 | |
---|
4 | from zope.interface import classProvides, implements |
---|
5 | |
---|
6 | from collective.transmogrifier.interfaces import ISection, ISectionBlueprint |
---|
7 | from collective.transmogrifier.utils import defaultMatcher, Condition |
---|
8 | |
---|
9 | class OFSExporterSection(object): |
---|
10 | classProvides(ISectionBlueprint) |
---|
11 | implements(ISection) |
---|
12 | |
---|
13 | def __init__(self, transmogrifier, name, options, previous): |
---|
14 | self.previous = previous |
---|
15 | self.context = transmogrifier.context |
---|
16 | |
---|
17 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
18 | self.fileskey = options.get('files-key', '_files').strip() |
---|
19 | |
---|
20 | self.doc = minidom.Document() |
---|
21 | self.condition = Condition(options.get('condition', 'python:True'), |
---|
22 | transmogrifier, name, options) |
---|
23 | |
---|
24 | def __iter__(self): |
---|
25 | for item in self.previous: |
---|
26 | pathkey = self.pathkey(*item.keys())[0] |
---|
27 | |
---|
28 | if not pathkey: |
---|
29 | yield item; continue |
---|
30 | |
---|
31 | path = item[pathkey] |
---|
32 | files = item.setdefault(self.fileskey, {}) |
---|
33 | obj = self.context.unrestrictedTraverse(path, None) |
---|
34 | if obj is None: # path doesn't exist |
---|
35 | yield item; continue |
---|
36 | |
---|
37 | if obj.meta_type in ["File", "Image"]: |
---|
38 | name = getattr(obj, '__name__', obj.id()) |
---|
39 | data = getattr(obj, 'data', '') |
---|
40 | fields = {'__name__': name, |
---|
41 | 'title': getattr(obj, 'title', name), |
---|
42 | 'precondition': getattr(obj, 'precondition', ''), |
---|
43 | 'size': getattr(obj, 'get_size', lambda o:0)(), |
---|
44 | 'content_type': getattr(obj, 'content_type', ''), |
---|
45 | } |
---|
46 | |
---|
47 | if obj.meta_type == "Image": |
---|
48 | fields['width'] = getattr(obj, 'width', 0) |
---|
49 | fields['height'] = getattr(obj, 'height', 0) |
---|
50 | |
---|
51 | files['file-properties'] = { |
---|
52 | 'name': '.file-properties.xml', |
---|
53 | 'data': self.createManifest(fields), |
---|
54 | } |
---|
55 | |
---|
56 | if len(data) > 0: |
---|
57 | files[fields['__name__']] = { |
---|
58 | 'name': name, |
---|
59 | 'data': data, |
---|
60 | 'content_type': fields['content_type'], |
---|
61 | } |
---|
62 | |
---|
63 | yield item |
---|
64 | |
---|
65 | |
---|
66 | def createManifest(self, props): |
---|
67 | doc = self.doc |
---|
68 | |
---|
69 | root = doc.createElement('manifest') |
---|
70 | for name, val in props.items(): |
---|
71 | # create field node |
---|
72 | prop = doc.createElement('prop') |
---|
73 | |
---|
74 | # set id attribute |
---|
75 | attr_id = doc.createAttribute('id') |
---|
76 | attr_id.value = name |
---|
77 | prop.setAttributeNode(attr_id) |
---|
78 | |
---|
79 | # add value |
---|
80 | v = doc.createTextNode(str(val)) |
---|
81 | prop.appendChild(v) |
---|
82 | |
---|
83 | root.appendChild(prop) |
---|
84 | |
---|
85 | doc.appendChild(root) |
---|
86 | |
---|
87 | try: |
---|
88 | data = doc.toprettyxml(indent=' ', encoding='utf-8') |
---|
89 | except UnicodeDecodeError, e: |
---|
90 | # all comments are strings encoded in 'utf-8' and they will properly |
---|
91 | # saved in xml file, but if we explicitly give 'utf-8' encoding |
---|
92 | # UnicodeDecodeError will be raised when they have non-ascii chars |
---|
93 | data = doc.toprettyxml(indent=' ') |
---|
94 | |
---|
95 | doc.unlink() |
---|
96 | return data |
---|
97 | |
---|