Esempio n. 1
0
 def test_decorators(self):
     def foo(): return bottle.request.method
     bottle.get('/')(foo)
     bottle.post('/')(foo)
     bottle.put('/')(foo)
     bottle.delete('/')(foo)
     for verb in 'GET POST PUT DELETE'.split():
         self.assertBody(verb, '/', method=verb)
Esempio n. 2
0
 def test_decorators(self):
     app = bottle.Bottle()
     app.route('/g')('foo')
     bottle.route('/g')('foo')
     app.route('/g2', method='GET')('foo')
     bottle.get('/g2')('foo')
     app.route('/p', method='POST')('foo')
     bottle.post('/p')('foo')
     app.route('/p2', method='PUT')('foo')
     bottle.put('/p2')('foo')
     app.route('/d', method='DELETE')('foo')
     bottle.delete('/d')('foo')
     self.assertEqual(app.routes, bottle.app().routes)
Esempio n. 3
0
def routeapp(obj):
    for kw in dir(obj):
        attr = getattr(obj, kw)
        if hasattr(attr, 'error'):
            bottle.error(attr.error)(attr)
        if hasattr(attr, 'delete'):
            bottle.delete(attr.delete)(attr)
        if hasattr(attr, 'put'):
            bottle.put(attr.put)(attr)
        if hasattr(attr, 'post'):
            bottle.post(attr.post)(attr)
        if hasattr(attr, 'route'):
            bottle.route(attr.route)(attr)
Esempio n. 4
0
 def test_decorators(self):
     app = bottle.Bottle()
     app.route('/g')('foo')
     bottle.route('/g')('foo')
     app.route('/g2', method='GET')('foo')
     bottle.get('/g2')('foo')
     app.route('/p', method='POST')('foo')
     bottle.post('/p')('foo')
     app.route('/p2', method='PUT')('foo')
     bottle.put('/p2')('foo')
     app.route('/d', method='DELETE')('foo')
     bottle.delete('/d')('foo')
     self.assertEqual(app.routes, bottle.app().routes)
Esempio n. 5
0
    def run(self):
        # UI Functions
        route('/')(self.frontend)
        # route('/', method='OPTIONS')(self.options_handler)
        # route('/<path:path>', method='OPTIONS')(self.options_handler)
        # hook('after_request')(self.enable_cors)

        # API functions
        # v1
        route('/api/v1/')(self.index)
        route('/api/v1/status')(self.status)
        route('/api/v1/stations')(self.stations)
        route('/api/v1/streams')(self.streams)
        route('/api/v1/<station>/streams')(self.streams)
        route('/api/v1/<station>')(self.station)
        post('/api/v1/<station>')(self.set)
        route('/api/v1/<station>/<stream>')(self.stream)
        post('/api/v1/<station>/<stream>')(self.set)
        route('/api/v1/play')(self.play)
        route('/api/v1/pause')(self.pause)
        route('/api/v1/stop')(self.stop)

        # API functions
        # v1.1
        route('/api/v1.1/stations')(self.stations)
        route('/api/v1.1/stations/<station>')(self.station)
        route('/api/v1.1/stations/<station>/streams')(self.streams)
        route('/api/v1.1/stations/<station>/streams/<stream>')(self.stream)
        route('/api/v1.1/streams')(self.streams)
        route('/api/v1.1/streams/<station>')(self.streams)
        route('/api/v1.1/streams/<station>/<stream>')(self.stream)
        route('/api/v1.1/player')(self.status)
        post('/api/v1.1/player')(self.play)
        post('/api/v1.1/player/<station>/<stream>')(self.play)
        post('/api/v1.1/volume/<value>')(self.volume)
        put('/api/v1.1/player')(self.pause)
        delete('/api/v1.1/player')(self.stop)
        try:
            run(host=self.host, port=self.port,
                debug=BOTTLE_DEBUG, quiet=not BOTTLE_DEBUG)
        except (OSError, OverflowError) as exc_info:
            print("SERVER ERROR: %s." % exc_info)
            print("Check network settings (host, port) in config")
            sys.exit(1)
Esempio n. 6
0
    def run(self):
        # UI Functions
        route('/')(self.frontend)
        # route('/', method='OPTIONS')(self.options_handler)
        # route('/<path:path>', method='OPTIONS')(self.options_handler)
        # hook('after_request')(self.enable_cors)

        # API functions
        # v1
        route('/api/v1/')(self.index)
        route('/api/v1/status')(self.status)
        route('/api/v1/stations')(self.stations)
        route('/api/v1/streams')(self.streams)
        route('/api/v1/<station>/streams')(self.streams)
        route('/api/v1/<station>')(self.station)
        post('/api/v1/<station>')(self.set)
        route('/api/v1/<station>/<stream>')(self.stream)
        post('/api/v1/<station>/<stream>')(self.set)
        route('/api/v1/play')(self.play)
        route('/api/v1/pause')(self.pause)
        route('/api/v1/stop')(self.stop)

        # API functions
        # v1.1
        route('/api/v1.1/stations')(self.stations)
        route('/api/v1.1/stations/<station>')(self.station)
        route('/api/v1.1/stations/<station>/streams')(self.streams)
        route('/api/v1.1/stations/<station>/streams/<stream>')(self.stream)
        route('/api/v1.1/streams')(self.streams)
        route('/api/v1.1/streams/<station>')(self.streams)
        route('/api/v1.1/streams/<station>/<stream>')(self.stream)
        route('/api/v1.1/player')(self.status)
        post('/api/v1.1/player')(self.play)
        post('/api/v1.1/player/<station>/<stream>')(self.play)
        put('/api/v1.1/player')(self.pause)
        delete('/api/v1.1/player')(self.stop)
        run(host=self.host,
            port=self.port,
            debug=BOTTLE_DEBUG,
            quiet=not BOTTLE_DEBUG)
Esempio n. 7
0
    def run(self):
        # UI Functions
        route('/')(self.frontend)
        # route('/', method='OPTIONS')(self.options_handler)
        # route('/<path:path>', method='OPTIONS')(self.options_handler)
        # hook('after_request')(self.enable_cors)

        # API functions
        # v1
        route('/api/v1/')(self.index)
        route('/api/v1/status')(self.status)
        route('/api/v1/stations')(self.stations)
        route('/api/v1/streams')(self.streams)
        route('/api/v1/<station>/streams')(self.streams)
        route('/api/v1/<station>')(self.station)
        post('/api/v1/<station>')(self.set)
        route('/api/v1/<station>/<stream>')(self.stream)
        post('/api/v1/<station>/<stream>')(self.set)
        route('/api/v1/play')(self.play)
        route('/api/v1/pause')(self.pause)
        route('/api/v1/stop')(self.stop)

        # API functions
        # v1.1
        route('/api/v1.1/stations')(self.stations)
        route('/api/v1.1/stations/<station>')(self.station)
        route('/api/v1.1/stations/<station>/streams')(self.streams)
        route('/api/v1.1/stations/<station>/streams/<stream>')(self.stream)
        route('/api/v1.1/streams')(self.streams)
        route('/api/v1.1/streams/<station>')(self.streams)
        route('/api/v1.1/streams/<station>/<stream>')(self.stream)
        route('/api/v1.1/player')(self.status)
        post('/api/v1.1/player')(self.play)
        post('/api/v1.1/player/<station>/<stream>')(self.play)
        put('/api/v1.1/player')(self.pause)
        delete('/api/v1.1/player')(self.stop)
        run(host=self.host, port=self.port,
            debug=BOTTLE_DEBUG, quiet=not BOTTLE_DEBUG)
Esempio n. 8
0
		except ValueError:
			return bottle.HTTPError(400, "Invalid state: %s" % (state,))
	_wrapped.__name__ = 'put_%s' % (device.get_name(),)
	return _wrapped
def rest_list_states(device):
	""" Inside an HTTP OPTIONS, list a device's acceptable inputs """
	def _wrapped(*args, **kwargs):
		bottle.response.content_type = 'text/plain'
		return '\n'.join(device.get_acceptable_states()) + '\n'
	_wrapped.__name__ = 'options_%s' % (device.get_name(),)
	return _wrapped
for device in DEVICES:
	path = '/' + device._state_path()
	bottle.get(path)(rest_get_state(device))
	bottle.post(path)(rest_set_state(device))  # openhab can only post
	bottle.put(path)(rest_set_state(device))
	bottle.route(path, method='OPTIONS')(rest_list_states(device))
	# check for subdevices
	for name,subdev in device.subdevices.items():
		subpath = '/' + subdev._state_path()
		bottle.get(subpath)(rest_get_state(subdev))
		bottle.post(subpath)(rest_set_state(subdev))  # openhab can only post
		bottle.put(subpath)(rest_set_state(subdev))
		bottle.route(subpath, method='OPTIONS')(rest_list_states(subdev))
	# save to index
	klass = device.get_class()
	name = device.get_name()
	klass_devices = device_list.get(klass, {})
	klass_devices[name] = device
	device_list[klass] = klass_devices
Esempio n. 9
0
def create_rest_api(base_collection, url_base, restricted=False, override_={}, post_={}):
    def list_collection(**context):
        collection = base_collection.fetch(**context)
        bottle.response.context.update({'collection': collection})
        return collection
    def update_collection(**context): pass
    def add_to_collection(**context):
        new_item = bottle.request.json
        new_item['user'] = context['user']
        item = base_collection.new(**new_item)
        bottle.response.context.update({'item': item})
        return item
    def delete_collection(**context): pass
    def show_item(id, **context):
        item = base_collection.get(_id=id)
        bottle.response.context.update({'item': item})
        return item
    def update_item(id, **context):
        updated_item = bottle.request.json
        updated_item['_id'] = id
        item = base_collection.update(**updated_item)
        bottle.response.context.update({'item': item})
        return item
    def add_to_item(id, **context): pass
    def delete_item(id, **context):
        base_collection.remove(_id=id)
        return list_collection(**context)
    route_map = {
        # Collections
        '%s' % url_base: list_collection,
        '>%s' % url_base: update_collection,
        '#%s' % url_base: add_to_collection,
        'X%s' % url_base: delete_collection,
        # Entities
        '%s/:id' % url_base: show_item,
        '>%s/:id' % url_base: update_item,
        '#%s/:id' % url_base: add_to_item,
        'X%s/:id' % url_base: delete_item,
    }
    template_map = {
        # Collections
        list_collection: 'list',
        update_collection: 'list',
        add_to_collection: 'show',
        delete_collection: 'list',
        # Entities
        show_item: 'show',
        update_item: 'show',
        add_to_item: 'show',
        delete_item: 'list',
    }

    # Create the full callback for a route and action
    def make_templated_callback(callback):
        template_base = 'generic/'
        def wrapper(**context):
            #print context
            context['user'] = bottle.response.context.user.id
            cb=callback
            if override_.has_key(callback.__name__): cb = override_[callback.__name__]
            result = cb(**context)
            if post_.has_key(callback.__name__): result = post_[cb.__name__](**result)
            return render(template_base + template_map[callback], **result)
        if restricted: wrapper = require_auth(wrapper)
        return wrapper
    # Make a JSON version too
    def make_json_callback(callback):
        def wrapper(**context):
            context['user'] = bottle.response.context.user.id
            cb=callback
            if override_.has_key(callback.__name__): cb = override_[callback.__name__]
            #print "CONTEXT: %s" % context
            result = cb(**context)
            if post_.has_key(callback.__name__): result = post_[cb.__name__](**result)
            #print "RESULT: %s" % result
            return pymongo_to_json(result)
        if restricted: wrapper = require_auth(wrapper)
        return wrapper

    # Generate the routes
    for route in route_map.items():
        path, callback = route
        if path.startswith('#'):
            bottle.post(path[1:] + '.json')(make_json_callback(callback))
            bottle.post(path[1:])(make_templated_callback(callback))
        elif path.startswith('X'):
            bottle.delete(path[1:] + '.json')(make_json_callback(callback))
            bottle.delete(path[1:])(make_templated_callback(callback))
        elif path.startswith('>'):
            bottle.put(path[1:] + '.json')(make_json_callback(callback))
            bottle.put(path[1:])(make_templated_callback(callback))
        else:
            bottle.get(path + '.json')(make_json_callback(callback))
            bottle.get(path)(make_templated_callback(callback))
Esempio n. 10
0
    'X/'+_node_id_: [authenticate, delete_node],
}

# Define routes from the route map
def make_route_callback(callback):
    if type(callback) == list: callback = partial(*callback)
    def wrapper(**context):
        return callback(**get_session(**start_context(**context)))
    return wrapper

for route in route_map.items():
    path, callback = route
    if path.startswith('#'):
        bottle.post(path[1:])(make_route_callback(callback))
    elif path.startswith('X'):
        bottle.delete(path[1:])(make_route_callback(callback))
    elif path.startswith('>'):
        bottle.put(path[1:])(make_route_callback(callback))
    else:
        bottle.get(path)(make_route_callback(callback))

# Static fallback
@bottle.route('/<filepath:path>')
def static_file(filepath):
    return bottle.static_file(filepath, root='./static/')

# Run the server
bottle.debug(config.debug)
bottle.run(server=config.server, host='0.0.0.0', port=config.port, reloader=config.debug)