<commit_before># -*- coding: utf-8 -*-

import tg, pylons
from tg.controllers import TGController, CUSTOM_CONTENT_TYPE, \
                           WSGIAppController, RestController
from tg.decorators import expose, validate, override_template
from tg.util import no_warn
from routes import Mapper
from routes.middleware import RoutesMiddleware
from formencode import validators
from webob import Response, Request
from nose.tools import raises

from tests.base import TestWSGIController, make_app, setup_session_dir, \
                          teardown_session_dir

from wsgiref.simple_server import demo_app
from wsgiref.validate import validator
from pylons.controllers.xmlrpc import XMLRPCController

from pylons import config
config['renderers'] = ['genshi', 'mako', 'json']

def setup():
    setup_session_dir()
def teardown():
    teardown_session_dir()

def wsgi_app(environ, start_response):
    req = Request(environ)
    if req.method == 'POST':
        resp = Response(req.POST['data'])
    else:
        resp = Response("Hello from %s/%s"%(req.script_name, req.path_info))
    return resp(environ, start_response)

class XMLRpcTestController(XMLRPCController):
    def textvalue(self):
        return 'hi from xmlrpc'
    textvalue.signature = [ ['string'] ]

class BeforeController(TGController):

    def _before(self, *args, **kw):
        pylons.tmpl_context.var = '__my_before__'
        
    def _after(self, *args, **kw):
        global_craziness = '__my_after__'

    @expose()
    def index(self):
        assert pylons.tmpl_context.var
        return pylons.tmpl_context.var

class NewBeforeController(TGController):
    def _before(self, *args, **kw):
        pylons.tmpl_context.var = '__my_before__'
        pylons.tmpl_context.args = args
        pylons.tmpl_context.params = kw

    def _after(self, *args, **kw):
        global_craziness = '__my_after__'

    @expose()
    def index(self):
        assert pylons.tmpl_context.var
        return pylons.tmpl_context.var

    @expose()
    def with_args(self, *args, **kw):
        assert pylons.tmpl_context.args
        assert pylons.tmpl_context.params
        return pylons.tmpl_context.var+pylons.tmpl_context.params['environ']['webob._parsed_query_vars'][0]['x']

class SubController(object):
    mounted_app = WSGIAppController(wsgi_app)

    before = BeforeController()
    newbefore = NewBeforeController()

    @expose('genshi')
    def unknown_template(self):
        return "sub unknown template"

    @expose()
    def foo(self,):
        return 'sub_foo'

    @expose()
    def index(self):
        return 'sub index'

    @expose()
    def default(self, *args):
        return ("recieved the following args (from the url): %s" %str([str(a) for a in args]))

    @expose()
    def redirect_me(self, target, **kw):
        tg.redirect(target, **kw)

    @expose()
    def redirect_sub(self):
        tg.redirect('index')

    @expose()
    def hello(self, name):
        return "Why HELLO! " + name

class SubController3(object):
    @expose()
    def get_all(self):
        return 'Sub 3'

class SubController2(object):
    @expose()
    def index(self):
        tg.redirect('list')

    @expose()
    def list(self, **kw):
        return "hello list"

class LookupHelper:

    def __init__(self, var):
        self.var = var

    @expose()
    def index(self):
        return self.var

class LookupHelperWithArgs:

    @expose()
    def get_here(self, *args):
        return "%s"%args

    @expose()
    def post_with_mixed_args(self, arg1, arg2, **kw):
        return "%s%s" % (arg1, arg2)

class LookupControllerWithArgs(TGController):

    @expose()
    def _lookup(self, *args):
        l = LookupHelperWithArgs()
#        print 'inside _lookup', l, args
        return l, args

class LookupController(TGController):

    @expose()
    def _lookup(self, a, *args):
        return LookupHelper(a), args
    
class LookupWithEmbeddedLookupController(TGController):
    
    @expose()
    def _lookup(self, *args):
        return LookupControllerWithArgs(), args

class LookupHelperWithIndex:

    @expose()
    def index(self):
        return "helper index"
    
    @expose()
    def method(self):
        return "helper method"
    

class LookupControllerWithIndexHelper(TGController):

    @expose()
    def _lookup(self, a, *args):
        return LookupHelperWithIndex(), args

    @expose()
    def index(self):
        return "second controller with index"

class LookupWithEmbeddedLookupWithHelperWithIndex(TGController):

    @expose()
    def _lookup(self, a, *args):
        return LookupControllerWithIndexHelper(), args
    
    @expose()
    def index(self):
        return "first controller with index"

class LookupControllerWithSubcontroller(TGController):

    class SubController(object): pass

    @expose()
    def _lookup(self, a, *args):
        return self.SubController(), args

class RemoteErrorHandler(TGController):
    @expose()
    def errors_here(self, *args, **kw):
        return "REMOTE ERROR HANDLER"

class NotFoundController(TGController):pass

class DefaultWithArgsController(TGController):
    @expose()
    def default(self, a, b=None, **kw):
        return "DEFAULT WITH ARGS %s %s"%(a, b)

class DefaultWithArgsAndValidatorsController(TGController):
    @expose()
    def failure(self, *args, **kw):
        return "FAILURE"

    @expose()
    @validate({'a': validators.Int(),
              'b': validators.StringBool()}, error_handler=failure)
    def default(self, a, b=None, **kw):
        return "DEFAULT WITH ARGS AND VALIDATORS %s %s"%(a, b)

class SubController4:
    default_with_args = DefaultWithArgsController()

class SubController5:
    default_with_args = DefaultWithArgsAndValidatorsController()

class HelperWithSpecificArgs(TGController):
    
    @expose()
    def index(self, **kw):
        return str(kw)

    @expose()
    def method(self, arg1, arg2, **kw):
        return str((arg1, arg2, kw))
    
class SelfCallingLookupController(TGController):

    @expose()
    def _lookup(self, a, *args):
        if a in ['a', 'b', 'c']:
            return SelfCallingLookupController(), args
        a = [a]
        a.extend(args)
        return HelperWithSpecificArgs(), a
    
    @expose()
    def index(self, *args, **kw):
        return str((args, kw))

class BasicTGController(TGController):
    mounted_app = WSGIAppController(wsgi_app)
    xml_rpc = WSGIAppController(XMLRpcTestController())

    error_controller = RemoteErrorHandler()

    lookup = LookupController()
    lookup_with_args = LookupControllerWithArgs()
    lookup_with_sub = LookupControllerWithSubcontroller()
    self_calling = SelfCallingLookupController()

    @expose(content_type='application/rss+xml')
    def ticket2351(self, **kw):
        return 'test'

    @expose()
    def index(self, **kwargs):
        return 'hello world'

    @expose()
    def default(self, *remainder):
        return "Main Default Page called for url /%s"%list(remainder)

    @expose()
    def feed(self, feed=None):
        return feed

    sub = SubController()
    sub2 = SubController2()
    sub4 = SubController4()
    sub5 = SubController5()
    
    embedded_lookup = LookupWithEmbeddedLookupController()
    embedded_lookup_with_index = LookupWithEmbeddedLookupWithHelperWithIndex()

    @expose()
    def test_args(self, id, one=None, two=2, three=3):
        r = dict(id=id, one=str(one), two=str(two), three=str(three))
        return str(r)

    @expose()
    def redirect_me(self, target, **kw):
        tg.redirect(target, kw)

    @expose()
    def hello(self, name, silly=None):
        return "Hello " + name

    @expose()
    def optional_and_req_args(self, id, one=None, two=2, three=3):
        r = dict(id=id, one=str(one), two=str(two), three=str(three))

        return str(r)

    @expose()
    def ticket2412(self, arg1):
        return arg1

    @expose()
    def redirect_cookie(self, name):
        pylons.response.set_cookie('name', name)
        tg.redirect('/hello_cookie')

    @expose()
    def hello_cookie(self):
        return "Hello " + pylons.request.cookies['name']

    @expose()
    def flash_redirect(self):
        tg.flash("Wow, flash!")
        tg.redirect("/flash_after_redirect")

    @expose()
    def flash_unicode(self):
        tg.flash(u"Привет, мир!")
        tg.redirect("/flash_after_redirect")

    @expose()
    def flash_after_redirect(self):
        return tg.get_flash()

    @expose()
    def flash_status(self):
        return tg.get_status()

    @expose()
    def flash_no_redirect(self):
        tg.flash("Wow, flash!")
        return tg.get_flash()

    @expose('json')
    @validate(validators={"some_int": validators.Int()})
    def validated_int(self, some_int):
        assert isinstance(some_int, int)
        return dict(response=some_int)

    @expose('json')
    @validate(validators={"a":validators.Int()})
    def validated_and_unvalidated(self, a, b):
        assert isinstance(a, int)
        assert isinstance(b, unicode)
        return dict(int=a,str=b)

    @expose()
    def error_handler(self, **kw):
        return 'VALIDATION ERROR HANDLER'

    @expose('json')
    @validate(validators={"a":validators.Int()}, error_handler=error_handler)
    def validated_with_error_handler(self, a, b):
        assert isinstance(a, int)
        assert isinstance(b, unicode)
        return dict(int=a,str=b)

    @expose('json')
    @validate(validators={"a":validators.Int()}, error_handler=error_controller.errors_here)
    def validated_with_remote_error_handler(self, a, b):
        assert isinstance(a, int)
        assert isinstance(b, unicode)
        return dict(int=a,str=b)

    @expose()
    @expose('json')
    def stacked_expose(self):
        return dict(got_json=True)

    @expose('json')
    def bad_json(self):
        return [(1, 'a'), 'b']

    @expose()
    def custom_content_type_in_controller(self):
        pylons.response.headers['content-type'] = 'image/png'
        return 'PNG'

    @expose('json', content_type='application/json')
    def custom_content_type_in_controller_charset(self):
        pylons.response.headers['content-type'] = 'application/json; charset=utf-8'
        return dict(result='TXT')

    @expose(content_type=CUSTOM_CONTENT_TYPE)
    def custom_content_type_with_ugliness(self):
        pylons.response.headers['content-type'] = 'image/png'
        return 'PNG'

    @expose(content_type='image/png')
    def custom_content_type_in_decorator(self):
        return 'PNG'

    @expose()
    def test_204(self, *args, **kw):
        from webob.exc import HTTPNoContent
        raise HTTPNoContent().exception

    @expose()
    def custom_content_type_replace_header(self):
        replace_header(pylons.response.headerlist, 'Content-Type', 'text/xml')
        return "<?xml version='1.0'?>"

    @expose()
    def multi_value_kws(sekf, *args, **kw):
        assert kw['foo'] == ['1', '2'], kw

class TestNotFoundController(TestWSGIController):
    def __init__(self, *args, **kargs):
        TestWSGIController.__init__(self, *args, **kargs)
        self.app = make_app(NotFoundController)

    def test_not_found(self):
        r = self.app.get('/something', status=404)
        assert '404 Not Found' in r, r

    def test_not_found_blank(self):
        r = self.app.get('/', status=404)
        assert '404 Not Found' in r, r

    def test_not_found_unicode(self):
        r = self.app.get('/права', status=404)
        assert '404 Not Found' in r, r

class TestWSGIAppController(TestWSGIController):
    def __init__(self, *args, **kargs):
        TestWSGIController.__init__(self, *args, **kargs)
        class TestedWSGIAppController(WSGIAppController):
            def __init__(self):
                def test_app(environ, start_response):
                    if environ['CONTENT_LENGTH'] in (-1, '-1'):
                        del environ['CONTENT_LENGTH']
                    return validator(demo_app)(environ, start_response)
                super(TestedWSGIAppController, self).__init__(test_app)
        self.app = make_app(TestedWSGIAppController)

    def test_valid_wsgi(self):
        try:
            r = self.app.get('/some_url')
        except Exception, e:
            raise AssertionError(str(e))
        assert 'some_url' in r

class TestTGController(TestWSGIController):
    def setUp(self, *args, **kargs):
        
        TestWSGIController.setUp(self, *args, **kargs)
        self.app = make_app(BasicTGController)

    def test_lookup(self):
        r = self.app.get('/lookup/EYE')<commit_msg>fixes to support python 2.7<commit_after># -*- coding: utf-8 -*-

import tg, pylons
from tg.controllers import TGController, CUSTOM_CONTENT_TYPE, \
                           WSGIAppController, RestController
from tg.decorators import expose, validate, override_template
from tg.util import no_warn
from routes import Mapper
from routes.middleware import RoutesMiddleware
from formencode import validators
from webob import Response, Request
from nose.tools import raises

from tests.base import TestWSGIController, make_app, setup_session_dir, \
                          teardown_session_dir

from wsgiref.simple_server import demo_app
from wsgiref.validate import validator
from pylons.controllers.xmlrpc import XMLRPCController

from pylons import config
config['renderers'] = ['genshi', 'mako', 'json']

def setup():
    setup_session_dir()
def teardown():
    teardown_session_dir()

def wsgi_app(environ, start_response):
    req = Request(environ)
    if req.method == 'POST':
        resp = Response(req.POST['data'])
    else:
        resp = Response("Hello from %s/%s"%(req.script_name, req.path_info))
    return resp(environ, start_response)

class XMLRpcTestController(XMLRPCController):
    def textvalue(self):
        return 'hi from xmlrpc'
    textvalue.signature = [ ['string'] ]

class BeforeController(TGController):

    def _before(self, *args, **kw):
        pylons.tmpl_context.var = '__my_before__'
        
    def _after(self, *args, **kw):
        global_craziness = '__my_after__'

    @expose()
    def index(self):
        assert pylons.tmpl_context.var
        return pylons.tmpl_context.var

class NewBeforeController(TGController):
    def _before(self, *args, **kw):
        pylons.tmpl_context.var = '__my_before__'
        pylons.tmpl_context.args = args
        pylons.tmpl_context.params = kw

    def _after(self, *args, **kw):
        global_craziness = '__my_after__'

    @expose()
    def index(self):
        assert pylons.tmpl_context.var
        return pylons.tmpl_context.var

    @expose()
    def with_args(self, *args, **kw):
        assert pylons.tmpl_context.args
        assert pylons.tmpl_context.params
        return pylons.tmpl_context.var+pylons.tmpl_context.params['environ']['webob._parsed_query_vars'][0]['x']

class SubController(object):
    mounted_app = WSGIAppController(wsgi_app)

    before = BeforeController()
    newbefore = NewBeforeController()

    @expose('genshi')
    def unknown_template(self):
        return "sub unknown template"

    @expose()
    def foo(self,):
        return 'sub_foo'

    @expose()
    def index(self):
        return 'sub index'

    @expose()
    def default(self, *args):
        return ("recieved the following args (from the url): %s" %str([str(a) for a in args]))

    @expose()
    def redirect_me(self, target, **kw):
        tg.redirect(target, **kw)

    @expose()
    def redirect_sub(self):
        tg.redirect('index')

    @expose()
    def hello(self, name):
        return "Why HELLO! " + name

class SubController3(object):
    @expose()
    def get_all(self):
        return 'Sub 3'

class SubController2(object):
    @expose()
    def index(self):
        tg.redirect('list')

    @expose()
    def list(self, **kw):
        return "hello list"

class LookupHelper:

    def __init__(self, var):
        self.var = var

    @expose()
    def index(self):
        return self.var

class LookupHelperWithArgs:

    @expose()
    def get_here(self, *args):
        return "%s"%args

    @expose()
    def post_with_mixed_args(self, arg1, arg2, **kw):
        return "%s%s" % (arg1, arg2)

class LookupControllerWithArgs(TGController):

    @expose()
    def _lookup(self, *args):
        l = LookupHelperWithArgs()
#        print 'inside _lookup', l, args
        return l, args

class LookupController(TGController):

    @expose()
    def _lookup(self, a, *args):
        return LookupHelper(a), args
    
class LookupWithEmbeddedLookupController(TGController):
    
    @expose()
    def _lookup(self, *args):
        return LookupControllerWithArgs(), args

class LookupHelperWithIndex:

    @expose()
    def index(self):
        return "helper index"
    
    @expose()
    def method(self):
        return "helper method"
    

class LookupControllerWithIndexHelper(TGController):

    @expose()
    def _lookup(self, a, *args):
        return LookupHelperWithIndex(), args

    @expose()
    def index(self):
        return "second controller with index"

class LookupWithEmbeddedLookupWithHelperWithIndex(TGController):

    @expose()
    def _lookup(self, a, *args):
        return LookupControllerWithIndexHelper(), args
    
    @expose()
    def index(self):
        return "first controller with index"

class LookupControllerWithSubcontroller(TGController):

    class SubController(object): pass

    @expose()
    def _lookup(self, a, *args):
        return self.SubController(), args

class RemoteErrorHandler(TGController):
    @expose()
    def errors_here(self, *args, **kw):
        return "REMOTE ERROR HANDLER"

class NotFoundController(TGController):pass

class DefaultWithArgsController(TGController):
    @expose()
    def default(self, a, b=None, **kw):
        return "DEFAULT WITH ARGS %s %s"%(a, b)

class DefaultWithArgsAndValidatorsController(TGController):
    @expose()
    def failure(self, *args, **kw):
        return "FAILURE"

    @expose()
    @validate({'a': validators.Int(),
              'b': validators.StringBool()}, error_handler=failure)
    def default(self, a, b=None, **kw):
        return "DEFAULT WITH ARGS AND VALIDATORS %s %s"%(a, b)

class SubController4:
    default_with_args = DefaultWithArgsController()

class SubController5:
    default_with_args = DefaultWithArgsAndValidatorsController()

class HelperWithSpecificArgs(TGController):
    
    @expose()
    def index(self, **kw):
        return str(kw)

    @expose()
    def method(self, arg1, arg2, **kw):
        return str((arg1, arg2, kw))
    
class SelfCallingLookupController(TGController):

    @expose()
    def _lookup(self, a, *args):
        if a in ['a', 'b', 'c']:
            return SelfCallingLookupController(), args
        a = [a]
        a.extend(args)
        return HelperWithSpecificArgs(), a
    
    @expose()
    def index(self, *args, **kw):
        return str((args, kw))

class BasicTGController(TGController):
    mounted_app = WSGIAppController(wsgi_app)
    xml_rpc = WSGIAppController(XMLRpcTestController())

    error_controller = RemoteErrorHandler()

    lookup = LookupController()
    lookup_with_args = LookupControllerWithArgs()
    lookup_with_sub = LookupControllerWithSubcontroller()
    self_calling = SelfCallingLookupController()

    @expose(content_type='application/rss+xml')
    def ticket2351(self, **kw):
        return 'test'

    @expose()
    def index(self, **kwargs):
        return 'hello world'

    @expose()
    def default(self, *remainder):
        return "Main Default Page called for url /%s"%list(remainder)

    @expose()
    def feed(self, feed=None):
        return feed

    sub = SubController()
    sub2 = SubController2()
    sub4 = SubController4()
    sub5 = SubController5()
    
    embedded_lookup = LookupWithEmbeddedLookupController()
    embedded_lookup_with_index = LookupWithEmbeddedLookupWithHelperWithIndex()

    @expose()
    def test_args(self, id, one=None, two=2, three=3):
        r = dict(id=id, one=str(one), two=str(two), three=str(three))
        return str(r)

    @expose()
    def redirect_me(self, target, **kw):
        tg.redirect(target, kw)

    @expose()
    def hello(self, name, silly=None):
        return "Hello " + name

    @expose()
    def optional_and_req_args(self, id, one=None, two=2, three=3):
        r = dict(id=id, one=str(one), two=str(two), three=str(three))

        return str(r)

    @expose()
    def ticket2412(self, arg1):
        return arg1

    @expose()
    def redirect_cookie(self, name):
        pylons.response.set_cookie('name', name)
        tg.redirect('/hello_cookie')

    @expose()
    def hello_cookie(self):
        return "Hello " + pylons.request.cookies['name']

    @expose()
    def flash_redirect(self):
        tg.flash("Wow, flash!")
        tg.redirect("/flash_after_redirect")

    @expose()
    def flash_unicode(self):
        tg.flash(u"Привет, мир!")
        tg.redirect("/flash_after_redirect")

    @expose()
    def flash_after_redirect(self):
        return tg.get_flash()

    @expose()
    def flash_status(self):
        return tg.get_status()

    @expose()
    def flash_no_redirect(self):
        tg.flash("Wow, flash!")
        return tg.get_flash()

    @expose('json')
    @validate(validators={"some_int": validators.Int()})
    def validated_int(self, some_int):
        assert isinstance(some_int, int)
        return dict(response=some_int)

    @expose('json')
    @validate(validators={"a":validators.Int()})
    def validated_and_unvalidated(self, a, b):
        assert isinstance(a, int)
        assert isinstance(b, unicode)
        return dict(int=a,str=b)

    @expose()
    def error_handler(self, **kw):
        return 'VALIDATION ERROR HANDLER'

    @expose('json')
    @validate(validators={"a":validators.Int()}, error_handler=error_handler)
    def validated_with_error_handler(self, a, b):
        assert isinstance(a, int)
        assert isinstance(b, unicode)
        return dict(int=a,str=b)

    @expose('json')
    @validate(validators={"a":validators.Int()}, error_handler=error_controller.errors_here)
    def validated_with_remote_error_handler(self, a, b):
        assert isinstance(a, int)
        assert isinstance(b, unicode)
        return dict(int=a,str=b)

    @expose()
    @expose('json')
    def stacked_expose(self):
        return dict(got_json=True)

    @expose('json')
    def bad_json(self):
        return [(1, 'a'), 'b']

    @expose()
    def custom_content_type_in_controller(self):
        pylons.response.headers['content-type'] = 'image/png'
        return 'PNG'

    @expose('json', content_type='application/json')
    def custom_content_type_in_controller_charset(self):
        pylons.response.headers['content-type'] = 'application/json; charset=utf-8'
        return dict(result='TXT')

    @expose(content_type=CUSTOM_CONTENT_TYPE)
    def custom_content_type_with_ugliness(self):
        pylons.response.headers['content-type'] = 'image/png'
        return 'PNG'

    @expose(content_type='image/png')
    def custom_content_type_in_decorator(self):
        return 'PNG'

    @expose()
    def test_204(self, *args, **kw):
        from webob.exc import HTTPNoContent
        raise HTTPNoContent().exception

    @expose()
    def custom_content_type_replace_header(self):
        replace_header(pylons.response.headerlist, 'Content-Type', 'text/xml')
        return "<?xml version='1.0'?>"

    @expose()
    def multi_value_kws(sekf, *args, **kw):
        assert kw['foo'] == ['1', '2'], kw

class TestNotFoundController(TestWSGIController):
    def __init__(self, *args, **kargs):
        TestWSGIController.__init__(self, *args, **kargs)
        self.app = make_app(NotFoundController)

    def test_not_found(self):
        r = self.app.get('/something', status=404)
        assert '404 Not Found' in r, r

    def test_not_found_blank(self):
        r = self.app.get('/', status=404)
        assert '404 Not Found' in r, r

    def test_not_found_unicode(self):
        r = self.app.get('/права', status=404)
        assert '404 Not Found' in r, r

class TestWSGIAppController(TestWSGIController):
    def __init__(self, *args, **kargs):
        TestWSGIController.__init__(self, *args, **kargs)
        class TestedWSGIAppController(WSGIAppController):
            def __init__(self):
                def test_app(environ, start_response):
                    if environ.get('CONTENT_LENGTH', None) in (-1, '-1'):
                        del environ['CONTENT_LENGTH']
                    return validator(demo_app)(environ, start_response)
                super(TestedWSGIAppController, self).__init__(test_app)
        self.app = make_app(TestedWSGIAppController)

    def test_valid_wsgi(self):
        try:
            r = self.app.get('/some_url')
        except Exception, e:
            raise AssertionError(str(e))
        assert 'some_url' in r

class TestTGController(TestWSGIController):
    def setUp(self, *args, **kargs):
        
        TestWSGIController.setUp(self, *args, **kargs)
        self.app = make_app(BasicTGController)

    def test_lookup(self):
        r = self.app.get('/lookup/EYE')
{'fraction_non_alphanumeric': 0.12235565452521076, 'fraction_numerical': 0.005169397168760935, 'mean_word_length': 3.079980532121999, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 2, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, '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': '2871605', 'n_tokens_mistral': 8265, 'n_tokens_neox': 7985, 'n_words': 1760}