Example #1
0
def serve(files, host, port, debug, reload, cors, page_size, max_returned_rows,
          sql_time_limit_ms, sqlite_extensions, inspect_file, metadata,
          template_dir, static):
    """Serve up specified SQLite database files with a web UI"""
    if reload:
        import hupper
        hupper.start_reloader('datasette.cli.serve')

    inspect_data = None
    if inspect_file:
        inspect_data = json.load(open(inspect_file))

    metadata_data = None
    if metadata:
        metadata_data = json.loads(metadata.read())

    click.echo('Serve! files={} on port {}'.format(files, port))
    ds = Datasette(
        files,
        cache_headers=not debug and not reload,
        cors=cors,
        page_size=page_size,
        max_returned_rows=max_returned_rows,
        sql_time_limit_ms=sql_time_limit_ms,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
        template_dir=template_dir,
        static_mounts=static,
    )
    # Force initial hashing/table counting
    ds.inspect()
    ds.app().run(host=host, port=port, debug=debug)
Example #2
0
def main(args=None):
    if args is None:
        args = sys.argv[1:]
    opts = parse_options(args)
    if opts.reload:
        kw = {}
        if opts.poll:
            from hupper.polling import PollingFileMonitor
            pkw = {}
            if opts.poll_interval:
                pkw['poll_interval'] = opts.poll_interval
            kw['monitor_factory'] = lambda cb: PollingFileMonitor(cb, **pkw)

        if opts.watchdog:
            from hupper.watchdog import WatchdogFileMonitor
            kw['monitor_factory'] = WatchdogFileMonitor

        if opts.reload_interval:
            kw['reload_interval'] = opts.reload_interval

        hupper.start_reloader(__name__ + '.main', **kw)

    if hupper.is_active():
        hupper.get_reloader().watch_files([os.path.join(here, 'foo.ini')])
        hupper.get_reloader().watch_files(opts.watch_files)

    if opts.callback_file:
        with open(opts.callback_file, 'ab') as fp:
            fp.write('{:d}\n'.format(int(time.time())).encode('utf8'))
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        pass
Example #3
0
def main(
    log_level: Optional[str],
    dev: bool,
    debug: bool,
    mock_games: bool,
    api: bool,
    port: Optional[int] = None,
) -> None:
    if dev:
        hupper.start_reloader("spellbot.main")

    from .settings import Settings

    # Ensure that configure_logging() is called as early as possible
    settings = Settings()
    level = log_level if log_level is not None else (getenv("LOG_LEVEL")
                                                     or "INFO")
    configure_logging(level)

    loop = asyncio.new_event_loop()
    if debug:
        loop.set_debug(True)
    if api:
        from .web import launch_web_server

        launch_web_server(settings, loop, port or settings.PORT)
        loop.run_forever()
    else:
        from .client import build_bot

        assert settings.BOT_TOKEN is not None
        bot = build_bot(loop=loop, mock_games=mock_games)
        bot.run(settings.BOT_TOKEN)
Example #4
0
def serve(files, host, port, debug, reload, cors, page_size, max_returned_rows, sql_time_limit_ms, sqlite_extensions, inspect_file, metadata):
    """Serve up specified SQLite database files with a web UI"""
    if reload:
        import hupper
        hupper.start_reloader('datasette.cli.serve')

    inspect_data = None
    if inspect_file:
        inspect_data = json.load(open(inspect_file))

    metadata_data = None
    if metadata:
        metadata_data = json.loads(metadata.read())

    click.echo('Serve! files={} on port {}'.format(files, port))
    ds = Datasette(
        files,
        cache_headers=not debug and not reload,
        cors=cors,
        page_size=page_size,
        max_returned_rows=max_returned_rows,
        sql_time_limit_ms=sql_time_limit_ms,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
    )
    # Force initial hashing/table counting
    ds.inspect()
    ds.app().run(host=host, port=port, debug=debug)
Example #5
0
def run():
    if dodoo.framework().dodoo_run_mode == dodoo.RUNMODE.Develop:
        import hupper
        from .servers.watcher import file_changed

        hupper.reloader.FileMonitorProxy.file_changed = file_changed
        hupper.start_reloader(f"dodoo.main")
Example #6
0
def serve(reload=False):
    """Run a web server (for development only)."""
    if reload:
        import hupper

        hupper.start_reloader("copanier.serve")
    static(app, root=Path(__file__).parent / "static")
    simple_server(app, port=2244)
Example #7
0
def serve(reload=False):
    """Run a web server (for development only)."""
    # Debug only.
    static(app, "/explorer/", Path(__file__).parent / "explorer")
    if reload:
        hupper.start_reloader("trefle.bin.serve")
    traceback(app)
    simple_server(app)
Example #8
0
def serve(reload=False):
    """Run a web server (for development only)."""
    from roll.extensions import simple_server

    from .views import app

    if reload:
        import hupper

        hupper.start_reloader("egapro.bin.serve")
    simple_server(app, port=2626)
Example #9
0
def main(name):
    if HAVE_RELOADER:
        print('Live reload enabled')
        hupper.start_reloader('sud.cli.service.main')

    debug = config('DEBUG', default=False, cast=bool)
    loglevel = config('LOGLEVEL', default='DEBUG' if debug else 'INFO').upper()

    with StreamHandler(sys.stdout, level=loglevel).applicationbound():
        module_name = 'sud.services.{0}.server'.format(name.replace('-', '_'))
        module = import_module(module_name)
        module.main()
Example #10
0
def server(module, host, port):
    sys.path.append(os.getcwd())

    reloader = hupper.start_reloader("strawberry.cli.run", verbose=False)

    schema_module = importlib.import_module(module)

    reloader.watch_files([schema_module.__file__])

    app = Starlette(debug=True)

    app.add_middleware(CORSMiddleware,
                       allow_headers=["*"],
                       allow_origins=["*"],
                       allow_methods=["*"])

    graphql_app = GraphQL(schema_module.schema, debug=True)

    paths = ["/", "/graphql"]

    for path in paths:
        app.add_route(path, graphql_app)
        app.add_websocket_route(path, graphql_app)

    print(f"Running strawberry on http://{host}:{port}/ 🍓")

    uvicorn.run(app, host=host, port=port, log_level="error")
Example #11
0
def serve(args):
    """Run the web server for the application."""
    from waitress import serve as waitress_serve
    from ..wsgi import app

    if args.reload:
        try:
            import hupper
        except ImportError:
            sys.stderr.write(
                "Please install dev dependencies to use reloader.\n")
            sys.exit(1)
        hupper.start_reloader("fanboi2.cmd.ctl.main")

    waitress_serve(app, host=args.host, port=args.port)
    sys.exit(0)
Example #12
0
def server(schema, host, port, app_dir):
    sys.path.insert(0, app_dir)

    try:
        schema_symbol = import_module_symbol(schema,
                                             default_symbol_name="schema")
    except (ImportError, AttributeError) as exc:
        message = str(exc)
        raise click.BadArgumentUsage(message)

    if not isinstance(schema_symbol, Schema):
        message = "The `schema` must be an instance of strawberry.Schema"
        raise click.BadArgumentUsage(message)

    reloader = hupper.start_reloader("strawberry.cli.run", verbose=False)
    schema_module = importlib.import_module(schema_symbol.__module__)
    reloader.watch_files([schema_module.__file__])

    app = Starlette(debug=True)
    app.add_middleware(CORSMiddleware,
                       allow_headers=["*"],
                       allow_origins=["*"],
                       allow_methods=["*"])

    graphql_app = GraphQL(schema_symbol, debug=True)

    paths = ["/", "/graphql"]
    for path in paths:
        app.add_route(path, graphql_app)
        app.add_websocket_route(path, graphql_app)

    print(f"Running strawberry on http://{host}:{port}/ 🍓")
    uvicorn.run(app, host=host, port=port, log_level="error")
Example #13
0
def serve(
    files,
    host,
    port,
    debug,
    reload,
    cors,
    sqlite_extensions,
    inspect_file,
    metadata,
    template_dir,
    plugins_dir,
    static,
    config,
    version_note,
    help_config,
):
    """Serve up specified SQLite database files with a web UI"""
    if help_config:
        formatter = formatting.HelpFormatter()
        with formatter.section("Config options"):
            formatter.write_dl([
                (option.name, '{} (default={})'.format(option.help,
                                                       option.default))
                for option in CONFIG_OPTIONS
            ])
        click.echo(formatter.getvalue())
        sys.exit(0)
    if reload:
        import hupper

        reloader = hupper.start_reloader("datasette.cli.serve")
        if metadata:
            reloader.watch_files([metadata.name])

    inspect_data = None
    if inspect_file:
        inspect_data = json.load(open(inspect_file))

    metadata_data = None
    if metadata:
        metadata_data = json.loads(metadata.read())

    click.echo("Serve! files={} on port {}".format(files, port))
    ds = Datasette(
        files,
        cache_headers=not debug and not reload,
        cors=cors,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
        template_dir=template_dir,
        plugins_dir=plugins_dir,
        static_mounts=static,
        config=dict(config),
        version_note=version_note,
    )
    # Force initial hashing/table counting
    ds.inspect()
    ds.app().run(host=host, port=port, debug=debug)
Example #14
0
    def handle(self):
        if self.option("live-reload"):
            try:
                from livereload import Server
            except ImportError:
                raise ImportError(
                    "Could not find the livereload library. Install it by running 'pip install livereload==2.5.1'"
                )

            import glob

            server = Server(self.app)
            for filepath in glob.glob("resources/templates/**/*/"):
                server.watch(filepath)

            self.line("")
            self.info("Live reload server is starting...")
            self.info(
                "This will only work for templates. Changes to Python files may require a browser refresh."
            )
            self.line("")
            server.serve(
                port=self.option("port"),
                restart_delay=self.option("reload-interval"),
                liveport=5500,
                root=self.app.base_path,
                debug=True,
            )
            return

        reloader = hupper.start_reloader(self.app.make("server.runner"))

        # monitor an extra file
        reloader.watch_files([".env", self.app.get_storage_path()])
Example #15
0
def context_factory(cli, args):
    app = MyApp(args)
    if getattr(args, 'reload', False):
        reloader = hupper.start_reloader(__name__ + '.main')
        reloader.watch_files([app.config_file])
    app.setup_logging()
    return app
Example #16
0
def serve(host, port, debug, reload):

    if debug:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.INFO)

    if reload:
        hupper.start_reloader("openregister.cli.cli")

    store = MemoryStore()
    client = RegisterClient()
    client.load(store, name="register")

    server = RegisterServer(config=None, store=store)

    click.echo("Serving on port {}".format(port))

    server.server().run(host=host, port=port, debug=debug)
Example #17
0
def main() -> None:
    if config.RELOAD:
        if hupper is None:
            logger.error(
                "reload requires hupper - install with 'pip install -e .[dev]'"
            )
        else:
            reloader: hupper.interfaces.IReloaderProxy = hupper.start_reloader(
                "joust.__main__.main")
    asyncio.run(_serve())
Example #18
0
    def execute(cls, args, env):
        from waitress import serve

        reload = args.reload if (args.reload is not None) else env.core.debug
        if reload:
            from hupper import start_reloader
            start_reloader(
                'nextgisweb.script.main',
                reload_interval=0.25)
            logger.info("File monitor started")

        env.initialize()

        config = env.pyramid.make_app({})
        app = config.make_wsgi_app()
        logger.debug("WSGI application created")

        serve(
            app, host=args.host, port=args.port, threads=1,
            clear_untrusted_proxy_headers=True)
Example #19
0
def main():

    # start_reloader will only return in a monitored subprocess
    reloader = hupper.start_reloader('server.main')

    # monitor an extra file
    reloader.watch_files(
        [f'{here}/templates/{MAP_FILE}', f'{here}/templates/{INDEX_FILE}', f'{here}/{DB_FILE}'])

    # connex_app.run(debug=True)
    serve(connex_app.app)
Example #20
0
def main():
    arg_parser = get_default_arg_parser()
    arg_parser.add_argument('--reload',
                            action='store_true',
                            help='reload the application on a code change.')
    settings, config = configure_app(arg_parser=arg_parser)

    if config['args'].get('reload'):
        reloader = hupper.start_reloader(f'{__name__}.main')
        reloader.watch_files([config['args']['config_uri']])

    Callbot.watch_calls(**settings)
Example #21
0
def wsgi():
    """Simple Pyramid wsgi server for development purpose
    """
    load_init_function_from_entry_points()
    argv = [] + sys.argv
    Configuration.load('pyramid')
    sys.argv = argv
    configuration_post_load()
    BlokManager.load()
    config = Configurator()
    config.include_from_entry_point()
    config.load_config_bloks()

    wsgi_host = Configuration.get('wsgi_host')
    wsgi_port = int(Configuration.get('wsgi_port'))

    reload_at_change = Configuration.get('pyramid.reload_all', False)
    if reload_at_change:
        hupper.start_reloader('anyblok_pyramid.scripts.wsgi')

    app = config.make_wsgi_app()
    server = make_server(wsgi_host, wsgi_port, app)
    preload_databases(loadwithoutmigration=False)

    try:
        from colorama import Fore, Style
        start_msg = ("Pyramid development server running at %shttp://%s:%s%s" %
                     (Fore.BLUE, wsgi_host, wsgi_port, Style.RESET_ALL))
    except ImportError:
        logger.info("`pip install colorama` to get colored link")
        start_msg = "Pyramid development server running at http://%s:%s" % (
            wsgi_host, wsgi_port)

    logger.info(start_msg)
    print(start_msg)

    server.serve_forever()
Example #22
0
def serve(
    files,
    host,
    port,
    debug,
    reload,
    cors,
    page_size,
    sqlite_extensions,
    inspect_file,
    metadata,
    template_dir,
    plugins_dir,
    static,
    limit,
):
    """Serve up specified SQLite database files with a web UI"""
    if reload:
        import hupper

        reloader = hupper.start_reloader("datasette.cli.serve")
        if metadata:
            reloader.watch_files([metadata.name])

    inspect_data = None
    if inspect_file:
        inspect_data = json.load(open(inspect_file))

    metadata_data = None
    if metadata:
        metadata_data = json.loads(metadata.read())

    click.echo("Serve! files={} on port {}".format(files, port))
    ds = Datasette(
        files,
        cache_headers=not debug and not reload,
        cors=cors,
        page_size=page_size,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
        template_dir=template_dir,
        plugins_dir=plugins_dir,
        static_mounts=static,
        limits=dict(limit),
    )
    # Force initial hashing/table counting
    ds.inspect()
    ds.app().run(host=host, port=port, debug=debug)
Example #23
0
def context_factory(cli, args, with_db=False):
    if getattr(args, 'reload', False):
        import hupper
        reloader = hupper.start_reloader(
            __name__ + '.main',
            shutdown_interval=30,
        )
        reloader.watch_files([args.profile])

    app = App(args.profile)

    logging.basicConfig(
        level=logging.DEBUG if args.verbose else logging.INFO,
        format='%(asctime)-15s %(levelname)-8s [%(name)s] %(message)s',
    )

    try:
        yield app
    except Exception as ex:
        app.commit(exc=ex)
        raise
    else:
        app.commit()
Example #24
0
def main(log_level, verbose, database_url, database_env, dev,
         mock_games):  # pragma: no cover
    database_env = get_db_env(database_env)
    database_url = get_db_url(database_env, database_url)
    log_level = get_log_level(log_level)

    # We have to make sure that application directories exist
    # before we try to create we can run any of the migrations.
    ensure_application_directories_exist()

    token = getenv("SPELLBOT_TOKEN", None)
    if not token:
        print(  # noqa: T001
            "error: SPELLBOT_TOKEN environment variable not set",
            file=sys.stderr)
        sys.exit(1)

    auth = getenv("SPELLTABLE_AUTH", None)
    if not auth and not mock_games:
        print(  # noqa: T001
            "error: SPELLTABLE_AUTH environment variable not set",
            file=sys.stderr)
        sys.exit(1)

    client = SpellBot(
        token=token,
        auth=auth,
        db_url=database_url,
        log_level="DEBUG" if verbose else log_level,
        mock_games=mock_games,
    )

    if dev:
        reloader = hupper.start_reloader("spellbot.main")
        reloader.watch_files(ASSET_FILES)

    client.run()
Example #25
0
def serve(args):
    try:
        from waitress import serve
    except ImportError as e:
        raise RuntimeError('Waitress server is not installed') from e

    scheme = args.url_scheme
    host, port = args.host, args.port
    path = args.url_prefix.strip('/')
    if path:
        path = f'/{path}/'
    url = f'{scheme}://{host}:{port}{path}'

    if args.browser:
        import time
        import threading
        import webbrowser

        def openBrowser():
            time.sleep(1)
            webbrowser.open(url)

        t = threading.Thread(target=openBrowser)
        t.setDaemon(True)
        t.start()

    if args.reload:
        try:
            import hupper
        except ImportError as e:
            raise RuntimeError(
                'The hupper process monitor is not installed') from e
        if not hupper.is_active():
            print('Running Webware with reloading option...')
            args.browser = args.reload = False
            hupper.start_reloader('webware.Scripts.WaitressServer.serve',
                                  reload_interval=int(args.reload_interval),
                                  worker_args=[args])

    development = not args.prod
    from os import environ
    if development:
        environ['WEBWARE_DEVELOPMENT'] = 'true'
    elif 'WEBWARE_DEVELOPMENT' in environ:
        del environ['WEBWARE_DEVELOPMENT']
    try:
        # get application from WSGI script
        with open(args.wsgi_script, encoding='utf-8') as f:
            script = f.read()
        # set development flag in the script
        script = script.replace('development =',
                                f'development = {development} #')
        # do not change working directory in the script
        script = script.replace('workDir =', "workDir = '' #")
        scriptVars = {}
        exec(script, scriptVars)
        application = scriptVars['application']
    except Exception as e:
        raise RuntimeError(
            'Cannot find Webware application.\nIs the current directory'
            ' the application working directory?') from e

    args = vars(args)
    for arg in 'browser reload reload_interval prod wsgi_script'.split():
        del args[arg]
    if args['trusted_proxy_headers']:
        args['trusted_proxy_headers'] = args['trusted_proxy_headers'].split(
            ',')
    if not args['trusted_proxy']:
        if args['trusted_proxy_count'] == 1:
            del args['trusted_proxy_count']
        if not args['trusted_proxy_headers']:
            del args['trusted_proxy_headers']
    logLevel = args.pop('log_level')
    if logLevel:
        logLevel = logging.getLevelName(logLevel)
        if isinstance(logLevel, int):
            logger = logging.getLogger('waitress')
            logger.setLevel(logLevel)

    print(f"Waitress serving Webware application on {url}")
    serve(application, **args)
Example #26
0
    def run(self):  # pragma: no cover
        if not self.args.config_uri:
            self.out('You must give a config file')
            return 2
        app_spec = self.args.config_uri

        vars = self.get_config_vars()
        app_name = self.args.app_name

        base = os.getcwd()
        if not self._scheme_re.search(app_spec):
            config_path = os.path.join(base, app_spec)
            app_spec = 'config:' + app_spec
        else:
            config_path = None
        server_name = self.args.server_name
        if self.args.server:
            server_spec = 'egg:pyramid'
            assert server_name is None
            server_name = self.args.server
        else:
            server_spec = app_spec

        # do not open the browser on each reload so check hupper first
        if self.args.browser and not hupper.is_active():
            def open_browser():
                context = loadcontext(
                    SERVER, app_spec, name=server_name, relative_to=base,
                    global_conf=vars)
                url = 'http://127.0.0.1:{port}/'.format(**context.config())
                time.sleep(1)
                webbrowser.open(url)
            t = threading.Thread(target=open_browser)
            t.setDaemon(True)
            t.start()

        if self.args.reload and not hupper.is_active():
            if self.args.verbose > 1:
                self.out('Running reloading file monitor')
            hupper.start_reloader(
                'pyramid.scripts.pserve.main',
                reload_interval=int(self.args.reload_interval),
                verbose=self.args.verbose,
            )
            return 0

        if config_path:
            setup_logging(config_path, global_conf=vars)
            self.pserve_file_config(config_path, global_conf=vars)
            self.watch_files.append(config_path)

        if hupper.is_active():
            reloader = hupper.get_reloader()
            reloader.watch_files(self.watch_files)

        server = self.loadserver(
            server_spec, name=server_name, relative_to=base, global_conf=vars)

        app = self.loadapp(
            app_spec, name=app_name, relative_to=base, global_conf=vars)

        if self.args.verbose > 0:
            if hasattr(os, 'getpid'):
                msg = 'Starting server in PID %i.' % os.getpid()
            else:
                msg = 'Starting server.'
            self.out(msg)

        try:
            server(app)
        except (SystemExit, KeyboardInterrupt) as e:
            if self.args.verbose > 1:
                raise
            if str(e):
                msg = ' ' + str(e)
            else:
                msg = ''
            self.out('Exiting%s (-v to see traceback)' % msg)
Example #27
0
    def run(self):  # pragma: no cover
        if not self.args:
            self.out('You must give a config file')
            return 2
        app_spec = self.args[0]

        vars = self.get_options()
        app_name = self.options.app_name

        if not self._scheme_re.search(app_spec):
            app_spec = 'config:' + app_spec
        server_name = self.options.server_name
        if self.options.server:
            server_spec = 'egg:pyramid'
            assert server_name is None
            server_name = self.options.server
        else:
            server_spec = app_spec
        base = os.getcwd()

        # do not open the browser on each reload so check hupper first
        if self.options.browser and not hupper.is_active():

            def open_browser():
                context = loadcontext(SERVER,
                                      app_spec,
                                      name=server_name,
                                      relative_to=base,
                                      global_conf=vars)
                url = 'http://127.0.0.1:{port}/'.format(**context.config())
                time.sleep(1)
                webbrowser.open(url)

            t = threading.Thread(target=open_browser)
            t.setDaemon(True)
            t.start()

        if self.options.reload and not hupper.is_active():
            if self.options.verbose > 1:
                self.out('Running reloading file monitor')
            hupper.start_reloader(
                'pyramid.scripts.pserve.main',
                reload_interval=int(self.options.reload_interval),
                verbose=self.options.verbose,
            )
            return 0

        if hupper.is_active():
            reloader = hupper.get_reloader()
            if app_spec.startswith('config:'):
                reloader.watch_files([app_spec[len('config:'):]])

        log_fn = app_spec
        if log_fn.startswith('config:'):
            log_fn = app_spec[len('config:'):]
        elif log_fn.startswith('egg:'):
            log_fn = None
        if log_fn:
            log_fn = os.path.join(base, log_fn)
            setup_logging(log_fn, global_conf=vars)

        server = self.loadserver(server_spec,
                                 name=server_name,
                                 relative_to=base,
                                 global_conf=vars)

        app = self.loadapp(app_spec,
                           name=app_name,
                           relative_to=base,
                           global_conf=vars)

        if self.options.verbose > 0:
            if hasattr(os, 'getpid'):
                msg = 'Starting server in PID %i.' % os.getpid()
            else:
                msg = 'Starting server.'
            self.out(msg)

        def serve():
            try:
                server(app)
            except (SystemExit, KeyboardInterrupt) as e:
                if self.options.verbose > 1:
                    raise
                if str(e):
                    msg = ' ' + str(e)
                else:
                    msg = ''
                self.out('Exiting%s (-v to see traceback)' % msg)

        serve()
Example #28
0
    def run(self):  # pragma: no cover
        if not self.args.config_uri:
            self.out('You must give a config file')
            return 2
        config_uri = self.args.config_uri
        config_vars = parse_vars(self.args.config_vars)
        app_spec = self.args.config_uri
        app_name = self.args.app_name

        loader = self._get_config_loader(config_uri)
        loader.setup_logging(config_vars)

        self.pserve_file_config(loader, global_conf=config_vars)

        server_name = self.args.server_name
        if self.args.server:
            server_spec = 'egg:pyramid'
            assert server_name is None
            server_name = self.args.server
        else:
            server_spec = app_spec

        server_loader = loader
        if server_spec != app_spec:
            server_loader = self.get_config_loader(server_spec)

        # do not open the browser on each reload so check hupper first
        if self.args.browser and not hupper.is_active():
            url = self.open_url

            if not url:
                url = self.guess_server_url(
                    server_loader, server_name, config_vars)

            if not url:
                self.out('WARNING: could not determine the server\'s url to '
                         'open the browser. To fix this set the "open_url" '
                         'setting in the [pserve] section of the '
                         'configuration file.')

            else:
                def open_browser():
                    time.sleep(1)
                    webbrowser.open(url)
                t = threading.Thread(target=open_browser)
                t.setDaemon(True)
                t.start()

        if self.args.reload and not hupper.is_active():
            if self.args.verbose > 1:
                self.out('Running reloading file monitor')
            hupper.start_reloader(
                'pyramid.scripts.pserve.main',
                reload_interval=int(self.args.reload_interval),
                verbose=self.args.verbose,
                worker_kwargs=self.worker_kwargs
            )
            return 0

        config_path = self.get_config_path(loader)
        self.watch_files.add(config_path)

        server_path = self.get_config_path(server_loader)
        self.watch_files.add(server_path)

        if hupper.is_active():
            reloader = hupper.get_reloader()
            reloader.watch_files(list(self.watch_files))

        server = server_loader.get_wsgi_server(server_name, config_vars)

        app = loader.get_wsgi_app(app_name, config_vars)

        if self.args.verbose > 0:
            if hasattr(os, 'getpid'):
                msg = 'Starting server in PID %i.' % os.getpid()
            else:
                msg = 'Starting server.'
            self.out(msg)

        try:
            server(app)
        except (SystemExit, KeyboardInterrupt) as e:
            if self.args.verbose > 1:
                raise
            if str(e):
                msg = ' ' + str(e)
            else:
                msg = ''
            self.out('Exiting%s (-v to see traceback)' % msg)
Example #29
0
def serve(
    files,
    immutable,
    host,
    port,
    uds,
    reload,
    cors,
    sqlite_extensions,
    inspect_file,
    metadata,
    template_dir,
    plugins_dir,
    static,
    memory,
    config,
    settings,
    secret,
    root,
    get,
    version_note,
    help_settings,
    pdb,
    open_browser,
    create,
    crossdb,
    nolock,
    ssl_keyfile,
    ssl_certfile,
    return_instance=False,
):
    """Serve up specified SQLite database files with a web UI"""
    if help_settings:
        formatter = formatting.HelpFormatter()
        with formatter.section("Settings"):
            formatter.write_dl([(option.name,
                                 f"{option.help} (default={option.default})")
                                for option in SETTINGS])
        click.echo(formatter.getvalue())
        sys.exit(0)
    if reload:
        import hupper

        reloader = hupper.start_reloader("datasette.cli.serve")
        if immutable:
            reloader.watch_files(immutable)
        if metadata:
            reloader.watch_files([metadata.name])

    inspect_data = None
    if inspect_file:
        with open(inspect_file) as fp:
            inspect_data = json.load(fp)

    metadata_data = None
    if metadata:
        metadata_data = parse_metadata(metadata.read())

    combined_settings = {}
    if config:
        click.echo(
            "--config name:value will be deprecated in Datasette 1.0, use --setting name value instead",
            err=True,
        )
        combined_settings.update(config)
    combined_settings.update(settings)

    kwargs = dict(
        immutables=immutable,
        cache_headers=not reload,
        cors=cors,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
        template_dir=template_dir,
        plugins_dir=plugins_dir,
        static_mounts=static,
        settings=combined_settings,
        memory=memory,
        secret=secret,
        version_note=version_note,
        pdb=pdb,
        crossdb=crossdb,
        nolock=nolock,
    )

    # if files is a single directory, use that as config_dir=
    if 1 == len(files) and os.path.isdir(files[0]):
        kwargs["config_dir"] = pathlib.Path(files[0])
        files = []

    # Verify list of files, create if needed (and --create)
    for file in files:
        if not pathlib.Path(file).exists():
            if create:
                sqlite3.connect(file).execute("vacuum")
            else:
                raise click.ClickException(
                    "Invalid value for '[FILES]...': Path '{}' does not exist."
                    .format(file))

    # De-duplicate files so 'datasette db.db db.db' only attaches one /db
    files = list(dict.fromkeys(files))

    try:
        ds = Datasette(files, **kwargs)
    except SpatialiteNotFound:
        raise click.ClickException("Could not find SpatiaLite extension")
    except StartupError as e:
        raise click.ClickException(e.args[0])

    if return_instance:
        # Private utility mechanism for writing unit tests
        return ds

    # Run the "startup" plugin hooks
    asyncio.get_event_loop().run_until_complete(ds.invoke_startup())

    # Run async soundness checks - but only if we're not under pytest
    asyncio.get_event_loop().run_until_complete(check_databases(ds))

    if get:
        client = TestClient(ds)
        response = client.get(get)
        click.echo(response.text)
        exit_code = 0 if response.status == 200 else 1
        sys.exit(exit_code)
        return

    # Start the server
    url = None
    if root:
        url = "http://{}:{}{}?token={}".format(host, port,
                                               ds.urls.path("-/auth-token"),
                                               ds._root_token)
        print(url)
    if open_browser:
        if url is None:
            # Figure out most convenient URL - to table, database or homepage
            path = asyncio.get_event_loop().run_until_complete(
                initial_path_for_datasette(ds))
            url = f"http://{host}:{port}{path}"
        webbrowser.open(url)
    uvicorn_kwargs = dict(host=host,
                          port=port,
                          log_level="info",
                          lifespan="on",
                          workers=1)
    if uds:
        uvicorn_kwargs["uds"] = uds
    if ssl_keyfile:
        uvicorn_kwargs["ssl_keyfile"] = ssl_keyfile
    if ssl_certfile:
        uvicorn_kwargs["ssl_certfile"] = ssl_certfile
    uvicorn.run(ds.app(), **uvicorn_kwargs)
Example #30
0
def serve(
    files,
    immutable,
    host,
    port,
    debug,
    reload,
    cors,
    sqlite_extensions,
    inspect_file,
    metadata,
    template_dir,
    plugins_dir,
    static,
    memory,
    config,
    version_note,
    help_config,
):
    """Serve up specified SQLite database files with a web UI"""
    if help_config:
        formatter = formatting.HelpFormatter()
        with formatter.section("Config options"):
            formatter.write_dl([
                (option.name, "{} (default={})".format(option.help,
                                                       option.default))
                for option in CONFIG_OPTIONS
            ])
        click.echo(formatter.getvalue())
        sys.exit(0)
    if reload:
        import hupper

        reloader = hupper.start_reloader("datasette.cli.serve")
        reloader.watch_files(files)
        if metadata:
            reloader.watch_files([metadata.name])

    inspect_data = None
    if inspect_file:
        inspect_data = json.load(open(inspect_file))

    metadata_data = None
    if metadata:
        metadata_data = json.loads(metadata.read())

    click.echo("Serve! files={} (immutables={}) on port {}".format(
        files, immutable, port))
    ds = Datasette(
        files,
        immutables=immutable,
        cache_headers=not debug and not reload,
        cors=cors,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
        template_dir=template_dir,
        plugins_dir=plugins_dir,
        static_mounts=static,
        config=dict(config),
        memory=memory,
        version_note=version_note,
    )
    # Run async sanity checks - but only if we're not under pytest
    asyncio.get_event_loop().run_until_complete(ds.run_sanity_checks())

    # Start the server
    uvicorn.run(ds.app(), host=host, port=port, log_level="info")
Example #31
0
def main(args=sys.argv[1:]):
    if '--reload' in args:
        # start_reloader will only return in a monitored subprocess
        hupper.start_reloader('bot.main')

    client.run(TOKEN)
Example #32
0
def serve(
    files,
    immutable,
    host,
    port,
    debug,
    reload,
    cors,
    sqlite_extensions,
    inspect_file,
    metadata,
    template_dir,
    plugins_dir,
    static,
    memory,
    config,
    secret,
    root,
    version_note,
    help_config,
    return_instance=False,
):
    """Serve up specified SQLite database files with a web UI"""
    if help_config:
        formatter = formatting.HelpFormatter()
        with formatter.section("Config options"):
            formatter.write_dl([
                (option.name, "{} (default={})".format(option.help,
                                                       option.default))
                for option in CONFIG_OPTIONS
            ])
        click.echo(formatter.getvalue())
        sys.exit(0)
    if reload:
        import hupper

        reloader = hupper.start_reloader("datasette.cli.serve")
        if immutable:
            reloader.watch_files(immutable)
        if metadata:
            reloader.watch_files([metadata.name])

    inspect_data = None
    if inspect_file:
        inspect_data = json.load(open(inspect_file))

    metadata_data = None
    if metadata:
        metadata_data = parse_metadata(metadata.read())

    kwargs = dict(
        immutables=immutable,
        cache_headers=not debug and not reload,
        cors=cors,
        inspect_data=inspect_data,
        metadata=metadata_data,
        sqlite_extensions=sqlite_extensions,
        template_dir=template_dir,
        plugins_dir=plugins_dir,
        static_mounts=static,
        config=dict(config),
        memory=memory,
        secret=secret,
        version_note=version_note,
    )

    # if files is a single directory, use that as config_dir=
    if 1 == len(files) and os.path.isdir(files[0]):
        kwargs["config_dir"] = pathlib.Path(files[0])
        files = []

    ds = Datasette(files, **kwargs)

    if return_instance:
        # Private utility mechanism for writing unit tests
        return ds

    # Run the "startup" plugin hooks
    asyncio.get_event_loop().run_until_complete(ds.invoke_startup())

    # Run async sanity checks - but only if we're not under pytest
    asyncio.get_event_loop().run_until_complete(check_databases(ds))

    # Start the server
    if root:
        print("http://{}:{}/-/auth-token?token={}".format(
            host, port, ds._root_token))
    uvicorn.run(ds.app(),
                host=host,
                port=port,
                log_level="info",
                lifespan="on")
Example #33
0
    def run(self):  # pragma: no cover
        if not self.args.config_uri:
            self.out('You must give a config file')
            return 2
        config_uri = self.args.config_uri
        config_vars = parse_vars(self.args.config_vars)
        app_spec = self.args.config_uri
        app_name = self.args.app_name

        loader = self._get_config_loader(config_uri)
        loader.setup_logging(config_vars)

        self.pserve_file_config(loader, global_conf=config_vars)

        server_name = self.args.server_name
        if self.args.server:
            server_spec = 'egg:pyramid'
            assert server_name is None
            server_name = self.args.server
        else:
            server_spec = app_spec

        server_loader = loader
        if server_spec != app_spec:
            server_loader = self.get_config_loader(server_spec)

        # do not open the browser on each reload so check hupper first
        if self.args.browser and not hupper.is_active():
            url = self.open_url

            if not url:
                url = self.guess_server_url(server_loader, server_name,
                                            config_vars)

            if not url:
                self.out('WARNING: could not determine the server\'s url to '
                         'open the browser. To fix this set the "open_url" '
                         'setting in the [pserve] section of the '
                         'configuration file.')

            else:

                def open_browser():
                    time.sleep(1)
                    webbrowser.open(url)

                t = threading.Thread(target=open_browser)
                t.setDaemon(True)
                t.start()

        if self.args.reload and not hupper.is_active():
            if self.args.verbose > 1:
                self.out('Running reloading file monitor')
            hupper.start_reloader('pyramid.scripts.pserve.main',
                                  reload_interval=int(
                                      self.args.reload_interval),
                                  verbose=self.args.verbose,
                                  worker_kwargs=self.worker_kwargs)
            return 0

        config_path = self.get_config_path(loader)
        self.watch_files.add(config_path)

        server_path = self.get_config_path(server_loader)
        self.watch_files.add(server_path)

        if hupper.is_active():
            reloader = hupper.get_reloader()
            reloader.watch_files(list(self.watch_files))

        server = server_loader.get_wsgi_server(server_name, config_vars)

        app = loader.get_wsgi_app(app_name, config_vars)

        if self.args.verbose > 0:
            if hasattr(os, 'getpid'):
                msg = 'Starting server in PID %i.' % os.getpid()
            else:
                msg = 'Starting server.'
            self.out(msg)

        try:
            server(app)
        except (SystemExit, KeyboardInterrupt) as e:
            if self.args.verbose > 1:
                raise
            if str(e):
                msg = ' ' + str(e)
            else:
                msg = ''
            self.out('Exiting%s (-v to see traceback)' % msg)