<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import argparse
import sys

sys.dont_write_bytecode = True
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server

MISSING_DEPENDENCY = False
try:
    from bottle import route, Bottle
except ImportError:
    sys.stdout.write("You'll need to `pip install bottle` to run this demo\n")
    MISSING_DEPENDENCY = True
try:
    import django
except ImportError:
    sys.stdout.write("You'll need to `pip install Django` to run this demo\n")
    MISSING_DEPENDENCY = True

if MISSING_DEPENDENCY:
    sys.exit(1)

try:
    from meinheld import server as meinheld_serve
    HAS_MEINHELD = True
except ImportError as e:
    HAS_MEINHELD = False

try:
    from waitress import serve as waitress_serve
    HAS_WAITRESS = True
except ImportError as e:
    HAS_WAITRESS = False

import detour

# ------------------------------------------------------------------------------
# Some raw WSGI responses ...
# ------------------------------------------------------------------------------

def one_view(environ, start_response):
    setup_testing_defaults(environ)
    start_response('200 OK', [('content-type', 'text/html')])
    return ('Hello world from wsgi-detour==%s' % detour.get_version(),)


def another_view(environ, start_response):
    setup_testing_defaults(environ)
    start_response('200 OK', [('content-type', 'text/html')])
    return ('Another view from wsgi-detour==%s' % detour.get_version(),)

# ------------------------------------------------------------------------------
# Now we set up a Bottle instance
# ------------------------------------------------------------------------------
bottle_app = Bottle()
@bottle_app.route('/')
def bottle_hello_world():
    return u"this went to Bottle"

@bottle_app.route('/more/<var>')
def bottle_goodbye_cruel_world(var):
    return u"%s also went to Bottle" % var

# ------------------------------------------------------------------------------
# Now we set up a Django instance
# ------------------------------------------------------------------------------
from django.conf import settings
settings.configure(
    ROOT_URLCONF=__name__,
    DEBUG=True,
    ALLOWED_HOSTS=['*'],
)
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse
from django.conf.urls import url

def django_hello_world(request):
    return HttpResponse(u"I'm from Django!")

def django_goodbye_cruel_world(request, arg):
    return HttpResponse(u"%s also went to Django" % arg)


urlpatterns = (
    url('^$', django_hello_world),
    url('^more/(?P<arg>.+?)$', django_goodbye_cruel_world),
)
django_app = get_wsgi_application()

# ------------------------------------------------------------------------------
# The WSGI App which Detour wraps over as a fallthrough...
# ------------------------------------------------------------------------------

def fallback(environ, start_response):
    setup_testing_defaults(environ)
    start_response('404 Not Found', [('content-type', 'text/html')])
    return ('Fallback from wsgi-detour==%s' % detour.get_version(),)

# ------------------------------------------------------------------------------
# Example Detour setup.
# ------------------------------------------------------------------------------
application = detour.Detour(app=fallback, mounts=(
    ('/raw/one_view', one_view),<commit_msg>Let the fallback point at the other apps slightly nicer.<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import argparse
import sys

sys.dont_write_bytecode = True
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server

MISSING_DEPENDENCY = False
try:
    from bottle import route, Bottle
except ImportError:
    sys.stdout.write("You'll need to `pip install bottle` to run this demo\n")
    MISSING_DEPENDENCY = True
try:
    import django
except ImportError:
    sys.stdout.write("You'll need to `pip install Django` to run this demo\n")
    MISSING_DEPENDENCY = True

if MISSING_DEPENDENCY:
    sys.exit(1)

try:
    from meinheld import server as meinheld_serve
    HAS_MEINHELD = True
except ImportError as e:
    HAS_MEINHELD = False

try:
    from waitress import serve as waitress_serve
    HAS_WAITRESS = True
except ImportError as e:
    HAS_WAITRESS = False

import detour

# ------------------------------------------------------------------------------
# Some raw WSGI responses ...
# ------------------------------------------------------------------------------

def one_view(environ, start_response):
    setup_testing_defaults(environ)
    start_response('200 OK', [('content-type', 'text/html')])
    return ('Hello world from wsgi-detour==%s' % detour.get_version(),)


def another_view(environ, start_response):
    setup_testing_defaults(environ)
    start_response('200 OK', [('content-type', 'text/html')])
    return ('Another view from wsgi-detour==%s' % detour.get_version(),)

# ------------------------------------------------------------------------------
# Now we set up a Bottle instance
# ------------------------------------------------------------------------------
bottle_app = Bottle()
@bottle_app.route('/')
def bottle_hello_world():
    return u"this went to Bottle"

@bottle_app.route('/more/<var>')
def bottle_goodbye_cruel_world(var):
    return u"%s also went to Bottle" % var

# ------------------------------------------------------------------------------
# Now we set up a Django instance
# ------------------------------------------------------------------------------
from django.conf import settings
settings.configure(
    ROOT_URLCONF=__name__,
    DEBUG=True,
    ALLOWED_HOSTS=['*'],
)
from django.core.wsgi import get_wsgi_application
from django.http import HttpResponse
from django.conf.urls import url

def django_hello_world(request):
    return HttpResponse(u"I'm from Django!")

def django_goodbye_cruel_world(request, arg):
    return HttpResponse(u"%s also went to Django" % arg)


urlpatterns = (
    url('^$', django_hello_world),
    url('^more/(?P<arg>.+?)$', django_goodbye_cruel_world),
)
django_app = get_wsgi_application()

# ------------------------------------------------------------------------------
# The WSGI App which Detour wraps over as a fallthrough...
# ------------------------------------------------------------------------------

def fallback(environ, start_response):
    setup_testing_defaults(environ)
    start_response('404 Not Found', [('content-type', 'text/html')])
    data = """
    Fallback from wsgi-detour==%s
    <ul>
    <li><a href="/raw/one_view">raw WSGI</a></li>
    <li><a href="/raw/another_view">a different raw WSGI</a></li>
    <li><a href="/bottled/">a Bottle app</a>
        <ul>
            <li><a href="/bottled/more/yes">a Bottle view accepting an argument</a></li>
            <li><a href="/bottled/nope">a Bottle error page</a></li>
        </ul>
    </li>
    <li><a href="/django/">a Django app</a>
        <ul>
            <li><a href="/django/more/yes">a Django view accepting an argument</a></li>
            <li><a href="/django/nope">a Django error page</a></li>
        </ul>
    </li>
    </ul>
    """ % detour.get_version()
    return (data,)

# ------------------------------------------------------------------------------
# Example Detour setup.
# ------------------------------------------------------------------------------
application = detour.Detour(app=fallback, mounts=(
    ('/raw/one_view', one_view),
{'fraction_non_alphanumeric': 0.3051853806570788, 'fraction_numerical': 0.002902757619738752, 'mean_word_length': 1.7433948606587044, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5675007', 'n_tokens_mistral': 2149, 'n_tokens_neox': 2067, 'n_words': 534}