source: products/quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/writer.py @ 1413

Last change on this file since 1413 was 407, checked in by piv, 18 years ago

set @@View permissions to cmf.ModifyPortalContent?

File size: 2.3 KB
Line 
1import os.path
2import time
3
4from zope.interface import classProvides, implements
5
6from collective.transmogrifier.interfaces import ISection, ISectionBlueprint
7from collective.transmogrifier.utils import defaultMatcher
8
9from Products.CMFCore import utils
10
11# import monkey pathes for GS TarballContext
12import quintagroup.transmogrifier.patches
13
14try:
15    from Products.GenericSetup import context
16except ImportError:
17    from Products.CMFSetup import context
18
19class WriterSection(object):
20    classProvides(ISectionBlueprint)
21    implements(ISection)
22
23    def __init__(self, transmogrifier, name, options, previous):
24        self.previous = previous
25        self.context = transmogrifier.context
26
27        self.pathkey = defaultMatcher(options, 'path-key', name, 'path')
28        self.fileskey = defaultMatcher(options, 'files-key', name, 'files')
29
30        if 'prefix' in options:
31            self.prefix = options['prefix'].strip()
32        else:
33            self.prefix = ''
34
35        context_type = options.get('context', 'tarball').strip()
36
37        setup_tool = utils.getToolByName(self.context, 'portal_setup')
38        if context_type == 'directory':
39            profile_path = options.get('path', '')
40            self.export_context = context.DirectoryExportContext(setup_tool, profile_path)
41        elif context_type == 'tarball':
42            self.export_context = context.TarballExportContext(setup_tool)
43        elif context_type == 'snapshot':
44            items = ('snapshot',) + time.gmtime()[:6]
45            snapshot_id = '%s-%4d%02d%02d%02d%02d%02d' % items
46            self.export_context = context.SnapshotExportContext(setup_tool, snapshot_id)
47        else:
48            self.export_context = context.TarballExportContext(setup_tool)
49
50    def __iter__(self):
51        for item in self.previous:
52            pathkey = self.pathkey(*item.keys())[0]
53            fileskey = self.fileskey(*item.keys())[0]
54
55            if not (pathkey and fileskey): # path doesn't exist or no data to write
56                yield item; continue
57
58            item['_export_context'] = self.export_context
59
60            path = item[pathkey]
61
62            item_path = os.path.join(self.prefix, path)
63            item_path = item_path.rstrip('/')
64
65            for k, v in item[fileskey].items():
66                self.export_context.writeDataFile(v['name'], v['data'], 'text/xml', subdir=item_path)
67
68            yield item
Note: See TracBrowser for help on using the repository browser.