Beispiel #1
0
    def items_from_json(filename, data, fingerprinter):
        "Builds raindrop 'schema items' from a json file"

        try:
            src = json.loads(data)
        except ValueError, exc:
            logger.error("Failed to load %r: %s", filename, exc)
            return []
Beispiel #2
0
 def maybeThrowError(self, resp):
     if resp.status >= 400:
         text = resp.read()
         try:
             result = json.loads(text)
         except ValueError:
             raise APIErrorResponse(400, text)
         raise APIErrorResponse(resp.status, result)
Beispiel #3
0
 def get_args(self, req, *req_args, **opt_args):
     _json = True
     if '_json' in opt_args:
         _json = opt_args.pop('_json')
     supplied = {}
     for name, val in req['query'].iteritems():
         if _json:
             try:
                 val = json.loads(val)
             except ValueError, why:
                 raise APIErrorResponse(400, "invalid json in param '%s': %s" % (name, why))
         supplied[name] = val
Beispiel #4
0
 def split_doc_id(cls, doc_id, decode_key=True):
     if not doc_id.startswith("rc!"):
         raise ValueError("Not a raindrop content docid")
     rt, enc_rdkey, schema = doc_id.split("!", 3)
     if decode_key:
         # decode the key portion.
         prefix, b64part = enc_rdkey.split(".", 1)
         json_str = base64.decodestring(b64part)
         rdkey = [prefix, json.loads(json_str)]
     else:
         rdkey = enc_rdkey
     return rt, rdkey, schema
 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)
Beispiel #6
0
def main():
    # build the args we support.
    start = datetime.datetime.now()
    all_args = {}
    for n, v in globals().iteritems():
        # explicit check for functions so other globals don't match..
        if type(v)==type(main) and getattr(v, '__doc__', None):
            all_args[n.replace('_', '-')] = v

    all_arg_names = sorted(all_args.keys())
    description= __doc__ + "\nCommands\n  help\n  " + \
                 "\n  ".join(all_args.keys()) + \
                 "\nUse '%prog help command-name' for help on a specific command.\n"

    parser = optparse.OptionParser("%prog [options]",
                                   description=description,
                                   formatter=HelpFormatter())

    for opt in opts.get_program_options():
        parser.add_option(opt)
    for opt in opts.get_request_options():
        parser.add_option(opt)
    options, args = parser.parse_args()

    opts.setup_logging(options)

    init_config(options.config)
    proto.init_protocols()

    # patch up keys.
    if options.keys:
        def _fix_unicode(result):
            if isinstance(result, list):
                for i, v in enumerate(result):
                    result[i] = _fix_unicode(result[i])
            elif isinstance(result, unicode):
                result = result.encode('utf-8')
            return result
        for i, val in enumerate(options.keys):
            try:
                # help windows - replace single quote with double...
                val = val.replace("'", '"')
                options.keys[i] = _fix_unicode(json.loads(val))
            except ValueError, why:
                parser.error("Invalid key value %r: %s" % (val, why))
Beispiel #7
0
def main():
    # for now, so we know the couch defaults
    cfg = config.init_config().couches['local']
    parser = optparse.OptionParser("%prog [options]",
                                   description="the raindrop api runner")
    parser.add_option("", "--couchdb-host",
                help="Specifies the hostname to use for couchdb requests",
                default=cfg['host'])

    parser.add_option("", "--couchdb-port",
                help="Specifies the port number to use for couchdb requests",
                default=cfg['port'])

    parser.add_option("", "--show-perf", action="store_true",
                help="Reports some information about how long some things take")

    options, args = parser.parse_args()

    # no args used
    if len(args) != 0:
        print >> sys.stderr, __doc__
        print >> sys.stderr, "this program takes no arguments"
        sys.exit(1)

    if options.show_perf:
        global note_timeline
        note_timeline = do_note_timeline
    CouchDB.default_host = options.couchdb_host
    CouchDB.default_port = options.couchdb_port

    log("raindrops keep falling on my api...")
    # and now the main loop.
    while True:
        line = sys.stdin.readline()
        if not line:
            break
        req = json.loads(line)
        resp = process(req, options)

        json.dump(resp, sys.stdout)
        sys.stdout.write("\n")
        sys.stdout.flush()
    log("raindrop api runner terminating normally")
Beispiel #8
0
 def _request(self, method, uri, body = None, headers = None):
     return json.loads(self._rawrequest(method, uri, body, headers))
 def _xlate_error(self, exc):
     try:
         val = json.loads(exc.body)
     except ValueError:
         val = exc.body
     raise APIErrorResponse(exc.status, val)
Beispiel #10
0
def _build_views_doc_from_directory(ddir):
    # all we look for is the views.  And the lists.  And the shows :)
    ret = {}
    fprinter = Fingerprinter()
    ret_views = ret['views'] = {}
    mtail = "-map"
    rtail = "-reduce"
    ltail = "-list"
    stail = "-show"
    rwtail = "-rewrites"
    optstail = "-options"
    files = os.listdir(ddir)
    for fn in files:
        if not fn.endswith(".js"):
            continue
        fqf = os.path.join(ddir, fn)

        tail = mtail + ".js"
        if fn.endswith(tail):
            view_name = fn[:-len(tail)]
            info = ret_views.setdefault(view_name, {})
            try:
                with open(fqf) as f:
                    data = f.read()
                    info['map'] = data
                    fprinter.get_finger(view_name+tail).update(data)
            except (OSError, IOError):
                logger.warning("can't open map file %r - skipping this view", fqf)
                continue

        tail = rtail + ".js"
        if fn.endswith(tail):
            view_name = fn[:-len(tail)]
            info = ret_views.setdefault(view_name, {})
            try:
                with open(fqf) as f:
                    # support the 'builtin' reduce functionality - if line 1
                    # starts with an '_', only that line is used.
                    first = f.readline()
                    if first.startswith('_'):
                        data = first.strip()
                    else:
                        data = first + f.read()
                    info['reduce'] = data
                    fprinter.get_finger(view_name+tail).update(data)
            except (OSError, IOError):
                # no reduce - no problem...
                logger.debug("no reduce file %r - skipping reduce for view '%s'",
                             fqr, view_name)

        tail = ltail + ".js"
        if fn.endswith(tail):
            list_name = fn[:-len(tail)]
            info = ret.setdefault('lists', {})
            try:
                with open(fqf) as f:
                    data = f.read()
                    info[list_name] = data
                    fprinter.get_finger(list_name+tail).update(data)
            except (OSError, IOError):
                logger.warning("can't open list file %r - skipping this list", fqf)
                continue

        tail = rwtail + ".js"
        if fn.endswith(tail):
            rewrite_name = fn[:-len(tail)]
            try:
                with open(fqf) as f:
                    data = f.readline()
                    ret_rewrites = ret.setdefault('rewrites', [])
                    ret_rewrites.extend(eval(data))
                    fprinter.get_finger(rewrite_name+tail).update(data)
            except (OSError, IOError):
                logger.warning("can't open list file %r - skipping this rewrite", fqf)
                continue

        tail = optstail + ".json"
        if fn.endswith(tail):
            view_name = fn[:-len(tail)]
            try:
                with open(fqf) as f:
                    data = f.read()
                    info = ret_views.setdefault(view_name, {})
                    info['options'] = json.loads(data)
                    fprinter.get_finger(view_name+tail).update(data)
            except ValueError, why:
                logger.warning("can't json-decode %r: %s", fqf, why)
                continue