def prerun_start(task_id, task, signal, sender, args, kwargs): """ Populates kwargs A context_uid or context_path will be resolved as context instead. """ logger.debug('signal prerun for %r' % task_id) task_env = prepare() request = task_env['request'] if hasattr(request, 'set_celery_userid'): if 'authenticated_userid' in kwargs: request.set_celery_userid(kwargs['authenticated_userid']) logger.debug("authenticated_userid set to %s", kwargs['authenticated_userid']) else: logger.debug( "authenticated_userid wasn't sent as a kwarg to task - so it will be None." ) root = request.root context = None context_uid = kwargs.pop('context_uid', None) if context_uid: context = request.resolve_uid(context_uid) if context: request.context = context else: request.context = root logger.debug('context is %r' % context) kwargs.update(root=root, request=request) if context: kwargs['context'] = context transaction.begin()
def __call__(self, *args, **kwargs): # Prepare the pyramid environment. if 'pyramid_config' in self.app.conf: pyramid_config = self.app.conf['pyramid_config'] env = prepare(registry=pyramid_config.registry) # noqa # Now run the original... return super(PyramidAwareTask, self).__call__(*args, **kwargs)
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"]
def extension(args, console, settings): """Build the browser extensions. The first argument is the base URL of an h installation: http://localhost:5000 An optional second argument can be used to specify the location for static assets. Examples: http://static.example.com/ chrome-extension://extensionid/public """ if len(args) == 1: console.error('You must supply a url to the hosted backend.') return 2 elif len(args) == 2: assets_url = settings['webassets.base_url'] else: settings['webassets.base_url'] = args[2] assets_url = args[2] base_url = args[1] # Fully-qualify the static asset url parts = urlparse(assets_url) if not parts.netloc: base = urlparse(base_url) parts = (base.scheme, base.netloc, parts.path, parts.params, parts.query, parts.fragment) assets_url = urlunparse(parts) # Set up the assets url and source path mapping settings['webassets.base_dir'] = abspath('./build/chrome/public') settings['webassets.base_url'] = assets_url settings['webassets.paths'] = json.dumps( {resolve('h:').abspath(): assets_url}) # Remove any existing build if exists('./build/chrome'): rmtree('./build/chrome') # Copy over all the assets assets(settings) makedirs('./build/chrome/public/lib/images') merge('./pdf.js/build/chromium', './build/chrome') merge('./h/browser/chrome', './build/chrome') merge('./h/images', './build/chrome/public/images') merge('./h/lib/images', './build/chrome/public/lib/images') config = Configurator(settings=settings) config.include('h') config.add_subscriber(add_base_url, BeforeRender) config.commit() # Build it request = Request.blank('/app', base_url=base_url) chrome(prepare(registry=config.registry, request=request))
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
def check_sw_versions(event = None, env = None): """ Make sure software and database versions are up to date. """ #This should be changed into something more sensible. #Env vars? from sys import argv script_names = ['bin/arche', 'bin/evolver', 'bin/pshell'] if argv[0] in script_names: return if env is None: from pyramid.scripting import prepare env = prepare() root = env['root'] if not IRoot.providedBy(root): logger.info("Root object is %r, so check_sw_versions won't run", root) return registry = env['registry'] names = set() for ar in registry.registeredAdapters(): if ar.provided == IEvolver: names.add(ar.name) needed = set() for name in names: evolver = registry.getAdapter(root, IEvolver, name = name) logger.debug("Evolver '%s': DB ver: %s Software version: %s", name, evolver.db_version, evolver.sw_version) if evolver.needs_upgrade: needed.add(evolver.name) if needed: msg = "The following packages aren't up to date: '%s'\n" % "', '".join(needed) msg += "Run 'bin/arche <your paster ini> evolve <package name>' to upgrade" raise EvolverVersionError(msg) env['closer']()
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 """ options = {"sanity_check": False} app = get_wsgi_app(config_uri, defaults=options) initializer = initializer_from_app(app) 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
def test_path_from_view(self, spec): def hi_request(request): return Response("Hi") with Configurator() as config: config.add_route("hi", "/hi") config.add_view(hi_request, route_name="hi") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths( spec, "hi", operations={ "get": { "parameters": [], "responses": { "200": "..params.." } } }, ) assert "/hi" in spec._paths assert "get" in spec._paths["/hi"] expected = {"parameters": [], "responses": {"200": "..params.."}} assert spec._paths["/hi"]["get"] == expected
def get_pyramid_request(self): if not hasattr(self.request, 'pyramid_env'): registry = self.app.pyramid_config.registry env = prepare(registry=registry) self.request.update(pyramid_env=env) return self.request.pyramid_env["request"]
def test_path_with_multiple_methods(self, spec): def hi_request(request): return Response("Hi") with Configurator() as config: config.add_route("hi", "/hi") config.add_view(hi_request, route_name="hi", request_method=["get", "post"]) config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths( spec, "hi", operations=dict( get={ "description": "get a greeting", "responses": { "200": "..params.." }, }, post={ "description": "post a greeting", "responses": { "200": "..params.." }, }, ), ) get_op = spec._paths["/hi"]["get"] post_op = spec._paths["/hi"]["post"] assert get_op["description"] == "get a greeting" assert post_op["description"] == "post a greeting"
def __call__(self, *args, **kwargs): # Prepare the pyramid environment. if 'pyramid_config' in self.app.conf: pyramid_config = self.app.conf['pyramid_config'] env = prepare(registry=pyramid_config.registry) # Now run the original... return super(PyramidAwareTask, self).__call__(*args, **kwargs)
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"]
def _make_fake_request(discussion): from pyramid.scripting import _make_request, prepare request = None if discussion: request = _make_request('/debate/' + discussion.slug) request.matchdict = {'discussion_slug': discussion.slug} r = prepare(request) return r['request'], r['closer']
def __call__(self, *args, **kwargs): registry = self.app.pyramid_config.registry pyramid_env = scripting.prepare(registry=registry) try: return super().__call__(pyramid_env["request"], *args, **kwargs) finally: pyramid_env["closer"]()
def extension(args, console, settings): """Build the browser extensions. The first argument is the base URL of an h installation: http://localhost:5000 An optional second argument can be used to specify the location for static assets. Examples: http://static.example.com/ chrome-extension://extensionid/public """ if len(args) == 1: console.error('You must supply a url to the hosted backend.') return 2 elif len(args) == 2: assets_url = settings['webassets.base_url'] else: settings['webassets.base_url'] = args[2] assets_url = args[2] base_url = args[1] # Fully-qualify the static asset url parts = urlparse(assets_url) if not parts.netloc: base = urlparse(base_url) parts = (base.scheme, base.netloc, parts.path, parts.params, parts.query, parts.fragment) assets_url = urlunparse(parts) # Set up the assets url and source path mapping settings['webassets.base_dir'] = abspath('./build/chrome/public') settings['webassets.base_url'] = assets_url settings['webassets.paths'] = json.dumps({ resolve('h:static').abspath(): assets_url }) # Turn off the webassets cache and manifest settings['webassets.cache'] = None settings['webassets.manifest'] = None config = Configurator(settings=settings) config.include('h') config.add_subscriber(add_base_url, BeforeRender) config.commit() # Build it request = Request.blank('/app', base_url=base_url) chrome(prepare(registry=config.registry, request=request)) # XXX: Change when webassets allows setting the cache option # As of 0.10 it's only possible to pass a sass config with string values rmtree('./build/chrome/public/.sass-cache')
def create_app_env(apps=None, pyramid_settings=None): global_config = { 'apps': '\n'.join(apps or []), } settings = pyramid_settings or {} settings['testing'] = True wsgi_app = simple_app(global_config, **settings) env = prepare() env['app'] = wsgi_app return env
def _create_app_env(data_root, apps=None): settings = { 'storage.data_root': data_root, } if apps: settings['pyramid.includes'] = apps wsgi_app = main({'testing': True}, **settings) env = prepare() env['app'] = wsgi_app return env
def app_request(app): """ A real request. This request is almost identical to a real request but it has some drawbacks in tests as it's harder to mock data and is heavier. """ with prepare(registry=app.registry) as env: request = env['request'] request.host = 'example.com' yield request
def main(argv=sys.argv): args = parser.parse_args(argv[1:]) loader = Loader(args.config_uri) dictConfig(loader.logging_config(args.appname)) app = loader.load_app(args.appname) env = prepare(registry=app.registry) try: do_fetch(env['request']) finally: env['closer']()
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"]
def test_path_is_translated_to_swagger_template(self, spec): def get_pet(pet_id): return "representation of pet {pet_id}".format(pet_id=pet_id) with Configurator() as config: config.add_route("pet", "/pet/{pet_id}") config.add_view(get_pet, route_name="pet") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "pet") assert "/pet/{pet_id}" in spec._paths
def task_prerun_signal(task_id, task, args, kwargs, **kwaargs): if hasattr(celery, "pyramid"): env = celery.pyramid env = prepare(registry=env["request"].registry) proper_base_url = env["request"].registry.settings["mailing.app_url"] tmp_req = Request.blank("/", base_url=proper_base_url) # ensure tasks generate url for right domain from config env["request"].environ["HTTP_HOST"] = tmp_req.environ["HTTP_HOST"] env["request"].environ["SERVER_PORT"] = tmp_req.environ["SERVER_PORT"] env["request"].environ["SERVER_NAME"] = tmp_req.environ["SERVER_NAME"] env["request"].environ["wsgi.url_scheme"] = tmp_req.environ["wsgi.url_scheme"] get_current_request().tm.begin()
def __init__(self, config_file, app_env, server_env): self.config_file = config_file if not os.path.isfile(config_file): click.echo("%s was not found, please create it or use -c" % config_file) sys.exit(1) self.app_env = app_env self.server_env = server_env self.app = safe_load_app(config_file, name=app_env) self.pyramid_env = prepare()
def test_autodoc_off_empty(self, spec): def hi_request(request): return Response("Hi") with Configurator() as config: config.add_route("hi", "/hi") config.add_view(hi_request, route_name="hi") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "hi", autodoc=False) assert "/hi" in spec._paths assert not spec._paths["/hi"].keys()
def test_routes_with_regex(self, spec): def get_pet(pet_id): return "" with Configurator() as config: config.add_route("pet", "/pet/{pet_id:\d+}") config.add_view(get_pet, route_name="pet") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "pet") assert "/pet/{pet_id}" in spec._paths
def task_prerun_signal(task_id, task, args, kwargs, **kwaargs): if hasattr(celery, 'pyramid'): env = celery.pyramid env = prepare(registry=env['request'].registry) proper_base_url = env['request'].registry.settings['mailing.app_url'] tmp_req = Request.blank('/', base_url=proper_base_url) # ensure tasks generate url for right domain from config env['request'].environ['HTTP_HOST'] = tmp_req.environ['HTTP_HOST'] env['request'].environ['SERVER_PORT'] = tmp_req.environ['SERVER_PORT'] env['request'].environ['SERVER_NAME'] = tmp_req.environ['SERVER_NAME'] env['request'].environ['wsgi.url_scheme'] = \ tmp_req.environ['wsgi.url_scheme'] get_current_request().tm.begin()
def __init__(self, config_file, app_env, server_env): self.config_file = config_file if not os.path.isfile(config_file): click.echo( "%s was not found, please create it or use -c" % config_file ) sys.exit(1) self.app_env = app_env self.server_env = server_env self.app = safe_load_app(config_file, name=app_env) self.pyramid_env = prepare()
def bootstrap(config_uri, request=None, options=None): """ Load a WSGI application from the PasteDeploy config file specified by ``config_uri``. The environment will be configured as if it is currently serving ``request``, leaving a natural environment in place to write scripts that can generate URLs and utilize renderers. This function returns a dictionary with ``app``, ``root``, ``closer``, ``request``, and ``registry`` keys. ``app`` is the WSGI app loaded (based on the ``config_uri``), ``root`` is the traversal root resource of the Pyramid application, and ``closer`` is a parameterless callback that may be called when your script is complete (it pops a threadlocal stack). .. note:: Most operations within :app:`Pyramid` expect to be invoked within the context of a WSGI request, thus it's important when loading your application to anchor it when executing scripts and other code that is not normally invoked during active WSGI requests. .. note:: For a complex config file containing multiple :app:`Pyramid` applications, this function will setup the environment under the context of the last-loaded :app:`Pyramid` application. You may load a specific application yourself by using the lower-level functions :meth:`pyramid.paster.get_app` and :meth:`pyramid.scripting.prepare` in conjunction with :attr:`pyramid.config.global_registries`. ``config_uri`` -- specifies the PasteDeploy config file to use for the interactive shell. The format is ``inifile#name``. If the name is left off, ``main`` will be assumed. ``request`` -- specified to anchor the script to a given set of WSGI parameters. For example, most people would want to specify the host, scheme and port such that their script will generate URLs in relation to those parameters. A request with default parameters is constructed for you if none is provided. You can mutate the request's ``environ`` later to setup a specific host/port/scheme/etc. ``options`` Is passed to get_app for use as variable assignments like {'http_port': 8080} and then use %(http_port)s in the config file. See :ref:`writing_a_script` for more information about how to use this function. """ app = get_app(config_uri, options=options) env = prepare(request) env['app'] = app return env
def get_routes(self, config_file, path_blacklist=None, path_whitelist=None, module_blacklist=None, module_whitelist=None): app = load_app(config_file) env = prepare() registry = env['registry'] config = Configurator(registry=registry) mapper = config.get_routes_mapper() try: # only supported in pyramid 1.6 routes = mapper.get_routes(include_static=True) except: routes = mapper.get_routes() mapped_routes = [] for route in routes: route_data = get_route_data(route, registry) for name, pattern, view, method, docs, src in route_data: if path_blacklist: if self.matches_pattern(path_blacklist, pattern): continue if path_whitelist: if not self.matches_pattern(path_whitelist, pattern): continue if module_blacklist: if self.matches_pattern(module_blacklist, view): continue if module_whitelist: if not self.matches_pattern(module_whitelist, view): continue mapped_routes.append({ 'name': name, 'pattern': pattern, 'view': view, 'method': method, 'docs': trim(docs), 'view_module': src.get('module_name'), 'view_callable': src.get('callable_name'), 'source_lines': src.get('source_lines'), }) return mapped_routes
def task_prerun_signal(task_id, task, args, kwargs, **kwaargs): if hasattr(celery, "pyramid"): env = celery.pyramid env = prepare(registry=env["request"].registry) proper_base_url = env["request"].registry.settings["base_url"] tmp_request = Request.blank("/", base_url=proper_base_url) # ensure tasks generate url for right domain from config env["request"].environ["HTTP_HOST"] = tmp_request.environ["HTTP_HOST"] env["request"].environ["SERVER_PORT"] = tmp_request.environ["SERVER_PORT"] env["request"].environ["SERVER_NAME"] = tmp_request.environ["SERVER_NAME"] env["request"].environ["wsgi.url_scheme"] = tmp_request.environ[ "wsgi.url_scheme" ] get_current_request().tm.begin()
def test_api_prefix(self, spec): def api_routes(config): config.add_route("pet", "/pet/{pet_id}") def get_pet(pet_id): return "representation of pet {pet_id}".format(pet_id=pet_id) with Configurator() as config: config.include(api_routes, route_prefix="/api/v1/") config.add_view(get_pet, route_name="pet") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "pet") assert "/api/v1/pet/{pet_id}" in spec._paths
def test_autodoc_on_method(self, spec): def hi_request(request): return Response("Hi") with Configurator() as config: config.add_route("hi", "/hi") config.add_view(hi_request, route_name="hi", request_method="GET") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "hi") assert "/hi" in spec._paths assert "get" in spec._paths["/hi"] assert list(spec._paths["/hi"].keys()) == ["get"] expected = {"responses": {}} assert spec._paths["/hi"]["get"] == expected
def app_request(app, tm): """ A real request. This request is almost identical to a real request but it has some drawbacks in tests as it's harder to mock data and is heavier. """ env = prepare(registry=app.registry) request = env['request'] request.host = 'example.com' request.tm = tm yield request env['closer']()
def handler_inner(*args, **kwargs): from alchemist import scheduler pyramid_env = scripting.prepare(registry=scheduler.pyramid_registry) try: def handler_func(request): return func(request, *args, **kwargs) handler = tm_tween_factory(handler_func, pyramid_env["registry"]) result = handler(pyramid_env["request"]) return result except Exception: logging.error('Error in handling scheduled task', exc_info=1) finally: pyramid_env["closer"]()
def main(argv=sys.argv): if len(argv) < 3: usage(argv) config_uri = argv[1] appname = argv[2] loader = Loader(config_uri) dictConfig(loader.logging_config(appname)) app = loader.load_app(appname) env = prepare(registry=app.registry) env['app'] = app try: engine = env['request'].db_session.bind Base.metadata.create_all(engine) finally: env['closer']()
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"]
def test_integration_with_docstring_introspection(self, spec): def hello(): """A greeting endpoint. --- x-extension: value get: description: get a greeting responses: 200: description: a pet to be returned schema: $ref: #/definitions/Pet post: description: post a greeting responses: 200: description:some data foo: description: not a valid operation responses: 200: description: more junk """ return "hi" with Configurator() as config: config.add_route("hello", "/hello") config.add_view(hello, route_name="hello") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "hello") get_op = spec._paths["/hello"]["get"] post_op = spec._paths["/hello"]["post"] extension = spec._paths["/hello"]["x-extension"] assert get_op["description"] == "get a greeting" assert post_op["description"] == "post a greeting" assert "foo" not in spec._paths["/hello"] assert extension == "value"
def __call__(self, *args, **kwargs): registry = self.app.pyramid_config.registry pyramid_env = scripting.prepare(registry=registry) try: underlying = super().__call__ if getattr(self, "pyramid", True): def handler(request): return underlying(request, *args, **kwargs) else: def handler(request): return underlying(*args, **kwargs) handler = tm_tween_factory(handler, pyramid_env["registry"]) result = handler(pyramid_env["request"]) finally: pyramid_env["closer"]() return result
def app_request(app, tm, dbsession): """ A real request. This request is almost identical to a real request but it has some drawbacks in tests as it's harder to mock data and is heavier. """ with prepare(registry=app.registry) as env: request = env["request"] request.host = "example.com" # without this, request.dbsession will be joined to the same transaction # manager but it will be using a different sqlalchemy.orm.Session using # a separate database transaction request.dbsession = dbsession request.tm = tm yield request
def test_autodoc_on(self, spec): def hi_request(request): return Response("Hi") with Configurator() as config: config.add_route("hi", "/hi") config.add_view(hi_request, route_name="hi") config.make_wsgi_app() with prepare(config.registry): add_pyramid_paths(spec, "hi") assert "/hi" in spec._paths assert "get" in spec._paths["/hi"] assert "head" in spec._paths["/hi"] assert "post" in spec._paths["/hi"] assert "put" in spec._paths["/hi"] assert "patch" in spec._paths["/hi"] assert "delete" in spec._paths["/hi"] assert "options" in spec._paths["/hi"] expected = {"responses": {}} assert spec._paths["/hi"]["get"] == expected
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"]
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
def extension(args, console, settings): """Build the browser extensions. The first argument is the base URL of an h installation: http://localhost:5000 An optional second argument can be used to specify the location for static assets. Examples: http://static.example.com/ chrome-extension://extensionid/public """ if len(args) == 1: console.error('You must supply a url to the hosted backend.') return 2 elif len(args) == 2: assets_url = settings['webassets.base_url'] else: settings['webassets.base_url'] = args[2] assets_url = args[2] base_url = args[1] # Fully-qualify the static asset url parts = urlparse(assets_url) if not parts.netloc: base = urlparse(base_url) parts = (base.scheme, base.netloc, parts.path, parts.params, parts.query, parts.fragment) assets_url = urlunparse(parts) # Set up the assets url and source path mapping settings['webassets.base_dir'] = abspath('./build/chrome/public') settings['webassets.base_url'] = assets_url settings['webassets.paths'] = json.dumps({ resolve('h:').abspath(): assets_url }) # Remove any existing build if exists('./build/chrome'): rmtree('./build/chrome') # Copy over all the assets assets(settings) makedirs('./build/chrome/public/lib/images') merge('./pdf.js/build/chromium', './build/chrome') merge('./h/browser/chrome', './build/chrome') merge('./h/images', './build/chrome/public/images') merge('./h/lib/images', './build/chrome/public/lib/images') config = Configurator(settings=settings) config.include('h') config.add_subscriber(add_base_url, BeforeRender) config.commit() # Build it request = Request.blank('/app', base_url=base_url) chrome(prepare(registry=config.registry, request=request))
def check_catalog_on_startup(event = None, env = None): """ Check that the catalog has all required indexes and that they're up to date. Ment to be run by the IApplicationCreated event. """ def _commit_and_cleanup(closer, commit=False, registry=None): if commit: import transaction transaction.commit() _unregister_index_utils(registry) closer() #Split this into other functions? #This should be changed into something more sensible. #Env vars? from sys import argv #FIXME: This makes arche unusable on windows, or if someone types #"./bin/arche" this won't work. script_names = ['bin/arche', 'bin/pshell'] if argv[0] in script_names: return _unregister_index_utils() if env is None: from pyramid.scripting import prepare env = prepare() root = env['root'] reg = env['registry'] if not IRoot.providedBy(root): logger.info("Root object is %r, so check_catalog_on_startup won't run" % root) return _commit_and_cleanup(env['closer'], commit=False, registry=reg) catalog = root.catalog registered_keys = {} index_needs_indexing = [] for util in reg.getAllUtilitiesRegisteredFor(ICatalogIndexes): for (key, index) in util.items(): if key in registered_keys: raise CatalogConfigError("Both %r and %r tried to add the same key %r" % (util.name, registered_keys[key], key)) registered_keys[key] = util.name if key not in catalog: if key == 'type_name': raise CatalogConfigError( "'type_name' was missing in the catalog." "This shouldn't happen, so you need to to a full reindex with:\n" "arche <paster.ini> create_catalog && arche <paster.ini> reindex_catalog") logger.warn("%r requires the index %r, will add it and run index operation", util.name, key) index_needs_indexing.append(key) catalog[key] = index continue if catalog[key].discriminator != index.discriminator: logger.warn("%r exists, but the discriminator has changed. " "It will need to be readded and reindexed.", key) del catalog[key] index_needs_indexing.append(key) catalog[key] = index request = env['request'] # Clean up unused indexes indexes_to_remove = set() for key in catalog: if key not in registered_keys: indexes_to_remove.add(key) for key in indexes_to_remove: logger.warn("Removing catalog index '%s' since it's not registered anywhere.", key) del catalog[key] # Finally reindex any that needs reindexing if index_needs_indexing: quick_reindex(request, index_needs_indexing) _commit_and_cleanup(env['closer'], commit=bool(index_needs_indexing or indexes_to_remove), registry=reg)
def get_env(self): registry = self.app.conf.PYRAMID_REGISTRY base_url = registry.settings["websauna.site_url"] request = Request.blank("/", base_url=base_url) env = scripting.prepare(request=request, registry=registry) return env
def extension(args, console, settings): """Build the browser extensions. The first argument is the target browser for which to build the extension. Choices are: chrome or firefox. The second argument is the base URL of an h installation: http://localhost:5000 An optional third argument can be used to specify the location for static assets. Examples: http://static.example.com/ chrome-extension://extensionid/public """ if len(args) == 1: console.error('You must supply a browser name: `chrome` or `firefox`.') return 2 elif len(args) == 2: console.error('You must supply a url to the hosted backend.') return 2 elif len(args) == 3: assets_url = settings['webassets.base_url'] else: settings['webassets.base_url'] = args[3] assets_url = args[3] browser = args[1] base_url = args[2] # Fully-qualify the static asset url parts = urlparse(assets_url) if not parts.netloc: base = urlparse(base_url) parts = (base.scheme, base.netloc, parts.path, parts.params, parts.query, parts.fragment) assets_url = urlunparse(parts) # Set up the assets url and source path mapping if browser == 'chrome': settings['webassets.base_dir'] = abspath('./build/chrome/public') elif browser == 'firefox': settings['webassets.base_dir'] = abspath('./build/firefox/data') else: console.error('You must supply a browser name: `chrome` or `firefox`.') return 2 settings['webassets.base_url'] = assets_url settings['webassets.paths'] = json.dumps({ resolve('h:static').abspath(): assets_url }) # Turn off the webassets cache and manifest settings['webassets.cache'] = None settings['webassets.manifest'] = None # Turn off the API settings['h.feature.api'] = False config = Configurator(settings=settings) config.include('h') config.set_root_factory('h.resources.RootFactory') config.add_subscriber(add_base_url, BeforeRender) config.commit() # Build it request = Request.blank('/app', base_url=base_url) build_extension(prepare(registry=config.registry, request=request), browser, settings['webassets.base_dir']) # XXX: Change when webassets allows setting the cache option # As of 0.10 it's only possible to pass a sass config with string values try: rmtree('./build/' + browser + '/public/.sass-cache') except OSError: pass # newer Sass doesn't write this it seems
def get_request(self, path='/'): self.app # to bootstrap env request = Request.blank(path) env = prepare(request=request, registry=self.config.registry) self.addCleanup(env['closer']) return request
def __init__(self, config_file, app_env, server_env): self.config_file = config_file self.app_env = app_env self.server_env = server_env self.app = load_app(config_file, name=app_env) self.pyramid_env = prepare()
def _callFUT(self, request=None, registry=None): from pyramid.scripting import prepare return prepare(request, registry)