comparison MoinMoin/wikiutil.py @ 101:2202f548cbb0

use own urllib wrapper, see wikiutil imported from: moin--main--1.5--patch-103
author Thomas Waldmann <tw@waldmann-edv.de>
date Sat, 15 Oct 2005 16:56:39 +0000
parents 699811601bed
children a54cebaab73e
comparison
equal deleted inserted replaced
100:9aa2836c997b 101:2202f548cbb0
90 try: 90 try:
91 return s.decode(charset) 91 return s.decode(charset)
92 except UnicodeError: 92 except UnicodeError:
93 pass 93 pass
94 raise UnicodeError('The string %r cannot be decoded.' % s) 94 raise UnicodeError('The string %r cannot be decoded.' % s)
95
96
97 # this is a thin wrapper around urllib (urllib only handles str, not unicode)
98 # with py <= 2.4.1, it would give incorrect results with unicode
99 # with py == 2.4.2, it crashes with unicode, if it contains non-ASCII chars
100 def url_quote(s, safe='/', want_unicode=False):
101 """
102 Wrapper around urllib.quote doing the encoding/decoding as usually wanted:
103
104 @param s: the string to quote (can be str or unicode, if it is unicode,
105 config.charset is used to encode it before calling urllib)
106 @param safe: just passed through to urllib
107 @param want_unicode: for the less usual case that you want to get back
108 unicode and not str, set this to True
109 Default is False.
110 """
111 is_unicode = isinstance(s, unicode)
112 if is_unicode:
113 s = s.encode(config.charset)
114 s = urllib.quote(s, safe)
115 if want_unicode:
116 s = s.decode(config.charset) # ascii would also work
117 return s
118
119 def url_quote_plus(s, safe='/', want_unicode=False):
120 """
121 Wrapper around urllib.quote_plus doing the encoding/decoding as usually wanted:
122
123 @param s: the string to quote (can be str or unicode, if it is unicode,
124 config.charset is used to encode it before calling urllib)
125 @param safe: just passed through to urllib
126 @param want_unicode: for the less usual case that you want to get back
127 unicode and not str, set this to True
128 Default is False.
129 """
130 is_unicode = isinstance(s, unicode)
131 if is_unicode:
132 s = s.encode(config.charset)
133 s = urllib.quote_plus(s, safe)
134 if want_unicode:
135 s = s.decode(config.charset) # ascii would also work
136 return s
137
138 def url_unquote(s, want_unicode=True):
139 """
140 Wrapper around urllib.unquote doing the encoding/decoding as usually wanted:
141
142 @param s: the string to unquote (can be str or unicode, if it is unicode,
143 config.charset is used to encode it before calling urllib)
144 @param want_unicode: for the less usual case that you want to get back
145 str and not unicode, set this to False.
146 Default is True.
147 """
148 is_unicode = isinstance(s, unicode)
149 if is_unicode:
150 s = s.encode(config.charset) # ascii would also work
151 s = urllib.unquote(s)
152 if want_unicode:
153 s = s.decode(config.charset)
154 return s
95 155
96 156
97 # FIXME: better name would be quoteURL, as this is useful for any 157 # FIXME: better name would be quoteURL, as this is useful for any
98 # string, not only wiki names. 158 # string, not only wiki names.
99 def quoteWikinameURL(pagename, charset=config.charset): 159 def quoteWikinameURL(pagename, charset=config.charset):