summaryrefslogtreecommitdiff
path: root/src/leap/mx/couchdb.py
blob: 9b69091186d50d2d4639ad13571be65a86ee81d2 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- encoding: utf-8 -*-
'''
couchdb.py
==========
Classes for working with CouchDB or BigCouch instances which store email alias
maps, user UUIDs, and GPG keyIDs.

@authors: Isis Agora Lovecruft
@version: 0.0.1-beta
@license: see included LICENSE file
'''

try:
    from paisley import client
except ImportError:
    print "This software requires paisley. Please see the README file"
    print "for instructions on getting required dependencies."

try:
    from twisted.internet import defer
except ImportError:
    print "This software requires Twisted. Please see the README file"
    print "for instructions on getting required dependencies."

from leap.mx.util import log


class ConnectedCouchDB(client.CouchDB):
    """Connect to a CouchDB instance.

    CouchDB document for testing is '_design', and the view is simply
    a preconfigured set of mapped responses.
    """
    def __init__(self, host, port=5984, dbName=None, username=None,
                 password=None, *args, **kwargs):
        """
        Connect to a CouchDB instance.

        :param str host: A hostname string for the CouchDB server.
        :param int port: The port of the CouchDB server.
        :param str dbName: (optional) The default database to bind queries to.
        :param str username: (optional) The username for authorization.
        :param str password: (optional) The password for authorization.
        :returns: A :class:`twisted.internet.defer.Deferred` representing the
                  the client connection to the CouchDB instance.
        """
        super(client.CouchDB, self).__init__(host,
                                             port=port,
                                             dbName=dbName,
                                             username=username,
                                             password=password,
                                             *args, **kwargs)
        if dbName is None:
            databases = self.listDB()
            log.msg("Available databases: %s" % databases)

    def createDB(self, dbName):
        """Overrides ``paisley.client.CouchDB.createDB``."""
        pass

    def deleteDB(self, dbName):
        """Overrides ``paisley.client.CouchDB.deleteDB``."""
        pass

    def queryByEmailOrAlias(self, alias, dbDoc="User",
                            view="by_email_or_alias"):
        """
        Check to see if a particular email or alias exists.

        @param alias: A string representing the email or alias to check.
        @param dbDoc: The CouchDB document to open.
        @param view: The view of the CouchDB document to use.
        """
        assert isinstance(alias, str), "Email or alias queries must be string"

        ## Prepend a forward slash, in case we forgot it:
        if not alias.startswith('/'):
            alias = '/' + alias

        d = self.openDoc(dbDoc)
        d.addCallbacks(self.openView, log.err, (view))
        d.addCallbacks(self.get, log.err, (alias))
        d.addCallbacks(self.parseResult, log.err)

        @d.addCallback
        def show_answer(result):
            log.msg("Query: %s" % alias)
            log.msg("Answer: %s" % alias)

        return d

    def query(self, uri):
        """
        Query a CouchDB instance that we are connected to.
        """
        try:
            self.checkURI(uri) ## xxx write checkURI()
            ## xxx we might be able to use self._parseURI()
        except SchemeNotSupported, sns: ## xxx where in paisley is this?
            log.exception(sns) ## xxx need log.exception()

        d = self.get(uri)
        @d.addCallback
        def parse_answer(answer):
            return answer

        return answer

    @defer.inlineCallbacks
    def listUsersAndEmails(self, limit=1000, reverse=False):
        """
        List all users and email addresses, up to the given limit.
        """
        query = "/users/_design/User/_view/by_email_or_alias/?reduce=false"
        answer = yield self.query(query, limit=limit, reverse=reverse)
        
        if answer:
            parsed = yield self.parseResult(answer)
            if parsed:
                log.msg("%s" % parsed)
            else:
                log.msg("No answer from database, perhaps there are no users.")
        else:
            log.msg("Problem querying CouchDB instance...")