Show More
@@ -39,7 +39,7 b' from dulwich.repo import Repo as Dulwich' | |||
|
39 | 39 | from dulwich.server import update_server_info |
|
40 | 40 | |
|
41 | 41 | from vcsserver import exceptions, settings, subprocessio |
|
42 | from vcsserver.utils import safe_str, safe_int | |
|
42 | from vcsserver.utils import safe_str, safe_int, safe_unicode | |
|
43 | 43 | from vcsserver.base import RepoFactory, obfuscate_qs |
|
44 | 44 | from vcsserver.hgcompat import ( |
|
45 | 45 | hg_url as url_parser, httpbasicauthhandler, httpdigestauthhandler) |
@@ -840,7 +840,11 b' class GitRemote(RemoteBase):' | |||
|
840 | 840 | if author.email: |
|
841 | 841 | return u"{} <{}>".format(author.name, author.email) |
|
842 | 842 | |
|
843 | return u"{}".format(author.raw_name) | |
|
843 | try: | |
|
844 | return u"{}".format(author.name) | |
|
845 | except Exception: | |
|
846 | return u"{}".format(safe_unicode(author.raw_name)) | |
|
847 | ||
|
844 | 848 | return _author(repo_id, commit_id) |
|
845 | 849 | |
|
846 | 850 | @reraise_safe_exceptions |
@@ -37,18 +37,16 b' def safe_int(val, default=None):' | |||
|
37 | 37 | return val |
|
38 | 38 | |
|
39 | 39 | |
|
40 |
def safe_str(unicode_, to_encoding= |
|
|
40 | def safe_str(unicode_, to_encoding=None): | |
|
41 | 41 | """ |
|
42 | 42 | safe str function. Does few trick to turn unicode_ into string |
|
43 | 43 | |
|
44 | In case of UnicodeEncodeError, we try to return it with encoding detected | |
|
45 | by chardet library if it fails fallback to string with errors replaced | |
|
46 | ||
|
47 | 44 | :param unicode_: unicode to encode |
|
45 | :param to_encoding: encode to this type UTF8 default | |
|
48 | 46 | :rtype: str |
|
49 | 47 | :returns: str object |
|
50 | 48 | """ |
|
51 | ||
|
49 | to_encoding = to_encoding or ['utf8'] | |
|
52 | 50 | # if it's not basestr cast to str |
|
53 | 51 | if not isinstance(unicode_, basestring): |
|
54 | 52 | return str(unicode_) |
@@ -65,15 +63,38 b" def safe_str(unicode_, to_encoding=['utf" | |||
|
65 | 63 | except UnicodeEncodeError: |
|
66 | 64 | pass |
|
67 | 65 | |
|
66 | return unicode_.encode(to_encoding[0], 'replace') | |
|
67 | ||
|
68 | ||
|
69 | def safe_unicode(str_, from_encoding=None): | |
|
70 | """ | |
|
71 | safe unicode function. Does few trick to turn str_ into unicode | |
|
72 | ||
|
73 | :param str_: string to decode | |
|
74 | :param from_encoding: encode from this type UTF8 default | |
|
75 | :rtype: unicode | |
|
76 | :returns: unicode object | |
|
77 | """ | |
|
78 | from_encoding = from_encoding or ['utf8'] | |
|
79 | ||
|
80 | if isinstance(str_, unicode): | |
|
81 | return str_ | |
|
82 | ||
|
83 | if not isinstance(from_encoding, (list, tuple)): | |
|
84 | from_encoding = [from_encoding] | |
|
85 | ||
|
68 | 86 | try: |
|
69 | import chardet | |
|
70 | encoding = chardet.detect(unicode_)['encoding'] | |
|
71 | if encoding is None: | |
|
72 | raise UnicodeEncodeError() | |
|
87 | return unicode(str_) | |
|
88 | except UnicodeDecodeError: | |
|
89 | pass | |
|
73 | 90 | |
|
74 | return unicode_.encode(encoding) | |
|
75 | except (ImportError, UnicodeEncodeError): | |
|
76 | return unicode_.encode(to_encoding[0], 'replace') | |
|
91 | for enc in from_encoding: | |
|
92 | try: | |
|
93 | return unicode(str_, enc) | |
|
94 | except UnicodeDecodeError: | |
|
95 | pass | |
|
96 | ||
|
97 | return unicode(str_, from_encoding[0], 'replace') | |
|
77 | 98 | |
|
78 | 99 | |
|
79 | 100 | class AttributeDict(dict): |
General Comments 0
You need to be logged in to leave comments.
Login now