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

Last change on this file since 679 was 679, checked in by crchemist, 17 years ago

Fixup error on deletion of qPloneCaptchas properties.

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