source: products/quintagroup.transmogrifier/trunk/quintagroup/transmogrifier/exportimport.py @ 1446

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

merge from plone 2.1 branch: r2465-2483

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