Example #1
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()
Example #2
0
    def test_unicode_content(self):
        buf = StringIO()
        envelope = multipart.write_multipart(buf, boundary='==123456789==')
        envelope.add('text/plain', u'Iñtërnâtiônàlizætiøn')
        envelope.close()
        self.assertEqual(u'''Content-Type: multipart/mixed; boundary="==123456789=="

--==123456789==
Content-Length: 27
Content-MD5: 5eYoIG5zsa5ps3/Gl2Kh4Q==
Content-Type: text/plain;charset=utf-8

Iñtërnâtiônàlizætiøn
--==123456789==--
'''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
Example #3
0
def dump_db(dburl, username=None, password=None, boundary=None,
            output=sys.stdout, bulk_size=BULK_SIZE):

    db = Database(dburl)
    if username is not None and password is not None:
        db.resource.credentials = username, password

    envelope = write_multipart(output, boundary=boundary)
    start, num = 0, db.info()['doc_count']
    while start < num:
        opts = {'limit': bulk_size, 'skip': start}
        dump_docs(envelope, [row.id for row in db.view('_all_docs', **opts)], db)
        start += bulk_size

    envelope.close()
Example #4
0
def dump_db(dburl, username=None, password=None, boundary=None,
            output=sys.stdout, bulk_size=BULK_SIZE):

    db = Database(dburl)
    if username is not None and password is not None:
        db.resource.credentials = username, password

    envelope = write_multipart(output, boundary=boundary)
    start, num = 0, db.info()['doc_count']
    while start < num:
        opts = {'limit': bulk_size, 'skip': start, 'include_docs': True}
        dump_docs(envelope, [row.doc for row in db.view('_all_docs', **opts)])
        start += bulk_size

    envelope.close()
Example #5
0
    def test_unicode_content(self):
        buf = StringIO()
        envelope = multipart.write_multipart(buf, boundary='==123456789==')
        envelope.add('text/plain', u'Iñtërnâtiônàlizætiøn')
        envelope.close()
        self.assertEqual(
            u'''Content-Type: multipart/mixed; boundary="==123456789=="

--==123456789==
Content-Length: 27
Content-MD5: 5eYoIG5zsa5ps3/Gl2Kh4Q==
Content-Type: text/plain;charset=utf-8

Iñtërnâtiônàlizætiøn
--==123456789==--
'''.encode('utf-8'),
            buf.getvalue().replace(b'\r\n', b'\n'))
Example #6
0
    def test_unicode_headers_charset(self):
        # http://code.google.com/p/couchdb-python/issues/detail?id=179
        buf = StringIO()
        envelope = multipart.write_multipart(buf, boundary='==123456789==')
        envelope.add('application/json;charset=utf-8',
                     '{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09",'
                     ' "_id": "文档"}',
                     headers={'Content-ID': u"文档"})
        self.assertEqual(u'''Content-Type: multipart/mixed; boundary="==123456789=="

--==123456789==
Content-ID: =?utf-8?b?5paH5qGj?=
Content-Length: 63
Content-MD5: Cpw3iC3xPua8YzKeWLzwvw==
Content-Type: application/json;charset=utf-8

{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09", "_id": "文档"}
'''.encode('utf-8'), buf.getvalue().replace(b'\r\n', b'\n'))
Example #7
0
def dump_db(dburl, username=None, password=None, boundary=None,
            output=None, bulk_size=BULK_SIZE):

    if output is None:
        output = sys.stdout if sys.version_info[0] < 3 else sys.stdout.buffer

    db = Database(dburl)
    if username is not None and password is not None:
        db.resource.credentials = username, password

    envelope = write_multipart(output, boundary=boundary)
    start, num = 0, db.info()['doc_count']
    while start < num:
        opts = {'limit': bulk_size, 'skip': start, 'include_docs': True}
        docs = (row.doc for row in db.view('_all_docs', **opts))
        dump_docs(envelope, db, docs)
        start += bulk_size

    envelope.close()
Example #8
0
    def test_unicode_content(self):
        buf = StringIO()
        envelope = multipart.write_multipart(buf, boundary="==123456789==")
        envelope.add("text/plain", u"Iñtërnâtiônàlizætiøn")
        envelope.close()
        self.assertEqual(
            u"""Content-Type: multipart/mixed; boundary="==123456789=="

--==123456789==
Content-Length: 27
Content-MD5: 5eYoIG5zsa5ps3/Gl2Kh4Q==
Content-Type: text/plain;charset=utf-8

Iñtërnâtiônàlizætiøn
--==123456789==--
""".encode(
                "utf-8"
            ),
            buf.getvalue().replace(b"\r\n", b"\n"),
        )
Example #9
0
    def test_unicode_headers(self):
        # http://code.google.com/p/couchdb-python/issues/detail?id=179
        buf = StringIO()
        envelope = multipart.write_multipart(buf, boundary='==123456789==')
        envelope.add('application/json',
                     '{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09",'
                     ' "_id": "文档"}',
                     headers={'Content-ID': u"文档"})
        self.assertEqual(
            u'''Content-Type: multipart/mixed; boundary="==123456789=="

--==123456789==
Content-ID: =?utf-8?b?5paH5qGj?=
Content-Length: 63
Content-MD5: Cpw3iC3xPua8YzKeWLzwvw==
Content-Type: application/json;charset=utf-8

{"_rev": "3-bc27b6930ca514527d8954c7c43e6a09", "_id": "文档"}
'''.encode('utf-8'),
            buf.getvalue().replace(b'\r\n', b'\n'))
Example #10
0
    def dump(self, docs, db):

        output = sys.stdout
        boundary = None
        envelope = write_multipart(output, boundary=boundary)
        total = len(docs)

        for n, docid in enumerate(docs):

            if not docid:
                continue

            doc = db.get(docid, attachments=True)
            attachments = doc.pop('_attachments', {})
            jsondoc = json.encode(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, b64decode(info['data']), {
                        'Content-ID': name
                    })
                parts.close()

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

            progress(n+1, total, docid, stream=sys.stderr)

        envelope.close()
Example #11
0
def dump_db(dburl, username=None, password=None, boundary=None,
            output=sys.stdout):
    db = Database(dburl)
    if username is not None and password is not None:
        db.resource.credentials = username, password

    envelope = write_multipart(output, boundary=boundary)
    for docid in db:

        doc = db.get(docid, attachments=True)
        print('Dumping document %r' % doc.id, file=sys.stderr)
        attachments = doc.pop('_attachments', {})
        jsondoc = json.encode(doc)

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

            for name, info in list(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, b64decode(info['data']), {
                    'Content-ID': name
                })
            parts.close()

        else:
            envelope.add('application/json', jsondoc, {
                'Content-ID': doc.id,
                'ETag': '"%s"' % doc.rev
            })

    envelope.close()
Example #12
0
def dump_db(dburl, username=None, password=None, boundary=None,
            output=sys.stdout):
    db = Database(dburl)
    if username is not None and password is not None:
        db.resource.credentials = username, password

    envelope = write_multipart(output, boundary=boundary)
    for docid in db:

        doc = db.get(docid, attachments=True)
        print >> sys.stderr, 'Dumping document %r' % doc.id
        attachments = doc.pop('_attachments', {})
        jsondoc = json.encode(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, b64decode(info['data']), {
                    'Content-ID': name
                })
            parts.close()

        else:
            envelope.add('application/json', jsondoc, {
                'Content-ID': doc.id,
                'ETag': '"%s"' % doc.rev
            })

    envelope.close()
Example #13
0
 def test_unicode_content_ascii(self):
     buf = StringIO()
     envelope = multipart.write_multipart(buf, boundary='==123456789==')
     self.assertRaises(UnicodeEncodeError, envelope.add,
                       'text/plain;charset=ascii', u'Iñtërnâtiônàlizætiøn')
Example #14
0
 def test_unicode_content_ascii(self):
     buf = StringIO()
     envelope = multipart.write_multipart(buf, boundary='==123456789==')
     self.assertRaises(UnicodeEncodeError, envelope.add,
                       'text/plain;charset=ascii', u'Iñtërnâtiônàlizætiøn')