<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import tornado . web <EOL> from fnmatch import fnmatch <EOL> from inspect import getmembers <EOL> from utility import app_path <EOL> def reg_ui_method ( name = None , intro = None ) : <EOL> """<STR_LIT>""" <EOL> def actual ( handler ) : <EOL> '''<STR_LIT>''' <EOL> if not hasattr ( handler , '<STR_LIT>' ) : <EOL> handler . __reg_ui__ = True <EOL> handler . __uiname__ = name <EOL> handler . __intro__ = intro <EOL> return handler <EOL> return actual <EOL> def url ( pattern = None , order = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> def actual ( handler ) : <EOL> '''<STR_LIT>''' <EOL> assert ( issubclass ( handler , tornado . web . RequestHandler ) ) <EOL> if not hasattr ( handler , "<STR_LIT>" ) or not handler . __urls__ : <EOL> handler . __urls__ = [ ] <EOL> if not pattern : <EOL> p = '<STR_LIT>' . format ( handler . __module__ , handler . __name__ ) . lower ( ) <EOL> p = p . replace ( '<STR_LIT:.>' , '<STR_LIT:/>' ) <EOL> handler . __urls__ . append ( ( p , <NUM_LIT:0> ) ) <EOL> else : <EOL> handler . __urls__ . append ( ( pattern , order ) ) <EOL> return handler <EOL> return actual <EOL> def load_handlers ( handler_dir = '<STR_LIT:action>' , member_filter = None ) : <EOL> '''<STR_LIT>''' <EOL> dirs = handler_dir <EOL> if isinstance ( handler_dir , str ) : <EOL> dirs = ( handler_dir , ) <EOL> handlers = [ ] <EOL> for path in dirs : <EOL> handlers . extend ( _load_handlers ( path , member_filter = member_filter ) ) <EOL> return handlers <EOL> def _load_handlers ( handler_dir = '<STR_LIT:action>' , member_filter = None ) : <EOL> '''<STR_LIT>''' <EOL> path = app_path ( handler_dir ) <EOL> sys . path . append ( os . getcwd ( ) ) <EOL> py_filter = lambda f : fnmatch ( f , '<STR_LIT>' ) and not f . startswith ( '<STR_LIT>' ) <EOL> if not member_filter : <EOL> member_filter = lambda m : isinstance ( <EOL> m , type ) and hasattr ( m , '<STR_LIT>' ) and m . __urls__ <EOL> names = [ os . path . splitext ( n ) [ <NUM_LIT:0> ] for n in os . listdir ( path ) if py_filter ( n ) ] <EOL> modules = [ __import__ ( <EOL> "<STR_LIT>" . format ( handler_dir , n ) ) . __dict__ [ n ] for n in names ] <EOL> ret = { } <EOL> for m in modules : <EOL> members = dict ( ( "<STR_LIT>" . format ( <EOL> v . __module__ , k ) , v ) for k , v in getmembers ( m , member_filter ) ) <EOL> ret . update ( members ) <EOL> return _sorted_hanlders ( ret . values ( ) ) <EOL> def _sorted_hanlders ( handlers ) : <EOL> '''<STR_LIT>''' <EOL> handlers = [ ( pattern , order , h ) for h in handlers for pattern , <EOL> order in h . __urls__ ] <EOL> handlers . sort ( cmp = cmp , key = lambda x : x [ <NUM_LIT:1> ] ) <EOL> return [ ( pattern , handler ) for pattern , _ , handler in handlers ] <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT:url>" , "<STR_LIT>" ] </s>
