Example #1
0
    def __init__(
        self,
        config=None,
        bin=None,
        manager=None,
        router=None,
        jinja=None,
    ):
        if config is None:
            config = {}
        if manager is None:
            manager = Manager()
        if bin is None:
            bin = assets.Bin(manager=manager)
        if router is None:
            router = Router(mapper=SimpleMapper())

        self._response_callbacks = defaultdict(list)

        self.bin = bin
        self.config = config
        self.manager = manager
        self.router = router

        self.bind_bin(bin)
        if jinja is not None:
            self.bind_jinja_environment(jinja)
Example #2
0
    def test_it_routes_simple_views(self):
        minion = Application(router=Router(mapper=SimpleMapper()))

        @minion.route(b"/show")
        def show(request):
            return Response(b"Hello World!")

        response = minion.serve(Request(url=URL(path=b"/show")), path=b"/show")
        self.assertEqual(response, Response(b"Hello World!"))
Example #3
0
def create_app(config=None):
    root = TreeResource()
    bin = Bin().with_globals(engine=engine_from_config(config=config))
    app = Application(
        router=Router(mapper=TraversalMapper(root=root)),
        bin=bin,
    )

    music.init_app(bin=bin, root=root)

    return app
Example #4
0
class TestApplicationIntegration(TestCase):
    def setUp(self):
        self.manager = Manager()
        self.router = Router(mapper=SimpleMapper())
        self.app = core.Application(manager=self.manager, router=self.router)
        self.request = Request(url=URL(path=b"/"))

    def test_it_serves_mapped_requests(self):
        self.router.add(
            self.request.url.path, lambda request : Response(b"Hello"),
        )
        self.assertEqual(
            self.app.serve(self.request, path=self.request.url.path),
            Response(b"Hello"),
        )

    def test_it_serves_404s_for_unmapped_requests_by_default(self):
        self.assertEqual(
            self.app.serve(self.request, path=self.request.url.path).code, 404,
        )
Example #5
0
    def test_it_delegates_routes_to_the_router(self):
        class Router(object):
            def add(this, route, fn, baz):
                self.added = route, fn, baz

        application = core.Application(router=Router())

        @application.route("/foo", baz=2)
        def fn(request):
            pass

        self.assertEqual(getattr(self, "added", None), ("/foo", fn, 2))
Example #6
0
class TestApplicationIntegration(TestCase):
    def setUp(self):
        self.manager = Manager()
        self.router = Router(mapper=SimpleMapper())
        self.app = core.Application(manager=self.manager, router=self.router)
        self.request = Request(url=URL(path=[u""]))

    def test_it_serves_mapped_requests(self):
        self.router.add(
            self.request.url.path,
            lambda request: Response(b"Hello"),
        )
        self.assertEqual(
            self.app.serve(self.request, path=self.request.url.path),
            Response(b"Hello"),
        )

    def test_it_serves_404s_for_unmapped_requests_by_default(self):
        self.assertEqual(
            self.app.serve(self.request, path=self.request.url.path).code,
            404,
        )
Example #7
0
 def setUp(self):
     self.manager = Manager()
     self.router = Router(mapper=SimpleMapper())
     self.app = core.Application(manager=self.manager, router=self.router)
     self.request = Request(url=URL(path=[u""]))
Example #8
0
from minion.core import Application
from minion.request import Response
from minion.routing import Router, RoutesMapper
from minion.wsgi import create_app

app = Application(router=Router(mapper=RoutesMapper()))


@app.route("/greet/{user}", greeting="Hey")
def greet(request, greeting, user):
    return Response("{} {}\n".format(greeting, user).encode("utf-8"))


wsgi = create_app(app)
Example #9
0
class Application(object):
    """
    A Minion application.

    Arguments:

        config (collections.MutableMapping):

            any app configuration

        bin (assets.Bin):

            an asset bin containing assets used by views.

            If unprovided, an empty one will be created (until it is
            populated).

        manager (request.Manager):

            a request manager, which managers state during each request.

            If unprovided, one will be created and used.

        router:

            an object satisfying the router interface (see `minion.routing`) to
            use for route addition and generation for this application.

            If unprovided, a router with simple dictionary lookup will be used.

        jinja (jinja2.Environment):

            a pre-configured jinja2 environment, if using jinja2 is desired.

            (One can be added later, or multiple environments used, by calling
            `bind_jinja_environment`.)

    """

    config = attr.ib(default=attr.Factory(dict))
    manager = attr.ib(default=attr.Factory(Manager), repr=False)
    bin = attr.ib(default=None, repr=False)
    router = attr.ib(
        default=attr.Factory(lambda: Router(mapper=SimpleMapper())), )
    _jinja = attr.ib(default=None)

    def __attrs_post_init__(self):
        if self.bin is None:
            self.bin = assets.Bin().add(manager=self.manager)
        self.bin = self.bound_bin(self.bin)
        if self._jinja is not None:
            self.bind_jinja_environment(self._jinja)

    def route(self, route, **kwargs):
        def _add_route(fn):
            self.router.add(route, fn, **kwargs)
            return fn

        return _add_route

    def serve(self, request, path):
        self.manager.request_started(request)
        response = self.router.route(request=request, path=path)
        self.manager.request_served(request, response)
        return response

    def bound_bin(self, bin):
        """
        Bind an asset bin to this application.

        Returns:

            Bin: a bin containing this application's relevant globals

        """

        return bin.add(
            app=lambda bin: self,
            config=lambda bin: self.config,
            manager=lambda bin: self.manager,
            router=lambda bin: self.router,
        )

    def bind_jinja_environment(self, environment, asset_name="jinja"):
        """
        Bind useful pieces of the application to the given Jinja2 environment.

        Arguments:

            environment (jinja2.Environment):

                the environment to bind

            asset_name (str):

                a name to bind to in the application's asset bin.

                The default is ``'jinja'``.

        """

        environment.globals.update([
            ("app", self),
            ("config", self.config),
            ("router", self.router),
        ], )
        self.bin = self.bin.add(**{asset_name: lambda bin: environment})
Example #10
0
 def setUp(self):
     self.manager = Manager()
     self.router = Router(mapper=SimpleMapper())
     self.app = core.Application(manager=self.manager, router=self.router)
     self.request = Request(url=URL(path=b"/"))
Example #11
0
import jinja2
import werkzeug.wrappers

from minion.core import Application
from minion.request import Response
from minion.routing import Router, WerkzeugMapper
from minion.wsgi import create_app
import examples.static

CSS = os.path.join(os.path.dirname(examples.static.__file__), "flaskr.css")
loader = jinja2.FileSystemLoader(
    os.path.join(os.path.dirname(__file__), "templates", "flaskr"), )
app = Application(
    jinja=jinja2.Environment(loader=loader),
    router=Router(mapper=WerkzeugMapper()),
    config={
        "credentials": {
            "user": "******",
            "password": "******"
        },
        "database": {
            "uri": "/tmp/flaskr.db"
        },
    },
)


@app.route("/")
@app.bin.needs(["db", "jinja"])
def show_entries(request, db, jinja):