<s> import re <EOL> import sys <EOL> from django . conf import settings <EOL> from django . core import urlresolvers <EOL> from django . core . exceptions import ImproperlyConfigured , PermissionDenied <EOL> from django . http import Http404 <EOL> from functools import update_wrapper <EOL> from inspect import isclass <EOL> from utkik . utils import import_string , cached_property , uncamel <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:url>' ] <EOL> handler404 = '<STR_LIT>' <EOL> handler500 = '<STR_LIT>' <EOL> class UtkikException ( Exception ) : <EOL> pass <EOL> class ViewWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , view ) : <EOL> self . view = view <EOL> if not hasattr ( view , '<STR_LIT>' ) and isinstance ( view , object ) : <EOL> view . __name__ = view . __class__ . __name__ <EOL> update_wrapper ( self , view ) <EOL> @ property <EOL> def func_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __name__ <EOL> def __call__ ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if isclass ( self . view ) : <EOL> view = self . view ( ) <EOL> if hasattr ( view , '<STR_LIT>' ) : <EOL> return view . dispatch ( request , * args , ** kwargs ) <EOL> else : <EOL> view = self . view <EOL> if callable ( view ) : <EOL> return view ( request , * args , ** kwargs ) <EOL> except ( Http404 , PermissionDenied , SystemExit ) : <EOL> raise <EOL> except Exception as ex : <EOL> try : <EOL> cls , e , trace = sys . exc_info ( ) <EOL> msg = '<STR_LIT>' % ( <EOL> cls . __name__ , self . view . __module__ , self . view . __name__ , e <EOL> ) <EOL> except Exception : <EOL> raise ex <EOL> else : <EOL> raise UtkikException ( msg ) . with_traceback ( trace ) <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( self . view . __module__ , self . view . __name__ ) ) <EOL> def __getattr__ ( self , name ) : <EOL> return getattr ( self . view , name ) <EOL> class LazyViewWrapper ( ViewWrapper ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dot_name ) : <EOL> module , name = dot_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> if module in settings . INSTALLED_APPS : <EOL> module += '<STR_LIT>' <EOL> self . __module__ = module <EOL> self . __name__ = name <EOL> self . dot_name = '<STR_LIT>' % ( module , name ) <EOL> @ cached_property <EOL> def view ( self ) : <EOL> """<STR_LIT>""" <EOL> view = import_string ( self . dot_name ) <EOL> self . __doc__ = view . __doc__ <EOL> return view <EOL> class RegexURLPattern ( urlresolvers . RegexURLPattern ) : <EOL> @ cached_property <EOL> def callback ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> return LazyViewWrapper ( self . _callback_str ) <EOL> return ViewWrapper ( self . _callback ) <EOL> except Exception as e : <EOL> raise urlresolvers . ViewDoesNotExist ( e ) <EOL> def include ( arg , namespace = None , app_name = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( arg , tuple ) : <EOL> if namespace : <EOL> raise ImproperlyConfigured ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> urlconf_module , app_name , namespace = arg <EOL> else : <EOL> urlconf_module = arg <EOL> return ( urlconf_module , app_name , namespace ) <EOL> def patterns ( prefix , * args ) : <EOL> """<STR_LIT>""" <EOL> pattern_list = [ ] <EOL> for t in args : <EOL> if isinstance ( t , ( list , tuple ) ) : <EOL> t = url ( prefix = prefix , * t ) <EOL> elif isinstance ( t , ( RegexURLPattern , urlresolvers . RegexURLPattern ) ) : <EOL> t . add_prefix ( prefix ) <EOL> pattern_list . append ( t ) <EOL> return pattern_list <EOL> def url ( regex , view , kwargs = None , name = None , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( view , ( list , tuple ) ) : <EOL> urlconf_module , app_name , namespace = view <EOL> return urlresolvers . RegexURLResolver ( regex , urlconf_module , kwargs , <EOL> app_name = app_name , namespace = namespace <EOL> ) <EOL> else : <EOL> if isinstance ( view , str ) : <EOL> if not view : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' % regex <EOL> ) <EOL> if prefix : <EOL> view = '<STR_LIT>' % ( prefix , view ) <EOL> if name is None : <EOL> name = uncamel ( view . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] ) <EOL> return RegexURLPattern ( regex , view , kwargs , name ) </s>
