[1429] | 1 | # -*- coding: utf-8 -*- |
---|
[1392] | 2 | import os |
---|
[690] | 3 | import tempfile |
---|
| 4 | |
---|
[282] | 5 | from zope.interface import implements |
---|
[690] | 6 | from zope.app.annotation.interfaces import IAnnotations |
---|
[282] | 7 | |
---|
| 8 | from collective.transmogrifier.interfaces import ITransmogrifier |
---|
| 9 | from collective.transmogrifier.transmogrifier import _load_config, constructPipeline |
---|
[690] | 10 | from collective.transmogrifier.transmogrifier import configuration_registry |
---|
[282] | 11 | |
---|
[407] | 12 | try: |
---|
| 13 | from Products.GenericSetup import context as gscontext |
---|
| 14 | except ImportError: |
---|
| 15 | from Products.CMFSetup import context as gscontext |
---|
[282] | 16 | |
---|
| 17 | from quintagroup.transmogrifier.writer import WriterSection |
---|
| 18 | from quintagroup.transmogrifier.reader import ReaderSection |
---|
[690] | 19 | from quintagroup.transmogrifier.configview import ANNOKEY |
---|
[282] | 20 | |
---|
| 21 | EXPORT_CONFIG = 'export' |
---|
| 22 | IMPORT_CONFIG = 'import' |
---|
| 23 | |
---|
[690] | 24 | CONFIGFILE = None |
---|
| 25 | def 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 | |
---|
[282] | 52 | def exportSiteStructure(context): |
---|
| 53 | transmogrifier = ITransmogrifier(context.getSite()) |
---|
| 54 | |
---|
| 55 | # we don't use transmogrifer's __call__ method, because we need to do |
---|
| 56 | # some modification in pipeline sections |
---|
| 57 | |
---|
[690] | 58 | config_name = registerPersistentConfig(context.getSite(), 'export') |
---|
| 59 | if config_name is None: |
---|
| 60 | transmogrifier._raw = _load_config(EXPORT_CONFIG) |
---|
| 61 | else: |
---|
| 62 | transmogrifier._raw = _load_config(config_name) |
---|
| 63 | global CONFIGFILE |
---|
| 64 | CONFIGFILE = None |
---|
[282] | 65 | transmogrifier._data = {} |
---|
| 66 | |
---|
| 67 | options = transmogrifier._raw['transmogrifier'] |
---|
| 68 | sections = options['pipeline'].splitlines() |
---|
| 69 | pipeline = constructPipeline(transmogrifier, sections) |
---|
| 70 | |
---|
| 71 | last_section = pipeline.gi_frame.f_locals['self'] |
---|
| 72 | |
---|
| 73 | # if 'quintagroup.transmogrifier.writer' section's export context is |
---|
| 74 | # tarball replace it with given function argument |
---|
| 75 | while hasattr(last_section, 'previous'): |
---|
| 76 | if isinstance(last_section, WriterSection) and \ |
---|
[339] | 77 | isinstance(last_section.export_context, gscontext.TarballExportContext): |
---|
[282] | 78 | last_section.export_context = context |
---|
| 79 | last_section = last_section.previous |
---|
| 80 | # end cycle if we get empty starter section |
---|
| 81 | if type(last_section) == type(iter(())): |
---|
| 82 | break |
---|
| 83 | last_section = last_section.gi_frame.f_locals['self'] |
---|
| 84 | |
---|
| 85 | # Pipeline execution |
---|
| 86 | for item in pipeline: |
---|
| 87 | pass # discard once processed |
---|
| 88 | |
---|
| 89 | def importSiteStructure(context): |
---|
| 90 | transmogrifier = ITransmogrifier(context.getSite()) |
---|
| 91 | |
---|
| 92 | # we don't use transmogrifer's __call__ method, because we need to do |
---|
| 93 | # some modification in pipeline sections |
---|
| 94 | |
---|
[690] | 95 | config_name = registerPersistentConfig(context.getSite(), 'import') |
---|
| 96 | if config_name is None: |
---|
| 97 | transmogrifier._raw = _load_config(IMPORT_CONFIG) |
---|
| 98 | else: |
---|
| 99 | transmogrifier._raw = _load_config(config_name) |
---|
| 100 | global CONFIGFILE |
---|
| 101 | CONFIGFILE = None |
---|
[282] | 102 | transmogrifier._data = {} |
---|
| 103 | |
---|
[1392] | 104 | # this function is also called when adding Plone site, so call standard handler |
---|
| 105 | path = '' |
---|
[1449] | 106 | prefix = 'structure' |
---|
[1392] | 107 | if 'reader' in transmogrifier._raw: |
---|
| 108 | path = transmogrifier._raw['reader'].get('path', '') |
---|
[1449] | 109 | prefix = transmogrifier._raw['reader'].get('prefix', 'structure') |
---|
| 110 | if not context.readDataFile('.objects.xml', subdir=os.path.join(path, prefix)): |
---|
[1392] | 111 | try: |
---|
| 112 | from Products.GenericSetup.interfaces import IFilesystemImporter |
---|
| 113 | IFilesystemImporter(context.getSite()).import_(context, 'structure', True) |
---|
| 114 | except ImportError: |
---|
| 115 | pass |
---|
| 116 | return |
---|
| 117 | |
---|
[282] | 118 | options = transmogrifier._raw['transmogrifier'] |
---|
| 119 | sections = options['pipeline'].splitlines() |
---|
| 120 | pipeline = constructPipeline(transmogrifier, sections) |
---|
| 121 | |
---|
| 122 | last_section = pipeline.gi_frame.f_locals['self'] |
---|
| 123 | |
---|
| 124 | # if 'quintagroup.transmogrifier.writer' section's export context is |
---|
| 125 | # tarball replace it with given function argument |
---|
| 126 | while hasattr(last_section, 'previous'): |
---|
| 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 |
---|