File: load.py

package info (click to toggle)
python-couchdb 0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 232 kB
  • ctags: 246
  • sloc: python: 1,229; makefile: 26
file content (70 lines) | stat: -rw-r--r-- 2,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007 Christopher Lenz
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.

from base64 import b64encode
from email import message_from_file
from optparse import OptionParser
import simplejson as json
import sys

from couchdb import __version__ as VERSION
from couchdb.client import Database

def load_db(fileobj, dburl, username=None, password=None):
    envelope = message_from_file(fileobj)
    db = Database(dburl)
    if username is not None and password is not None:
        db.resource.http.add_credentials(username, password)

    for part in envelope.get_payload():
        docid = part['Content-ID']
        if part.is_multipart(): # doc has attachments
            for subpart in part.walk():
                if subpart is part:
                    continue
                if 'Content-ID' not in subpart:
                    doc = json.loads(subpart.get_payload())
                    doc['_attachments'] = {}
                else:
                    data = subpart.get_payload()
                    doc['_attachments'][subpart['Content-ID']] = {
                        'data': b64encode(data),
                        'content-type': subpart['Content-Type'],
                        'length': len(data)
                    }
        else:
            doc = json.loads(part.get_payload())
        del doc['_rev']
        print>>sys.stderr, 'Loading document %r' % docid
        db[docid] = doc

def main():
    parser = OptionParser(usage='%prog [options] dburl', version=VERSION)
    parser.add_option('--input', action='store', dest='input', metavar='FILE',
                      help='the name of the file to read from')
    parser.add_option('-u', '--username', action='store', dest='username',
                      help='the username to use for authentication')
    parser.add_option('-p', '--password', action='store', dest='password',
                      help='the password to use for authentication')
    parser.set_defaults(input='-')
    options, args = parser.parse_args()

    if len(args) != 1:
        return parser.error('incorrect number of arguments')

    if options.input != '-':
        fileobj = open(options.input)
    else:
        fileobj = sys.stdin

    load_db(fileobj, args[0], username=options.username,
            password=options.password)

if __name__ == '__main__':
    main()