Ejemplo n.º 1
0
 def _calc_doc_id_for_schema_item(cls, si):
     key_type, key_val = si["rd_key"]
     enc_key_val = encode_provider_id(json.dumps(key_val))
     key_part = "%s.%s" % (key_type, enc_key_val)
     sch_id = si["rd_schema_id"]
     bits = ["rc", key_part, sch_id]
     return "!".join(bits)
Ejemplo n.º 2
0
    def view(self, viewname, **kw):
        # The javascript impl has a painful separation of the 'keys' param
        # from other params - hide that.
        kw = kw.copy()
        keys = kw.get('keys')
        if keys is not None:
            del kw['keys']
        # stale is a little tricky - 'ok' is the only valid option.  So if
        # the user puts 'stale=None' we assume stale is *not* ok!
        if 'stale' in kw:
            stale = kw['stale']
            assert stale in (None, 'ok'), stale # only ok and None are allowed!
            if stale is None:
                del kw['stale']
        else:
            kw['stale'] = 'ok'
        viewParts = viewname.split('/')
        viewPath = self.uri + "_design/" + viewParts[0] + "/_view/" \
            + viewParts[1] + self.encodeOptions(kw)
        if keys is None:
            resp = self.request("GET", viewPath);
        else:
            resp = self.request("POST", viewPath,
                                headers={"Content-Type": "application/json"},
                                body=json.dumps({'keys':keys}))

        if resp.status == 404:
          return None
        self.maybeThrowError(resp)
        return json.load(resp);
Ejemplo n.º 3
0
def show_info(parser, options):
    """Print a list of all extensions, loggers etc"""
    dm = model.get_doc_model()
    print "Database:"
    info = dm.db.infoDB()
    fmt = "  %(doc_count)d docs total, %(doc_del_count)d deleted, " \
          "update seq at %(update_seq)d, %(disk_size)d bytes."
    print fmt % info
    # ouch - this seems a painful way of fetching total unique keys?
    results = dm.open_view(
                startkey=["key"],
                endkey=["key", {}],
                group_level=2)
    print "  %d unique raindrop keys" % len(results['rows'])

    print "API groupings:"
    from urllib import urlencode
    dbconfig = get_config().couches['local']
    try:
        summaries = _call_api(dbconfig, "_api/inflow/grouping/summary")
        print " %d groupings exist" % len(summaries)
        for gs in summaries:
            title = gs.get('title') or gs['rd_key']
            opts = {'limit': 60, 'message_limit': 2,
                    'keys': json.dumps([gs['rd_key']]),
                    }
            path = "_api/inflow/conversations/in_groups?" + urlencode(opts)
            this = _call_api(dbconfig, path)
            print "  %s: %d conversations" % (title, len(this))
    except dm.db.Error, exc:
        print "Failed to call the API:", exc
Ejemplo n.º 4
0
 def allDocs(self, keys, **options):
     assert keys, "don't call me if you don't want any docs!"
     uri = self.uri + "_all_docs" + self.encodeOptions(options)
     resp = self.request("POST", uri,
                         headers={"Content-Type": "application/json"},
                         body=json.dumps({'keys':keys}))
     self.maybeThrowError(resp)
     return json.load(resp);
Ejemplo n.º 5
0
def _encode_options(options):
    retval = {}
    for name, value in options.items():
        if name in ('key', 'startkey', 'endkey', 'include_docs') \
                or not isinstance(value, basestring):
            value = json.dumps(value, allow_nan=False)
        retval[name] = value
    return retval
Ejemplo n.º 6
0
 def allDocs(self, keys, **options):
     assert keys, "don't call me if you don't want any docs!"
     uri = self.uri + "_all_docs" + self.encodeOptions(options)
     try:
         resp = self.request("POST", uri,
                             headers={"Content-Type": "application/json"},
                             body=json.dumps({'keys':keys}))
     except self.doc_model.db.Error, exc:
         self._xlate_error(exc)
Ejemplo n.º 7
0
 def encodeOptions(self, options):
     use = {}
     for name, value in options.iteritems():
         if name in ('key', 'startkey', 'endkey', 'include_docs') \
                 or not isinstance(value, basestring):
             value = json.dumps(value, allow_nan=False)
         use[name] = value
     if not use:
         return ''
     return "?" + urlencode(use)
Ejemplo n.º 8
0
 def do_timings(api, desc=None, **kw):
     api_path = tpath % (couch['name'], api)
     if kw:
         opts = kw.copy()
         for opt_name in opts:
             opts[opt_name] = json.dumps(opts[opt_name])
         api_path += "?" + urlencode(opts)
     resp, reqt = timeit(make_req, api_path)
     dat, respt = timeit(resp.read)
     if not desc:
         desc = api
     if resp.status != 200:
         print "*** api %r failed with %s: %s" % (desc, resp.status, resp.reason)
     print "Made '%s' API request in %.3f, read response in %.3f (size was %s)" \
           % (desc, reqt, respt, format_num_bytes(len(dat)))
     return json.loads(dat)
Ejemplo n.º 9
0
 def updateDocuments(self, user_docs):
     # update/insert/delete multiple docs in a single request using
     # _bulk_docs
     # from couchdb-python.
     docs = []
     for doc in user_docs:
         if isinstance(doc, dict):
             docs.append(doc)
         elif hasattr(doc, 'items'):
             docs.append(dict(doc.items()))
         else:
             raise TypeError('expected dict, got %s' % type(doc))
     uri = "/%s/_bulk_docs" % self.dbName
     body = json.dumps({'docs': docs})
     headers = {"Content-Type": "application/json"}
     return self._request('POST', uri, body, headers)
Ejemplo n.º 10
0
def get_api_handler(options, req):
    # path format is "db_name/external_name/app_name/class_name/method_name
    if len(req.get('path', [])) != 5:
        raise APILoadError("invalid api request format")
    dbname = req['path'][0]
    cache_key = tuple(req['path'][:4])
    try:
        return _handlers[cache_key]
    except KeyError:
        # first request for this handler
        pass

    # Load the schemas which declare they implement this end-point
    apiid = req['path'][2:4] # the 'app' name and the 'class' name.
    path = "/%s/_design/raindrop!content!all/_view/api_endpoints" % dbname
    req_options = {'key': json.dumps(apiid), 'include_docs': 'true'}
    uri = path + "?" + urlencode(req_options)

    c = httplib.HTTPConnection(options.couchdb_host, options.couchdb_port)
    c.request("GET", uri)
    resp = c.getresponse()
    if resp.status != 200:
        raise APILoadError("api query failure (%s: %s) to %s:%s", resp.status,
                           resp.reason, options.couchdb_host, options.couchdb_port)
    result = json.load(resp)
    resp.close()
    rows = result['rows']
    if not rows:
        raise APILoadError("No such API end-point %s", apiid)
    if len(rows) != 1: # should only be one doc with this criteria!
        raise APILoadError("too many docs say they implement this api!")
    doc = rows[0]['doc']
    if doc.get('content_type') != 'application/x-python' or not doc.get('code'):
        raise APILoadError("document is not a python implemented API (%s)", doc['content_type'])

    # Now dynamically compile the code we loaded.
    globs = api_globals.copy()
    try:
        exec doc['code'] in globs
    except Exception, exc:
        raise APILoadError("Failed to initialize api: %s", exc)
Ejemplo n.º 11
0
    def listDoc(self, **kw):
        """
        List all documents in a given database.
        """

        # Responses: {u'rows': [{u'_rev': -1825937535, u'_id': u'mydoc'}],
        # u'view': u'_all_docs'}, 404 Object Not Found
        uri = "/%s/_all_docs" % (self.dbName,)
        opts = kw.copy()
        if 'keys' in opts:
            method = 'POST'
            body_ob = {'keys': opts.pop('keys')}
            body = json.dumps(body_ob, allow_nan=False)
        else:
            method = 'GET'
            body = None
        args = _encode_options(opts)
        if args:
            uri += "?%s" % (urlencode(args),)

        headers = {"Accept": "application/json"}
        return self._request(method, uri, body, headers)
Ejemplo n.º 12
0
    def openView(self, docId, viewId, **kwargs):
        try:
            headers = {"Accept": "application/json"}
            uri = "/%s/_design/%s/_view/%s" % (self.dbName, docId, viewId)

            opts = kwargs.copy()
            if 'keys' in opts:
                method = 'POST'
                body_ob = {'keys': opts.pop('keys')}
                body = json.dumps(body_ob, allow_nan=False)
                assert isinstance(body, str), body
            else:
                method = 'GET'
                body = None
            args = _encode_options(opts)
            if args:
                uri += "?%s" % (urlencode(args),)

            return self._request(method, uri, body, headers)
        except:
            raise
            return {}
Ejemplo n.º 13
0
def log(msg, *args):
    print json.dumps(["log", (msg % args)])
Ejemplo n.º 14
0
 def postob(self, uri, ob):
     body = json.dumps(ob, allow_nan=False)
     assert isinstance(body, str), body # must be ready to send on the wire
     return self.post(uri, body)