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

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

temporarily dirty fix to make import of content with directory context POSSIBLE!

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