|
a/Allura/allura/lib/helpers.py |
|
b/Allura/allura/lib/helpers.py |
|
... |
|
... |
74 |
try:
|
74 |
try:
|
75 |
return urllib.quote_plus(str(url), safe=safe)
|
75 |
return urllib.quote_plus(str(url), safe=safe)
|
76 |
except UnicodeEncodeError:
|
76 |
except UnicodeEncodeError:
|
77 |
return urllib.quote_plus(url.encode('utf-8'), safe=safe)
|
77 |
return urllib.quote_plus(url.encode('utf-8'), safe=safe)
|
78 |
|
78 |
|
79 |
def really_unicode(s):
|
79 |
def _attempt_encodings(s, encodings):
|
80 |
if s is None: return u''
|
80 |
if s is None: return u''
|
81 |
# try naive conversion to unicode
|
|
|
82 |
try:
|
|
|
83 |
return unicode(s)
|
|
|
84 |
except UnicodeDecodeError:
|
|
|
85 |
pass
|
|
|
86 |
# Try to guess the encoding
|
|
|
87 |
encodings = [
|
|
|
88 |
lambda:'utf-8',
|
|
|
89 |
lambda:chardet.detect(s[:1024])['encoding'],
|
|
|
90 |
lambda:chardet.detect(s)['encoding'],
|
|
|
91 |
lambda:'latin-1',
|
|
|
92 |
]
|
|
|
93 |
for enc in encodings:
|
81 |
for enc in encodings:
|
94 |
try:
|
82 |
try:
|
|
|
83 |
if enc is None:
|
|
|
84 |
return unicode(s) # try default encoding
|
|
|
85 |
else:
|
95 |
return unicode(s, enc())
|
86 |
return unicode(s, enc)
|
96 |
except UnicodeDecodeError:
|
87 |
except (UnicodeDecodeError, LookupError):
|
97 |
pass
|
88 |
pass
|
98 |
# Return the repr of the str -- should always be safe
|
89 |
# Return the repr of the str -- should always be safe
|
99 |
return unicode(repr(str(s)))[1:-1]
|
90 |
return unicode(repr(str(s)))[1:-1]
|
|
|
91 |
|
|
|
92 |
def really_unicode(s):
|
|
|
93 |
# Try to guess the encoding
|
|
|
94 |
def encodings():
|
|
|
95 |
yield None
|
|
|
96 |
yield 'utf-8'
|
|
|
97 |
yield chardet.detect(s[:1024])['encoding']
|
|
|
98 |
yield chardet.detect(s)['encoding']
|
|
|
99 |
yield 'latin-1'
|
|
|
100 |
return _attempt_encodings(s, encodings())
|
100 |
|
101 |
|
101 |
def find_project(url_path):
|
102 |
def find_project(url_path):
|
102 |
from allura import model as M
|
103 |
from allura import model as M
|
103 |
for n in M.Neighborhood.query.find():
|
104 |
for n in M.Neighborhood.query.find():
|
104 |
if url_path.strip("/").startswith(n.url_prefix.strip("/")):
|
105 |
if url_path.strip("/").startswith(n.url_prefix.strip("/")):
|