1.1 --- a/MoinMoin/wikiutil.py Tue Feb 10 13:41:20 2009 +0100
1.2 +++ b/MoinMoin/wikiutil.py Wed Feb 11 02:34:33 2009 +0100
1.3 @@ -25,6 +25,8 @@
1.4 from MoinMoin.support.python_compatibility import rsplit
1.5 from inspect import getargspec, isfunction, isclass, ismethod
1.6
1.7 +from MoinMoin import web # needed so that next line works:
1.8 +import werkzeug.utils
1.9
1.10 # Exceptions
1.11 class InvalidFileNameError(Exception):
1.12 @@ -88,107 +90,67 @@
1.13 raise UnicodeError('The string %r cannot be decoded.' % s)
1.14
1.15
1.16 -# this is a thin wrapper around urllib (urllib only handles str, not unicode)
1.17 -# with py <= 2.4.1, it would give incorrect results with unicode
1.18 -# with py == 2.4.2, it crashes with unicode, if it contains non-ASCII chars
1.19 -def url_quote(s, safe='/', want_unicode=False):
1.20 - """
1.21 - Wrapper around urllib.quote doing the encoding/decoding as usually wanted:
1.22 +def url_quote(s, safe='/', want_unicode=None):
1.23 + """ see werkzeug.utils.url_quote, we use a different safe param default value """
1.24 + try:
1.25 + assert want_unicode is None
1.26 + except AssertionError:
1.27 + log.exception("call with deprecated want_unicode param, please fix caller")
1.28 + return werkzeug.utils.url_quote(s, charset=config.charset, safe=safe)
1.29
1.30 - @param s: the string to quote (can be str or unicode, if it is unicode,
1.31 - config.charset is used to encode it before calling urllib)
1.32 - @param safe: just passed through to urllib
1.33 - @param want_unicode: for the less usual case that you want to get back
1.34 - unicode and not str, set this to True
1.35 - Default is False.
1.36 - """
1.37 - if isinstance(s, unicode):
1.38 - s = s.encode(config.charset)
1.39 - elif not isinstance(s, str):
1.40 - s = str(s)
1.41 - s = urllib.quote(s, safe)
1.42 - if want_unicode:
1.43 - s = s.decode(config.charset) # ascii would also work
1.44 - return s
1.45 +def url_quote_plus(s, safe='/', want_unicode=None):
1.46 + """ see werkzeug.utils.url_quote_plus, we use a different safe param default value """
1.47 + try:
1.48 + assert want_unicode is None
1.49 + except AssertionError:
1.50 + log.exception("call with deprecated want_unicode param, please fix caller")
1.51 + return werkzeug.utils.url_quote_plus(s, charset=config.charset, safe=safe)
1.52
1.53 -def url_quote_plus(s, safe='/', want_unicode=False):
1.54 - """
1.55 - Wrapper around urllib.quote_plus doing the encoding/decoding as usually wanted:
1.56 +def url_unquote(s, want_unicode=None):
1.57 + """ see werkzeug.utils.url_unquote """
1.58 + try:
1.59 + assert want_unicode is None
1.60 + except AssertionError:
1.61 + log.exception("call with deprecated want_unicode param, please fix caller")
1.62 + return werkzeug.utils.url_unquote(s, charset=config.charset, errors='fallback:iso-8859-1')
1.63
1.64 - @param s: the string to quote (can be str or unicode, if it is unicode,
1.65 - config.charset is used to encode it before calling urllib)
1.66 - @param safe: just passed through to urllib
1.67 - @param want_unicode: for the less usual case that you want to get back
1.68 - unicode and not str, set this to True
1.69 - Default is False.
1.70 - """
1.71 - if isinstance(s, unicode):
1.72 - s = s.encode(config.charset)
1.73 - elif not isinstance(s, str):
1.74 - s = str(s)
1.75 - s = urllib.quote_plus(s, safe)
1.76 - if want_unicode:
1.77 - s = s.decode(config.charset) # ascii would also work
1.78 - return s
1.79
1.80 -def url_unquote(s, want_unicode=True):
1.81 - """
1.82 - Wrapper around urllib.unquote doing the encoding/decoding as usually wanted:
1.83 +def parseQueryString(qstr, want_unicode=None):
1.84 + """ see werkzeug.utils.url_decode """
1.85 + try:
1.86 + assert want_unicode is None
1.87 + except AssertionError:
1.88 + log.exception("call with deprecated want_unicode param, please fix caller")
1.89 + return werkzeug.utils.url_decode(qstr, charset=config.charset, errors='fallback:iso-8859-1',
1.90 + decode_keys=False, include_empty=False)
1.91
1.92 - @param s: the string to unquote (can be str or unicode, if it is unicode,
1.93 - config.charset is used to encode it before calling urllib)
1.94 - @param want_unicode: for the less usual case that you want to get back
1.95 - str and not unicode, set this to False.
1.96 - Default is True.
1.97 - """
1.98 - if isinstance(s, unicode):
1.99 - s = s.encode(config.charset) # ascii would also work
1.100 - s = urllib.unquote(s)
1.101 - if want_unicode:
1.102 - try:
1.103 - s = decodeUserInput(s, [config.charset, 'iso-8859-1', ]) # try hard
1.104 - except UnicodeError:
1.105 - s = s.decode('ascii', 'replace') # better than crashing
1.106 - return s
1.107 -
1.108 -def parseQueryString(qstr, want_unicode=True):
1.109 - """ Parse a querystring "key=value&..." into a dict.
1.110 - """
1.111 - is_unicode = isinstance(qstr, unicode)
1.112 - if is_unicode:
1.113 - qstr = qstr.encode(config.charset)
1.114 - values = {}
1.115 - for key, value in cgi.parse_qs(qstr).items():
1.116 - if len(value) < 2:
1.117 - v = ''.join(value)
1.118 - if want_unicode:
1.119 - try:
1.120 - v = unicode(v, config.charset)
1.121 - except UnicodeDecodeError:
1.122 - v = unicode(v, 'iso-8859-1', 'replace')
1.123 - values[key] = v
1.124 - return values
1.125 -
1.126 -def makeQueryString(qstr=None, want_unicode=False, **kw):
1.127 +def makeQueryString(qstr=None, want_unicode=None, **kw):
1.128 """ Make a querystring from arguments.
1.129
1.130 kw arguments overide values in qstr.
1.131
1.132 - If a string is passed in, it's returned verbatim and
1.133 - keyword parameters are ignored.
1.134 + If a string is passed in, it's returned verbatim and keyword parameters are ignored.
1.135 +
1.136 + See also: werkzeug.utils.url_encode
1.137
1.138 @param qstr: dict to format as query string, using either ascii or unicode
1.139 @param kw: same as dict when using keywords, using ascii or unicode
1.140 @rtype: string
1.141 @return: query string ready to use in a url
1.142 """
1.143 + try:
1.144 + assert want_unicode is None
1.145 + except AssertionError:
1.146 + log.exception("call with deprecated want_unicode param, please fix caller")
1.147 if qstr is None:
1.148 qstr = {}
1.149 + elif isinstance(qstr, (str, unicode)):
1.150 + return qstr
1.151 if isinstance(qstr, dict):
1.152 qstr.update(kw)
1.153 - items = ['%s=%s' % (url_quote_plus(key, want_unicode=want_unicode), url_quote_plus(value, want_unicode=want_unicode)) for key, value in qstr.items()]
1.154 - qstr = '&'.join(items)
1.155 - return qstr
1.156 + return werkzeug.utils.url_encode(qstr, charset=config.charset, encode_keys=True)
1.157 + else:
1.158 + raise ValueError("Unsupported argument type, should be dict.")
1.159
1.160
1.161 def quoteWikinameURL(pagename, charset=config.charset):
1.162 @@ -203,35 +165,13 @@
1.163 @rtype: string
1.164 @return: the quoted filename, all unsafe characters encoded
1.165 """
1.166 - pagename = pagename.encode(charset)
1.167 - return urllib.quote(pagename)
1.168 + # XXX please note that urllib.quote and werkzeug.utils.url_quote have
1.169 + # XXX different defaults for safe=...
1.170 + return werkzeug.utils.url_quote(pagename, charset=charset, safe='/')
1.171
1.172
1.173 -def escape(s, quote=0):
1.174 - """ Escape possible html tags
1.175 +escape = werkzeug.utils.escape
1.176
1.177 - Replace special characters '&', '<' and '>' by SGML entities.
1.178 - (taken from cgi.escape so we don't have to include that, even if we
1.179 - don't use cgi at all)
1.180 -
1.181 - @param s: (unicode) string to escape
1.182 - @param quote: bool, should transform '\"' to '"'
1.183 - @rtype: when called with a unicode object, return unicode object - otherwise return string object
1.184 - @return: escaped version of s
1.185 - """
1.186 - if not isinstance(s, (str, unicode)):
1.187 - s = str(s)
1.188 -
1.189 - # Must first replace &
1.190 - s = s.replace("&", "&")
1.191 -
1.192 - # Then other...
1.193 - s = s.replace("<", "<")
1.194 - s = s.replace(">", ">")
1.195 - if quote:
1.196 - s = s.replace('"', """)
1.197 - s = s.replace("'", "'")
1.198 - return s
1.199
1.200 def clean_input(text, max_len=201):
1.201 """ Clean input: