Example #1
0
def mkapp():
    db_init()
    return web.Application(
        (
            url(r'/({})'.format(UUID_PATTERN), RAProtoHandler, name='index'),
        ),
        debug=config.debug
    )
Example #2
0
def handle_initdb(args):
    '''
    Handler for db initialization.
    '''
    db_init()
    config.db.create_tables()

    if args.fixture:
        load_fixtures(args.fixture, config.db)

    log.info('Database init successfully.')
Example #3
0
    def start_server(app_io_loop, app_start_lock):
        # initialize db by hand because we need it in the worker thread
        config.db_conf = {'type': 'sqlite', 'url': ':memory:'}
        db_init()
        load_dev_fixtures(config.db)

        app = mkapp()
        server = HTTPServer(app, io_loop=app_io_loop)
        server.add_socket(unused_port[0])
        app_io_loop.add_callback(callback=app_start_lock.release)
        app_io_loop.start()
Example #4
0
def db():
    temp_dir = mkdtemp(prefix='iottalk-')
    temp_db = os.path.join(temp_dir, 'test.db')
    config.db_conf = {
        'type': 'sqlite',
        'url': temp_db,
    }
    db_init()
    load_dev_fixtures(config.db)
    yield config.db
    config.db.disconnect()
    config._Config__db = None
    rmtree(temp_dir)
Example #5
0
def iotctl_shell(args):
    '''
    Start a python shell with the db setup.

    If the IPython is available, we will invoke IPython embed shell.
    '''
    banner = (
        'IoTtalk shell has following variables available:\n'
        '    config:          the iot.config.config object .\n'
        '    db:              config.db\n'
        '    iot:             the iottalk core package.\n'
        '    list_apps:       the function that lists all registered apps\n'
        '    deregister_apps: the function that deregisters specified apps')

    locals_ = {
        'config': config,
        'db': config.db,
        'iot': iot,
        'list_apps': list_apps,
        'deregister_apps': deregister_apps,
    }

    db_init()

    with orm.db_session:
        try:
            import IPython
        except ImportError:
            pass
        else:
            return IPython.embed(
                argv=sys.argv[2:], header='{}'.format(banner), user_ns=locals_)

        # fallback to builtin interactive shell
        cprt = ('Type "help", "copyright", "credits" or "license" '
                'for more information.')
        py_banner = 'Python {ver} on {platform}\n{cprt}\n\n{iottalk}'.format(
            ver=sys.version, platform=sys.platform, cprt=cprt, iottalk=banner)
        code.interact(banner=py_banner, local=locals_)