source: products/quintagroup.transmogrifier/trunk/quintagroup/transmogrifier/reader.py @ 1589

Last change on this file since 1589 was 415, checked in by chervol, 18 years ago

rm

File size: 3.4 KB
Line 
1import os.path
2
3from zope.interface import classProvides, implements
4from zope.annotation.interfaces import IAnnotations
5
6from collective.transmogrifier.interfaces import ISection, ISectionBlueprint
7from collective.transmogrifier.utils import defaultMatcher
8
9from Products.GenericSetup import context
10from Products.CMFCore import utils
11
12# import monkey pathes for GS TarballContext
13import quintagroup.transmogrifier.patches
14from quintagroup.transmogrifier.logger import VALIDATIONKEY
15
16class ReaderSection(object):
17    classProvides(ISectionBlueprint)
18    implements(ISection)
19
20    def __init__(self, transmogrifier, name, options, previous):
21        self.previous = previous
22        self.context = transmogrifier.context
23        self.options = options
24
25        self.anno = IAnnotations(transmogrifier)
26        self.storage = self.anno.setdefault(VALIDATIONKEY, [])
27
28        self.pathkey = options.get('path-key', '_path').strip()
29        self.fileskey = options.get('files-key', '_files').strip()
30        self.contextkey = options.get('context-key', '_import_context').strip()
31
32        if 'prefix' in options:
33            self.prefix = options['prefix'].strip()
34            self.prefix = self.prefix.strip('/')
35        else:
36            self.prefix = ''
37
38        context_type = options.get('context', 'tarball').strip()
39        if context_type not in ['directory', 'tarball', 'snapshot']:
40            context_type = 'tarball'
41
42        path = options.get('path', '').strip()
43
44        setup_tool = utils.getToolByName(self.context, 'portal_setup')
45        if context_type == 'directory':
46            self.import_context = context.DirectoryImportContext(setup_tool, path)
47        elif context_type == 'tarball':
48            if os.path.exists(path):
49                archive = file(path, 'rb')
50                archive_bits = archive.read()
51                archive.close()
52            else:
53                archive_bits = ''
54            self.import_context = context.TarballImportContext(setup_tool, archive_bits)
55        elif context_type == 'snapshot':
56            self.import_context = context.SnapshotImportContext(setup_tool, path)
57
58    def walk(self, top):
59        names = self.import_context.listDirectory(top)
60        if names is None:
61            names = []
62        yield self.readFiles(top, names)
63        for name in names:
64            name = os.path.join(top, name)
65            if self.import_context.isDirectory(name):
66                for i in self.walk(name):
67                    yield i
68
69    def readFiles(self, top, names):
70        path = top[len(self.prefix):]
71        path = path.lstrip('/')
72        item = {self.pathkey: path}
73        for name in names:
74            full_name = os.path.join(top, name)
75            if self.import_context.isDirectory(full_name): continue
76            section = self.options.get(name, name).strip()
77            files = item.setdefault(self.fileskey, {})
78            files[section] = {
79                'name': name,
80                'data': self.import_context.readDataFile(name, top)
81            }
82        return item
83
84    def __iter__(self):
85        for item in self.previous:
86            yield item
87
88        for item in self.walk(self.prefix):
89            # add import context to item (some next section may use it)
90            item[self.contextkey] = self.import_context
91            self.storage.append(item[self.pathkey])
92            yield item
93
94        if VALIDATIONKEY in self.anno:
95            del self.anno[VALIDATIONKEY]
Note: See TracBrowser for help on using the repository browser.