Example #1
0
                def register_callback(fname, args, f, obj, lock):
                    def f_wrapper():
                        t0 = time.time()
                        args_time = aqu_lock_time = calling_time = json_time = 0
                        need_lock = getattr(f, 'need_lock', True)

                        # Warning : put the bottle.response set inside the wrapper
                        # because outside it will break bottle
                        d = {}
                        method = getattr(f, 'method', 'get').lower()
                        for aname in args:
                            v = None
                            if method == 'post':
                                v = bottle.request.forms.get(aname, None)
                                # Post args are zlibed and cPickled
                                if v is not None:
                                    v = zlib.decompress(v)
                                    v = cPickle.loads(v)
                            elif method == 'get':
                                v = bottle.request.GET.get(aname, None)
                            if v is None:
                                # Maybe we got a default value?
                                default_args = self.registered_fun_defaults.get(fname, {})
                                if not aname in default_args:
                                    raise Exception('Missing argument %s' % aname)
                                v = default_args[aname]
                            d[aname] = v
                        args_time = time.time() - t0

                        if need_lock:
                            logger.debug("HTTP: calling lock for %s" % fname)
                            lock.acquire()
                        aqu_lock_time = time.time() - t0
                        
                        try:
                            ret = f(**d)
                        # Always call the lock release if need
                        finally:
                            # Ok now we can release the lock
                            if need_lock:
                                lock.release()
                        calling_time = time.time() - t0
                        real_calling_time = calling_time - aqu_lock_time
                        encode = getattr(f, 'encode', 'json').lower()
                        j = json.dumps(ret)
                        json_time = time.time() - t0
                        logger.debug("Debug perf: %s [args:%s] [aqu_lock:%s] [real_calling:%s ] [calling:%s] [json:%s]" % (
                                fname, args_time, aqu_lock_time, real_calling_time, calling_time, json_time) )
                        
                        return j
                    # Ok now really put the route in place
                    bottle.route('/'+fname, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
                    # and the name with - instead of _ if need
                    fname_dash = fname.replace('_', '-')
                    if fname_dash != fname:
                        bottle.route('/'+fname_dash, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
Example #2
0
            def register_callback(fname, args, f, obj, lock):
                def f_wrapper():
                    need_lock = getattr(f, 'need_lock', True)

                    # Warning : put the bottle.response set inside the wrapper
                    # because outside it will break bottle
                    d = {}
                    method = getattr(f, 'method', 'get').lower()
                    for aname in args:
                        v = None
                        if method == 'post':
                            v = bottle.request.forms.get(aname, None)
                            # Post args are zlibed and cPickled
                            if v is not None:
                                v = zlib.decompress(v)
                                v = cPickle.loads(v)
                        elif method == 'get':
                            v = bottle.request.GET.get(aname, None)
                        if v is None:
                            # Maybe we got a default value?
                            default_args = self.registered_fun_defaults.get(
                                fname, {})
                            if not aname in default_args:
                                raise Exception('Missing argument %s' % aname)
                            v = default_args[aname]
                        d[aname] = v
                    if need_lock:
                        logger.debug("HTTP: calling lock for %s" % fname)
                        lock.acquire()

                    ret = f(**d)

                    # Ok now we can release the lock
                    if need_lock:
                        lock.release()

                    encode = getattr(f, 'encode', 'json').lower()
                    j = json.dumps(ret)
                    return j

                # Ok now really put the route in place
                bottle.route('/' + fname,
                             callback=f_wrapper,
                             method=getattr(f, 'method', 'get').upper())
Example #3
0
                def register_callback(fname, args, f, obj, lock):
                    def f_wrapper():
                        need_lock = getattr(f, 'need_lock', True)

                        # Warning : put the bottle.response set inside the wrapper
                        # because outside it will break bottle
                        d = {}
                        method = getattr(f, 'method', 'get').lower()
                        for aname in args:
                            v = None
                            if method == 'post':
                                v = bottle.request.forms.get(aname, None)
                                # Post args are zlibed and cPickled
                                if v is not None:
                                    v = zlib.decompress(v)
                                    v = cPickle.loads(v)
                            elif method == 'get':
                                v = bottle.request.GET.get(aname, None)
                            if v is None:
                                # Maybe we got a default value?
                                default_args = self.registered_fun_defaults.get(fname, {})
                                if not aname in default_args:
                                    raise Exception('Missing argument %s' % aname)
                                v = default_args[aname]
                            d[aname] = v
                        if need_lock:
                            logger.debug("HTTP: calling lock for %s" % fname)
                            lock.acquire()

                        try:
                            ret = f(**d)
                        # Always call the lock release if need
                        finally:
                            # Ok now we can release the lock
                            if need_lock:
                                lock.release()

                        encode = getattr(f, 'encode', 'json').lower()
                        j = json.dumps(ret)
                        return j
                    # Ok now really put the route in place
                    bottle.route('/'+fname, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
Example #4
0
            def register_callback(fname, args, f, obj, lock):
                def f_wrapper():
                    t0 = time.time()
                    args_time = aqu_lock_time = calling_time = json_time = 0
                    need_lock = getattr(f, 'need_lock', True)

                    # Warning : put the bottle.response set inside the wrapper
                    # because outside it will break bottle
                    d = {}
                    method = getattr(f, 'method', 'get').lower()
                    for aname in args:
                        v = None
                        if method == 'post':
                            v = bottle.request.forms.get(aname, None)
                            # Post args are zlibed and cPickled
                            if v is not None:
                                v = zlib.decompress(v)
                                v = cPickle.loads(v)
                        elif method == 'get':
                            v = bottle.request.GET.get(aname, None)
                        if v is None:
                            # Maybe we got a default value?
                            default_args = self.registered_fun_defaults.get(
                                fname, {})
                            if not aname in default_args:
                                raise Exception('Missing argument %s' % aname)
                            v = default_args[aname]
                        d[aname] = v

                    t1 = time.time()
                    args_time = t1 - t0

                    if need_lock:
                        logger.debug("HTTP: calling lock for %s", fname)
                        lock.acquire()

                    t2 = time.time()
                    aqu_lock_time = t2 - t1

                    try:
                        ret = f(**d)
                    # Always call the lock release if need
                    finally:
                        # Ok now we can release the lock
                        if need_lock:
                            lock.release()

                    t3 = time.time()
                    calling_time = t3 - t2

                    encode = getattr(f, 'encode', 'json').lower()
                    j = json.dumps(ret)
                    t4 = time.time()
                    json_time = t4 - t3

                    global_time = t4 - t0
                    logger.debug(
                        "Debug perf: %s [args:%s] [aqu_lock:%s] [calling:%s] [json:%s] [global:%s]",
                        fname, args_time, aqu_lock_time, calling_time,
                        json_time, global_time)
                    lst = [('args', args_time), ('aqulock', aqu_lock_time),
                           ('calling', calling_time), ('json', json_time),
                           ('global', global_time)]
                    # increase the stats timers
                    for (k, _t) in lst:
                        statsmgr.incr('http.%s.%s' % (fname, k), _t)

                    return j

                # Ok now really put the route in place
                bottle.route('/' + fname,
                             callback=f_wrapper,
                             method=getattr(f, 'method', 'get').upper())
                # and the name with - instead of _ if need
                fname_dash = fname.replace('_', '-')
                if fname_dash != fname:
                    bottle.route('/' + fname_dash,
                                 callback=f_wrapper,
                                 method=getattr(f, 'method', 'get').upper())
Example #5
0
    def register(self, obj):
        methods = inspect.getmembers(obj, predicate=inspect.ismethod)
        merge = [
            fname for (fname, f) in methods
            if fname in self.registered_fun_names
        ]
        if merge != []:
            methods_in = [
                m.__name__ for m in obj.__class__.__dict__.values()
                if inspect.isfunction(m)
            ]
            methods = [m for m in methods if m[0] in methods_in]
            print "picking only bound methods of class and not parents"
        print "List to register :%s" % methods
        for (fname, f) in methods:
            if fname.startswith('_'):
                continue
            # Get the args of the function to catch them in the queries
            argspec = inspect.getargspec(f)
            args = argspec.args
            varargs = argspec.varargs
            keywords = argspec.keywords
            defaults = argspec.defaults
            # If we got some defauts, save arg=value so we can lookup
            # for them after
            if defaults:
                default_args = zip(argspec.args[-len(argspec.defaults):],
                                   argspec.defaults)
                _d = {}
                for (argname, defavalue) in default_args:
                    _d[argname] = defavalue
                self.registered_fun_defaults[fname] = _d
            # remove useless self in args, because we alredy got a bonded method f
            if 'self' in args:
                args.remove('self')
            print "Registering", fname, args, obj
            self.registered_fun_names.append(fname)
            self.registered_fun[fname] = (f)

            # WARNING : we MUST do a 2 levels function here, or the f_wrapper
            # will be uniq and so will link to the last function again
            # and again
            def register_callback(fname, args, f, obj, lock):
                def f_wrapper():
                    t0 = time.time()
                    args_time = aqu_lock_time = calling_time = json_time = 0
                    need_lock = getattr(f, 'need_lock', True)

                    # Warning : put the bottle.response set inside the wrapper
                    # because outside it will break bottle
                    d = {}
                    method = getattr(f, 'method', 'get').lower()
                    for aname in args:
                        v = None
                        if method == 'post':
                            v = bottle.request.forms.get(aname, None)
                            # Post args are zlibed and cPickled
                            if v is not None:
                                v = zlib.decompress(v)
                                v = cPickle.loads(v)
                        elif method == 'get':
                            v = bottle.request.GET.get(aname, None)
                        if v is None:
                            # Maybe we got a default value?
                            default_args = self.registered_fun_defaults.get(
                                fname, {})
                            if not aname in default_args:
                                raise Exception('Missing argument %s' % aname)
                            v = default_args[aname]
                        d[aname] = v

                    t1 = time.time()
                    args_time = t1 - t0

                    if need_lock:
                        logger.debug("HTTP: calling lock for %s", fname)
                        lock.acquire()

                    t2 = time.time()
                    aqu_lock_time = t2 - t1

                    try:
                        ret = f(**d)
                    # Always call the lock release if need
                    finally:
                        # Ok now we can release the lock
                        if need_lock:
                            lock.release()

                    t3 = time.time()
                    calling_time = t3 - t2

                    encode = getattr(f, 'encode', 'json').lower()
                    j = json.dumps(ret)
                    t4 = time.time()
                    json_time = t4 - t3

                    global_time = t4 - t0
                    logger.debug(
                        "Debug perf: %s [args:%s] [aqu_lock:%s] [calling:%s] [json:%s] [global:%s]",
                        fname, args_time, aqu_lock_time, calling_time,
                        json_time, global_time)
                    lst = [('args', args_time), ('aqulock', aqu_lock_time),
                           ('calling', calling_time), ('json', json_time),
                           ('global', global_time)]
                    # increase the stats timers
                    for (k, _t) in lst:
                        statsmgr.incr('http.%s.%s' % (fname, k), _t)

                    return j

                # Ok now really put the route in place
                bottle.route('/' + fname,
                             callback=f_wrapper,
                             method=getattr(f, 'method', 'get').upper())
                # and the name with - instead of _ if need
                fname_dash = fname.replace('_', '-')
                if fname_dash != fname:
                    bottle.route('/' + fname_dash,
                                 callback=f_wrapper,
                                 method=getattr(f, 'method', 'get').upper())

            register_callback(fname, args, f, obj, self.lock)

        # Add a simple / page
        def slash():
            return "OK"

        bottle.route('/', callback=slash)
Example #6
0
        def register(self, obj):
            methods = inspect.getmembers(obj, predicate=inspect.ismethod)
            merge = [fname for (fname, f) in methods if fname in self.registered_fun_names ]
            if merge != []:
                methods_in = [m.__name__ for m in obj.__class__.__dict__.values() if inspect.isfunction(m)]
                methods = [m for m in methods if m[0] in methods_in]
                print "picking only bound methods of class and not parents"
            print "List to register :%s" % methods
            for (fname, f) in methods:
                if fname.startswith('_'):
                    continue
                # Get the args of the function to catch them in the queries
                argspec = inspect.getargspec(f)
                args = argspec.args
                varargs = argspec.varargs
                keywords = argspec.keywords
                defaults = argspec.defaults
                # If we got some defauts, save arg=value so we can lookup
                # for them after
                if defaults:
                    default_args = zip(argspec.args[-len(argspec.defaults):],argspec.defaults)
                    _d = {}
                    for (argname, defavalue) in default_args:
                        _d[argname] = defavalue
                    self.registered_fun_defaults[fname] = _d
                # remove useless self in args, because we alredy got a bonded method f
                if 'self' in args:
                    args.remove('self')
                print "Registering", fname, args, obj
                self.registered_fun_names.append(fname)
                self.registered_fun[fname] = (f)
                # WARNING : we MUST do a 2 levels function here, or the f_wrapper
                # will be uniq and so will link to the last function again
                # and again
                def register_callback(fname, args, f, obj, lock):
                    def f_wrapper():
                        t0 = time.time()
                        args_time = aqu_lock_time = calling_time = json_time = 0
                        need_lock = getattr(f, 'need_lock', True)

                        # Warning : put the bottle.response set inside the wrapper
                        # because outside it will break bottle
                        d = {}
                        method = getattr(f, 'method', 'get').lower()
                        for aname in args:
                            v = None
                            if method == 'post':
                                v = bottle.request.forms.get(aname, None)
                                # Post args are zlibed and cPickled
                                if v is not None:
                                    v = zlib.decompress(v)
                                    v = cPickle.loads(v)
                            elif method == 'get':
                                v = bottle.request.GET.get(aname, None)
                            if v is None:
                                # Maybe we got a default value?
                                default_args = self.registered_fun_defaults.get(fname, {})
                                if not aname in default_args:
                                    raise Exception('Missing argument %s' % aname)
                                v = default_args[aname]
                            d[aname] = v

                        t1 = time.time()
                        args_time = t1 - t0

                        if need_lock:
                            logger.debug("HTTP: calling lock for %s", fname)
                            lock.acquire()

                        t2 = time.time()
                        aqu_lock_time = t2 - t1
                        
                        try:
                            ret = f(**d)
                        # Always call the lock release if need
                        finally:
                            # Ok now we can release the lock
                            if need_lock:
                                lock.release()

                        t3 = time.time()
                        calling_time = t3 - t2

                        encode = getattr(f, 'encode', 'json').lower()
                        j = json.dumps(ret)
                        t4 = time.time() 
                        json_time = t4 - t3
                      
                        global_time = t4 - t0 
                        logger.debug("Debug perf: %s [args:%s] [aqu_lock:%s] [calling:%s] [json:%s] [global:%s]", 
                                fname, args_time, aqu_lock_time, calling_time, json_time, global_time )
                        lst = [('args',args_time), ('aqulock',aqu_lock_time), ('calling',calling_time), ('json',json_time), ('global',global_time)]
                        # increase the stats timers
                        for (k, _t) in lst:
                            statsmgr.incr('http.%s.%s' % (fname, k), _t)
                        
                        return j
                    # Ok now really put the route in place
                    bottle.route('/'+fname, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
                    # and the name with - instead of _ if need
                    fname_dash = fname.replace('_', '-')
                    if fname_dash != fname:
                        bottle.route('/'+fname_dash, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
                register_callback(fname, args, f, obj, self.lock)

            # Add a simple / page
            def slash():
                return "OK"
            bottle.route('/', callback=slash)
Example #7
0
                def register_callback(fname, args, f, obj, lock):
                    def f_wrapper():
                        t0 = time.time()
                        args_time = aqu_lock_time = calling_time = json_time = 0
                        need_lock = getattr(f, 'need_lock', True)

                        # Warning : put the bottle.response set inside the wrapper
                        # because outside it will break bottle
                        d = {}
                        method = getattr(f, 'method', 'get').lower()
                        for aname in args:
                            v = None
                            if method == 'post':
                                v = bottle.request.forms.get(aname, None)
                                # Post args are zlibed and cPickled (but in
                                # safemode)
                                if v is not None:
                                    v = zlib.decompress(v)
                                    v = SafeUnpickler.loads(v)
                            elif method == 'get':
                                v = bottle.request.GET.get(aname, None)
                            if v is None:
                                # Maybe we got a default value?
                                default_args = self.registered_fun_defaults.get(fname, {})
                                if aname not in default_args:
                                    raise Exception('Missing argument %s' % aname)
                                v = default_args[aname]
                            d[aname] = v

                        t1 = time.time()
                        args_time = t1 - t0

                        if need_lock:
                            logger.debug("HTTP: calling lock for %s", fname)
                            lock.acquire()

                        t2 = time.time()
                        aqu_lock_time = t2 - t1

                        try:
                            ret = f(**d)
                        # Always call the lock release if need
                        finally:
                            # Ok now we can release the lock
                            if need_lock:
                                lock.release()

                        t3 = time.time()
                        calling_time = t3 - t2

                        encode = getattr(f, 'encode', 'json').lower()
                        j = json.dumps(ret)
                        t4 = time.time()
                        json_time = t4 - t3

                        global_time = t4 - t0
                        logger.debug("Debug perf: %s [args:%s] [aqu_lock:%s]"
                                     "[calling:%s] [json:%s] [global:%s]",
                                     fname, args_time, aqu_lock_time, calling_time, json_time,
                                     global_time)
                        lst = [('args', args_time), ('aqulock', aqu_lock_time),
                               ('calling', calling_time), ('json', json_time),
                               ('global', global_time)]
                        # increase the stats timers
                        for (k, _t) in lst:
                            statsmgr.timing('http.%s.%s' % (fname, k), _t, 'perf')

                        return j
                    # Ok now really put the route in place
                    bottle.route('/' + fname, callback=f_wrapper,
                                 method=getattr(f, 'method', 'get').upper())
                    # and the name with - instead of _ if need
                    fname_dash = fname.replace('_', '-')
                    if fname_dash != fname:
                        bottle.route('/' + fname_dash, callback=f_wrapper,
                                     method=getattr(f, 'method', 'get').upper())
Example #8
0
        def register(self, obj):
            methods = inspect.getmembers(obj, predicate=inspect.ismethod)
            for (fname, f) in methods:
                # Slip private functions
                if fname.startswith('_'):
                    continue
                # Get the args of the function to catch them in the queries
                argspec = inspect.getargspec(f)
                args = argspec.args
                varargs = argspec.varargs
                keywords = argspec.keywords
                defaults = argspec.defaults
                # remove useless self in args, because we alredy got a bonded method f
                if 'self' in args:
                    args.remove('self')
                print "Registering", fname, args
                # WARNING : we MUST do a 2 levels function here, or the f_wrapper
                # will be uniq and so will link to the last function again
                # and again
                def register_callback(fname, args, f, obj, lock):
                    def f_wrapper():
                        need_lock = getattr(f, 'need_lock', True)
                        
                        # Warning : put the bottle.response set inside the wrapper
                        # because outside it will break bottle
                        d = {}
                        method = getattr(f, 'method', 'get').lower()
                        for aname in args:
                            v = None
                            if method == 'post':
                                v = bottle.request.forms.get(aname, None)
                                # Post args are zlibed and cPickled
                                if v is not None:
                                    v = zlib.decompress(v)
                                    v = cPickle.loads(v)
                            elif method == 'get':
                                v = bottle.request.GET.get(aname, None)
                            if v is None:
                                raise Exception('Missing argument %s' % aname)
                            d[aname] = v
                        if need_lock:
                            logger.debug("HTTP: calling lock for %s" % fname)
                            lock.acquire()

                        ret = f(**d)

                        # Ok now we can release the lock
                        if need_lock:
                            lock.release()

                        encode = getattr(f, 'encode', 'json').lower()
                        j = json.dumps(ret)
                        return j
                    # Ok now really put the route in place
                    bottle.route('/'+fname, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
                register_callback(fname, args, f, obj, self.lock)

            # Add a simple / page
            def slash():
                return "OK"
            bottle.route('/', callback=slash)
Example #9
0
        def register(self, obj):
            methods = inspect.getmembers(obj, predicate=inspect.ismethod)
            merge = [fname for (fname, f) in methods if fname in self.registered_fun ]
            if merge != []:
                methods_in = [m.__name__ for m in obj.__class__.__dict__.values() if inspect.isfunction(m)]
                methods = [m for m in methods if m[0] in methods_in]
                print "picking only bound methods of class and not parents"
            print "List to register :%s" % methods
            for (fname, f) in methods:
                if fname.startswith('_'):
                    continue
                # Get the args of the function to catch them in the queries
                argspec = inspect.getargspec(f)
                args = argspec.args
                varargs = argspec.varargs
                keywords = argspec.keywords
                defaults = argspec.defaults
                # If we got some defauts, save arg=value so we can lookup
                # for them after
                if defaults:
                    default_args = zip(argspec.args[-len(argspec.defaults):],argspec.defaults)
                    _d = {}
                    for (argname, defavalue) in default_args:
                        _d[argname] = defavalue
                    self.registered_fun_defaults[fname] = _d
                # remove useless self in args, because we alredy got a bonded method f
                if 'self' in args:
                    args.remove('self')
                print "Registering", fname, args, obj
                self.registered_fun.append(fname)
                # WARNING : we MUST do a 2 levels function here, or the f_wrapper
                # will be uniq and so will link to the last function again
                # and again
                def register_callback(fname, args, f, obj, lock):
                    def f_wrapper():
                        need_lock = getattr(f, 'need_lock', True)

                        # Warning : put the bottle.response set inside the wrapper
                        # because outside it will break bottle
                        d = {}
                        method = getattr(f, 'method', 'get').lower()
                        for aname in args:
                            v = None
                            if method == 'post':
                                v = bottle.request.forms.get(aname, None)
                                # Post args are zlibed and cPickled
                                if v is not None:
                                    v = zlib.decompress(v)
                                    v = cPickle.loads(v)
                            elif method == 'get':
                                v = bottle.request.GET.get(aname, None)
                            if v is None:
                                # Maybe we got a default value?
                                default_args = self.registered_fun_defaults.get(fname, {})
                                if not aname in default_args:
                                    raise Exception('Missing argument %s' % aname)
                                v = default_args[aname]
                            d[aname] = v
                        if need_lock:
                            logger.debug("HTTP: calling lock for %s" % fname)
                            lock.acquire()

                        ret = f(**d)

                        # Ok now we can release the lock
                        if need_lock:
                            lock.release()

                        encode = getattr(f, 'encode', 'json').lower()
                        j = json.dumps(ret)
                        return j
                    # Ok now really put the route in place
                    bottle.route('/'+fname, callback=f_wrapper, method=getattr(f, 'method', 'get').upper())
                register_callback(fname, args, f, obj, self.lock)

            # Add a simple / page
            def slash():
                return "OK"
            bottle.route('/', callback=slash)
Example #10
0
    def register(self, obj):
        methods = inspect.getmembers(obj, predicate=inspect.ismethod)
        merge = [
            fname for (fname, f) in methods if fname in self.registered_fun
        ]
        if merge != []:
            methods_in = [
                m.__name__ for m in obj.__class__.__dict__.values()
                if inspect.isfunction(m)
            ]
            methods = [m for m in methods if m[0] in methods_in]
            print "picking only bound methods of class and not parents"
        print "List to register :%s" % methods
        for (fname, f) in methods:
            if fname.startswith('_'):
                continue
            # Get the args of the function to catch them in the queries
            argspec = inspect.getargspec(f)
            args = argspec.args
            varargs = argspec.varargs
            keywords = argspec.keywords
            defaults = argspec.defaults
            # If we got some defauts, save arg=value so we can lookup
            # for them after
            if defaults:
                default_args = zip(argspec.args[-len(argspec.defaults):],
                                   argspec.defaults)
                _d = {}
                for (argname, defavalue) in default_args:
                    _d[argname] = defavalue
                self.registered_fun_defaults[fname] = _d
            # remove useless self in args, because we alredy got a bonded method f
            if 'self' in args:
                args.remove('self')
            print "Registering", fname, args, obj
            self.registered_fun.append(fname)

            # WARNING : we MUST do a 2 levels function here, or the f_wrapper
            # will be uniq and so will link to the last function again
            # and again
            def register_callback(fname, args, f, obj, lock):
                def f_wrapper():
                    need_lock = getattr(f, 'need_lock', True)

                    # Warning : put the bottle.response set inside the wrapper
                    # because outside it will break bottle
                    d = {}
                    method = getattr(f, 'method', 'get').lower()
                    for aname in args:
                        v = None
                        if method == 'post':
                            v = bottle.request.forms.get(aname, None)
                            # Post args are zlibed and cPickled
                            if v is not None:
                                v = zlib.decompress(v)
                                v = cPickle.loads(v)
                        elif method == 'get':
                            v = bottle.request.GET.get(aname, None)
                        if v is None:
                            # Maybe we got a default value?
                            default_args = self.registered_fun_defaults.get(
                                fname, {})
                            if not aname in default_args:
                                raise Exception('Missing argument %s' % aname)
                            v = default_args[aname]
                        d[aname] = v
                    if need_lock:
                        logger.debug("HTTP: calling lock for %s" % fname)
                        lock.acquire()

                    ret = f(**d)

                    # Ok now we can release the lock
                    if need_lock:
                        lock.release()

                    encode = getattr(f, 'encode', 'json').lower()
                    j = json.dumps(ret)
                    return j

                # Ok now really put the route in place
                bottle.route('/' + fname,
                             callback=f_wrapper,
                             method=getattr(f, 'method', 'get').upper())

            register_callback(fname, args, f, obj, self.lock)

        # Add a simple / page
        def slash():
            return "OK"

        bottle.route('/', callback=slash)
Example #11
0
    def register(self, obj):
        methods = inspect.getmembers(obj, predicate=inspect.ismethod)
        for (fname, f) in methods:
            # Slip private functions
            if fname.startswith('_'):
                continue
            # Get the args of the function to catch them in the queries
            argspec = inspect.getargspec(f)
            args = argspec.args
            varargs = argspec.varargs
            keywords = argspec.keywords
            defaults = argspec.defaults
            # remove useless self in args, because we alredy got a bonded method f
            if 'self' in args:
                args.remove('self')
            print "Registering", fname, args

            # WARNING : we MUST do a 2 levels function here, or the f_wrapper
            # will be uniq and so will link to the last function again
            # and again
            def register_callback(fname, args, f, obj, lock):
                def f_wrapper():
                    need_lock = getattr(f, 'need_lock', True)

                    # Warning : put the bottle.response set inside the wrapper
                    # because outside it will break bottle
                    d = {}
                    method = getattr(f, 'method', 'get').lower()
                    for aname in args:
                        v = None
                        if method == 'post':
                            v = bottle.request.forms.get(aname, None)
                            # Post args are zlibed and cPickled
                            if v is not None:
                                v = zlib.decompress(v)
                                v = cPickle.loads(v)
                        elif method == 'get':
                            v = bottle.request.GET.get(aname, None)
                        if v is None:
                            raise Exception('Missing argument %s' % aname)
                        d[aname] = v
                    if need_lock:
                        logger.debug("HTTP: calling lock for %s" % fname)
                        lock.acquire()

                    ret = f(**d)

                    # Ok now we can release the lock
                    if need_lock:
                        lock.release()

                    encode = getattr(f, 'encode', 'json').lower()
                    j = json.dumps(ret)
                    return j

                # Ok now really put the route in place
                bottle.route('/' + fname,
                             callback=f_wrapper,
                             method=getattr(f, 'method', 'get').upper())

            register_callback(fname, args, f, obj, self.lock)

        # Add a simple / page
        def slash():
            return "OK"

        bottle.route('/', callback=slash)