[1602] | 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 | |
---|
[1605] | 51 | if not self.condition(item, context=obj, |
---|
| 52 | fprop_info=fields): |
---|
| 53 | continue |
---|
| 54 | |
---|
[1602] | 55 | files['file-properties'] = { |
---|
| 56 | 'name': '.file-properties.xml', |
---|
| 57 | 'data': self.createManifest(fields), |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | if len(data) > 0: |
---|
| 61 | files[fields['__name__']] = { |
---|
| 62 | 'name': name, |
---|
| 63 | 'data': data, |
---|
| 64 | 'content_type': fields['content_type'], |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | yield item |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | def createManifest(self, props): |
---|
| 71 | doc = self.doc |
---|
| 72 | |
---|
| 73 | root = doc.createElement('manifest') |
---|
| 74 | for name, val in props.items(): |
---|
| 75 | # create field node |
---|
| 76 | prop = doc.createElement('prop') |
---|
| 77 | |
---|
| 78 | # set id attribute |
---|
| 79 | attr_id = doc.createAttribute('id') |
---|
| 80 | attr_id.value = name |
---|
| 81 | prop.setAttributeNode(attr_id) |
---|
| 82 | |
---|
| 83 | # add value |
---|
| 84 | v = doc.createTextNode(str(val)) |
---|
| 85 | prop.appendChild(v) |
---|
| 86 | |
---|
| 87 | root.appendChild(prop) |
---|
| 88 | |
---|
| 89 | doc.appendChild(root) |
---|
| 90 | |
---|
| 91 | try: |
---|
| 92 | data = doc.toprettyxml(indent=' ', encoding='utf-8') |
---|
| 93 | except UnicodeDecodeError, e: |
---|
| 94 | # all comments are strings encoded in 'utf-8' and they will properly |
---|
| 95 | # saved in xml file, but if we explicitly give 'utf-8' encoding |
---|
| 96 | # UnicodeDecodeError will be raised when they have non-ascii chars |
---|
| 97 | data = doc.toprettyxml(indent=' ') |
---|
| 98 | |
---|
| 99 | doc.unlink() |
---|
| 100 | return data |
---|
| 101 | |
---|
[1605] | 102 | |
---|
| 103 | class OFSImporterSection(object): |
---|
| 104 | classProvides(ISectionBlueprint) |
---|
| 105 | implements(ISection) |
---|
| 106 | |
---|
| 107 | def __init__(self, transmogrifier, name, options, previous): |
---|
| 108 | self.previous = previous |
---|
| 109 | self.context = transmogrifier.context |
---|
| 110 | |
---|
| 111 | self.pathkey = defaultMatcher(options, 'path-key', name, 'path') |
---|
| 112 | self.fileskey = defaultMatcher(options, 'files-key', name, 'files') |
---|
| 113 | self.contextkey = defaultMatcher(options, 'context-key', name, 'import_context') |
---|
| 114 | |
---|
| 115 | self.condition = Condition(options.get('condition', 'python:True'), |
---|
| 116 | transmogrifier, name, options) |
---|
| 117 | |
---|
| 118 | def __iter__(self): |
---|
| 119 | for item in self.previous: |
---|
| 120 | pathkey = self.pathkey(*item.keys())[0] |
---|
| 121 | fileskey = self.fileskey(*item.keys())[0] |
---|
| 122 | |
---|
| 123 | if not (pathkey and fileskey): |
---|
| 124 | yield item; continue |
---|
| 125 | if 'file-properties' not in item[fileskey]: |
---|
| 126 | yield item; continue |
---|
| 127 | |
---|
| 128 | path = item[pathkey] |
---|
| 129 | obj = self.context.unrestrictedTraverse(path, None) |
---|
| 130 | if obj is None: # path doesn't exist |
---|
| 131 | yield item; continue |
---|
| 132 | |
---|
| 133 | if obj.meta_type in ["File", "Image"]: |
---|
| 134 | try: |
---|
| 135 | manifest = item[fileskey]['file-properties']['data'] |
---|
| 136 | prop_info = self.parseManifest(manifest) |
---|
| 137 | # Update file/image properties |
---|
| 138 | if not self.condition(item, context=obj, |
---|
| 139 | fprop_info=prop_info): |
---|
| 140 | continue |
---|
| 141 | for prop_id, prop_val in prop_info.items(): |
---|
| 142 | setattr(obj, prop_id, prop_val) |
---|
| 143 | |
---|
| 144 | # Add data for file/image |
---|
| 145 | name = prop_info.get('__name__', None) |
---|
| 146 | if name is None: |
---|
| 147 | continue |
---|
| 148 | data = item[fileskey].get(name, None) |
---|
| 149 | if data is None: |
---|
| 150 | continue |
---|
| 151 | obj.data = data['data'] |
---|
| 152 | |
---|
| 153 | except Exception, e: |
---|
| 154 | print "Exception in ofsimporter section:" |
---|
| 155 | print '-'*60 |
---|
| 156 | traceback.print_exc() |
---|
| 157 | print '-'*60 |
---|
| 158 | |
---|
| 159 | yield item |
---|
| 160 | |
---|
| 161 | def parseManifest(self, manifest): |
---|
| 162 | doc = minidom.parseString(manifest) |
---|
| 163 | props = {} |
---|
| 164 | for prop in doc.getElementsByTagName('prop'): |
---|
| 165 | prop_id = str(prop.getAttribute('id')) |
---|
| 166 | if not prop_id: |
---|
| 167 | continue |
---|
| 168 | props[prop_id] = str(prop.firstChild.nodeValue.strip()) |
---|
| 169 | |
---|
| 170 | return props |
---|