Exemple #1
0
    def doLater(self, args):
        db = self.getRootCommand().db
        dbName = self.getRootCommand().getDatabase()

        totalRows = 0
        totalSize = 0

        res = yield _getViews(self)

        for design, views in res.items():
            self.stdout.write('%s:\n' % design)
            docId = design[len('_design/'):]

            for view in views:
                content = yield db.openView(dbName, docId, view, reduce=False)

                rows = int(content.get('total_rows', 0))
                size = len(json.dumps(content))

                self.stdout.write('  %50s: %d rows, %d chars\n' % (
                    view.encode('utf-8'), rows, size))
                totalRows += rows
                totalSize += size

        self.stdout.write('total: %d rows, %d chars\n' % (
            totalRows, totalSize))
Exemple #2
0
    def doLater(self, args):
        if not args:
            self.stderr.write('Please specify a design doc and view to dump.\n')
            defer.returnValue(3)
            return

        docId = args[0]
        viewId = args[1]

        self.debug('requesting view in %r %r with kwargs %r',
            docId, viewId, self._kwargs)

        res = yield self.getRootCommand().db.openView(
            self.getRootCommand().getDatabase(), docId, viewId,
            **self._kwargs)

        # adapted from couchdb.tools.dump
        from couchdb.multipart import write_multipart
        envelope = write_multipart(self.stdout, boundary=None)

        for row in res['rows']:
            doc = row['doc']

            print >> self.stderr, 'Dumping document %r' % doc['_id']
            attachments = doc.pop('_attachments', {})
            jsondoc = json.dumps(doc)

            if attachments:
                parts = envelope.open({
                    'Content-ID': doc['_id'],
                    'ETag': '"%s"' % doc['_rev']
                })
                parts.add('application/json', jsondoc)

                for name, info in attachments.items():
                    content_type = info.get('content_type')
                    if content_type is None: # CouchDB < 0.8
                        content_type = info.get('content-type')
                    parts.add(content_type, base64.b64decode(info['data']), {
                        'Content-ID': name
                    })
                parts.close()

            else:
                envelope.add('application/json', jsondoc, {
                    'Content-ID': doc['_id'],
                    'ETag': '"%s"' % doc['_rev']
                }, )

        envelope.close()