##// END OF EJS Templates
python3: code change for py3 support...
python3: code change for py3 support - python3.10 compat - switched hooks to binary msgpack protocol - fixed pygrack issues with subprocessio exhaustion causing huge memory use - multiple fixes and changes for python3 support - ALL tests pass

File last commit:

r1048:742e21ae python3
r1048:742e21ae python3
Show More
utils.py
137 lines | 4.1 KiB | text/x-python | PythonLexer
initial commit
r0 # RhodeCode VCSServer provides access to different vcs backends via network.
code: update copyrights to 2020
r850 # Copyright (C) 2014-2020 RhodeCode GmbH
initial commit
r0 #
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
sync: disable prompts to not allow in any case to block processes on input.
r381 import logging
caches: replaced beaker with dogpile cache.
r483 import hashlib
sync: disable prompts to not allow in any case to block processes on input.
r381
log = logging.getLogger(__name__)
initial commit
r0
git-lfs: added vcsserver handling of git-lfs objects....
r180 def safe_int(val, default=None):
"""
Returns int() of val if val is not convertable to int use default
instead
initial commit
r0
git-lfs: added vcsserver handling of git-lfs objects....
r180 :param val:
:param default:
"""
try:
val = int(val)
except (ValueError, TypeError):
val = default
return val
svn: fixed use of hgsubversion in the code
r1046 def safe_str(str_, to_encoding=None) -> str:
initial commit
r0 """
safe str function. Does few trick to turn unicode_ into string
svn: fixed use of hgsubversion in the code
r1046 :param str_: str to encode
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825 :param to_encoding: encode to this type UTF8 default
initial commit
r0 :rtype: str
:returns: str object
"""
svn: fixed use of hgsubversion in the code
r1046 if isinstance(str_, str):
return str_
# if it's bytes cast to str
if not isinstance(str_, bytes):
return str(str_)
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825 to_encoding = to_encoding or ['utf8']
initial commit
r0 if not isinstance(to_encoding, (list, tuple)):
to_encoding = [to_encoding]
for enc in to_encoding:
try:
svn: fixed use of hgsubversion in the code
r1046 return str(str_, enc)
except UnicodeDecodeError:
initial commit
r0 pass
svn: fixed use of hgsubversion in the code
r1046 return str(str_, to_encoding[0], 'replace')
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825
svn: fixed use of hgsubversion in the code
r1046 def safe_bytes(str_, from_encoding=None) -> bytes:
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825 """
svn: fixed use of hgsubversion in the code
r1046 safe bytes function. Does few trick to turn str_ into bytes string:
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825
:param str_: string to decode
:param from_encoding: encode from this type UTF8 default
:rtype: unicode
:returns: unicode object
"""
svn: fixed use of hgsubversion in the code
r1046 if isinstance(str_, bytes):
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825 return str_
svn: fixed use of hgsubversion in the code
r1046 if not isinstance(str_, str):
raise ValueError('safe_bytes cannot convert other types than str: got: {}'.format(type(str_)))
from_encoding = from_encoding or ['utf8']
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825 if not isinstance(from_encoding, (list, tuple)):
from_encoding = [from_encoding]
for enc in from_encoding:
try:
svn: fixed use of hgsubversion in the code
r1046 return str_.encode(enc)
git: use non-unicode author extraction as it's returned as bytes from backend, and we can...
r825 except UnicodeDecodeError:
pass
python3: code change for py3 support...
r1048 return str_.encode(from_encoding[0], 'replace')
def ascii_bytes(str_, allow_bytes=False) -> bytes:
"""
Simple conversion from str to bytes, with assumption that str_ is pure ASCII.
Fails with UnicodeError on invalid input.
This should be used where encoding and "safe" ambiguity should be avoided.
Where strings already have been encoded in other ways but still are unicode
string - for example to hex, base64, json, urlencoding, or are known to be
identifiers.
"""
if allow_bytes and isinstance(str_, bytes):
return str_
if not isinstance(str_, str):
raise ValueError('ascii_bytes cannot convert other types than str: got: {}'.format(type(str_)))
return str_.encode('ascii')
def ascii_str(str_):
"""
Simple conversion from bytes to str, with assumption that str_ is pure ASCII.
Fails with UnicodeError on invalid input.
This should be used where encoding and "safe" ambiguity should be avoided.
Where strings are encoded but also in other ways are known to be ASCII, and
where a unicode string is wanted without caring about encoding. For example
to hex, base64, urlencoding, or are known to be identifiers.
"""
if not isinstance(str_, bytes):
raise ValueError('ascii_str cannot convert other types than bytes: got: {}'.format(type(str_)))
return str_.decode('ascii')
svn: added support for hooks management of git and subversion....
r407
class AttributeDict(dict):
def __getattr__(self, attr):
return self.get(attr, None)
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
caches: replaced beaker with dogpile cache.
r483
def sha1(val):
return hashlib.sha1(val).hexdigest()