source: products/quintagroup.transmogrifier/branches/plone-2.1/quintagroup.transmogrifier/quintagroup/transmogrifier/exportimport.py @ 1457

Last change on this file since 1457 was 1457, checked in by piv, 14 years ago

merge r2491-2495 from trunk: not exception in atns namespace in case of empty text field, not run content import step in case quintagroup.transmogrifier profile is not selected

File size: 5.0 KB
Line 
1# -*- coding: utf-8 -*-
2import os
3import tempfile
4
5from zope.interface import implements
6from zope.app.annotation.interfaces import IAnnotations
7
8from collective.transmogrifier.interfaces import ITransmogrifier
9from collective.transmogrifier.transmogrifier import _load_config, constructPipeline
10from collective.transmogrifier.transmogrifier import configuration_registry
11
12try:
13    from Products.GenericSetup import context as gscontext
14except ImportError:
15    from Products.CMFSetup import context as gscontext
16
17from quintagroup.transmogrifier.writer import WriterSection
18from quintagroup.transmogrifier.reader import ReaderSection
19from quintagroup.transmogrifier.configview import ANNOKEY
20
21EXPORT_CONFIG = 'export'
22IMPORT_CONFIG = 'import'
23
24CONFIGFILE = None
25def registerPersistentConfig(site, type_):
26    """ Try to get persistent pipeline configuration of given type (export or import)
27        and register it for use with transmogrifier.
28    """
29    global CONFIGFILE
30    anno = IAnnotations(site)
31    key = '%s.%s' % (ANNOKEY, type_)
32    config = anno.has_key(key) and anno[key] or None
33
34    # unregister old config
35    name = 'persitent-%s' % type_
36    if name in configuration_registry._config_ids:
37        configuration_registry._config_ids.remove(name)
38        del configuration_registry._config_info[name]
39
40    # register new
41    if config is not None:
42        title = description = u'Persistent %s pipeline'
43        tf = tempfile.NamedTemporaryFile('w+t', suffix='.cfg')
44        tf.write(config)
45        tf.seek(0)
46        CONFIGFILE = tf
47        configuration_registry.registerConfiguration(name, title, description, tf.name)
48        return name
49    else:
50        return None
51
52def exportSiteStructure(context):
53
54    transmogrifier = ITransmogrifier(context.getSite())
55
56    # we don't use transmogrifer's __call__ method, because we need to do
57    # some modification in pipeline sections
58
59    config_name = registerPersistentConfig(context.getSite(), 'export')
60    if config_name is None:
61        transmogrifier._raw = _load_config(EXPORT_CONFIG)
62    else:
63        transmogrifier._raw = _load_config(config_name)
64        global CONFIGFILE
65        CONFIGFILE = None
66    transmogrifier._data = {}
67
68    options = transmogrifier._raw['transmogrifier']
69    sections = options['pipeline'].splitlines()
70    pipeline = constructPipeline(transmogrifier, sections)
71
72    last_section = pipeline.gi_frame.f_locals['self']
73
74    # if 'quintagroup.transmogrifier.writer' section's export context is
75    # tarball replace it with given function argument
76    while hasattr(last_section, 'previous'):
77        if isinstance(last_section, WriterSection) and \
78            isinstance(last_section.export_context, gscontext.TarballExportContext):
79            last_section.export_context = context
80        last_section = last_section.previous
81        # end cycle if we get empty starter section
82        if type(last_section) == type(iter(())):
83            break
84        last_section = last_section.gi_frame.f_locals['self']
85
86    # Pipeline execution
87    for item in pipeline:
88        pass # discard once processed
89
90def importSiteStructure(context):
91    # Only run step if a flag file is present
92    if context.readDataFile('quintagroup.transmogrifier-import.txt') is None:
93        return
94
95    transmogrifier = ITransmogrifier(context.getSite())
96
97    # we don't use transmogrifer's __call__ method, because we need to do
98    # some modification in pipeline sections
99
100    config_name = registerPersistentConfig(context.getSite(), 'import')
101    if config_name is None:
102        transmogrifier._raw = _load_config(IMPORT_CONFIG)
103    else:
104        transmogrifier._raw = _load_config(config_name)
105        global CONFIGFILE
106        CONFIGFILE = None
107    transmogrifier._data = {}
108
109    # this function is also called when adding Plone site, so call standard handler
110    path = ''
111    prefix = 'structure'
112    if 'reader' in transmogrifier._raw:
113        path = transmogrifier._raw['reader'].get('path', '')
114        prefix = transmogrifier._raw['reader'].get('prefix', 'structure')
115    if not context.readDataFile('.objects.xml', subdir=os.path.join(path, prefix)):
116        try:
117            from Products.GenericSetup.interfaces import IFilesystemImporter
118            IFilesystemImporter(context.getSite()).import_(context, 'structure', True)
119        except ImportError:
120            pass
121        return
122
123    options = transmogrifier._raw['transmogrifier']
124    sections = options['pipeline'].splitlines()
125    pipeline = constructPipeline(transmogrifier, sections)
126
127    last_section = pipeline.gi_frame.f_locals['self']
128
129    # if 'quintagroup.transmogrifier.writer' section's export context is
130    # tarball replace it with given function argument
131    while hasattr(last_section, 'previous'):
132        last_section = last_section.previous
133        # end cycle if we get empty starter section
134        if type(last_section) == type(iter(())):
135            break
136        last_section = last_section.gi_frame.f_locals['self']
137
138    # Pipeline execution
139    for item in pipeline:
140        pass # discard once processed
Note: See TracBrowser for help on using the repository browser.