-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlhelper.py
100 lines (93 loc) · 3.51 KB
/
urlhelper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python
# encoding: utf-8
"""
urlhelper.py
Created by Oskar Mathieu Gewalli on 2010-04-29.
Copyright (c) 2010 Gewalli. All rights reserved.
"""
import sys
import os
import unittest
import urllib
def urlencode(val):
return force_unicode(urllib.quote_plus(smart_str(val)))
def urldecode(val):
return force_unicode(urllib.unquote_plus(val.encode("utf-8")))
#django
def force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Similar to smart_unicode, except that lazy instances are resolved to
strings, rather than kept as lazy objects.
If strings_only is True, don't convert (some) non-string-like objects.
"""
if strings_only and is_protected_type(s):
return s
if not isinstance(s, basestring,):
if hasattr(s, '__unicode__'):
s = unicode(s)
else:
try:
s = unicode(str(s), encoding, errors)
except UnicodeEncodeError:
if not isinstance(s, Exception):
raise
# If we get to here, the caller has passed in an Exception
# subclass populated with non-ASCII data without special
# handling to display as a string. We need to handle this
# without raising a further exception. We do an
# approximation to what the Exception's standard str()
# output should be.
s = ' '.join([force_unicode(arg, encoding, strings_only,
errors) for arg in s])
elif not isinstance(s, unicode):
# Note: We use .decode() here, instead of unicode(s, encoding,
# errors), so that if s is a SafeString, it ends up being a
# SafeUnicode at the end.
s = s.decode(encoding, errors)
return s
#django
def smart_str(s, encoding='utf-8', strings_only=False, errors='strict'):
"""
Returns a bytestring version of 's', encoded as specified in 'encoding'.
If strings_only is True, don't convert (some) non-string-like objects.
"""
if strings_only and isinstance(s, (types.NoneType, int)):
return s
#if isinstance(s, Promise):
# return unicode(s).encode(encoding, errors)
elif not isinstance(s, basestring):
try:
return str(s)
except UnicodeEncodeError:
if isinstance(s, Exception):
# An Exception subclass containing non-ASCII data that doesn't
# know how to print itself properly. We shouldn't raise a
# further exception.
return ' '.join([smart_str(arg, encoding, strings_only,
errors) for arg in s])
return unicode(s).encode(encoding, errors)
elif isinstance(s, unicode):
return s.encode(encoding, errors)
elif s and encoding != 'utf-8':
return s.decode('utf-8', errors).encode(encoding, errors)
else:
return s
class urlhelperTests(unittest.TestCase):
def setUp(self):
pass
def test(self):
print urlencode(u"€")
print urllib.quote_plus(u"€".encode('utf-8'))
self.assertEqual(u"€",urldecode(urlencode(u"€")));
def testOfNonUnicode(self):
self.assertEqual(u"€",urldecode(urlencode("€")));
def testOfVal(self):
self.assertEqual(u":€",urldecode("%3A%E2%82%AC"));
self.assertEqual(u":€",urldecode(u"%3A%E2%82%AC"));
def testOfHeadbangingSmiley(self):
encoded = "%5Cm%2F%5C>.<%2F%5Cm%2F"
unencoded = "\m/\>.</\m/"
print unencoded
self.assertEqual(unencoded,urldecode(encoded));
if __name__ == '__main__':
unittest.main()