<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> import os <EOL> import sys <EOL> import pkgutil <EOL> import posixpath <EOL> import mimetypes <EOL> from time import time <EOL> from zlib import adler32 <EOL> from threading import RLock <EOL> from werkzeug . routing import BuildError <EOL> from werkzeug . urls import url_quote <EOL> from functools import update_wrapper <EOL> json_available = True <EOL> json = None <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> try : <EOL> from django . utils import simplejson as json <EOL> except ImportError : <EOL> json_available = False <EOL> from werkzeug . datastructures import Headers <EOL> from werkzeug . exceptions import NotFound <EOL> try : <EOL> from werkzeug . wsgi import wrap_file <EOL> except ImportError : <EOL> from werkzeug . utils import wrap_file <EOL> from jinja2 import FileSystemLoader <EOL> from . globals import session , _request_ctx_stack , _app_ctx_stack , current_app , request <EOL> def _assert_have_json ( ) : <EOL> """<STR_LIT>""" <EOL> if not json_available : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if not json_available or '<STR_LIT>' not in json . dumps ( '<STR_LIT:/>' ) : <EOL> def _tojson_filter ( * args , ** kwargs ) : <EOL> if __debug__ : <EOL> _assert_have_json ( ) <EOL> return json . dumps ( * args , ** kwargs ) . replace ( '<STR_LIT:/>' , '<STR_LIT>' ) <EOL> else : <EOL> _tojson_filter = json . dumps <EOL> _missing = object ( ) <EOL> _os_alt_seps = list ( sep for sep in [ os . path . sep , os . path . altsep ] <EOL> if sep not in ( None , '<STR_LIT:/>' ) ) <EOL> def _endpoint_from_view_func ( view_func ) : <EOL> """<STR_LIT>""" <EOL> assert view_func is not None , '<STR_LIT>' '<STR_LIT>' <EOL> return view_func . __name__ <EOL> def stream_with_context ( generator_or_function ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> gen = iter ( generator_or_function ) <EOL> except TypeError : <EOL> def decorator ( * args , ** kwargs ) : <EOL> gen = generator_or_function ( ) <EOL> return stream_with_context ( gen ) <EOL> return update_wrapper ( decorator , generator_or_function ) <EOL> def generator ( ) : <EOL> ctx = _request_ctx_stack . top <EOL> if ctx is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> with ctx : <EOL> yield None <EOL> try : <EOL> for item in gen : <EOL> yield item <EOL> finally : <EOL> if hasattr ( gen , '<STR_LIT>' ) : <EOL> gen . close ( ) <EOL> wrapped_g = generator ( ) <EOL> wrapped_g . next ( ) <EOL> return wrapped_g <EOL> def jsonify ( * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if __debug__ : <EOL> _assert_have_json ( ) <EOL> return current_app . response_class ( json . dumps ( dict ( * args , ** kwargs ) , <EOL> indent = None if request . is_xhr else <NUM_LIT:2> ) , mimetype = '<STR_LIT:application/json>' ) <EOL> def make_response ( * args ) : <EOL> """<STR_LIT>""" <EOL> if not args : <EOL> return current_app . response_class ( ) <EOL> if len ( args ) == <NUM_LIT:1> : <EOL> args = args [ <NUM_LIT:0> ] <EOL> return current_app . make_response ( args ) <EOL> def url_for ( endpoint , ** values ) : <EOL> """<STR_LIT>""" <EOL> appctx = _app_ctx_stack . top <EOL> reqctx = _request_ctx_stack . top <EOL> if appctx is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if reqctx is not None : <EOL> url_adapter = reqctx . url_adapter <EOL> blueprint_name = request . blueprint <EOL> if not reqctx . request . _is_old_module : <EOL> if endpoint [ : <NUM_LIT:1> ] == '<STR_LIT:.>' : <EOL> if blueprint_name is not None : <EOL> endpoint = blueprint_name + endpoint <EOL> else : <EOL> endpoint = endpoint [ <NUM_LIT:1> : ] <EOL> else : <EOL> if '<STR_LIT:.>' not in endpoint : <EOL> if blueprint_name is not None : <EOL> endpoint = blueprint_name + '<STR_LIT:.>' + endpoint <EOL> elif endpoint . startswith ( '<STR_LIT:.>' ) : <EOL> endpoint = endpoint [ <NUM_LIT:1> : ] <EOL> external = values . pop ( '<STR_LIT>' , False ) <EOL> else : <EOL> url_adapter = appctx . url_adapter <EOL> if url_adapter is None : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> external = values . pop ( '<STR_LIT>' , True ) <EOL> anchor = values . pop ( '<STR_LIT>' , None ) <EOL> method = values . pop ( '<STR_LIT>' , None ) <EOL> appctx . app . inject_url_defaults ( endpoint , values ) <EOL> try : <EOL> rv = url_adapter . build ( endpoint , values , method = method , <EOL> force_external = external ) <EOL> except BuildError , error : <EOL> values [ '<STR_LIT>' ] = external <EOL> values [ '<STR_LIT>' ] = anchor <EOL> values [ '<STR_LIT>' ] = method <EOL> return appctx . app . handle_url_build_error ( error , endpoint , values ) <EOL> rv = url_adapter . build ( endpoint , values , method = method , <EOL> force_external = external ) <EOL> if anchor is not None : <EOL> rv += '<STR_LIT:#>' + url_quote ( anchor ) <EOL> return rv <EOL> def get_template_attribute ( template_name , attribute ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( current_app . jinja_env . get_template ( template_name ) . module , <EOL> attribute ) <EOL> def flash ( message , category = '<STR_LIT:message>' ) : <EOL> """<STR_LIT>""" <EOL> flashes = session . get ( '<STR_LIT>' , [ ] ) <EOL> flashes . append ( ( category , message ) ) <EOL> session [ '<STR_LIT>' ] = flashes <EOL> def get_flashed_messages ( with_categories = False , category_filter = [ ] ) : <EOL> """<STR_LIT>""" <EOL> flashes = _request_ctx_stack . top . flashes <EOL> if flashes is None : <EOL> _request_ctx_stack . top . flashes = flashes = session . pop ( '<STR_LIT>' ) if '<STR_LIT>' in session else [ ] <EOL> if category_filter : <EOL> flashes = filter ( lambda f : f [ <NUM_LIT:0> ] in category_filter , flashes ) <EOL> if not with_categories : <EOL> return [ x [ <NUM_LIT:1> ] for x in flashes ] <EOL> return flashes <EOL> def send_file ( filename_or_fp , mimetype = None , as_attachment = False , <EOL> attachment_filename = None , add_etags = True , <EOL> cache_timeout = None , conditional = False ) : <EOL> """<STR_LIT>""" <EOL> mtime = None <EOL> if isinstance ( filename_or_fp , basestring ) : <EOL> filename = filename_or_fp <EOL> file = None <EOL> else : <EOL> from warnings import warn <EOL> file = filename_or_fp <EOL> filename = getattr ( file , '<STR_LIT:name>' , None ) <EOL> if not attachment_filename and not mimetype and isinstance ( filename , basestring ) : <EOL> warn ( DeprecationWarning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> stacklevel = <NUM_LIT:2> ) <EOL> if add_etags : <EOL> warn ( DeprecationWarning ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , stacklevel = <NUM_LIT:2> ) <EOL> if filename is not None : <EOL> if not os . path . isabs ( filename ) : <EOL> filename = os . path . join ( current_app . root_path , filename ) <EOL> if mimetype is None and ( filename or attachment_filename ) : <EOL> mimetype = mimetypes . guess_type ( filename or attachment_filename ) [ <NUM_LIT:0> ] <EOL> if mimetype is None : <EOL> mimetype = '<STR_LIT>' <EOL> headers = Headers ( ) <EOL> if as_attachment : <EOL> if attachment_filename is None : <EOL> if filename is None : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> attachment_filename = os . path . basename ( filename ) <EOL> headers . add ( '<STR_LIT>' , '<STR_LIT>' , <EOL> filename = attachment_filename ) <EOL> if current_app . use_x_sendfile and filename : <EOL> if file is not None : <EOL> file . close ( ) <EOL> headers [ '<STR_LIT>' ] = filename <EOL> data = None <EOL> else : <EOL> if file is None : <EOL> file = open ( filename , '<STR_LIT:rb>' ) <EOL> mtime = os . path . getmtime ( filename ) <EOL> data = wrap_file ( request . environ , file ) <EOL> rv = current_app . response_class ( data , mimetype = mimetype , headers = headers , <EOL> direct_passthrough = True ) <EOL> if mtime is not None : <EOL> rv . last_modified = int ( mtime ) <EOL> rv . cache_control . public = True <EOL> if cache_timeout is None : <EOL> cache_timeout = current_app . get_send_file_max_age ( filename ) <EOL> if cache_timeout is not None : <EOL> rv . cache_control . max_age = cache_timeout <EOL> rv . expires = int ( time ( ) + cache_timeout ) <EOL> if add_etags and filename is not None : <EOL> rv . set_etag ( '<STR_LIT>' % ( <EOL> os . path . getmtime ( filename ) , <EOL> os . path . getsize ( filename ) , <EOL> adler32 ( <EOL> filename . encode ( '<STR_LIT:utf8>' ) if isinstance ( filename , unicode ) <EOL> else filename <EOL> ) & <NUM_LIT> <EOL> ) ) <EOL> if conditional : <EOL> rv = rv . make_conditional ( request ) <EOL> if rv . status_code == <NUM_LIT> : <EOL> rv . headers . pop ( '<STR_LIT>' , None ) <EOL> return rv <EOL> def safe_join ( directory , filename ) : <EOL> """<STR_LIT>""" <EOL> filename = posixpath . normpath ( filename ) <EOL> for sep in _os_alt_seps : <EOL> if sep in filename : <EOL> raise NotFound ( ) <EOL> if os . path . isabs ( filename ) or filename . startswith ( '<STR_LIT>' ) : <EOL> raise NotFound ( ) <EOL> return os . path . join ( directory , filename ) <EOL> def send_from_directory ( directory , filename , ** options ) : <EOL> """<STR_LIT>""" <EOL> filename = safe_join ( directory , filename ) <EOL> if not os . path . isfile ( filename ) : <EOL> raise NotFound ( ) <EOL> options . setdefault ( '<STR_LIT>' , True ) <EOL> return send_file ( filename , ** options ) <EOL> def get_root_path ( import_name ) : <EOL> """<STR_LIT>""" <EOL> mod = sys . modules . get ( import_name ) <EOL> if mod is not None and hasattr ( mod , '<STR_LIT>' ) : <EOL> return os . path . dirname ( os . path . abspath ( mod . __file__ ) ) <EOL> loader = pkgutil . get_loader ( import_name ) <EOL> if loader is None or import_name == '<STR_LIT:__main__>' : <EOL> return os . getcwd ( ) <EOL> if hasattr ( loader , '<STR_LIT>' ) : <EOL> filepath = loader . get_filename ( import_name ) <EOL> else : <EOL> __import__ ( import_name ) <EOL> filepath = sys . modules [ import_name ] . __file__ <EOL> return os . path . dirname ( os . path . abspath ( filepath ) ) <EOL> def find_package ( import_name ) : <EOL> """<STR_LIT>""" <EOL> root_mod_name = import_name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> loader = pkgutil . get_loader ( root_mod_name ) <EOL> if loader is None or import_name == '<STR_LIT:__main__>' : <EOL> package_path = os . getcwd ( ) <EOL> else : <EOL> if hasattr ( loader , '<STR_LIT>' ) : <EOL> filename = loader . get_filename ( root_mod_name ) <EOL> elif hasattr ( loader , '<STR_LIT>' ) : <EOL> filename = loader . archive <EOL> else : <EOL> __import__ ( import_name ) <EOL> filename = sys . modules [ import_name ] . __file__ <EOL> package_path = os . path . abspath ( os . path . dirname ( filename ) ) <EOL> if loader . is_package ( root_mod_name ) : <EOL> package_path = os . path . dirname ( package_path ) <EOL> site_parent , site_folder = os . path . split ( package_path ) <EOL> py_prefix = os . path . abspath ( sys . prefix ) <EOL> if package_path . startswith ( py_prefix ) : <EOL> return py_prefix , package_path <EOL> elif site_folder . lower ( ) == '<STR_LIT>' : <EOL> parent , folder = os . path . split ( site_parent ) <EOL> if folder . lower ( ) == '<STR_LIT>' : <EOL> base_dir = parent <EOL> elif os . path . basename ( parent ) . lower ( ) == '<STR_LIT>' : <EOL> base_dir = os . path . dirname ( parent ) <EOL> else : <EOL> base_dir = site_parent <EOL> return base_dir , package_path <EOL> return None , package_path <EOL> class locked_cached_property ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , func , name = None , doc = None ) : <EOL> self . __name__ = name or func . __name__ <EOL> self . __module__ = func . __module__ <EOL> self . __doc__ = doc or func . __doc__ <EOL> self . func = func <EOL> self . lock = RLock ( ) <EOL> def __get__ ( self , obj , type = None ) : <EOL> if obj is None : <EOL> return self <EOL> with self . lock : <EOL> value = obj . __dict__ . get ( self . __name__ , _missing ) <EOL> if value is _missing : <EOL> value = self . func ( obj ) <EOL> obj . __dict__ [ self . __name__ ] = value <EOL> return value <EOL> class _PackageBoundObject ( object ) : <EOL> def __init__ ( self , import_name , template_folder = None ) : <EOL> self . import_name = import_name <EOL> self . template_folder = template_folder <EOL> self . root_path = get_root_path ( self . import_name ) <EOL> self . _static_folder = None <EOL> self . _static_url_path = None <EOL> def _get_static_folder ( self ) : <EOL> if self . _static_folder is not None : <EOL> return os . path . join ( self . root_path , self . _static_folder ) <EOL> def _set_static_folder ( self , value ) : <EOL> self . _static_folder = value <EOL> static_folder = property ( _get_static_folder , _set_static_folder ) <EOL> del _get_static_folder , _set_static_folder <EOL> def _get_static_url_path ( self ) : <EOL> if self . _static_url_path is None : <EOL> if self . static_folder is None : <EOL> return None <EOL> return '<STR_LIT:/>' + os . path . basename ( self . static_folder ) <EOL> return self . _static_url_path <EOL> def _set_static_url_path ( self , value ) : <EOL> self . _static_url_path = value <EOL> static_url_path = property ( _get_static_url_path , _set_static_url_path ) <EOL> del _get_static_url_path , _set_static_url_path <EOL> @ property <EOL> def has_static_folder ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . static_folder is not None <EOL> @ locked_cached_property <EOL> def jinja_loader ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . template_folder is not None : <EOL> return FileSystemLoader ( os . path . join ( self . root_path , <EOL> self . template_folder ) ) <EOL> def get_send_file_max_age ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return current_app . config [ '<STR_LIT>' ] <EOL> def send_static_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if not self . has_static_folder : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> cache_timeout = self . get_send_file_max_age ( filename ) <EOL> return send_from_directory ( self . static_folder , filename , <EOL> cache_timeout = cache_timeout ) <EOL> def open_resource ( self , resource , mode = '<STR_LIT:rb>' ) : <EOL> """<STR_LIT>""" <EOL> if mode not in ( '<STR_LIT:r>' , '<STR_LIT:rb>' ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return open ( os . path . join ( self . root_path , resource ) , mode ) </s>
