<s> """<STR_LIT>""" <EOL> import re <EOL> import os <EOL> import sys <EOL> import pkgutil <EOL> try : <EOL> from html . entities import name2codepoint <EOL> except ImportError : <EOL> from htmlentitydefs import name2codepoint <EOL> from werkzeug . _compat import unichr , text_type , string_types , iteritems , reraise , PY2 <EOL> from werkzeug . _internal import _DictAccessorProperty , _parse_signature , _missing <EOL> _format_re = re . compile ( r'<STR_LIT>' % ( ( '<STR_LIT>' , ) * <NUM_LIT:2> ) ) <EOL> _entity_re = re . compile ( r'<STR_LIT>' ) <EOL> _filename_ascii_strip_re = re . compile ( r'<STR_LIT>' ) <EOL> _windows_device_files = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> class cached_property ( property ) : <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> def __set__ ( self , obj , value ) : <EOL> obj . __dict__ [ self . __name__ ] = value <EOL> def __get__ ( self , obj , type = None ) : <EOL> if obj is None : <EOL> return self <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 environ_property ( _DictAccessorProperty ) : <EOL> """<STR_LIT>""" <EOL> read_only = True <EOL> def lookup ( self , obj ) : <EOL> return obj . environ <EOL> class header_property ( _DictAccessorProperty ) : <EOL> """<STR_LIT>""" <EOL> def lookup ( self , obj ) : <EOL> return obj . headers <EOL> class HTMLBuilder ( object ) : <EOL> """<STR_LIT>""" <EOL> _entity_re = re . compile ( r'<STR_LIT>' ) <EOL> _entities = name2codepoint . copy ( ) <EOL> _entities [ '<STR_LIT>' ] = <NUM_LIT> <EOL> _empty_elements = set ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:input>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:source>' , '<STR_LIT>' <EOL> ] ) <EOL> _boolean_attributes = set ( [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] ) <EOL> _plaintext_elements = set ( [ '<STR_LIT>' ] ) <EOL> _c_like_cdata = set ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __init__ ( self , dialect ) : <EOL> self . _dialect = dialect <EOL> def __call__ ( self , s ) : <EOL> return escape ( s ) <EOL> def __getattr__ ( self , tag ) : <EOL> if tag [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> raise AttributeError ( tag ) <EOL> def proxy ( * children , ** arguments ) : <EOL> buffer = '<STR_LIT:<>' + tag <EOL> for key , value in iteritems ( arguments ) : <EOL> if value is None : <EOL> continue <EOL> if key [ - <NUM_LIT:1> ] == '<STR_LIT:_>' : <EOL> key = key [ : - <NUM_LIT:1> ] <EOL> if key in self . _boolean_attributes : <EOL> if not value : <EOL> continue <EOL> if self . _dialect == '<STR_LIT>' : <EOL> value = '<STR_LIT>' + key + '<STR_LIT:">' <EOL> else : <EOL> value = '<STR_LIT>' <EOL> else : <EOL> value = '<STR_LIT>' + escape ( value ) + '<STR_LIT:">' <EOL> buffer += '<STR_LIT:U+0020>' + key + value <EOL> if not children and tag in self . _empty_elements : <EOL> if self . _dialect == '<STR_LIT>' : <EOL> buffer += '<STR_LIT>' <EOL> else : <EOL> buffer += '<STR_LIT:>>' <EOL> return buffer <EOL> buffer += '<STR_LIT:>>' <EOL> children_as_string = '<STR_LIT>' . join ( [ text_type ( x ) for x in children <EOL> if x is not None ] ) <EOL> if children_as_string : <EOL> if tag in self . _plaintext_elements : <EOL> children_as_string = escape ( children_as_string ) <EOL> elif tag in self . _c_like_cdata and self . _dialect == '<STR_LIT>' : <EOL> children_as_string = '<STR_LIT>' + children_as_string + '<STR_LIT>' <EOL> buffer += children_as_string + '<STR_LIT>' + tag + '<STR_LIT:>>' <EOL> return buffer <EOL> return proxy <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( <EOL> self . __class__ . __name__ , <EOL> self . _dialect <EOL> ) <EOL> html = HTMLBuilder ( '<STR_LIT:html>' ) <EOL> xhtml = HTMLBuilder ( '<STR_LIT>' ) <EOL> def get_content_type ( mimetype , charset ) : <EOL> """<STR_LIT>""" <EOL> if mimetype . startswith ( '<STR_LIT>' ) or mimetype == '<STR_LIT>' or ( mimetype . startswith ( '<STR_LIT>' ) and <EOL> mimetype . endswith ( '<STR_LIT>' ) ) : <EOL> mimetype += '<STR_LIT>' + charset <EOL> return mimetype <EOL> def format_string ( string , context ) : <EOL> """<STR_LIT>""" <EOL> def lookup_arg ( match ) : <EOL> x = context [ match . group ( <NUM_LIT:1> ) or match . group ( <NUM_LIT:2> ) ] <EOL> if not isinstance ( x , string_types ) : <EOL> x = type ( string ) ( x ) <EOL> return x <EOL> return _format_re . sub ( lookup_arg , string ) <EOL> def secure_filename ( filename ) : <EOL> r"""<STR_LIT>""" <EOL> if isinstance ( filename , text_type ) : <EOL> from unicodedata import normalize <EOL> filename = normalize ( '<STR_LIT>' , filename ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) <EOL> if not PY2 : <EOL> filename = filename . decode ( '<STR_LIT:ascii>' ) <EOL> for sep in os . path . sep , os . path . altsep : <EOL> if sep : <EOL> filename = filename . replace ( sep , '<STR_LIT:U+0020>' ) <EOL> filename = str ( _filename_ascii_strip_re . sub ( '<STR_LIT>' , '<STR_LIT:_>' . join ( <EOL> filename . split ( ) ) ) ) . strip ( '<STR_LIT>' ) <EOL> if os . name == '<STR_LIT>' and filename and filename . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . upper ( ) in _windows_device_files : <EOL> filename = '<STR_LIT:_>' + filename <EOL> return filename <EOL> def escape ( s , quote = None ) : <EOL> """<STR_LIT>""" <EOL> if s is None : <EOL> return '<STR_LIT>' <EOL> elif hasattr ( s , '<STR_LIT>' ) : <EOL> return text_type ( s . __html__ ( ) ) <EOL> elif not isinstance ( s , string_types ) : <EOL> s = text_type ( s ) <EOL> if quote is not None : <EOL> from warnings import warn <EOL> warn ( DeprecationWarning ( '<STR_LIT>' ) , stacklevel = <NUM_LIT:2> ) <EOL> s = s . replace ( '<STR_LIT:&>' , '<STR_LIT>' ) . replace ( '<STR_LIT:<>' , '<STR_LIT>' ) . replace ( '<STR_LIT:>>' , '<STR_LIT>' ) . replace ( '<STR_LIT:">' , "<STR_LIT>" ) <EOL> return s <EOL> def unescape ( s ) : <EOL> """<STR_LIT>""" <EOL> def handle_match ( m ) : <EOL> name = m . group ( <NUM_LIT:1> ) <EOL> if name in HTMLBuilder . _entities : <EOL> return unichr ( HTMLBuilder . _entities [ name ] ) <EOL> try : <EOL> if name [ : <NUM_LIT:2> ] in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return unichr ( int ( name [ <NUM_LIT:2> : ] , <NUM_LIT:16> ) ) <EOL> elif name . startswith ( '<STR_LIT:#>' ) : <EOL> return unichr ( int ( name [ <NUM_LIT:1> : ] ) ) <EOL> except ValueError : <EOL> pass <EOL> return u'<STR_LIT>' <EOL> return _entity_re . sub ( handle_match , s ) <EOL> def redirect ( location , code = <NUM_LIT> , Response = None ) : <EOL> """<STR_LIT>""" <EOL> if Response is None : <EOL> from werkzeug . wrappers import Response <EOL> display_location = escape ( location ) <EOL> if isinstance ( location , text_type ) : <EOL> from werkzeug . urls import iri_to_uri <EOL> location = iri_to_uri ( location , safe_conversion = True ) <EOL> response = Response ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( escape ( location ) , display_location ) , code , mimetype = '<STR_LIT>' ) <EOL> response . headers [ '<STR_LIT>' ] = location <EOL> return response <EOL> def append_slash_redirect ( environ , code = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> new_path = environ [ '<STR_LIT>' ] . strip ( '<STR_LIT:/>' ) + '<STR_LIT:/>' <EOL> query_string = environ . get ( '<STR_LIT>' ) <EOL> if query_string : <EOL> new_path += '<STR_LIT:?>' + query_string <EOL> return redirect ( new_path , code ) <EOL> def import_string ( import_name , silent = False ) : <EOL> """<STR_LIT>""" <EOL> import_name = str ( import_name ) . replace ( '<STR_LIT::>' , '<STR_LIT:.>' ) <EOL> try : <EOL> try : <EOL> __import__ ( import_name ) <EOL> except ImportError : <EOL> if '<STR_LIT:.>' not in import_name : <EOL> raise <EOL> else : <EOL> return sys . modules [ import_name ] <EOL> module_name , obj_name = import_name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> try : <EOL> module = __import__ ( module_name , None , None , [ obj_name ] ) <EOL> except ImportError : <EOL> module = import_string ( module_name ) <EOL> try : <EOL> return getattr ( module , obj_name ) <EOL> except AttributeError as e : <EOL> raise ImportError ( e ) <EOL> except ImportError as e : <EOL> if not silent : <EOL> reraise ( <EOL> ImportStringError , <EOL> ImportStringError ( import_name , e ) , <EOL> sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> def find_modules ( import_path , include_packages = False , recursive = False ) : <EOL> """<STR_LIT>""" <EOL> module = import_string ( import_path ) <EOL> path = getattr ( module , '<STR_LIT>' , None ) <EOL> if path is None : <EOL> raise ValueError ( '<STR_LIT>' % import_path ) <EOL> basename = module . __name__ + '<STR_LIT:.>' <EOL> for importer , modname , ispkg in pkgutil . iter_modules ( path ) : <EOL> modname = basename + modname <EOL> if ispkg : <EOL> if include_packages : <EOL> yield modname <EOL> if recursive : <EOL> for item in find_modules ( modname , include_packages , True ) : <EOL> yield item <EOL> else : <EOL> yield modname <EOL> def validate_arguments ( func , args , kwargs , drop_extra = True ) : <EOL> """<STR_LIT>""" <EOL> parser = _parse_signature ( func ) <EOL> args , kwargs , missing , extra , extra_positional = parser ( args , kwargs ) [ : <NUM_LIT:5> ] <EOL> if missing : <EOL> raise ArgumentValidationError ( tuple ( missing ) ) <EOL> elif ( extra or extra_positional ) and not drop_extra : <EOL> raise ArgumentValidationError ( None , extra , extra_positional ) <EOL> return tuple ( args ) , kwargs <EOL> def bind_arguments ( func , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> args , kwargs , missing , extra , extra_positional , arg_spec , vararg_var , kwarg_var = _parse_signature ( func ) ( args , kwargs ) <EOL> values = { } <EOL> for ( name , has_default , default ) , value in zip ( arg_spec , args ) : <EOL> values [ name ] = value <EOL> if vararg_var is not None : <EOL> values [ vararg_var ] = tuple ( extra_positional ) <EOL> elif extra_positional : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> if kwarg_var is not None : <EOL> multikw = set ( extra ) & set ( [ x [ <NUM_LIT:0> ] for x in arg_spec ] ) <EOL> if multikw : <EOL> raise TypeError ( '<STR_LIT>' + <EOL> repr ( next ( iter ( multikw ) ) ) ) <EOL> values [ kwarg_var ] = extra <EOL> elif extra : <EOL> raise TypeError ( '<STR_LIT>' + <EOL> repr ( next ( iter ( extra ) ) ) ) <EOL> return values <EOL> class ArgumentValidationError ( ValueError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , missing = None , extra = None , extra_positional = None ) : <EOL> self . missing = set ( missing or ( ) ) <EOL> self . extra = extra or { } <EOL> self . extra_positional = extra_positional or [ ] <EOL> ValueError . __init__ ( self , '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> len ( self . missing ) , <EOL> len ( self . extra ) + len ( self . extra_positional ) <EOL> ) ) <EOL> class ImportStringError ( ImportError ) : <EOL> """<STR_LIT>""" <EOL> import_name = None <EOL> exception = None <EOL> def __init__ ( self , import_name , exception ) : <EOL> self . import_name = import_name <EOL> self . exception = exception <EOL> msg = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> name = '<STR_LIT>' <EOL> tracked = [ ] <EOL> for part in import_name . replace ( '<STR_LIT::>' , '<STR_LIT:.>' ) . split ( '<STR_LIT:.>' ) : <EOL> name += ( name and '<STR_LIT:.>' ) + part <EOL> imported = import_string ( name , silent = True ) <EOL> if imported : <EOL> tracked . append ( ( name , getattr ( imported , '<STR_LIT>' , None ) ) ) <EOL> else : <EOL> track = [ '<STR_LIT>' % ( n , i ) for n , i in tracked ] <EOL> track . append ( '<STR_LIT>' % name ) <EOL> msg = msg % ( import_name , '<STR_LIT:\n>' . join ( track ) , <EOL> exception . __class__ . __name__ , str ( exception ) ) <EOL> break <EOL> ImportError . __init__ ( self , msg ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . import_name , <EOL> self . exception ) <EOL> from werkzeug . datastructures import ( <EOL> MultiDict , CombinedMultiDict , Headers , EnvironHeaders ) <EOL> from werkzeug . http import parse_cookie , dump_cookie </s>
