<s> '''<STR_LIT>''' <EOL> import sys , re , logging , new <EOL> from smisk . mvc import http <EOL> from smisk . mvc import control <EOL> from smisk . core import URL <EOL> from smisk . config import config <EOL> from smisk . util . type import * <EOL> from smisk . util . python import wrap_exc_in_callable <EOL> from smisk . util . string import tokenize_path <EOL> from smisk . util . introspect import introspect <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> log = logging . getLogger ( __name__ ) <EOL> def _prep_path ( path ) : <EOL> return unicode ( path ) . rstrip ( u'<STR_LIT:/>' ) . lower ( ) <EOL> def _node_name ( node , fallback ) : <EOL> n = getattr ( node , '<STR_LIT>' , None ) <EOL> if n is None : <EOL> return fallback <EOL> return n <EOL> def _find_canonical_leaf ( leaf , rel_im_leaf ) : <EOL> canonical_leaf = leaf <EOL> try : <EOL> while <NUM_LIT:1> : <EOL> canonical_leaf = canonical_leaf . parent_leaf <EOL> except AttributeError : <EOL> pass <EOL> if isinstance ( canonical_leaf , FunctionType ) and canonical_leaf . __name__ not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> canonical_leaf = new . instancemethod ( canonical_leaf , rel_im_leaf . im_self , rel_im_leaf . im_class ) <EOL> return canonical_leaf <EOL> class Destination ( object ) : <EOL> '''<STR_LIT>''' <EOL> leaf = None <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , leaf ) : <EOL> self . leaf = leaf <EOL> self . formats = None <EOL> try : <EOL> self . formats = self . leaf . formats <EOL> except AttributeError : <EOL> pass <EOL> def _call_leaf ( self , * args , ** params ) : <EOL> return self . leaf ( * args , ** params ) <EOL> def __call__ ( self , * args , ** params ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . _call_leaf ( * args , ** params ) <EOL> except TypeError , e : <EOL> desc = e . args [ <NUM_LIT:0> ] <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> if not tb : <EOL> raise <EOL> while <NUM_LIT:1> : <EOL> if tb . tb_next : <EOL> tb = tb . tb_next <EOL> else : <EOL> break <EOL> if tb . tb_lineno != self . _call_leaf . im_func . func_code . co_firstlineno + <NUM_LIT:1> : <EOL> raise <EOL> GOT_MUL = '<STR_LIT>' <EOL> def req_args ( ) : <EOL> info = introspect . callable_info ( self . leaf ) <EOL> args = [ ] <EOL> for k , v in info [ '<STR_LIT:args>' ] : <EOL> if v is Undefined : <EOL> args . append ( k ) <EOL> return '<STR_LIT:U+002CU+0020>' . join ( args ) <EOL> if ( desc . find ( '<STR_LIT>' ) > <NUM_LIT:0> and desc . find ( '<STR_LIT>' ) > <NUM_LIT:0> ) or ( desc . find ( '<STR_LIT>' ) > <NUM_LIT:0> ) : <EOL> log . debug ( '<STR_LIT>' , exc_info = <NUM_LIT:1> ) <EOL> raise http . BadRequest ( '<STR_LIT>' % ( req_args ( ) , params , args ) ) <EOL> else : <EOL> p = desc . find ( GOT_MUL ) <EOL> if p > <NUM_LIT:0> : <EOL> raise http . BadRequest ( '<STR_LIT>' '<STR_LIT>' % ( self . uri , desc [ p + len ( GOT_MUL ) : ] , args , params ) ) <EOL> raise <EOL> @ property <EOL> def action ( self ) : <EOL> return self . leaf <EOL> _canonical_leaf = None <EOL> @ property <EOL> def canonical_leaf ( self ) : <EOL> if self . _canonical_leaf is None : <EOL> self . _canonical_leaf = _find_canonical_leaf ( self . leaf , self . leaf ) <EOL> log . debug ( '<STR_LIT>' , self , self . _canonical_leaf ) <EOL> return self . _canonical_leaf <EOL> @ property <EOL> def path ( self ) : <EOL> '''<STR_LIT>''' <EOL> return control . path_to ( self . canonical_leaf ) <EOL> @ property <EOL> def uri ( self ) : <EOL> '''<STR_LIT>''' <EOL> return control . uri_for ( self . canonical_leaf ) <EOL> @ property <EOL> def template_path ( self ) : <EOL> '''<STR_LIT>''' <EOL> return control . template_for ( self . canonical_leaf ) <EOL> def __str__ ( self ) : <EOL> if self . path : <EOL> return '<STR_LIT:/>' + '<STR_LIT:/>' . join ( self . path ) <EOL> else : <EOL> return self . __repr__ ( ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . canonical_leaf , self . uri ) <EOL> class Filter ( object ) : <EOL> def match ( self , method , url ) : <EOL> '''<STR_LIT>''' <EOL> return None2 <EOL> class RegExpFilter ( Filter ) : <EOL> def __init__ ( self , pattern , destination_path , regexp_flags = re . I , match_on_full_url = False , <EOL> methods = None , params = { } ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( regexp_flags , int ) : <EOL> regexp_flags = <NUM_LIT:0> <EOL> if isinstance ( pattern , RegexType ) : <EOL> self . pattern = pattern <EOL> elif not isinstance ( pattern , basestring ) : <EOL> raise ValueError ( '<STR_LIT>' % type ( pattern ) . __name__ ) <EOL> else : <EOL> self . pattern = re . compile ( pattern , regexp_flags ) <EOL> if not isinstance ( destination_path , ( basestring , URL ) ) : <EOL> raise ValueError ( '<STR_LIT>' % type ( destination_path ) . __name__ ) <EOL> self . destination_path = _prep_path ( destination_path ) <EOL> self . match_on_full_url = match_on_full_url <EOL> self . params = params <EOL> if isinstance ( methods , ( list , tuple ) ) : <EOL> self . methods = methods <EOL> elif methods is not None : <EOL> if not isinstance ( methods , basestring ) : <EOL> raise TypeError ( '<STR_LIT>' '<STR_LIT>' % type ( methods ) ) <EOL> self . methods = ( methods , ) <EOL> else : <EOL> self . methods = None <EOL> def match ( self , method , url ) : <EOL> '''<STR_LIT>''' <EOL> if method and self . methods is not None and method not in self . methods and ( not control . enable_reflection or method != '<STR_LIT>' ) : <EOL> return None2 <EOL> if self . match_on_full_url : <EOL> m = self . pattern . match ( unicode ( url ) ) <EOL> else : <EOL> m = self . pattern . match ( unicode ( url . path ) ) <EOL> if m is not None : <EOL> if self . params : <EOL> params = self . params . copy ( ) <EOL> else : <EOL> params = { } <EOL> for k , v in m . groupdict ( ) . items ( ) : <EOL> if isinstance ( k , unicode ) : <EOL> k = k . encode ( '<STR_LIT:utf-8>' ) <EOL> params [ k ] = v <EOL> return [ ] , params <EOL> return None2 <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __module__ , self . __class__ . __name__ , self . methods , self . pattern . pattern , self . destination_path , id ( self ) ) <EOL> class Router ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . cache = { } <EOL> self . filters = [ ] <EOL> def configure ( self , config_key = '<STR_LIT>' ) : <EOL> filters = config . get ( config_key , [ ] ) <EOL> if not isinstance ( filters , ( list , tuple ) ) : <EOL> raise TypeError ( '<STR_LIT>' % config_key ) <EOL> for filter in filters : <EOL> try : <EOL> if isinstance ( filter , ( tuple , list ) ) : <EOL> if len ( filter ) > <NUM_LIT:2> : <EOL> filter = { '<STR_LIT>' : filter [ <NUM_LIT:0> ] , '<STR_LIT>' : filter [ <NUM_LIT:1> ] , '<STR_LIT>' : filter [ <NUM_LIT:2> ] } <EOL> else : <EOL> filter = { '<STR_LIT>' : filter [ <NUM_LIT:0> ] , '<STR_LIT>' : filter [ <NUM_LIT:1> ] } <EOL> dest = URL ( filter [ '<STR_LIT>' ] ) <EOL> self . filter ( filter [ '<STR_LIT>' ] , dest , match_on_full_url = dest . scheme , <EOL> methods = filter . get ( '<STR_LIT>' , None ) ) <EOL> except TypeError , e : <EOL> e . args = ( '<STR_LIT>' % config_key , ) <EOL> raise <EOL> except IndexError , e : <EOL> e . args = ( '<STR_LIT>' % ( e . message , config_key ) , ) <EOL> raise <EOL> except KeyError , e : <EOL> e . args = ( '<STR_LIT>' % ( e . message , config_key ) , ) <EOL> raise <EOL> def filter ( self , pattern , destination_path , regexp_flags = re . I , match_on_full_url = False , <EOL> params = { } , methods = None ) : <EOL> '''<STR_LIT>''' <EOL> filter = RegExpFilter ( pattern , destination_path , regexp_flags , match_on_full_url , methods ) <EOL> for i in range ( len ( self . filters ) ) : <EOL> f = self . filters [ i ] <EOL> if isinstance ( f , RegExpFilter ) and f . pattern . pattern == pattern and f . methods == methods : <EOL> self . filters [ i ] = filter <EOL> log . debug ( '<STR_LIT>' , filter ) <EOL> return filter <EOL> self . filters . append ( filter ) <EOL> log . debug ( '<STR_LIT>' , filter ) <EOL> return filter <EOL> def __call__ ( self , method , url , args , params ) : <EOL> '''<STR_LIT>''' <EOL> for filter in self . filters : <EOL> dargs , dparams = filter . match ( method , url ) <EOL> if dargs != None : <EOL> dargs . extend ( args ) <EOL> dparams . update ( params ) <EOL> return self . _resolve_cached ( filter . destination_path ) , dargs , dparams <EOL> return self . _resolve_cached ( _prep_path ( url . path ) ) , args , params <EOL> def _resolve_cached ( self , raw_path ) : <EOL> try : <EOL> return self . cache [ raw_path ] <EOL> except KeyError : <EOL> dest = introspect . ensure_va_kwa ( self . _resolve ( raw_path ) ) <EOL> if dest is not None : <EOL> dest = Destination ( dest ) <EOL> self . cache [ raw_path ] = dest <EOL> return dest <EOL> def _resolve ( self , raw_path ) : <EOL> path = tokenize_path ( raw_path ) <EOL> node = control . root_controller ( ) <EOL> cls = node <EOL> log . debug ( '<STR_LIT>' , raw_path , path , node ) <EOL> if node is None : <EOL> return wrap_exc_in_callable ( http . ControllerNotFound ( '<STR_LIT>' ) ) <EOL> if not path : <EOL> try : <EOL> node = node ( ) . __call__ <EOL> log . debug ( '<STR_LIT>' , node ) <EOL> return node <EOL> except AttributeError : <EOL> return wrap_exc_in_callable ( http . MethodNotFound ( '<STR_LIT:/>' ) ) <EOL> for part in path : <EOL> log . debug ( '<STR_LIT>' , part ) <EOL> found = None <EOL> log . debug ( '<STR_LIT>' , part , node ) <EOL> try : <EOL> subclasses = node . __subclasses__ ( ) <EOL> except AttributeError : <EOL> log . debug ( '<STR_LIT>' ) <EOL> return wrap_exc_in_callable ( http . MethodNotFound ( raw_path ) ) <EOL> for subclass in node . __subclasses__ ( ) : <EOL> if _node_name ( subclass , subclass . controller_name ( ) ) == part : <EOL> if getattr ( subclass , '<STR_LIT>' , False ) : <EOL> continue <EOL> found = subclass <EOL> break <EOL> if found is not None : <EOL> node = found <EOL> cls = node <EOL> continue <EOL> log . debug ( '<STR_LIT>' , part , node ) <EOL> if type ( node ) is type : <EOL> node = node ( ) <EOL> for k , v in node . __dict__ . items ( ) : <EOL> if _node_name ( v , k . lower ( ) ) == part : <EOL> if getattr ( v , '<STR_LIT>' , False ) : <EOL> continue <EOL> if not control . leaf_is_visible ( v , cls ) : <EOL> node = None <EOL> else : <EOL> found = v <EOL> break <EOL> if found is not None : <EOL> node = found <EOL> node_type = type ( node ) <EOL> else : <EOL> return wrap_exc_in_callable ( http . MethodNotFound ( raw_path ) ) <EOL> if type ( node ) is type : <EOL> try : <EOL> cls = node <EOL> node = cls ( ) . __call__ <EOL> if not control . leaf_is_visible ( node , cls ) : <EOL> node = None <EOL> except AttributeError : <EOL> node = None <EOL> if node is None or not callable ( node ) : <EOL> return wrap_exc_in_callable ( http . MethodNotFound ( raw_path ) ) <EOL> log . debug ( '<STR_LIT>' , node ) <EOL> return node </s>
