<s> from __future__ import absolute_import <EOL> import re <EOL> import logging <EOL> import itertools <EOL> import os <EOL> import webob as wo <EOL> import pkg_resources as pr <EOL> import mimetypes <EOL> import inspect <EOL> import warnings <EOL> import wsgiref . util <EOL> from . widgets import Widget <EOL> from . util import MultipleReplacer <EOL> import tw2 . core . core <EOL> from . params import Param , Variable , ParameterError , Required <EOL> from . middleware import register_resource <EOL> from . js import encoder , js_symbol <EOL> from markupsafe import Markup <EOL> import six <EOL> log = logging . getLogger ( __name__ ) <EOL> mimetypes . init ( ) <EOL> mimetypes . types_map [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> class JSSymbol ( js_symbol ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kw ) : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( args ) == <NUM_LIT:1> and '<STR_LIT:src>' in kw : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> kw [ '<STR_LIT:src>' ] = args [ <NUM_LIT:0> ] <EOL> super ( JSSymbol , self ) . __init__ ( ** kw ) <EOL> self . src = self . _name <EOL> class ResourceBundle ( Widget ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def inject ( cls ) : <EOL> cls . req ( ) . prepare ( ) <EOL> def prepare ( self ) : <EOL> super ( ResourceBundle , self ) . prepare ( ) <EOL> rl = tw2 . core . core . request_local ( ) <EOL> rl_resources = rl . setdefault ( '<STR_LIT>' , [ ] ) <EOL> rl_location = rl [ '<STR_LIT>' ] . config . inject_resources_location <EOL> if self not in rl_resources : <EOL> for r in self . resources : <EOL> r . req ( ) . prepare ( ) <EOL> class Resource ( ResourceBundle ) : <EOL> location = Param ( <EOL> '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' , default = None ) <EOL> id = None <EOL> template = None <EOL> def prepare ( self ) : <EOL> super ( Resource , self ) . prepare ( ) <EOL> rl = tw2 . core . core . request_local ( ) <EOL> rl_resources = rl . setdefault ( '<STR_LIT>' , [ ] ) <EOL> rl_location = rl [ '<STR_LIT>' ] . config . inject_resources_location <EOL> if self not in rl_resources : <EOL> if self . location is '<STR_LIT>' : <EOL> self . location = rl_location <EOL> rl_resources . append ( self ) <EOL> class Link ( Resource ) : <EOL> '''<STR_LIT>''' <EOL> id = None <EOL> link = Param ( <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' + <EOL> '<STR_LIT>' , <EOL> ) <EOL> modname = Param ( <EOL> '<STR_LIT>' , <EOL> default = None , <EOL> ) <EOL> filename = Param ( <EOL> '<STR_LIT>' , <EOL> default = None , <EOL> ) <EOL> no_inject = Param ( <EOL> "<STR_LIT>" , <EOL> default = False , <EOL> ) <EOL> whole_dir = Param ( <EOL> "<STR_LIT>" , <EOL> default = False , <EOL> ) <EOL> @ classmethod <EOL> def guess_modname ( cls ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> frame , i = inspect . stack ( ) [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <NUM_LIT:0> <EOL> while frame . f_globals [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> frame , i = inspect . stack ( ) [ i ] [ <NUM_LIT:0> ] , i + <NUM_LIT:1> <EOL> return frame . f_globals [ '<STR_LIT>' ] <EOL> except Exception : <EOL> return None <EOL> @ classmethod <EOL> def post_define ( cls ) : <EOL> if not cls . no_inject : <EOL> if getattr ( cls , '<STR_LIT:filename>' , None ) and type ( cls . filename ) != property : <EOL> if not cls . modname : <EOL> cls . modname = cls . guess_modname ( ) <EOL> register_resource ( <EOL> cls . modname or '<STR_LIT>' , cls . filename , cls . whole_dir <EOL> ) <EOL> def prepare ( self ) : <EOL> rl = tw2 . core . core . request_local ( ) <EOL> if not self . no_inject : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> if not self . filename : <EOL> raise ParameterError ( <EOL> "<STR_LIT>" <EOL> ) <EOL> resources = rl [ '<STR_LIT>' ] . resources <EOL> self . link = resources . resource_path ( <EOL> self . modname or '<STR_LIT>' , self . filename <EOL> ) <EOL> super ( Link , self ) . prepare ( ) <EOL> def __hash__ ( self ) : <EOL> return hash ( <EOL> hasattr ( self , '<STR_LIT>' ) and self . link or ( ( self . modname or '<STR_LIT>' ) + self . filename ) <EOL> ) <EOL> def __eq__ ( self , other ) : <EOL> return ( isinstance ( other , Link ) and self . link == other . link <EOL> and self . modname == other . modname <EOL> and self . filename == other . filename ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> getattr ( self , '<STR_LIT>' , '<STR_LIT>' % ( self . modname , self . filename ) ) <EOL> ) <EOL> class DirLink ( Link ) : <EOL> '''<STR_LIT>''' <EOL> link = Variable ( ) <EOL> filename = Required <EOL> whole_dir = True <EOL> def prepare ( self ) : <EOL> resources = tw2 . core . core . request_local ( ) [ '<STR_LIT>' ] . resources <EOL> self . link = resources . resource_path ( <EOL> self . modname , <EOL> self . filename , <EOL> ) <EOL> class JSLink ( Link ) : <EOL> '''<STR_LIT>''' <EOL> location = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> class CSSLink ( Link ) : <EOL> '''<STR_LIT>''' <EOL> media = Param ( '<STR_LIT>' , default = '<STR_LIT:all>' ) <EOL> location = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> class JSSource ( Resource ) : <EOL> """<STR_LIT>""" <EOL> src = Param ( '<STR_LIT>' , default = None ) <EOL> location = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , JSSource ) and self . src == other . src <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . src ) <EOL> def prepare ( self ) : <EOL> super ( JSSource , self ) . prepare ( ) <EOL> if not self . src : <EOL> raise ValueError ( "<STR_LIT>" % self ) <EOL> self . src = Markup ( self . src ) <EOL> class CSSSource ( Resource ) : <EOL> """<STR_LIT>""" <EOL> src = Param ( '<STR_LIT>' , default = None ) <EOL> location = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , CSSSource ) and self . src == other . src <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . src ) <EOL> def prepare ( self ) : <EOL> super ( CSSSource , self ) . prepare ( ) <EOL> if not self . src : <EOL> raise ValueError ( "<STR_LIT>" % self ) <EOL> self . src = Markup ( self . src ) <EOL> class _JSFuncCall ( JSSource ) : <EOL> """<STR_LIT>""" <EOL> src = None <EOL> function = Param ( '<STR_LIT>' , default = None ) <EOL> args = Param ( '<STR_LIT>' , default = None ) <EOL> location = '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> if not self . src : <EOL> self . prepare ( ) <EOL> return self . src <EOL> def prepare ( self ) : <EOL> if not self . src : <EOL> args = '<STR_LIT>' <EOL> if isinstance ( self . args , dict ) : <EOL> args = encoder . encode ( self . args ) <EOL> elif self . args : <EOL> args = '<STR_LIT:U+002CU+0020>' . join ( encoder . encode ( a ) for a in self . args ) <EOL> self . src = '<STR_LIT>' % ( self . function , args ) <EOL> super ( _JSFuncCall , self ) . prepare ( ) <EOL> def __hash__ ( self ) : <EOL> if self . args : <EOL> if isinstance ( self . args , dict ) : <EOL> sargs = encoder . encode ( self . args ) <EOL> else : <EOL> sargs = '<STR_LIT:U+002CU+0020>' . join ( encoder . encode ( a ) for a in self . args ) <EOL> else : <EOL> sargs = None <EOL> return hash ( ( hasattr ( self , '<STR_LIT:src>' ) and self . src or '<STR_LIT>' ) + ( sargs or '<STR_LIT>' ) ) <EOL> def __eq__ ( self , other ) : <EOL> return ( getattr ( self , '<STR_LIT:src>' , None ) == getattr ( other , '<STR_LIT:src>' , None ) <EOL> and getattr ( self , '<STR_LIT:args>' , None ) == getattr ( other , '<STR_LIT:args>' , None ) <EOL> ) <EOL> class ResourcesApp ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , config ) : <EOL> self . _paths = { } <EOL> self . _dirs = [ ] <EOL> self . config = config <EOL> def register ( self , modname , filename , whole_dir = False ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( modname , pr . Requirement ) : <EOL> modname = os . path . basename ( pr . working_set . find ( modname ) . location ) <EOL> path = modname + '<STR_LIT:/>' + filename . lstrip ( '<STR_LIT:/>' ) <EOL> if whole_dir : <EOL> if path not in self . _dirs : <EOL> self . _dirs . append ( path ) <EOL> else : <EOL> if path not in self . _paths : <EOL> self . _paths [ path ] = ( modname , filename ) <EOL> def resource_path ( self , modname , filename ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( modname , pr . Requirement ) : <EOL> modname = os . path . basename ( pr . working_set . find ( modname ) . location ) <EOL> path = modname + '<STR_LIT:/>' + filename . lstrip ( '<STR_LIT:/>' ) <EOL> return self . config . script_name + self . config . res_prefix + path <EOL> def __call__ ( self , environ , start_response ) : <EOL> req = wo . Request ( environ ) <EOL> try : <EOL> path = environ [ '<STR_LIT>' ] <EOL> path = path [ len ( self . config . res_prefix ) : ] <EOL> if path not in self . _paths : <EOL> if '<STR_LIT:..>' in path : <EOL> raise IOError ( ) <EOL> for d in self . _dirs : <EOL> if path . startswith ( d . replace ( '<STR_LIT:\\>' , '<STR_LIT:/>' ) ) : <EOL> break <EOL> else : <EOL> raise IOError ( ) <EOL> modname , filename = path . lstrip ( '<STR_LIT:/>' ) . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> ct , enc = mimetypes . guess_type ( os . path . basename ( filename ) ) <EOL> if modname and modname != '<STR_LIT>' : <EOL> stream = pr . resource_stream ( modname , filename ) <EOL> else : <EOL> stream = open ( filename ) <EOL> except IOError : <EOL> resp = wo . Response ( status = "<STR_LIT>" ) <EOL> else : <EOL> stream = wsgiref . util . FileWrapper ( stream , self . config . bufsize ) <EOL> resp = wo . Response ( app_iter = stream , content_type = ct ) <EOL> if enc : <EOL> resp . content_type_params [ '<STR_LIT>' ] = enc <EOL> resp . cache_control = { '<STR_LIT>' : int ( self . config . res_max_age ) } <EOL> return resp ( environ , start_response ) <EOL> class _ResourceInjector ( MultipleReplacer ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> return MultipleReplacer . __init__ ( self , { <EOL> r'<STR_LIT>' : self . _injector_for_location ( '<STR_LIT>' ) , <EOL> r'<STR_LIT>' : self . _injector_for_location ( <EOL> '<STR_LIT>' , False <EOL> ) , <EOL> r'<STR_LIT>' : self . _injector_for_location ( '<STR_LIT>' ) , <EOL> r'<STR_LIT>' : self . _injector_for_location ( '<STR_LIT>' , False ) <EOL> } , re . I | re . M ) <EOL> def _injector_for_location ( self , key , after = True ) : <EOL> def inject ( group , resources , encoding ) : <EOL> inj = six . u ( '<STR_LIT:\n>' ) . join ( [ <EOL> r . display ( displays_on = '<STR_LIT:string>' ) <EOL> for r in resources <EOL> if r . location == key <EOL> ] ) <EOL> if after : <EOL> return group + inj <EOL> return inj + group <EOL> return inject <EOL> def __call__ ( self , html , resources = None , encoding = None ) : <EOL> """<STR_LIT>""" <EOL> if resources is None : <EOL> resources = tw2 . core . core . request_local ( ) . get ( '<STR_LIT>' , None ) <EOL> if resources : <EOL> encoding = encoding or find_charset ( html ) or '<STR_LIT:utf-8>' <EOL> html = MultipleReplacer . __call__ ( <EOL> self , html , resources , encoding <EOL> ) <EOL> tw2 . core . core . request_local ( ) . pop ( '<STR_LIT>' , None ) <EOL> return html <EOL> inject_resources = _ResourceInjector ( ) . __call__ <EOL> _charset_re = re . compile ( <EOL> r"<STR_LIT>" , re . I | re . M ) <EOL> def find_charset ( string ) : <EOL> m = _charset_re . search ( string ) <EOL> if m : <EOL> return m . group ( '<STR_LIT>' ) . lower ( ) </s>
