File: utils.py

package info (click to toggle)
python-werkzeug 1.0.1%2Bdfsg1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,888 kB
  • sloc: python: 21,897; javascript: 173; makefile: 36; xml: 16
file content (113 lines) | stat: -rw-r--r-- 2,892 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""
    coolmagic.utils
    ~~~~~~~~~~~~~~~

    This module contains the subclasses of the base request and response
    objects provided by werkzeug. The subclasses know about their charset
    and implement some additional functionality like the ability to link
    to view functions.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
"""
from os.path import dirname
from os.path import join

from jinja2 import Environment
from jinja2 import FileSystemLoader
from werkzeug.local import Local
from werkzeug.local import LocalManager
from werkzeug.wrappers import BaseRequest
from werkzeug.wrappers import BaseResponse


local = Local()
local_manager = LocalManager([local])
template_env = Environment(
    loader=FileSystemLoader(join(dirname(__file__), "templates"))
)
exported_views = {}


def export(string, template=None, **extra):
    """
    Decorator for registering view functions and adding
    templates to it.
    """

    def wrapped(f):
        endpoint = (f.__module__ + "." + f.__name__)[16:]
        if template is not None:
            old_f = f

            def f(**kwargs):
                rv = old_f(**kwargs)
                if not isinstance(rv, Response):
                    rv = TemplateResponse(template, **(rv or {}))
                return rv

            f.__name__ = old_f.__name__
            f.__doc__ = old_f.__doc__
        exported_views[endpoint] = (f, string, extra)
        return f

    return wrapped


def url_for(endpoint, **values):
    """
    Build a URL
    """
    return local.request.url_adapter.build(endpoint, values)


class Request(BaseRequest):
    """
    The concrete request object used in the WSGI application.
    It has some helper functions that can be used to build URLs.
    """

    charset = "utf-8"

    def __init__(self, environ, url_adapter):
        BaseRequest.__init__(self, environ)
        self.url_adapter = url_adapter
        local.request = self


class ThreadedRequest(object):
    """
    A pseudo request object that always poins to the current
    context active request.
    """

    def __getattr__(self, name):
        if name == "__members__":
            return [x for x in dir(local.request) if not x.startswith("_")]
        return getattr(local.request, name)

    def __setattr__(self, name, value):
        return setattr(local.request, name, value)


class Response(BaseResponse):
    """
    The concrete response object for the WSGI application.
    """

    charset = "utf-8"
    default_mimetype = "text/html"


class TemplateResponse(Response):
    """
    Render a template to a response.
    """

    def __init__(self, template_name, **values):
        from coolmagic import helpers

        values.update(request=local.request, h=helpers)
        template = template_env.get_template(template_name)
        Response.__init__(self, template.render(values))