Exemple #1
0
def compile_qs(line):
    items = line.split('&')
    fields = {}
    for item in items:
        if is_field_optional(item):
            mod = routrschema.opt
            item = item[1:-1]
        else:
            mod = lambda x: x
        if '=' in item:
            name, validator = item.split('=', 1)
            fields[name] = mod(validators[validator])
        else:
            fields[name] = mod(bool)
    return routrschema.qs(**fields)
Exemple #2
0
def template_view():
    return {'var': uuid.uuid4()}

def show_request(request, path):
    return {
        'method': request.method,
        'path': path,
        'headers': dict(request.headers),
    }

routes = route(
    GET('/', hello, name='hello'),
    GET('/environ', environ, name='environ', renderer='json'),
    GET('/page/{pk:int}', page, name='page'),
    GET('/search', qs(query=opt(str)), search, name='search'),
    GET('/template', template_view, name='template', renderer='template.html'),
    GET('/request/{path:path}', show_request, renderer='json'),
)

env = Environment(loader=PackageLoader('app', 'templates'))
env.globals['reverse'] = routes.reverse


def application(environ, start_response):
    request = Request(environ)

    try:
        trace = routes(request)
        view = trace.target
        args, kwargs = trace.args, trace.kwargs
Exemple #3
0
        result['title'] = doc.title
    return result


def analyse_html(url=None):
    doc = Document.from_url(url)
    return Response(doc.html)


routes = route(
    GET(
        '/analyse',
        qs(
            url=str,
            image=opt(bool),
            text=opt(bool),
            html=opt(bool),
            title=opt(bool),
            author=opt(bool),
        ), analyse),
    GET('/analyse.html', qs(url=str), analyse_html),
)


def app(environ, start_response):
    request = Request(environ)
    try:
        tr = routes(request)
        response = tr.target(*tr.args, **tr.kwargs)
    except NoMatchFound as e:
        response = e.response
    except HTTPError as e:
Exemple #4
0
        result['image'] = doc.image
    if author:
        result['author'] = doc.author
    if title:
        result['title'] = doc.title
    return result

def analyse_html(url=None):
    doc = Document.from_url(url)
    return Response(doc.html)

routes = route(
    GET('/analyse', qs(
            url=str,
            image=opt(bool),
            text=opt(bool),
            html=opt(bool),
            title=opt(bool),
            author=opt(bool),
        ),
        analyse),
    GET('/analyse.html', qs(url=str), analyse_html),
    )

def app(environ, start_response):
    request = Request(environ)
    try:
        tr = routes(request)
        response = tr.target(*tr.args, **tr.kwargs)
    except NoMatchFound as e:
        response = e.response
    except HTTPError as e: