Exemple #1
0
def init_websauna(config_uri: str, sanity_check: bool=False) -> Request:
    """Initialize Websauna WSGI application for a command line oriented script.

    :param config_uri: Path to config INI file

    :param sanity_check: Perform database sanity check on start

    :return: Dummy request object pointing to a site root, having registry and every configured.
    """

    monkey_patch_paster_config_parser()

    setup_logging(config_uri)

    # Paster thinks we are a string
    if sanity_check:
        sanity_check = "true"
    else:
        sanity_check = "false"

    bootstrap_env = bootstrap(config_uri, options=dict(sanity_check=sanity_check))
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    request = pyramid_env["request"]

    # Export application object for testing
    request.app = app

    return pyramid_env["request"]
Exemple #2
0
def ini_settings(request, test_config_path) -> dict:
    """Load INI settings for test run from py.test command line.

    Example:

         py.test yourpackage -s --ini=test.ini


    :return: A dictionary representing the key/value pairs in an ``app`` section within the file represented by ``config_uri``
    """

    # This enables our INI inclusion mechanism
    # TODO: Don't use get_appsettings() from paster, but create a INI includer compatible version
    from websauna.utils.configincluder import monkey_patch_paster_config_parser
    monkey_patch_paster_config_parser()

    # Setup Python logging from the INI
    setup_logging(test_config_path)

    # Read [app] section
    config = get_appsettings(test_config_path)

    # To pass the config filename itself forward
    config["_ini_file"] = test_config_path

    return config
Exemple #3
0
def ini_settings(request, test_config_path) -> dict:
    """Load INI settings for test run from py.test command line.

    Example:

         py.test yourpackage -s --ini=test.ini


    :return: A dictionary representing the key/value pairs in an ``app`` section within the file represented by ``config_uri``
    """

    # This enables our INI inclusion mechanism
    # TODO: Don't use get_appsettings() from paster, but create a INI includer compatible version
    from websauna.utils.configincluder import monkey_patch_paster_config_parser
    monkey_patch_paster_config_parser()

    # Setup Python logging from the INI
    setup_logging(test_config_path)

    # Read [app] section
    config = get_appsettings(test_config_path)

    # To pass the config filename itself forward
    config["_ini_file"] = test_config_path

    return config
Exemple #4
0
def init_websauna_script_env(config_uri: str) -> dict:
    """Initialize Websauna WSGI application for a IPython notebook.

    :param config_uri: Path to config INI file

    :return: Dictionary of shell variables
    """

    monkey_patch_paster_config_parser()

    setup_logging(config_uri)

    bootstrap_env = bootstrap(config_uri, options=dict(sanity_check=False))
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    registry = initializer.config.registry
    dbsession = create_dbsession(registry)

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    pyramid_env["app"] = app
    pyramid_env["initializer"] = initializer

    # Websauna specific
    # Set up the request with websauna.site_url setting as the base URL
    request = make_routable_request(dbsession, registry)
    pyramid_env["request"] = request
    pyramid_env["dbsession"] = dbsession

    return pyramid_env
Exemple #5
0
def init_websauna(config_uri: str, sanity_check: bool = False) -> Request:
    """Initialize Websauna WSGI application for a command line oriented script.

    :param config_uri: Path to config INI file

    :param sanity_check: Perform database sanity check on start

    :return: Dummy request object pointing to a site root, having registry and every configured.
    """

    monkey_patch_paster_config_parser()

    setup_logging(config_uri)

    # Paster thinks we are a string
    if sanity_check:
        sanity_check = "true"
    else:
        sanity_check = "false"

    bootstrap_env = bootstrap(config_uri,
                              options=dict(sanity_check=sanity_check))
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    request = pyramid_env["request"]

    # Export application object for testing
    request.app = app

    return pyramid_env["request"]
Exemple #6
0
def ini_settings(request) -> dict:
    """Load INI settings for test run from py.test command line.

    Example:

         py.test yourpackage -s --ini=test.ini


    :return: Adictionary representing the key/value pairs in an ``app`` section within the file represented by ``config_uri``
    """

    if not getattr(request.config.option, "ini", None):
        raise RuntimeError("You need to give --ini test.ini command line option to py.test to find our test settings")

    from websauna.utils.configincluder import monkey_patch_paster_config_parser
    monkey_patch_paster_config_parser()

    config_uri = os.path.abspath(request.config.option.ini)
    setup_logging(config_uri)
    config = get_appsettings(config_uri)

    # To pass the config filename itself forward
    config["_ini_file"] = config_uri

    return config
Exemple #7
0
def init_websauna(config_uri: str,
                  sanity_check: bool = False,
                  console_app=False,
                  extra_options=None) -> Request:
    """Initialize Websauna WSGI application for a command line oriented script.

    :param config_uri: Path to config INI file

    :param sanity_check: Perform database sanity check on start

    :param console_app: Set true to setup console-mode logging. See :func:`setup_console_logging`

    :param extra_options: Passed through bootstrap() and is available as :attr:`websauna.system.Initializer.global_options`.

    :return: Faux Request object pointing to a site root, having registry and every configured.
    """

    monkey_patch_paster_config_parser()

    if console_app:
        setup_console_logging()
    else:
        setup_logging(config_uri)

    # Paster thinks we are a string
    if sanity_check:
        sanity_check = "true"
    else:
        sanity_check = "false"

    options = {"sanity_check": sanity_check}

    if extra_options:
        options.update(extra_options)

    bootstrap_env = bootstrap(config_uri, options=options)
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    request = pyramid_env["request"]

    # Request needs a transaction manager
    # NOTE: I'd like to use new TransactionManager() here,
    # but a lot of legacy command line apps rely on transaction.manager thread local
    import transaction  # Delayed import to avoid issues with gevent
    request.tm = transaction.manager

    # Export application object for test suites
    request.app = app

    return pyramid_env["request"]
Exemple #8
0
    def configure_scheduler(self):
        """Configure Celery."""

        # Patch pyramid_celery to use our config loader
        import websauna.system.task.celery

        # Patch various paster internals
        from websauna.utils.configincluder import monkey_patch_paster_config_parser
        monkey_patch_paster_config_parser()
        self.config.include("pyramid_celery")

        self.config.configure_celery(self.global_config["__file__"])

        self.celery = websauna.system.task.celery.celery_app
Exemple #9
0
    def configure_scheduler(self):
        """Configure Celery."""

        # Patch pyramid_celery to use our config loader
        import websauna.system.task.celery

        # Patch various paster internals
        from websauna.utils.configincluder import monkey_patch_paster_config_parser
        monkey_patch_paster_config_parser()
        self.config.include("pyramid_celery")

        self.config.configure_celery(self.global_config["__file__"])

        self.celery = websauna.system.task.celery.celery_app
Exemple #10
0
def main(argv=sys.argv):
    monkey_patch_paster_config_parser()

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # Print out the connection URL with the password masked out
    request = init_websauna(config_uri)
    url = request.registry.settings.get("sqlalchemy.url")

    engine = request.dbsession.get_bind()
    print("Connecting to {}".format(engine))

    os.system("pgcli {}".format(url))
Exemple #11
0
def init_websauna(config_uri: str, sanity_check: bool=False, console_app=False, extra_options=None) -> Request:
    """Initialize Websauna WSGI application for a command line oriented script.

    :param config_uri: Path to config INI file

    :param sanity_check: Perform database sanity check on start

    :param console_app: Set true to setup console-mode logging. See :func:`setup_console_logging`

    :param extra_options: Passed through bootstrap() and is available as :attr:`websauna.system.Initializer.global_options`.

    :return: Faux Request object pointing to a site root, having registry and every configured.
    """

    monkey_patch_paster_config_parser()

    if console_app:
        setup_console_logging()
    else:
        setup_logging(config_uri)

    # Paster thinks we are a string
    if sanity_check:
        sanity_check = "true"
    else:
        sanity_check = "false"

    options = {
        "sanity_check": sanity_check
    }

    if extra_options:
        options.update(extra_options)

    bootstrap_env = bootstrap(config_uri, options=options)
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    request = pyramid_env["request"]

    # Export application object for testing
    request.app = app

    return pyramid_env["request"]
Exemple #12
0
def main(argv=sys.argv):
    monkey_patch_paster_config_parser()

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # Print out the connection URL with the password masked out
    request = init_websauna(config_uri)
    url = request.registry.settings.get("sqlalchemy.url")

    engine = request.dbsession.get_bind()
    print("Connecting to {}".format(engine))

    if not which("pgcli"):
        sys.exit("pgcli not installed - please install websauna as pip install websauna[dev] to get this dependency")

    os.system("pgcli {}".format(url))
Exemple #13
0
def ini_settings(request, test_config_path) -> dict:
    """Load INI settings for test run from py.test command line.

    Example:

         py.test yourpackage -s --ini=test.ini


    :return: A dictionary representing the key/value pairs in an ``app`` section within the file represented by ``config_uri``
    """

    from websauna.utils.configincluder import monkey_patch_paster_config_parser
    monkey_patch_paster_config_parser()

    setup_logging(test_config_path)
    config = get_appsettings(test_config_path)

    # To pass the config filename itself forward
    config["_ini_file"] = test_config_path

    return config
Exemple #14
0
def main(argv=sys.argv):
    monkey_patch_paster_config_parser()

    if len(argv) < 2:
        usage(argv)

    config_uri = argv[1]

    # Print out the connection URL with the password masked out
    request = init_websauna(config_uri)
    url = request.registry.settings.get("sqlalchemy.url")

    engine = request.dbsession.get_bind()
    print("Connecting to {}".format(engine))

    if not which("pgcli"):
        sys.exit(
            "pgcli not installed - please install websauna as pip install websauna[utils] to get this dependency"
        )

    os.system("pgcli {}".format(url))
Exemple #15
0
def init_websauna_script_env(config_uri: str) -> dict:
    """Initialize Websauna WSGI application for a IPython notebook.

    :param config_uri: Path to config INI file

    :return: Dictionary of shell variables
    """

    monkey_patch_paster_config_parser()

    setup_logging(config_uri)

    bootstrap_env = bootstrap(config_uri, options=dict(sanity_check=False))
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    pyramid_env["app"] = app
    pyramid_env["initializer"] = initializer

    return pyramid_env
Exemple #16
0
def init_websauna(config_uri) -> Request:
    """Initialize Websauna WSGI application for a command line oriented script.

    :return: Dummy request object pointing to a site root, having registry and every configured.
    """

    monkey_patch_paster_config_parser()

    setup_logging(config_uri)

    bootstrap_env = bootstrap(config_uri, options=dict(sanity_check=False))
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    request = pyramid_env["request"]

    # Export application object for testing
    request.app = app

    return pyramid_env["request"]
Exemple #17
0
def init_websauna_script_env(config_uri: str) -> dict:
    """Initialize Websauna WSGI application for a IPython notebook.

    :param config_uri: Path to config INI file

    :return: Dictionary of shell variables
    """

    monkey_patch_paster_config_parser()

    setup_logging(config_uri)

    bootstrap_env = bootstrap(config_uri, options=dict(sanity_check=False))
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    pyramid_env = scripting.prepare(registry=app.initializer.config.registry)
    pyramid_env["app"] = app
    pyramid_env["initializer"] = initializer

    return pyramid_env
Exemple #18
0
"""Config file includer aware wrapper for pserve."""
import sys
from pkg_resources import load_entry_point

from websauna.utils.configincluder import monkey_patch_paster_config_parser
from websauna.utils.configincluder import IncludeAwareConfigParser

monkey_patch_paster_config_parser()

def main():
    sys.exit(
        load_entry_point('pyramid', 'console_scripts', 'pserve')()
    )
Exemple #19
0
def init_websauna(config_uri: str,
                  sanity_check: bool = False,
                  console_app=False,
                  extra_options=None) -> Request:
    """Initialize Websauna WSGI application for a command line oriented script.

    Example:

    .. code-block:: python

        import sys
        from websauna.system.devop.cmdline import init_websauna

        config_uri = sys.argv[1]
        request = init_websauna(config_uri)

    :param config_uri: Path to config INI file

    :param sanity_check: Perform database sanity check on start

    :param console_app: Set true to setup console-mode logging. See :func:`setup_console_logging`

    :param extra_options: Passed through bootstrap() and is available as :attr:`websauna.system.Initializer.global_options`.

    :return: Faux Request object pointing to a site root, having registry and every configured.
    """

    monkey_patch_paster_config_parser()

    if console_app:
        setup_console_logging()
    else:
        setup_logging(config_uri)

    # Paster thinks we are a string
    if sanity_check:
        sanity_check = "true"
    else:
        sanity_check = "false"

    options = {"sanity_check": sanity_check}

    if extra_options:
        options.update(extra_options)

    bootstrap_env = bootstrap(config_uri, options=options)
    app = bootstrap_env["app"]
    initializer = getattr(app, "initializer", None)
    assert initializer is not None, "Configuration did not yield to Websauna application with Initializer set up"

    registry = initializer.config.registry
    dbsession = create_dbsession(registry)

    # Set up the request with websauna.site_url setting as the base URL
    request = make_routable_request(dbsession, registry)

    # This exposes the app object for the integration tests e.g test_static_asset
    # TODO: Find a cleaner way to do this
    request.app = app

    return request
Exemple #20
0
from websauna.utils.configincluder import monkey_patch_paster_config_parser
monkey_patch_paster_config_parser()

import os
import sys

import transaction

from websauna.system.devop.cmdline import init_websauna
from websauna.system.model.meta import Base


def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> [ARG1, ARG2...] \n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):

    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]

    request = init_websauna(config_uri)
    with transaction.manager:
        engine = request.dbsession.get_bind()

        # Always enable UUID extension for PSQL
        # TODO: Convenience for now, because we assume UUIDs, but make this somehow configurable